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