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
;
24 import lib
.TestEnvironment
;
25 import lib
.TestParameters
;
26 import util
.SOfficeFactory
;
29 import com
.sun
.star
.container
.XNameAccess
;
30 import com
.sun
.star
.container
.XNameContainer
;
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
.style
.XStyle
;
35 import com
.sun
.star
.style
.XStyleFamiliesSupplier
;
36 import com
.sun
.star
.uno
.Any
;
37 import com
.sun
.star
.uno
.AnyConverter
;
38 import com
.sun
.star
.uno
.Type
;
39 import com
.sun
.star
.uno
.UnoRuntime
;
40 import com
.sun
.star
.uno
.XInterface
;
41 import com
.sun
.star
.xml
.sax
.XDocumentHandler
;
44 * Test for object which is represented by service
45 * <code>com.sun.star.comp.Calc.XMLStylesExporter</code>. <p>
46 * Object implements the following interfaces :
48 * <li><code>com::sun::star::lang::XInitialization</code></li>
49 * <li><code>com::sun::star::document::ExportFilter</code></li>
50 * <li><code>com::sun::star::document::XFilter</code></li>
51 * <li><code>com::sun::star::document::XExporter</code></li>
52 * <li><code>com::sun::star::beans::XPropertySet</code></li>
54 * @see com.sun.star.lang.XInitialization
55 * @see com.sun.star.document.ExportFilter
56 * @see com.sun.star.document.XFilter
57 * @see com.sun.star.document.XExporter
58 * @see com.sun.star.beans.XPropertySet
59 * @see ifc.lang._XInitialization
60 * @see ifc.document._ExportFilter
61 * @see ifc.document._XFilter
62 * @see ifc.document._XExporter
63 * @see ifc.beans._XPropertySet
65 public class XMLStylesExporter
extends TestCase
{
66 static XComponent xSheetDoc
;
67 protected int counter
= 0;
70 * New spreadsheet document created.
73 protected void initialize( TestParameters tParam
, PrintWriter log
) throws Exception
{
74 SOfficeFactory SOF
= SOfficeFactory
.getFactory( tParam
.getMSF() );
75 log
.println( "creating a calc document" );
76 xSheetDoc
= SOF
.openDoc("scalc","_blank");
80 * Spreadsheet document disposed
83 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
84 log
.println( " disposing xCalcDoc " );
85 util
.DesktopTools
.closeDoc(xSheetDoc
);
89 * Creating a TestEnvironment for the interfaces to be tested.
90 * Creates an instance of the service
91 * <code>com.sun.star.comp.Calc.XMLStylesExporter</code> with
92 * argument which is an implementation of <code>XDocumentHandler</code>
93 * and which can check if required tags and character data is
95 * The calc document is set as a source document for exporter
96 * created. New style 'NewStyle' added to style family "Cell Styles".
97 * This made for checking if this style is successfully exported within
98 * the document styles information.
99 * Object relations created :
101 * <li> <code>'MediaDescriptor'</code> for
102 * {@link ifc.document._XFilter} interface </li>
103 * <li> <code>'XFilter.Checker'</code> for
104 * {@link ifc.document._XFilter} interface </li>
105 * <li> <code>'SourceDocument'</code> for
106 * {@link ifc.document._XExporter} interface </li>
110 protected TestEnvironment
createTestEnvironment(TestParameters tParam
, PrintWriter log
) throws Exception
{
112 SOfficeFactory SOF
= SOfficeFactory
.getFactory( tParam
.getMSF() );
113 XMultiServiceFactory xMSF
= tParam
.getMSF() ;
114 XInterface oObj
= null;
115 FilterChecker filter
= new FilterChecker(log
);
116 Any arg
= new Any(new Type(XDocumentHandler
.class),filter
);
118 String newName
= "NewStyle" + counter
++;
119 // Adding tags for checking existence of head tag and property value
120 filter
.addTag(new XMLTools
.Tag("office:document-styles"));
121 filter
.addTag(new XMLTools
.Tag("style:style","style:name", newName
));
123 oObj
= (XInterface
) xMSF
.createInstanceWithArguments(
124 "com.sun.star.comp.Calc.XMLStylesExporter", new Object
[] {arg
});
125 XExporter xEx
= UnoRuntime
.queryInterface(XExporter
.class,oObj
);
126 xEx
.setSourceDocument(xSheetDoc
);
128 // Obtaining and changing property values
129 XStyleFamiliesSupplier styleSup
= UnoRuntime
.queryInterface(
130 XStyleFamiliesSupplier
.class, xSheetDoc
);
131 XNameAccess StyleFamilies
= styleSup
.getStyleFamilies();
132 String
[] styleFamiliesNames
= StyleFamilies
.getElementNames();
133 XNameContainer StyleFamilyName
= (XNameContainer
)
134 AnyConverter
.toObject(new Type(XNameContainer
.class),
135 StyleFamilies
.getByName(styleFamiliesNames
[0]));
136 Object SC
= SOF
.createInstance(
137 xSheetDoc
, "com.sun.star.style.CellStyle");
138 XStyle StyleCell
= UnoRuntime
.queryInterface(XStyle
.class,SC
);
139 StyleFamilyName
.insertByName(newName
, StyleCell
);
141 log
.println("fill sheet 1 with content...");
142 util
.CalcTools
.fillCalcSheetWithContent(xSheetDoc
, 0, 3, 3, 50, 100);
144 // create testobject here
145 log
.println( "creating a new environment" );
146 TestEnvironment tEnv
= new TestEnvironment( oObj
);
148 tEnv
.addObjRelation("MediaDescriptor", XMLTools
.createMediaDescriptor(
149 new String
[] {"FilterName"},
150 new Object
[] {"scalc: StarOffice XML (Calc)"}));
151 tEnv
.addObjRelation("SourceDocument",xSheetDoc
);
152 tEnv
.addObjRelation("XFilter.Checker", filter
) ;
157 * This class checks the XML for tags and data required and returns
158 * checking result to <code>XFilter</code> interface test. All
159 * the information about errors occurred in XML data is written
161 * @see ifc.document._XFilter
163 private class FilterChecker
extends XMLTools
.XMLChecker
164 implements ifc
.document
._XFilter
.FilterChecker
{
167 * Creates a class which will write information
168 * into log specified.
170 private FilterChecker(PrintWriter log
) {
174 * <code>_XFilter.FilterChecker</code> interface method
175 * which returns the result of XML checking.
176 * @return <code>true</code> if the XML data exported was
177 * valid (i.e. all necessary tags and character data exists),
178 * <code>false</code> if some errors occurred.
180 public boolean checkFilter() {