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
.container
.XNamed
;
32 import com
.sun
.star
.document
.XExporter
;
33 import com
.sun
.star
.lang
.XComponent
;
34 import com
.sun
.star
.lang
.XMultiServiceFactory
;
35 import com
.sun
.star
.sheet
.XSpreadsheet
;
36 import com
.sun
.star
.sheet
.XSpreadsheetDocument
;
37 import com
.sun
.star
.sheet
.XSpreadsheets
;
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.XMLExporter</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 XMLExporter
extends TestCase
{
68 static XComponent xSheetDoc
;
71 * New spreadsheet document created.
73 protected void initialize( TestParameters tParam
, PrintWriter log
) {
74 SOfficeFactory SOF
= SOfficeFactory
.getFactory( (XMultiServiceFactory
)tParam
.getMSF() );
76 log
.println( "creating a calc document" );
77 xSheetDoc
= SOF
.openDoc("scalc","_blank");
78 } catch ( com
.sun
.star
.uno
.Exception e
) {
79 // Some exception occurs.FAILED
80 e
.printStackTrace( log
);
81 throw new StatusException( "Couldn't create document", e
);
86 * Spreadsheet document disposed
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.XMLExporter</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. Checking whether tags for table corresponding to some sheet
103 * Object relations created :
105 * <li> <code>'MediaDescriptor'</code> for
106 * {@link ifc.document._XFilter} interface </li>
107 * <li> <code>'XFilter.Checker'</code> for
108 * {@link ifc.document._XFilter} interface </li>
109 * <li> <code>'SourceDocument'</code> for
110 * {@link ifc.document._XExporter} interface </li>
113 protected synchronized TestEnvironment
createTestEnvironment(TestParameters tParam
, PrintWriter log
) {
115 XMultiServiceFactory xMSF
= (XMultiServiceFactory
)tParam
.getMSF() ;
116 XInterface oObj
= null;
117 FilterChecker Filter
= new FilterChecker(log
);
118 Any arg
= new Any(new Type(XDocumentHandler
.class), Filter
);
119 final String SHEET_NAME
= "XMLExporter_SheetTestName";
122 oObj
= (XInterface
) xMSF
.createInstanceWithArguments(
123 "com.sun.star.comp.Calc.XMLExporter", new Object
[] {arg
} );
124 XExporter xEx
= UnoRuntime
.queryInterface
125 (XExporter
.class,oObj
);
126 xEx
.setSourceDocument(xSheetDoc
);
129 XSpreadsheetDocument xSpreadsheetDoc
= UnoRuntime
.queryInterface(XSpreadsheetDocument
.class, xSheetDoc
);
130 XSpreadsheets xSpreadsheets
= xSpreadsheetDoc
.getSheets();
131 XIndexAccess xSheetsIndexArray
= UnoRuntime
.queryInterface(XIndexAccess
.class, xSpreadsheets
);
132 XSpreadsheet xSheet
= (XSpreadsheet
) AnyConverter
.toObject(
133 new Type(XSpreadsheet
.class),xSheetsIndexArray
.getByIndex(0));
134 XNamed xSheetNamed
= UnoRuntime
.queryInterface(XNamed
.class, xSheet
);
135 xSheetNamed
.setName(SHEET_NAME
);
137 log
.println("fill sheet with contnet...");
138 util
.CalcTools
.fillCalcSheetWithContent(xSheet
, 3, 3, 50, 50);
140 } catch (com
.sun
.star
.uno
.Exception e
) {
141 e
.printStackTrace(log
) ;
142 throw new StatusException("Can't create component.", e
) ;
143 } catch (java
.lang
.Exception e
) {
144 e
.printStackTrace(log
);
145 throw new StatusException("Can't create environment.", e
);
148 // adding tags which must be contained in XML output
149 Filter
.addTag( new XMLTools
.Tag("office:document") );
150 Filter
.addTagEnclosed(
151 new XMLTools
.Tag("office:meta"),
152 new XMLTools
.Tag("office:document") );
153 Filter
.addTagEnclosed(
154 new XMLTools
.Tag("office:settings"),
155 new XMLTools
.Tag("office:document") );
156 Filter
.addTagEnclosed(
157 new XMLTools
.Tag("office:script"),
158 new XMLTools
.Tag("office:document") );
159 Filter
.addTagEnclosed(
160 new XMLTools
.Tag("office:styles"),
161 new XMLTools
.Tag("office:document") );
162 Filter
.addTagEnclosed(
163 new XMLTools
.Tag("office:body"),
164 new XMLTools
.Tag("office:document") );
165 Filter
.addTagEnclosed(
166 new XMLTools
.Tag("table:table"),
167 new XMLTools
.Tag("office:body") );
169 new XMLTools
.Tag("table:table", "table:name", SHEET_NAME
) );
171 // create testobject here
172 log
.println( "creating a new environment" );
173 TestEnvironment tEnv
= new TestEnvironment( oObj
);
175 tEnv
.addObjRelation("MediaDescriptor", XMLTools
.createMediaDescriptor(
176 new String
[] {"FilterName"},
177 new Object
[] {"scalc: StarOffice XML (Calc)"}));
178 tEnv
.addObjRelation("SourceDocument",xSheetDoc
);
179 tEnv
.addObjRelation("XFilter.Checker", Filter
) ;
184 * This class checks the XML for tags and data required and returns
185 * checking result to <code>XFilter</code> interface test. All
186 * the information about errors occurred in XML data is written
188 * @see ifc.document._XFilter
190 protected class FilterChecker
extends XMLTools
.XMLChecker
191 implements ifc
.document
._XFilter
.FilterChecker
{
194 * Creates a class which will write information
195 * into log specified.
197 public FilterChecker(PrintWriter log
) {
201 * <code>_XFilter.FilterChecker</code> interface method
202 * which returns the result of XML checking.
203 * @return <code>true</code> if the XML data exported was
204 * valid (i.e. all necessary tags and character data exists),
205 * <code>false</code> if some errors occurred.
207 public boolean checkFilter() {