1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XMLContentExporter.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import java
.io
.PrintWriter
;
35 import lib
.StatusException
;
37 import lib
.TestEnvironment
;
38 import lib
.TestParameters
;
39 import util
.SOfficeFactory
;
42 import com
.sun
.star
.container
.XIndexAccess
;
43 import com
.sun
.star
.document
.XExporter
;
44 import com
.sun
.star
.lang
.XComponent
;
45 import com
.sun
.star
.lang
.XMultiServiceFactory
;
46 import com
.sun
.star
.sheet
.XSpreadsheet
;
47 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
48 import com
.sun
.star
.sheet
.XSpreadsheets
;
49 import com
.sun
.star
.table
.XCell
;
50 import com
.sun
.star
.uno
.Any
;
51 import com
.sun
.star
.uno
.AnyConverter
;
52 import com
.sun
.star
.uno
.Type
;
53 import com
.sun
.star
.uno
.UnoRuntime
;
54 import com
.sun
.star
.uno
.XInterface
;
55 import com
.sun
.star
.xml
.sax
.XDocumentHandler
;
58 * Test for object which is represented by service
59 * <code>com.sun.star.comp.Calc.XMLContentExporter</code>. <p>
60 * Object implements the following interfaces :
62 * <li><code>com::sun::star::lang::XInitialization</code></li>
63 * <li><code>com::sun::star::document::ExportFilter</code></li>
64 * <li><code>com::sun::star::document::XFilter</code></li>
65 * <li><code>com::sun::star::document::XExporter</code></li>
66 * <li><code>com::sun::star::beans::XPropertySet</code></li>
68 * @see com.sun.star.lang.XInitialization
69 * @see com.sun.star.document.ExportFilter
70 * @see com.sun.star.document.XFilter
71 * @see com.sun.star.document.XExporter
72 * @see com.sun.star.beans.XPropertySet
73 * @see ifc.lang._XInitialization
74 * @see ifc.document._ExportFilter
75 * @see ifc.document._XFilter
76 * @see ifc.document._XExporter
77 * @see ifc.beans._XPropertySet
79 public class XMLContentExporter
extends TestCase
{
81 static XComponent xSheetDoc
;
82 static ContentFilterChecker Filter
;
85 * New spreadsheet document created.
87 protected void initialize( TestParameters tParam
, PrintWriter log
) {
88 SOfficeFactory SOF
= SOfficeFactory
.getFactory( (XMultiServiceFactory
)tParam
.getMSF() );
90 log
.println( "creating a calc document" );
91 xSheetDoc
= SOF
.openDoc("scalc","_blank");
93 } catch ( com
.sun
.star
.uno
.Exception e
) {
94 // Some exception occures.FAILED
95 e
.printStackTrace( log
);
96 throw new StatusException( "Couldn't create document", e
);
100 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
101 log
.println( " disposing xCalcDoc " );
102 util
.DesktopTools
.closeDoc(xSheetDoc
);
106 * Creating a Testenvironment for the interfaces to be tested.
107 * Creates an instance of the service
108 * <code>com.sun.star.comp.Calc.XMLContentExporter</code> with
109 * argument which is an implementation of <code>XDocumentHandler</code>
110 * and which can check if required tags and character data is
112 * The calc document is set as a source document for exporter
113 * created. A cell in the sheet is set to some value. This made
114 * for checking if this value is successfully exported within
115 * the document content.
116 * Object relations created :
118 * <li> <code>'MediaDescriptor'</code> for
119 * {@link ifc.document._XFilter} interface </li>
120 * <li> <code>'XFilter.Checker'</code> for
121 * {@link ifc.document._XFilter} interface </li>
122 * <li> <code>'SourceDocument'</code> for
123 * {@link ifc.document._XExporter} interface </li>
126 protected synchronized TestEnvironment
createTestEnvironment(TestParameters tParam
, PrintWriter log
) {
128 XMultiServiceFactory xMSF
= (XMultiServiceFactory
)tParam
.getMSF() ;
129 XInterface oObj
= null;
130 final String CELL_TEXT
= "XMLContentExporter";
132 ContentFilterChecker Filter
= new ContentFilterChecker(log
);
134 Any arg
= new Any(new Type(XDocumentHandler
.class), Filter
);
136 oObj
= (XInterface
) xMSF
.createInstanceWithArguments(
137 "com.sun.star.comp.Calc.XMLContentExporter",
138 new Object
[] {arg
} );
139 XExporter xEx
= (XExporter
) UnoRuntime
.queryInterface
140 (XExporter
.class,oObj
);
141 xEx
.setSourceDocument(xSheetDoc
);
143 // Setting some string to a cell
144 XSpreadsheetDocument xSpreadsheetDoc
= (XSpreadsheetDocument
)
145 UnoRuntime
.queryInterface(XSpreadsheetDocument
.class, xSheetDoc
);
146 XSpreadsheets xSpreadsheets
= xSpreadsheetDoc
.getSheets();
147 XIndexAccess xSheetsIndexArray
= (XIndexAccess
)
148 UnoRuntime
.queryInterface(XIndexAccess
.class, xSpreadsheets
);
149 XSpreadsheet xSheet
= (XSpreadsheet
) AnyConverter
.toObject(
150 new Type(XSpreadsheet
.class),xSheetsIndexArray
.getByIndex(0));
151 XCell xCell
= xSheet
.getCellByPosition(0, 0);
152 xCell
.setFormula(CELL_TEXT
);
154 log
.println("fill sheet 1 with contnet...");
155 util
.CalcTools
.fillCalcSheetWithContent(xSheetDoc
, 1, 1, 1, 5, 5);
157 } catch (com
.sun
.star
.uno
.Exception e
) {
158 e
.printStackTrace(log
) ;
159 throw new StatusException("Can't create component.", e
) ;
160 } catch (java
.lang
.Exception e
) {
161 e
.printStackTrace(log
);
162 throw new StatusException("Can't create environment.", e
);
165 // adding tags which must be contained in XML output
166 Filter
.addTag("office:document-content");
167 Filter
.addTagEnclosed("office:body", "office:document-content");
168 Filter
.addTagEnclosed("office:script", "office:document-content");
169 Filter
.addTagEnclosed("table:table", "office:body");
170 Filter
.addTagEnclosed("table:table-column", "table:table");
171 Filter
.addTagEnclosed("table:table-row", "table:table");
172 Filter
.addTagEnclosed("table:table-cell", "table:table-row");
173 Filter
.addTagEnclosed("text:p", "table:table-cell");
174 Filter
.addCharactersEnclosed(CELL_TEXT
, "text:p");
176 // create testobject here
177 log
.println( "creating a new environment" );
178 TestEnvironment tEnv
= new TestEnvironment( oObj
);
180 tEnv
.addObjRelation("MediaDescriptor", XMLTools
.createMediaDescriptor(
181 new String
[] {"FilterName"},
182 new Object
[] {"scalc: StarOffice XML (Calc)"}));
183 tEnv
.addObjRelation("SourceDocument",xSheetDoc
);
184 tEnv
.addObjRelation("XFilter.Checker", Filter
);
190 * This class checks the XML for tags and data required and returns
191 * checking result to <code>XFilter</code> interface test. All
192 * the information about errors occured in XML data is written
194 * @see ifc.document._XFilter
196 protected class ContentFilterChecker
extends XMLTools
.XMLTagsChecker
197 implements ifc
.document
._XFilter
.FilterChecker
{
200 * Creates a class which will write information
201 * into log specified.
203 public ContentFilterChecker(PrintWriter log
) {
208 * <code>_XFilter.FilterChecker</code> interface method
209 * which returns the result of XML checking.
210 * @return <code>true</code> if the XML data exported was
211 * valid (i.e. all necessary tags and character data exists),
212 * <code>false</code> if some errors occured.
214 public boolean checkFilter() {