xref: /AOO41X/main/qadevOOo/tests/java/mod/_sw/XMLStylesImporter.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._sw;
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.beans.XPropertySet;
39 import com.sun.star.lang.XMultiServiceFactory;
40 import com.sun.star.text.XFootnotesSupplier;
41 import com.sun.star.text.XTextDocument;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.uno.XInterface;
44 
45 /**
46  * Test for object which is represented by service
47  * <code>com.sun.star.comp.Writer.XMLStylesImporter</code>. <p>
48  * Object implements the following interfaces :
49  * <ul>
50  *  <li><code>com::sun::star::lang::XInitialization</code></li>
51  *  <li><code>com::sun::star::document::XImporter</code></li>
52  *  <li><code>com::sun::star::document::XFilter</code></li>
53  *  <li><code>com::sun::star::document::ImportFilter</code></li>
54  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
55  *  <li><code>com::sun::star::xml::sax::XDocumentHandler</code></li>
56 
57  * </ul>
58  * @see com.sun.star.lang.XInitialization
59  * @see com.sun.star.document.XImporter
60  * @see com.sun.star.document.XFilter
61  * @see com.sun.star.document.ImportFilter
62  * @see com.sun.star.beans.XPropertySet
63  * @see com.sun.star.xml.sax.XDocumentHandler
64  * @see ifc.lang._XInitialization
65  * @see ifc.document._XImporter
66  * @see ifc.document._XFilter
67  * @see ifc.document._XExporter
68  * @see ifc.beans._XPropertySet
69  * @see ifc.xml.sax._XDocumentHandler
70  */
71 public class XMLStylesImporter extends TestCase {
72     XTextDocument xTextDoc;
73 
74     /**
75     * New text document created.
76     */
77     protected void initialize( TestParameters tParam, PrintWriter log ) {
78         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
79 
80         try {
81             log.println( "creating a textdocument" );
82             xTextDoc = SOF.createTextDoc( null );
83         } catch ( com.sun.star.uno.Exception e ) {
84             // Some exception occures.FAILED
85             e.printStackTrace( log );
86             throw new StatusException( "Couldn't create document", e );
87         }
88     }
89 
90     /**
91     * Text document destroyed.
92     */
93     protected void cleanup( TestParameters tParam, PrintWriter log ) {
94         log.println( "    disposing xTextDoc " );
95         util.DesktopTools.closeDoc(xTextDoc);
96     }
97 
98     /**
99     * Creating a Testenvironment for the interfaces to be tested.
100     * Creates an instance of the service
101     * <code>com.sun.star.comp.Writer.XMLStylesImporter</code><p>
102     *
103     * The text document is set as a target document for importer.
104     * Imported XML-data contains only style tags including
105     * title tag with test prefix.
106     * After import prefixes getting from target document is checked.
107     *     Object relations created :
108     * <ul>
109     *  <li> <code>'XDocumentHandler.XMLData'</code> for
110     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
111     *  <li> <code>'XDocumentHandler.ImportChecker'</code> for
112     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
113     *  <li> <code>'TargetDocument'</code> for
114     *      {@link ifc.document._XImporter} interface </li>
115     * </ul>
116     */
117     public synchronized TestEnvironment createTestEnvironment( TestParameters tParam,
118                                                   PrintWriter log )
119                                                     throws StatusException {
120 
121         XInterface oObj = null;
122         Object oInt = null ;
123         final String impPrefix = "Prefix" ;
124 
125         // creation of testobject here
126         // first we write what we are intend to do to log file
127         log.println( "creating a test environment" );
128 
129         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
130 
131         try {
132             oInt = xMSF.createInstance
133                 ("com.sun.star.comp.Writer.XMLStylesImporter") ;
134 
135         } catch (com.sun.star.uno.Exception e) {
136             e.printStackTrace(log) ;
137             throw new StatusException("Can't create component.", e) ;
138         }
139 
140         oObj = (XInterface) oInt ;
141 
142         // create testobject here
143         log.println( "creating a new environment for Paragraph object" );
144         TestEnvironment tEnv = new TestEnvironment( oObj );
145 
146         // adding relation
147         tEnv.addObjRelation("TargetDocument", xTextDoc) ;
148 
149         // adding relation for XDocumentHandler
150         String[][] xml = new String[][] {
151             {"start", "office:document-styles",
152                 "xmlns:office", "CDATA", "http://openoffice.org/2000/office",
153                 "xmlns:style", "CDATA", "http://openoffice.org/2000/style",
154                 "xmlns:text", "CDATA", "http://openoffice.org/2000/text"
155                 },
156             {"start", "office:styles"},
157             {"start", "text:footnotes-configuration",
158                 "style:num-prefix", "CDATA", impPrefix},
159             {"end", "text:footnotes-configuration"},
160             {"end", "office:styles"},
161             {"end", "office:document-styles"}} ;
162 
163         tEnv.addObjRelation("XDocumentHandler.XMLData", xml) ;
164 
165         XFootnotesSupplier supp = (XFootnotesSupplier) UnoRuntime.queryInterface
166             (XFootnotesSupplier.class, xTextDoc);
167         final XPropertySet set = supp.getFootnoteSettings();
168         final PrintWriter logF = log ;
169 
170         tEnv.addObjRelation("XDocumentHandler.ImportChecker",
171             new ifc.xml.sax._XDocumentHandler.ImportChecker() {
172                 public boolean checkImport() {
173                     try {
174                         String prefix = (String) set.getPropertyValue("Prefix");
175                         logF.println("Prefix returned = '" + prefix + "'") ;
176                         return impPrefix.equals(prefix) ;
177                     } catch (com.sun.star.uno.Exception e) {
178                         logF.println("Exception occured while checking filter :") ;
179                         e.printStackTrace(logF) ;
180                         return false ;
181                     }
182                 }
183             }) ;
184 
185         return tEnv;
186     } // finish method getTestEnvironment
187 }
188 
189