2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import java
.io
.PrintWriter
;
23 import lib
.StatusException
;
25 import lib
.TestEnvironment
;
26 import lib
.TestParameters
;
27 import util
.SOfficeFactory
;
30 import com
.sun
.star
.container
.XIndexAccess
;
31 import com
.sun
.star
.document
.XExporter
;
32 import com
.sun
.star
.lang
.XComponent
;
33 import com
.sun
.star
.lang
.XMultiServiceFactory
;
34 import com
.sun
.star
.sheet
.XSpreadsheet
;
35 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
36 import com
.sun
.star
.sheet
.XSpreadsheets
;
37 import com
.sun
.star
.table
.XCell
;
38 import com
.sun
.star
.uno
.Any
;
39 import com
.sun
.star
.uno
.AnyConverter
;
40 import com
.sun
.star
.uno
.Type
;
41 import com
.sun
.star
.uno
.UnoRuntime
;
42 import com
.sun
.star
.uno
.XInterface
;
43 import com
.sun
.star
.xml
.sax
.XDocumentHandler
;
46 * Test for object which is represented by service
47 * <code>com.sun.star.comp.Calc.XMLContentExporter</code>. <p>
48 * Object implements the following interfaces :
50 * <li><code>com::sun::star::lang::XInitialization</code></li>
51 * <li><code>com::sun::star::document::ExportFilter</code></li>
52 * <li><code>com::sun::star::document::XFilter</code></li>
53 * <li><code>com::sun::star::document::XExporter</code></li>
54 * <li><code>com::sun::star::beans::XPropertySet</code></li>
56 * @see com.sun.star.lang.XInitialization
57 * @see com.sun.star.document.ExportFilter
58 * @see com.sun.star.document.XFilter
59 * @see com.sun.star.document.XExporter
60 * @see com.sun.star.beans.XPropertySet
61 * @see ifc.lang._XInitialization
62 * @see ifc.document._ExportFilter
63 * @see ifc.document._XFilter
64 * @see ifc.document._XExporter
65 * @see ifc.beans._XPropertySet
67 public class XMLContentExporter
extends TestCase
{
69 static XComponent xSheetDoc
;
70 static ContentFilterChecker Filter
;
73 * New spreadsheet document created.
75 protected void initialize( TestParameters tParam
, PrintWriter log
) {
76 SOfficeFactory SOF
= SOfficeFactory
.getFactory( (XMultiServiceFactory
)tParam
.getMSF() );
78 log
.println( "creating a calc document" );
79 xSheetDoc
= SOF
.openDoc("scalc","_blank");
81 } catch ( com
.sun
.star
.uno
.Exception e
) {
82 // Some exception occurs.FAILED
83 e
.printStackTrace( log
);
84 throw new StatusException( "Couldn't create document", e
);
88 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
89 log
.println( " disposing xCalcDoc " );
90 util
.DesktopTools
.closeDoc(xSheetDoc
);
94 * Creating a Testenvironment for the interfaces to be tested.
95 * Creates an instance of the service
96 * <code>com.sun.star.comp.Calc.XMLContentExporter</code> with
97 * argument which is an implementation of <code>XDocumentHandler</code>
98 * and which can check if required tags and character data is
100 * The calc document is set as a source document for exporter
101 * created. A cell in the sheet is set to some value. This made
102 * for checking if this value is successfully exported within
103 * the document content.
104 * Object relations created :
106 * <li> <code>'MediaDescriptor'</code> for
107 * {@link ifc.document._XFilter} interface </li>
108 * <li> <code>'XFilter.Checker'</code> for
109 * {@link ifc.document._XFilter} interface </li>
110 * <li> <code>'SourceDocument'</code> for
111 * {@link ifc.document._XExporter} interface </li>
114 protected synchronized TestEnvironment
createTestEnvironment(TestParameters tParam
, PrintWriter log
) {
116 XMultiServiceFactory xMSF
= (XMultiServiceFactory
)tParam
.getMSF() ;
117 XInterface oObj
= null;
118 final String CELL_TEXT
= "XMLContentExporter";
120 ContentFilterChecker Filter
= new ContentFilterChecker(log
);
122 Any arg
= new Any(new Type(XDocumentHandler
.class), Filter
);
124 oObj
= (XInterface
) xMSF
.createInstanceWithArguments(
125 "com.sun.star.comp.Calc.XMLContentExporter",
126 new Object
[] {arg
} );
127 XExporter xEx
= UnoRuntime
.queryInterface
128 (XExporter
.class,oObj
);
129 xEx
.setSourceDocument(xSheetDoc
);
131 // Setting some string to a cell
132 XSpreadsheetDocument xSpreadsheetDoc
= UnoRuntime
.queryInterface(XSpreadsheetDocument
.class, xSheetDoc
);
133 XSpreadsheets xSpreadsheets
= xSpreadsheetDoc
.getSheets();
134 XIndexAccess xSheetsIndexArray
= UnoRuntime
.queryInterface(XIndexAccess
.class, xSpreadsheets
);
135 XSpreadsheet xSheet
= (XSpreadsheet
) AnyConverter
.toObject(
136 new Type(XSpreadsheet
.class),xSheetsIndexArray
.getByIndex(0));
137 XCell xCell
= xSheet
.getCellByPosition(0, 0);
138 xCell
.setFormula(CELL_TEXT
);
140 log
.println("fill sheet 1 with contnet...");
141 util
.CalcTools
.fillCalcSheetWithContent(xSheetDoc
, 1, 1, 1, 5, 5);
143 } catch (com
.sun
.star
.uno
.Exception e
) {
144 e
.printStackTrace(log
) ;
145 throw new StatusException("Can't create component.", e
) ;
146 } catch (java
.lang
.Exception e
) {
147 e
.printStackTrace(log
);
148 throw new StatusException("Can't create environment.", e
);
151 // adding tags which must be contained in XML output
152 Filter
.addTag("office:document-content");
153 Filter
.addTagEnclosed("office:body", "office:document-content");
154 Filter
.addTagEnclosed("office:script", "office:document-content");
155 Filter
.addTagEnclosed("table:table", "office:body");
156 Filter
.addTagEnclosed("table:table-column", "table:table");
157 Filter
.addTagEnclosed("table:table-row", "table:table");
158 Filter
.addTagEnclosed("table:table-cell", "table:table-row");
159 Filter
.addTagEnclosed("text:p", "table:table-cell");
160 Filter
.addCharactersEnclosed(CELL_TEXT
, "text:p");
162 // create testobject here
163 log
.println( "creating a new environment" );
164 TestEnvironment tEnv
= new TestEnvironment( oObj
);
166 tEnv
.addObjRelation("MediaDescriptor", XMLTools
.createMediaDescriptor(
167 new String
[] {"FilterName"},
168 new Object
[] {"scalc: StarOffice XML (Calc)"}));
169 tEnv
.addObjRelation("SourceDocument",xSheetDoc
);
170 tEnv
.addObjRelation("XFilter.Checker", Filter
);
176 * This class checks the XML for tags and data required and returns
177 * checking result to <code>XFilter</code> interface test. All
178 * the information about errors occurred in XML data is written
180 * @see ifc.document._XFilter
182 protected class ContentFilterChecker
extends XMLTools
.XMLTagsChecker
183 implements ifc
.document
._XFilter
.FilterChecker
{
186 * Creates a class which will write information
187 * into log specified.
189 public ContentFilterChecker(PrintWriter log
) {
194 * <code>_XFilter.FilterChecker</code> interface method
195 * which returns the result of XML checking.
196 * @return <code>true</code> if the XML data exported was
197 * valid (i.e. all necessary tags and character data exists),
198 * <code>false</code> if some errors occurred.
200 public boolean checkFilter() {