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