001: package com.xoetrope.export.html;
002:
003: import com.xoetrope.export.*;
004: import com.xoetrope.export.TagExporter;
005: import com.xoetrope.export.util.FileCopy;
006: import java.awt.Color;
007: import java.io.File;
008: import java.io.FileOutputStream;
009: import java.util.Enumeration;
010: import java.util.Hashtable;
011: import java.util.Vector;
012: import net.xoetrope.xui.XProject;
013: import net.xoetrope.xui.XProjectManager;
014: import net.xoetrope.xui.style.XStyle;
015: import net.xoetrope.xui.style.XStyleManager;
016:
017: /**
018: * Exporter for exporting to HTML files.
019: *
020: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
021: * the GNU Public License (GPL), please see license.txt for more details. If
022: * you make commercial use of this software you must purchase a commercial
023: * license from Xoetrope.</p>
024: * <p> $Revision: 1.7 $</p>
025: */
026: public class HTMLExporter extends Exporter {
027:
028: /**
029: * Hashtable of styles which are referenced by the export. Used to form the
030: * styles tag of the document
031: */
032: protected Hashtable styles;
033:
034: /**
035: * The name of the file which will be generated.
036: */
037: protected String exportFileName;
038:
039: /**
040: * Constructor creates the hashtable for the styles
041: * @param instance the current page or object that provides the methods or fields used in the evaluation
042: */
043: public HTMLExporter(XProject project, Object instance) {
044: super (project, instance);
045: styles = new Hashtable();
046: }
047:
048: /**
049: * Get the content type supported by this exporter
050: * @return "HTML"
051: */
052: public String getContentType() {
053: return "HTML";
054: }
055:
056: /**
057: * Overloaded from the base abstract function. Retrieve a new
058: * HTMLTableBlock from this exporter.
059: * @return a new TableBlock Object
060: */
061: public TableBlock getTableBlock() {
062: return new HTMLTableBlock();
063: }
064:
065: /**
066: * Overloaded from the base abstract function. Retrieve a new
067: * HTMLTableModelBlock from this exporter.
068: * @return a new TableModelBlock Object
069: */
070: public TableModelBlock getTableModelBlock() {
071: return new HTMLTableModelBlock();
072: }
073:
074: /**
075: * Overloaded from the base abstract function. Retrieve a new
076: * HTMLArrayArrayBlock from this exporter.
077: * @return a new ArrayArrayBlock Object
078: */
079: public ArrayArrayBlock getArrayArrayBlock() {
080: return new HTMLArrayArrayBlock(this );
081: }
082:
083: /**
084: * Overloaded from the base abstract function. Retrieve a new
085: * HTMLFragmentBlock from this exporter.
086: * @return a new FragmentBlock Object
087: */
088: public FragmentBlock getFragmentBlock() {
089: return new HTMLFragmentBlock();
090: }
091:
092: /**
093: * Overloaded from the base abstract function. Retrieve a new
094: * HTMLFreeFormBlock from this exporter.
095: * @return a new FreeFormBlock Object
096: */
097: public Block getFreeFormBlock() {
098: return new HTMLFreeFormItem();
099: }
100:
101: /**
102: * Export to the named file.
103: * @param fileName the name of the file to be exported to.
104: */
105: public void export(String fileName) {
106: exportFileName = fileName;
107: StringBuffer blockContents = new StringBuffer();
108: int numItems = blocks.size();
109: for (int i = 0; i < numItems; i++) {
110: Block Block = (Block) blocks.get(i);
111: exportBlock(Block, blockContents);
112: if (Block instanceof HTMLTableModelBlock) {
113: String style = ((HTMLTableModelBlock) Block)
114: .getTableStyle();
115: styles.put(style, style);
116: String headerStyle = ((HTMLTableModelBlock) Block)
117: .getHeaderStyle();
118: if (headerStyle != null)
119: styles.put(headerStyle, headerStyle);
120: }
121: }
122: content.append("<HTML>");
123: content.append("<HEAD>");
124: content.append(generateStyleInfo());
125: content.append("</HEAD>");
126: content.append("<BODY>");
127: content.append(blockContents.toString());
128: content.append("</BODY>");
129: content.append("</HTML>");
130: writeToFile(fileName, content);
131: }
132:
133: private void exportBlock(Block Block, StringBuffer content) {
134: content.append(((TagExporter) Block).getStartTag());
135: Vector blockItems = Block.getBlocks();
136: int numItems = blockItems.size();
137: for (int item = 0; item < numItems; item++) {
138: HTMLBlockItem blockItem = (HTMLBlockItem) blockItems
139: .get(item);
140: exportItem(blockItem, content);
141: }
142: content.append(((TagExporter) Block).getEndTag());
143: }
144:
145: private void exportItem(HTMLBlockItem blockItem,
146: StringBuffer content) {
147: content.append(blockItem.getStartText());
148: if (blockItem.getStyle() != null)
149: styles.put(blockItem.getStyle(), blockItem.getStyle());
150:
151: Vector subBlockItems = blockItem.getBlocks();
152: int numSubItems = subBlockItems.size();
153: for (int i = 0; i < numSubItems; i++) {
154: HTMLBlockItem subBlockItem = (HTMLBlockItem) subBlockItems
155: .get(i);
156: exportItem(subBlockItem, content);
157: }
158: if ((blockItem.getTag() != null)
159: && (blockItem.getTag().compareTo(
160: HTMLFreeFormBlock.TAG_IMG) == 0)) {
161: String src = blockItem.getAttrib(Block.ATTRIB_SRC);
162: File file = new File(exportFileName);
163: String path = file.getParent();
164: FileCopy.copyFile(currentProject, src, path
165: + File.separatorChar + src);
166: }
167: content.append(blockItem.getEndTag());
168: }
169:
170: /**
171: * Overloaded from the base abstract function. Retrieve the file extension
172: * '.html'
173: */
174: public String getFileExtension() {
175: return ".html";
176: }
177:
178: public void writeToFile(String fileName, StringBuffer content) {
179: try {
180: FileOutputStream fos = new FileOutputStream(fileName);
181: fos.write(content.toString().getBytes());
182: fos.flush();
183: fos.close();
184: } catch (Exception e) {
185: e.printStackTrace();
186: }
187: }
188:
189: private String generateStyleInfo() {
190: StringBuffer styleInfo = new StringBuffer("<STYLE>");
191: XStyleManager styleMgr = XProjectManager.getStyleManager();
192: if (styleMgr != null) {
193: Enumeration keys = styles.keys();
194: while (keys.hasMoreElements()) {
195: String style = (String) keys.nextElement();
196: XStyle xStyle = styleMgr.getStyle(style);
197: String fontFace = xStyle
198: .getStyleAsString(XStyle.FONT_FACE);
199: String fontSize = xStyle
200: .getStyleAsString(XStyle.FONT_SIZE);
201: String fontWeight = xStyle
202: .getStyleAsString(XStyle.FONT_WEIGHT);
203: String fontItalic = xStyle
204: .getStyleAsString(XStyle.FONT_ITALIC);
205: Color colourFore = xStyle
206: .getStyleAsColor(XStyle.COLOR_FORE);
207: Color colourBack = xStyle
208: .getStyleAsColor(XStyle.COLOR_BACK);
209: styleInfo
210: .append("." + style.replaceAll("/", "_") + "{");
211: if (fontFace != null)
212: styleInfo.append("FONT-FAMILY: " + fontFace + ";");
213: if (fontSize != null)
214: styleInfo.append("FONT-SIZE: " + fontSize + ";");
215: if (fontWeight != null)
216: styleInfo
217: .append("FONT-WEIGHT: "
218: + ((fontWeight.compareTo("1") == 0) ? "bold"
219: : "normal") + ";");
220: if (fontItalic != null)
221: styleInfo
222: .append("FONT-STYLE: "
223: + ((fontItalic.compareTo("1") == 0) ? "italic"
224: : "normal") + ";");
225: if (colourFore != null)
226: styleInfo.append("COLOR: #"
227: + getHexColour(colourFore) + ";");
228: if (colourBack != null)
229: styleInfo.append("BACKGROUND: #"
230: + getHexColour(colourBack) + ";");
231: styleInfo.append("}");
232: }
233:
234: styleInfo.append("</STYLE>");
235: return styleInfo.toString();
236: } else {
237: return "";
238: }
239: }
240:
241: private String getHexColour(Color colour) {
242: String r = getColour(colour.getRed());
243: String g = getColour(colour.getGreen());
244: String b = getColour(colour.getBlue());
245: return r + g + b;
246: }
247:
248: private String getColour(int value) {
249: String ret = Integer.toHexString(value);
250: if (ret.length() == 1)
251: ret = "0" + ret;
252: return ret;
253: }
254:
255: }
|