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 
34 import com.sun.star.container.XNameAccess;
35 import com.sun.star.document.XImporter;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.sheet.XSpreadsheetDocument;
39 import com.sun.star.style.XStyleFamiliesSupplier;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XInterface;
42 
43 /**
44  * Test for object which is represented by service
45  * <code>com.sun.star.comp.Calc.XMLStylesImporter</code>. <p>
46  * Object implements the following interfaces :
47  * <ul>
48  *  <li><code>com::sun::star::lang::XInitialization</code></li>
49  *  <li><code>com::sun::star::document::XImporter</code></li>
50  *  <li><code>com::sun::star::document::XFilter</code></li>
51  *  <li><code>com::sun::star::document::ImportFilter</code></li>
52  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
53  *  <li><code>com::sun::star::xml::sax::XDocumentHandler</code></li>
54 
55  * </ul>
56  * @see com.sun.star.lang.XInitialization
57  * @see com.sun.star.document.XImporter
58  * @see com.sun.star.document.XFilter
59  * @see com.sun.star.document.ImportFilter
60  * @see com.sun.star.beans.XPropertySet
61  * @see com.sun.star.xml.sax.XDocumentHandler
62  * @see ifc.lang._XInitialization
63  * @see ifc.document._XImporter
64  * @see ifc.document._XFilter
65  * @see ifc.document._XExporter
66  * @see ifc.beans._XPropertySet
67  * @see ifc.xml.sax._XDocumentHandler
68  */
69 public class XMLStylesImporter extends TestCase {
70     static XSpreadsheetDocument xSheetDoc;
71     static XComponent comp ;
72 
73     /**
74     * New spreadsheet document created.
75     */
initialize( TestParameters tParam, PrintWriter log )76     protected void initialize( TestParameters tParam, PrintWriter log ) {
77         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
78 
79         try {
80             log.println( "creating a Spreadsheet document" );
81             xSheetDoc = SOF.createCalcDoc( null );
82             comp = (XComponent) UnoRuntime.queryInterface
83                 (XComponent.class, xSheetDoc) ;
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 destroyed.
93     */
cleanup( TestParameters tParam, PrintWriter log )94     protected void cleanup( TestParameters tParam, PrintWriter log ) {
95         log.println( "    disposing document " );
96         util.DesktopTools.closeDoc(comp);
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.XMLStylesImporter</code><p>
103     *
104     * The calc document is set as a target document for importer.
105     * Imported XML-data contains only style tags including tag
106     * with new style name.
107     * After import style names getting from
108     * target document is checked.
109     *     Object relations created :
110     * <ul>
111     *  <li> <code>'XDocumentHandler.XMLData'</code> for
112     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
113     *  <li> <code>'XDocumentHandler.ImportChecker'</code> for
114     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
115     *  <li> <code>'TargetDocument'</code> for
116     *      {@link ifc.document._XImporter} interface </li>
117     * </ul>
118     */
createTestEnvironment( TestParameters tParam, PrintWriter log )119     public synchronized TestEnvironment createTestEnvironment( TestParameters tParam,
120                                                   PrintWriter log )
121                                                     throws StatusException {
122 
123         XInterface oObj = null;
124         Object oInt = null ;
125 
126         // creation of testobject here
127         // first we write what we are intend to do to log file
128         log.println( "creating a test environment" );
129 
130         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
131 
132         try {
133             oInt = xMSF.createInstance
134                 ("com.sun.star.comp.Calc.XMLStylesImporter") ;
135             XImporter imp = (XImporter) UnoRuntime.queryInterface
136                 (XImporter.class, oInt) ;
137             imp.setTargetDocument(comp) ;
138         } catch (com.sun.star.uno.Exception e) {
139             e.printStackTrace(log) ;
140             throw new StatusException("Can't create component.", e) ;
141         }
142 
143         oObj = (XInterface) oInt ;
144 
145         // create testobject here
146         log.println( "creating a new environment for Paragraph object" );
147         TestEnvironment tEnv = new TestEnvironment( oObj );
148 
149         // adding relation
150         tEnv.addObjRelation("TargetDocument", comp) ;
151 
152         final String impStyleName = "XMLStylesImporter" ;
153 
154         // adding relation for XDocumentHandler
155         String[][] xml = new String[][] {
156             {"start", "office:document-styles",
157                 "xmlns:office", "CDATA", "http://openoffice.org/2000/office",
158                 "xmlns:style", "CDATA", "http://openoffice.org/2000/style",
159                 "xmlns:fo", "CDATA", "http://www.w3.org/1999/XSL/Format",
160                 "xmlns:table", "CDATA", "http://openoffice.org/2000/table"},
161             {"start", "office:styles"},
162             {"start", "style:style",
163                 "style:name", "CDATA", impStyleName,
164                 "style:family", "CDATA", "table-cell",
165                 "style:parent-style-name", "CDATA", "Default"},
166             { "start", "style:properties",
167                 "fo:width", "CDATA", "12cm",
168                 "fo:background-color", "CDATA", "light-grey"},
169             {"end", "style:properties"},
170             {"end", "style:style"},
171             {"end", "office:styles"},
172             {"end", "office:document-styles"}} ;
173 
174         tEnv.addObjRelation("XDocumentHandler.XMLData", xml) ;
175 
176         XNameAccess styles = null ;
177         try {
178             XStyleFamiliesSupplier sup = (XStyleFamiliesSupplier)
179                 UnoRuntime.queryInterface
180                 (XStyleFamiliesSupplier.class, xSheetDoc);
181             XNameAccess oStyleFamilies = sup.getStyleFamilies();
182             Object family = oStyleFamilies.getByName("CellStyles") ;
183             styles = (XNameAccess) UnoRuntime.queryInterface
184                 (XNameAccess.class, family) ;
185             log.println("Styles before:") ;
186             String[] names = styles.getElementNames() ;
187             for (int i = 0; i < names.length; i++) {
188                 log.println("  " + names[i]) ;
189             }
190         } catch (com.sun.star.uno.Exception e) {
191             e.printStackTrace(log) ;
192         }
193         final XNameAccess stylesF = styles ;
194         final PrintWriter logF = log ;
195 
196         tEnv.addObjRelation("XDocumentHandler.ImportChecker",
197             new ifc.xml.sax._XDocumentHandler.ImportChecker() {
198                 public boolean checkImport() {
199                     logF.println("Styles after:") ;
200                     String[] names = stylesF.getElementNames() ;
201                     for (int i = 0; i < names.length; i++) {
202                         logF.println("  " + names[i]) ;
203                     }
204                     return stylesF.hasByName(impStyleName) ;
205                 }
206             }) ;
207 
208         return tEnv ;
209     }
210 }
211 
212