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