xref: /AOO41X/main/qadevOOo/tests/java/mod/_sc/XMLStylesExporter.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._sc;
29 
30 import java.io.PrintWriter;
31 
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.SOfficeFactory;
37 import util.XMLTools;
38 
39 import com.sun.star.container.XNameAccess;
40 import com.sun.star.container.XNameContainer;
41 import com.sun.star.document.XExporter;
42 import com.sun.star.lang.XComponent;
43 import com.sun.star.lang.XMultiServiceFactory;
44 import com.sun.star.style.XStyle;
45 import com.sun.star.style.XStyleFamiliesSupplier;
46 import com.sun.star.uno.Any;
47 import com.sun.star.uno.AnyConverter;
48 import com.sun.star.uno.Type;
49 import com.sun.star.uno.UnoRuntime;
50 import com.sun.star.uno.XInterface;
51 import com.sun.star.xml.sax.XDocumentHandler;
52 
53 /**
54  * Test for object which is represented by service
55  * <code>com.sun.star.comp.Calc.XMLStylesExporter</code>. <p>
56  * Object implements the following interfaces :
57  * <ul>
58  *  <li><code>com::sun::star::lang::XInitialization</code></li>
59  *  <li><code>com::sun::star::document::ExportFilter</code></li>
60  *  <li><code>com::sun::star::document::XFilter</code></li>
61  *  <li><code>com::sun::star::document::XExporter</code></li>
62  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
63  * </ul>
64  * @see com.sun.star.lang.XInitialization
65  * @see com.sun.star.document.ExportFilter
66  * @see com.sun.star.document.XFilter
67  * @see com.sun.star.document.XExporter
68  * @see com.sun.star.beans.XPropertySet
69  * @see ifc.lang._XInitialization
70  * @see ifc.document._ExportFilter
71  * @see ifc.document._XFilter
72  * @see ifc.document._XExporter
73  * @see ifc.beans._XPropertySet
74  */
75 public class XMLStylesExporter extends TestCase {
76     static XComponent xSheetDoc;
77     protected int counter = 0;
78 
79     /**
80      * New spreadsheet document created.
81      */
82     protected void initialize( TestParameters tParam, PrintWriter log ) {
83         SOfficeFactory SOF = SOfficeFactory.getFactory(  (XMultiServiceFactory) tParam.getMSF() );
84 
85         try {
86             log.println( "creating a calc document" );
87             xSheetDoc = SOF.openDoc("scalc","_blank");
88         } catch ( com.sun.star.uno.Exception e ) {
89             // Some exception occures.FAILED
90             e.printStackTrace( log );
91             throw new StatusException( "Couldn't create document", e );
92         }
93     }
94 
95     /**
96      * Spreadsheet document disposed
97      */
98     protected void cleanup( TestParameters tParam, PrintWriter log ) {
99         log.println( "    disposing xCalcDoc " );
100         util.DesktopTools.closeDoc(xSheetDoc);
101     }
102 
103     /**
104     * Creating a Testenvironment for the interfaces to be tested.
105     * Creates an instance of the service
106     * <code>com.sun.star.comp.Calc.XMLStylesExporter</code> with
107     * argument which is an implementation of <code>XDocumentHandler</code>
108     * and which can check if required tags and character data is
109     * exported. <p>
110     * The calc document is set as a source document for exporter
111     * created. New style 'NewStyle' added to style family "Cell Styles".
112     * This made for checking if this style is successfully exported within
113     * the document styles information.
114     *     Object relations created :
115     * <ul>
116     *  <li> <code>'MediaDescriptor'</code> for
117     *      {@link ifc.document._XFilter} interface </li>
118     *  <li> <code>'XFilter.Checker'</code> for
119     *      {@link ifc.document._XFilter} interface </li>
120     *  <li> <code>'SourceDocument'</code> for
121     *      {@link ifc.document._XExporter} interface </li>
122     * </ul>
123     */
124     protected synchronized TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
125 
126         SOfficeFactory SOF = SOfficeFactory.getFactory(  (XMultiServiceFactory) tParam.getMSF() );
127         XMultiServiceFactory xMSF =  (XMultiServiceFactory) tParam.getMSF() ;
128         XInterface oObj = null;
129         FilterChecker filter = new FilterChecker(log);
130         Any arg = new Any(new Type(XDocumentHandler.class),filter);
131 
132         String newName = "NewStyle" + counter++;
133         // Adding tags for checking existance of head tag and property value
134         filter.addTag(new XMLTools.Tag("office:document-styles"));
135         filter.addTag(new XMLTools.Tag("style:style","style:name", newName));
136 
137         try {
138             oObj = (XInterface) xMSF.createInstanceWithArguments(
139                 "com.sun.star.comp.Calc.XMLStylesExporter", new Object[] {arg});
140             XExporter xEx = (XExporter)
141                 UnoRuntime.queryInterface(XExporter.class,oObj);
142             xEx.setSourceDocument(xSheetDoc);
143 
144             // Obtaining and changing property values
145             XStyleFamiliesSupplier styleSup = (XStyleFamiliesSupplier)
146                 UnoRuntime.queryInterface(
147                     XStyleFamiliesSupplier.class, xSheetDoc);
148             XNameAccess StyleFamilies = styleSup.getStyleFamilies();
149             String[] styleFamiliesNames = StyleFamilies.getElementNames();
150             XNameContainer StyleFamilyName = (XNameContainer)
151                 AnyConverter.toObject(new Type(XNameContainer.class),
152                     StyleFamilies.getByName(styleFamiliesNames[0]));
153             Object SC = SOF.createInstance(
154                 xSheetDoc, "com.sun.star.style.CellStyle");
155             XStyle StyleCell = (XStyle)
156                 UnoRuntime.queryInterface(XStyle.class,SC);
157             StyleFamilyName.insertByName(newName, StyleCell);
158 
159             log.println("fill sheet 1 with contnet...");
160             util.CalcTools.fillCalcSheetWithContent(xSheetDoc, 1, 3, 3, 50, 100);
161 
162         } catch (com.sun.star.uno.Exception e) {
163             e.printStackTrace(log);
164             throw new StatusException("Can't create environment.", e);
165         } catch (java.lang.Exception e) {
166             e.printStackTrace(log);
167             throw new StatusException("Can't create environment.", e);
168         }
169 
170         // create testobject here
171         log.println( "creating a new environment" );
172         TestEnvironment tEnv = new TestEnvironment( oObj );
173 
174         tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
175             new String[] {"FilterName"},
176             new Object[] {"scalc: StarOffice XML (Calc)"}));
177         tEnv.addObjRelation("SourceDocument",xSheetDoc);
178         tEnv.addObjRelation("XFilter.Checker", filter) ;
179         return tEnv;
180     }
181 
182     /**
183      * This class checks the XML for tags and data required and returns
184      * checking result to <code>XFilter</code> interface test. All
185      * the information about errors occured in XML data is written
186      * to log specified.
187      * @see ifc.document._XFilter
188      */
189     protected class FilterChecker extends XMLTools.XMLChecker
190         implements ifc.document._XFilter.FilterChecker {
191 
192         /**
193          * Creates a class which will write information
194          * into log specified.
195          */
196         public FilterChecker(PrintWriter log) {
197             super(log,false) ;
198         }
199         /**
200          * <code>_XFilter.FilterChecker</code> interface method
201          * which returns the result of XML checking.
202          * @return <code>true</code> if the XML data exported was
203          * valid (i.e. all necessary tags and character data exists),
204          * <code>false</code> if some errors occured.
205          */
206         public boolean checkFilter() {
207             return check();
208         }
209     }
210 }
211 
212