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
.XNameAccess
;
31 import com
.sun
.star
.container
.XNameContainer
;
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
.style
.XStyle
;
36 import com
.sun
.star
.style
.XStyleFamiliesSupplier
;
37 import com
.sun
.star
.uno
.Any
;
38 import com
.sun
.star
.uno
.AnyConverter
;
39 import com
.sun
.star
.uno
.Type
;
40 import com
.sun
.star
.uno
.UnoRuntime
;
41 import com
.sun
.star
.uno
.XInterface
;
42 import com
.sun
.star
.xml
.sax
.XDocumentHandler
;
45 * Test for object which is represented by service
46 * <code>com.sun.star.comp.Calc.XMLStylesExporter</code>. <p>
47 * Object implements the following interfaces :
49 * <li><code>com::sun::star::lang::XInitialization</code></li>
50 * <li><code>com::sun::star::document::ExportFilter</code></li>
51 * <li><code>com::sun::star::document::XFilter</code></li>
52 * <li><code>com::sun::star::document::XExporter</code></li>
53 * <li><code>com::sun::star::beans::XPropertySet</code></li>
55 * @see com.sun.star.lang.XInitialization
56 * @see com.sun.star.document.ExportFilter
57 * @see com.sun.star.document.XFilter
58 * @see com.sun.star.document.XExporter
59 * @see com.sun.star.beans.XPropertySet
60 * @see ifc.lang._XInitialization
61 * @see ifc.document._ExportFilter
62 * @see ifc.document._XFilter
63 * @see ifc.document._XExporter
64 * @see ifc.beans._XPropertySet
66 public class XMLStylesExporter
extends TestCase
{
67 static XComponent xSheetDoc
;
68 protected int counter
= 0;
71 * New spreadsheet document created.
74 protected void initialize( TestParameters tParam
, PrintWriter log
) {
75 SOfficeFactory SOF
= SOfficeFactory
.getFactory( tParam
.getMSF() );
78 log
.println( "creating a calc document" );
79 xSheetDoc
= SOF
.openDoc("scalc","_blank");
80 } catch ( com
.sun
.star
.uno
.Exception e
) {
81 // Some exception occurs.FAILED
82 e
.printStackTrace( log
);
83 throw new StatusException( "Couldn't create document", e
);
88 * Spreadsheet document disposed
91 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
92 log
.println( " disposing xCalcDoc " );
93 util
.DesktopTools
.closeDoc(xSheetDoc
);
97 * Creating a Testenvironment for the interfaces to be tested.
98 * Creates an instance of the service
99 * <code>com.sun.star.comp.Calc.XMLStylesExporter</code> with
100 * argument which is an implementation of <code>XDocumentHandler</code>
101 * and which can check if required tags and character data is
103 * The calc document is set as a source document for exporter
104 * created. New style 'NewStyle' added to style family "Cell Styles".
105 * This made for checking if this style is successfully exported within
106 * the document styles information.
107 * Object relations created :
109 * <li> <code>'MediaDescriptor'</code> for
110 * {@link ifc.document._XFilter} interface </li>
111 * <li> <code>'XFilter.Checker'</code> for
112 * {@link ifc.document._XFilter} interface </li>
113 * <li> <code>'SourceDocument'</code> for
114 * {@link ifc.document._XExporter} interface </li>
118 protected synchronized TestEnvironment
createTestEnvironment(TestParameters tParam
, PrintWriter log
) {
120 SOfficeFactory SOF
= SOfficeFactory
.getFactory( tParam
.getMSF() );
121 XMultiServiceFactory xMSF
= tParam
.getMSF() ;
122 XInterface oObj
= null;
123 FilterChecker filter
= new FilterChecker(log
);
124 Any arg
= new Any(new Type(XDocumentHandler
.class),filter
);
126 String newName
= "NewStyle" + counter
++;
127 // Adding tags for checking existence of head tag and property value
128 filter
.addTag(new XMLTools
.Tag("office:document-styles"));
129 filter
.addTag(new XMLTools
.Tag("style:style","style:name", newName
));
132 oObj
= (XInterface
) xMSF
.createInstanceWithArguments(
133 "com.sun.star.comp.Calc.XMLStylesExporter", new Object
[] {arg
});
134 XExporter xEx
= UnoRuntime
.queryInterface(XExporter
.class,oObj
);
135 xEx
.setSourceDocument(xSheetDoc
);
137 // Obtaining and changing property values
138 XStyleFamiliesSupplier styleSup
= UnoRuntime
.queryInterface(
139 XStyleFamiliesSupplier
.class, xSheetDoc
);
140 XNameAccess StyleFamilies
= styleSup
.getStyleFamilies();
141 String
[] styleFamiliesNames
= StyleFamilies
.getElementNames();
142 XNameContainer StyleFamilyName
= (XNameContainer
)
143 AnyConverter
.toObject(new Type(XNameContainer
.class),
144 StyleFamilies
.getByName(styleFamiliesNames
[0]));
145 Object SC
= SOF
.createInstance(
146 xSheetDoc
, "com.sun.star.style.CellStyle");
147 XStyle StyleCell
= UnoRuntime
.queryInterface(XStyle
.class,SC
);
148 StyleFamilyName
.insertByName(newName
, StyleCell
);
150 log
.println("fill sheet 1 with content...");
151 util
.CalcTools
.fillCalcSheetWithContent(xSheetDoc
, 0, 3, 3, 50, 100);
153 } catch (com
.sun
.star
.uno
.Exception e
) {
154 e
.printStackTrace(log
);
155 throw new StatusException("Can't create environment.", e
);
156 } catch (java
.lang
.Exception e
) {
157 e
.printStackTrace(log
);
158 throw new StatusException("Can't create environment.", e
);
161 // create testobject here
162 log
.println( "creating a new environment" );
163 TestEnvironment tEnv
= new TestEnvironment( oObj
);
165 tEnv
.addObjRelation("MediaDescriptor", XMLTools
.createMediaDescriptor(
166 new String
[] {"FilterName"},
167 new Object
[] {"scalc: StarOffice XML (Calc)"}));
168 tEnv
.addObjRelation("SourceDocument",xSheetDoc
);
169 tEnv
.addObjRelation("XFilter.Checker", filter
) ;
174 * This class checks the XML for tags and data required and returns
175 * checking result to <code>XFilter</code> interface test. All
176 * the information about errors occurred in XML data is written
178 * @see ifc.document._XFilter
180 private class FilterChecker
extends XMLTools
.XMLChecker
181 implements ifc
.document
._XFilter
.FilterChecker
{
184 * Creates a class which will write information
185 * into log specified.
187 private FilterChecker(PrintWriter log
) {
191 * <code>_XFilter.FilterChecker</code> interface method
192 * which returns the result of XML checking.
193 * @return <code>true</code> if the XML data exported was
194 * valid (i.e. all necessary tags and character data exists),
195 * <code>false</code> if some errors occurred.
197 public boolean checkFilter() {