001:package com.xoetrope.export.xl;
002:
003:import com.xoetrope.export.*;
004:import com.xoetrope.export.html.HTMLFreeFormBlock;
005:import com.xoetrope.export.util.FileCopy;
006:import java.awt.Point;
007:import java.io.File;
008:import java.util.Hashtable;
009:import java.util.Vector;
010:import jxl.Workbook;
011:import jxl.format.Alignment;
012:import jxl.write.Label;
013:import jxl.write.WritableCellFormat;
014:import jxl.write.WritableFont;
015:import jxl.write.WritableFont.FontName;
016:import jxl.write.WritableImage;
017:import jxl.write.WritableSheet;
018:import jxl.write.WritableWorkbook;
019:import jxl.write.WriteException;
020:import net.xoetrope.xui.PageSupport;
021:import net.xoetrope.xui.XProject;
022:import net.xoetrope.xui.style.XStyle;
023:import net.xoetrope.xui.style.XStyleManager;
024:
025:/**
026: *
027: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
028: * the GNU Public License (GPL), please see license.txt for more details. If
029: * you make commercial use of this software you must purchase a commercial
030: * license from Xoetrope.</p>
031: * <p> $Revision: 1.8 $</p>
032: */
033:public class XLExporter extends Exporter
034:{
035: protected WritableWorkbook workbook;
036: protected WritableSheet sheet;
037: protected int row = -1, col = -1;
038: protected XStyleManager styleMgr;
039: protected Hashtable spanCells;
040: protected String savePath;
041:
042: /**
043: * @param instance the current page or object that provides the methods or fields used in the evaluation
044: */
045: public XLExporter( XProject project, Object instance )
046: {
047: super ( project, instance );
048: styleMgr = currentProject.getStyleManager();
049: spanCells = new Hashtable();
050: }
051:
052: /**
053: * Get the content type supported by this exporter
054: * @return "XL"
055: */
056: public String getContentType()
057: {
058: return "XL";
059: }
060:
061: public void addRow()
062: {
063: row++;
064: }
065:
066: public void addColumn()
067: {
068: col++;
069: }
070:
071: public void addRow( int num )
072: {
073: row+=num;
074: }
075:
076: public void addColumn( int num )
077: {
078: col+=num;
079: }
080:
081: public int getRow()
082: {
083: return row;
084: }
085:
086: public int getColumn()
087: {
088: return col;
089: }
090:
091: public void setRow( int value )
092: {
093: row = value;
094: }
095:
096: public void setColumn( int value )
097: {
098: col = value;
099: }
100:
101: /**
102: * Overloaded from the base abstract function. Retrieve a new
103: * XLTableBlock from this exporter.
104: *
105: *
106: * @return a new TableBlock Object
107: */
108: public TableBlock getTableBlock()
109: {
110: XLTableBlock Block = new XLTableBlock( this );
111: Block.setWorksheet( sheet );
112: return Block;
113: }
114:
115: /**
116: * Overloaded from the base abstract function. Retrieve a new
117: * XLTableModelBlock from this exporter.
118: *
119: *
120: * @return a new TableModelBlock Object
121: */
122: public TableModelBlock getTableModelBlock()
123: {
124: XLTableModelBlock Block = new XLTableModelBlock( this );
125: Block.setWorksheet( sheet );
126: return Block;
127: }
128:
129: /**
130: * Retrieve a new ArrayArrayBlock from this exporter
131: *
132: * @return a new ArrayArrayBlock Object
133: */
134: public ArrayArrayBlock getArrayArrayBlock()
135: {
136: XLArrayArrayBlock Block = new XLArrayArrayBlock( this );
137: Block.setWorksheet( sheet );
138: return Block;
139: }
140:
141: /**
142: * Overloaded from the base abstract function. Retrieve a new
143: * XLFragmentBlock from this exporter.
144: *
145: *
146: * @return a new FragmentBlock Object
147: */
148: public FragmentBlock getFragmentBlock()
149: {
150: XLFragmentBlock Block = new XLFragmentBlock( this );
151: Block.setRow( row );
152: Block.setColumn( col );
153: return Block;
154: }
155:
156: /**
157: * Overloaded from the base abstract function. Retrieve a new
158: * XLFreeFormBlock from this exporter.
159: *
160: *
161: * @return a new FreeFormBlock Object
162: */
163: public Block getFreeFormBlock()
164: {
165: Block block = new XLFreeFormItem( this );
166: return block;
167: }
168:
169: public void addBlock( Block blockExport )
170: {
171: super .addBlock( blockExport );
172: content.append( blockExport.getContent() );
173: }
174:
175: /**
176: * Export to the named file.
177: * @param fileName the name of the file to be exported to.
178: */
179: public void export( String fileName )
180: {
181: savePath = new File( fileName ).getParent();
182: createWorkBook( fileName );
183:
184: Point offset = new Point ( 0, 0 );
185: int numBlocks = blocks.size();
186: for ( int i = 0; i < numBlocks; i++ ) {
187: Block block = ( Block ) blocks.get( i );
188: exportBlock( block, offset );
189: }
190: writeToFile( fileName );
191: }
192:
193: private void exportBlock( Block block, Point offset )
194: {
195: Vector blockItems = block.getBlocks();
196: int numBlocks = blockItems.size();
197: for ( int item = 0; item < numBlocks; item++ ) {
198: XLBlockItem blockItem = ( XLBlockItem ) blockItems.get( item );
199: offset = exportItem( blockItem, offset, true );
200: }
201: offset.setLocation( offset.x, offset.y + 1 );
202: }
203:
204: private Point exportItem( XLBlockItem blockItem, Point offset, boolean top )
205: {
206: Vector subBlockItems = blockItem.getBlocks();
207: if ( top )
208: offset = exportBlockItem( blockItem, offset );
209:
210: int numSubBlockItems = subBlockItems.size();
211: for ( int i = 0; i < numSubBlockItems; i++ ) {
212: XLBlockItem subBlockItem = (XLBlockItem)subBlockItems.get( i );
213: exportBlockItem( subBlockItem, offset );
214: offset = exportItem( subBlockItem, offset, false );
215: }
216: return offset;
217: }
218:
219: private Point exportBlockItem( XLBlockItem subBlockItem, Point offset )
220: {
221: if ( subBlockItem instanceof XLCellBlockItem ) {
222: String style = subBlockItem.getStyle();
223: XLCellBlockItem cellBlockItem = ( ( XLCellBlockItem ) subBlockItem );
224: setCell( cellBlockItem, style, offset );
225: }
226: //Point newOffset = new Point( offset.x, offset.y + 1 );
227: //return newOffset;
228: return offset;
229: }
230:
231: private Point setCell( XLCellBlockItem cellBlockItem, String style, Point offset )
232: {
233: String tag = cellBlockItem.getTag();
234: if ( ( tag != null ) && ( tag.equals( HTMLFreeFormBlock.TAG_IMG ) ) ) {
235: WritableImage img = createImage( cellBlockItem, offset );
236: sheet.addImage( img );
237: }
238: else {
239: Label lbl = createLabel( cellBlockItem, style, offset );
240: try {
241: sheet.addCell( lbl );
242: }
243: catch ( WriteException ex ) {
244: ex.printStackTrace();
245: }
246: }
247: return offset;
248: }
249:
250: public void addSkipCells( int rows, int cols )
251: {
252: if ( rows > 0 ) {
253: for ( int i = 0; i < rows; i++ ) {
254: String key = getColumn() + "A:" + ( i + getRow() + 1 ) + "A";
255: spanCells.put( key, key );
256: }
257: for ( int i = 0; i < cols; i++ ) {
258: String key = ( i + getColumn() + 1 ) + "A:" + getRow() + "A";
259: spanCells.put( key, key );
260: }
261: }
262: }
263:
264: public void skipSpanCells()
265: {
266: boolean span = true;
267: while ( span ) {
268: String key = getColumn() + "A:" + getRow() + "A";
269: if ( spanCells.get( key ) != null )
270: addColumn();
271: else
272: span = false;
273: }
274: }
275:
276: private Label createLabel( XLCellBlockItem cellBlockItem, String styleName, Point offset )
277: {
278: WritableFont font = null;
279: if ( styleName == null ) {
280: FontName fontname = WritableFont.ARIAL;
281: font = new WritableFont( fontname, 10 );
282: } else {
283: if ( styleMgr != null ) {
284: XStyle style = styleMgr.getStyle( styleName );
285: FontName fontname = WritableFont.createFont( style.getStyleAsString( XStyle.FONT_FACE ) );
286: font = new WritableFont( fontname, style.getStyleAsInt( XStyle.FONT_SIZE ) );
287: } else {
288: font = new WritableFont( WritableFont.ARIAL, 10 );
289: }
290: }
291: WritableCellFormat format = new WritableCellFormat ( font );
292: applyStyles( cellBlockItem, format );
293:
294: Label lbl = new Label( cellBlockItem.getX() + offset.x, cellBlockItem.getY() + offset.y, cellBlockItem.getValue(), format );
295: return lbl;
296: }
297:
298: private WritableImage createImage( XLCellBlockItem cellBlockItem, Point offset )
299: {
300: double cellWidth = 62.0;
301: double cellHeight = 17.0;
302: int spanHeight = 0;
303:
304: String src = cellBlockItem.getAttrib( Block.ATTRIB_SRC );
305: String w = cellBlockItem.getAttrib( "w" );
306: String h = cellBlockItem.getAttrib( "h" );
307: double width = 1.0;
308: double height = 1.0;
309: double x = cellBlockItem.getX() + offset.x;
310: double y = cellBlockItem.getY() + offset.y;
311: if ( w != null ) {
312: width = Double.parseDouble( w ) / cellWidth;
313: height = Double.parseDouble( h ) / cellHeight;
314: spanHeight = ( int )( height );
315: offset.setLocation( offset.x, offset.y + spanHeight );
316: }
317: FileCopy.copyFile( currentProject, src, savePath + File.separatorChar + src );
318: WritableImage img = new WritableImage( x, y, width, height, new File( savePath + File.separatorChar + src ) );
319: return img;
320: }
321:
322: private void applyStyles( XLCellBlockItem cellBlockItem, WritableCellFormat format )
323: {
324: String align = cellBlockItem.getAttrib( cellBlockItem.ATTRIB_ALIGN );
325: if ( align != null ) {
326: try {
327: if ( align.compareTo( "left" ) == 0 )
328: format.setAlignment( Alignment.LEFT );
329: else if ( align.compareTo( "center" ) == 0 )
330: format.setAlignment( Alignment.CENTRE );
331: else if ( align.compareTo( "right" ) == 0 )
332: format.setAlignment( Alignment.RIGHT );
333: } catch ( Exception e ) {
334: e.printStackTrace();
335: }
336: }
337: }
338:
339: private void createWorkBook( String fileName )
340: {
341: try {
342: workbook = Workbook.createWorkbook( new File( fileName ) );
343: sheet = workbook.createSheet( "export", 0 );
344: }
345: catch (Exception e) {
346: e.printStackTrace();
347: }
348: }
349:
350: /**
351: * Overloaded from the base abstract function. Retrieve the file extension
352: * '.html'
353: */
354: public String getFileExtension()
355: {
356: return ".xls";
357: }
358:
359:
360: public void writeToFile( String fileName )
361: {
362: savePath = new File( fileName ).getParent();
363: try {
364: workbook.write();
365: workbook.close();
366: }
367: catch ( Exception e ) {
368: e.printStackTrace();
369: }
370: }
371:
372:}
|