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._sm;
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.XComponent;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XInterface;
43 
44 /**
45  * Test for object which is represented by service
46  * <code>com.sun.star.comp.Math.XMLImporter</code>. <p>
47  * Object implements the following interfaces :
48  * <ul>
49  *  <li><code>com::sun::star::lang::XInitialization</code></li>
50  *  <li><code>com::sun::star::document::XImporter</code></li>
51  *  <li><code>com::sun::star::document::XFilter</code></li>
52  *  <li><code>com::sun::star::document::ImportFilter</code></li>
53  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
54  *  <li><code>com::sun::star::xml::sax::XDocumentHandler</code></li>
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 XMLImporter extends TestCase {
70     XComponent xMathDoc;
71 
72 
73     /**
74     * New math document created.
75     */
76     protected void initialize( TestParameters tParam, PrintWriter log ) {
77 
78         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
79         try {
80             xMathDoc = SOF.openDoc("smath","_blank");
81         } catch (com.sun.star.uno.Exception ex) {
82             ex.printStackTrace( log );
83             throw new StatusException( "Couldn't create document", ex );
84         }
85     }
86 
87     /**
88     * Disposes document.
89     */
90     protected void cleanup( TestParameters tParam, PrintWriter log ) {
91         log.println( "    disposing xMathDoc " );
92         xMathDoc.dispose();
93     }
94 
95     /**
96     * Creating a Testenvironment for the interfaces to be tested.
97     * Creates an instance of the service
98     * <code>com.sun.star.comp.Math.XMLImporter</code><p>
99     *
100     * The chart document is set as a target document for importer.
101     * Imported XML-data contains the tag which specifies a formula
102     * in the document.
103     * After import the formula getting from target document is checked.
104     *     Object relations created :
105     * <ul>
106     *  <li> <code>'XDocumentHandler.XMLData'</code> for
107     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
108     *  <li> <code>'XDocumentHandler.ImportChecker'</code> for
109     *      {@link ifc.xml.sax._XDocumentHandler} interface </li>
110     *  <li> <code>'TargetDocument'</code> for
111     *      {@link ifc.document._XImporter} interface </li>
112     * </ul>
113     */
114     public synchronized TestEnvironment createTestEnvironment
115             ( TestParameters Param, PrintWriter log )
116             throws StatusException {
117 
118         XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
119         XInterface oObj = null;
120         final String impFormula = "a - b" ;
121 
122         try {
123             oObj = (XInterface)xMSF.createInstance(
124                     "com.sun.star.comp.Math.XMLImporter");
125         } catch (com.sun.star.uno.Exception e) {
126             e.printStackTrace(log);
127             throw new StatusException("Unexpected exception", e);
128         }
129 
130         TestEnvironment tEnv = new TestEnvironment(oObj);
131 
132         tEnv.addObjRelation("TargetDocument",xMathDoc);
133 
134         String[][] xml = new String[][] {
135             {"start", "math:math",
136                 "xmlns:math", "CDATA", "http://www.w3.org/1998/Math/MathML"},
137             {"start", "math:semantics"},
138             {"start", "math:annotation",
139                 "math:encoding", "CDATA", "StarMath 5.0"},
140             {"chars", impFormula},
141             {"end", "math:annotation"},
142             {"end", "math:semantics"},
143             {"end", "math:math"}} ;
144 
145         tEnv.addObjRelation("XDocumentHandler.XMLData", xml) ;
146 
147         final PrintWriter logF = log ;
148         final XPropertySet xPS = (XPropertySet) UnoRuntime.queryInterface
149             (XPropertySet.class, xMathDoc) ;
150 
151         tEnv.addObjRelation("XDocumentHandler.ImportChecker",
152             new ifc.xml.sax._XDocumentHandler.ImportChecker() {
153                 public boolean checkImport() {
154                     try {
155                         String gFormula = (String) xPS.getPropertyValue
156                             ("Formula") ;
157                         logF.println("Formula returned = '" + gFormula + "'") ;
158                         return impFormula.equals(gFormula) ;
159                     } catch (com.sun.star.uno.Exception e) {
160                         logF.println("Exception occured while checking filter :") ;
161                         e.printStackTrace(logF) ;
162                         return false ;
163                     }
164                 }
165             }) ;
166 
167         return tEnv;
168     }
169 }
170 
171