001: package com.xoetrope.export;
002:
003: import com.xoetrope.export.html.HTMLExporter;
004: import com.xoetrope.export.pdf.PdfExporter;
005: import com.xoetrope.export.xl.XLExporter;
006: import java.util.Vector;
007: import net.xoetrope.xui.XProject;
008: import net.xoetrope.xui.evaluator.XDefaultAttributeEvaluator;
009: import net.xoetrope.xui.PageSupport;
010:
011: /**
012: * Abstract class for exporting to different formats.
013: *
014: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
015: * the GNU Public License (GPL), please see license.txt for more details. If
016: * you make commercial use of this software you must purchase a commercial
017: * license from Xoetrope.</p>
018: * <p> $Revision: 1.8 $</p>
019: */
020: public abstract class Exporter {
021: protected StringBuffer content;
022: protected Vector blocks;
023: protected XProject currentProject;
024:
025: public static final int HTML_EXPORTER = 1;
026: public static final int XL_EXPORTER = 2;
027: public static final int PDF_EXPORTER = 3;
028: protected int exportType;
029: protected XDefaultAttributeEvaluator evaluator;
030: protected Object instance;
031:
032: /**
033: * Create a new exporter and create a Vector for the export blocks which will
034: * be added
035: * @param instance the current page or object that provides the methods or fields used in the evaluation
036: */
037: public Exporter(XProject project, Object instance) {
038: currentProject = project;
039: this .instance = instance;
040: content = new StringBuffer();
041: blocks = new Vector();
042:
043: evaluator = (XDefaultAttributeEvaluator) currentProject
044: .getObject("DefaultAttributeEvaluator");
045: if (evaluator == null) {
046: evaluator = new XDefaultAttributeEvaluator(currentProject);
047: evaluator.setCurrentProject(currentProject);
048: currentProject.setObject("DefaultAttributeEvaluator",
049: evaluator);
050: }
051: }
052:
053: /**
054: * Get the project that this exporter belongs to
055: * @return the project
056: */
057: public XProject getCurrentProject() {
058: return currentProject;
059: }
060:
061: /**
062: * Get the content type supported by this exporter
063: * @return "HTML or XL or PDF depending on the instance"
064: */
065: public abstract String getContentType();
066:
067: /**
068: * Retrieve the content of the export.
069: *
070: * @return the content of the export
071: */
072: public String getContents() {
073: return content.toString();
074: }
075:
076: /**
077: * Retrieve a new TableBlock from this exporter
078: *
079: * @return a new TableBlock Object
080: */
081: public abstract TableBlock getTableBlock();
082:
083: /**
084: * Retrieve a new TableModelBlock from this exporter
085: *
086: * @return a new TableModelBlock Object
087: */
088: public abstract TableModelBlock getTableModelBlock();
089:
090: /**
091: * Retrieve a new ArrayArrayBlock from this exporter
092: *
093: * @return a new ArrayArrayBlock Object
094: */
095: public abstract ArrayArrayBlock getArrayArrayBlock();
096:
097: /**
098: * Retrieve a new FragmentBlock from this exporter
099: *
100: * @return a new FragmentBlock Object
101: */
102: public abstract FragmentBlock getFragmentBlock();
103:
104: /**
105: * Retrieve a new FreeFormBlock from this exporter
106: *
107: * @return a new FreeFormBlock Object
108: */
109: public abstract Block getFreeFormBlock();
110:
111: /**
112: * Retrieve a new TableBlock from this exporter
113: */
114: public abstract void export(String fileName);
115:
116: /**
117: * Retrieve the file extension to be used for this export type.
118: * @return a string containing the extension including the dot.
119: */
120: public abstract String getFileExtension();
121:
122: /**
123: * Append a new Block Object to the export Vector
124: * @param blockExport the Block object to be appended
125: */
126: public void addBlock(Block block) {
127: blocks.add(block);
128: }
129:
130: /**
131: * Retrieve a new Exporter of type specified by the type argument. Supported
132: * types are HTML_EXPORTER, PDF_EXPORTER and XL_EXPORTER.
133: * @param instance the current page or object that provides the methods or fields used in the evaluation
134: * @param currentProject the owner project
135: * @param type constant of either HTML_EXPORTER or XL_EXPORTER
136: */
137: public static Exporter getExporter(XProject currentProject,
138: Object instance, int type) {
139: if (type == HTML_EXPORTER)
140: return new HTMLExporter(currentProject, instance);
141: else if (type == XL_EXPORTER)
142: return new XLExporter(currentProject, instance);
143: else if (type == PDF_EXPORTER)
144: return new PdfExporter(currentProject, instance);
145:
146: return null;
147: }
148:
149: /**
150: * Create and return a new Block Object from the class specified by the
151: * fully qualified className parameter
152: * @param className the name of the Block class to create
153: * @return the created Block Object
154: */
155: public Block getBlock(String className) {
156: try {
157: Block exporter = (Block) Class.forName(className.trim())
158: .newInstance();
159: return exporter;
160: } catch (Exception e) {
161: return null;
162: }
163: }
164:
165: /**
166: * Evaluate portions of the path within the evaluation tags '${}'. For example
167: * 'foo1/${foo2()}/foo3'. The foo2 function in the evaluator class will be
168: * called in order to get the path information.
169: * @param the path being replaced
170: * @return the fixed path
171: */
172: protected String evaluatePath(String path) {
173: if (path == null)
174: return "";
175:
176: return evaluator.evaluateAttribute(instance, path).toString();
177: }
178:
179: /**
180: * Call the argumentless method in the evaluatorClass and return the result of
181: * it.
182: * @param methodName the name of the function to call
183: * @return the result of the function call
184: */
185: public Object evaluate(String arg) {
186: if (arg == null)
187: return "";
188:
189: return evaluator.evaluateAttribute(instance, arg);
190: }
191: }
|