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