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.Draw;
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 import util.XMLTools;
34 
35 import com.sun.star.container.XNamed;
36 import com.sun.star.document.XExporter;
37 import com.sun.star.drawing.XDrawPage;
38 import com.sun.star.drawing.XDrawPages;
39 import com.sun.star.drawing.XDrawPagesSupplier;
40 import com.sun.star.lang.XComponent;
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.uno.Any;
43 import com.sun.star.uno.Exception;
44 import com.sun.star.uno.Type;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XInterface;
47 import com.sun.star.xml.sax.XDocumentHandler;
48 
49 /**
50  * Test for object which is represented by service
51  * <code>com.sun.star.comp.Draw.XMLExporter</code>. <p>
52  * Object implements the following interfaces :
53  * <ul>
54  *  <li><code>com::sun::star::lang::XInitialization</code></li>
55  *  <li><code>com::sun::star::document::ExportFilter</code></li>
56  *  <li><code>com::sun::star::document::XFilter</code></li>
57  *  <li><code>com::sun::star::document::XExporter</code></li>
58  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
59  * </ul>
60  * @see com.sun.star.lang.XInitialization
61  * @see com.sun.star.document.ExportFilter
62  * @see com.sun.star.document.XFilter
63  * @see com.sun.star.document.XExporter
64  * @see com.sun.star.beans.XPropertySet
65  * @see ifc.lang._XInitialization
66  * @see ifc.document._ExportFilter
67  * @see ifc.document._XFilter
68  * @see ifc.document._XExporter
69  * @see ifc.beans._XPropertySet
70  */
71 public class XMLExporter extends TestCase {
72     XComponent xDrawDoc = null;
73 
74     /**
75     * New text document created.
76     */
initialize( TestParameters tParam, PrintWriter log )77     protected void initialize( TestParameters tParam, PrintWriter log ) {
78 
79         // get a soffice factory object
80         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
81 
82         try {
83             log.println( "creating a drawdocument" );
84             xDrawDoc = SOF.createDrawDoc(null);
85         } catch ( Exception e ) {
86             // Some exception occures.FAILED
87             e.printStackTrace( log );
88             throw new StatusException( "Couldn't create document", e );
89         }
90     }
91 
92     /**
93     * Document disposed here.
94     */
cleanup( TestParameters tParam, PrintWriter log )95     protected void cleanup( TestParameters tParam, PrintWriter log ) {
96         log.println( "    disposing xDrawDoc " );
97         xDrawDoc.dispose();
98     }
99 
100     /**
101     * Creating a Testenvironment for the interfaces to be tested.
102     * Creates an instance of the service
103     * <code>com.sun.star.comp.Draw.XMLExporter</code> with
104     * argument which is an implementation of <code>XDocumentHandler</code>
105     * and which can check if required tags and character data is
106     * exported. <p>
107     * The draw document is set as a source document for exporter
108     * created. Then a new page (Slide) created in this document and
109     * both two pages renamed to 'NewSlide1' and 'NewSlide2'. After this
110     * filter checks that new tags exist in the XML output.
111     *     Object relations created :
112     * <ul>
113     *  <li> <code>'MediaDescriptor'</code> for
114     *      {@link ifc.document._XFilter} interface </li>
115     *  <li> <code>'XFilter.Checker'</code> for
116     *      {@link ifc.document._XFilter} interface </li>
117     *  <li> <code>'SourceDocument'</code> for
118     *      {@link ifc.document._XExporter} interface </li>
119     * </ul>
120     */
createTestEnvironment(TestParameters tParam, PrintWriter log)121     protected synchronized TestEnvironment createTestEnvironment
122             (TestParameters tParam, PrintWriter log) {
123 
124         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
125         XInterface oObj = null;
126         FilterChecker filter = new FilterChecker(log);
127         Any arg = new Any(new Type(XDocumentHandler.class),filter);
128 
129         // Adding tags for checking existance of head tag and other tags
130         filter.addTag(new XMLTools.Tag("office:document"));
131         filter.addTag(new XMLTools.Tag("office:body"));
132         filter.addTagEnclosed(
133             new XMLTools.Tag("draw:page"),
134             new XMLTools.Tag("office:body"));
135         filter.addTag(new XMLTools.Tag("draw:page","draw:name","NewSlide1"));
136         filter.addTag(new XMLTools.Tag("draw:page","draw:name","NewSlide2"));
137 
138         try {
139             oObj = (XInterface) xMSF.createInstanceWithArguments(
140                 "com.sun.star.comp.Draw.XMLExporter", new Object[] {arg});
141 
142 
143             XDrawPagesSupplier supp = (XDrawPagesSupplier)
144                 UnoRuntime.queryInterface(XDrawPagesSupplier.class, xDrawDoc);
145             XDrawPages set = supp.getDrawPages();
146 
147             // This is an XML-export BUG (new slide named "NewSlide2" can not be exported to XML)
148             set.insertNewByIndex(1);
149 
150             XDrawPage page1 = (XDrawPage)
151                 UnoRuntime.queryInterface(XDrawPage.class, set.getByIndex(0));
152             XNamed NPage1 = (XNamed)
153                 UnoRuntime.queryInterface(XNamed.class,page1);
154             NPage1.setName("NewSlide1");
155             XDrawPage page2 = (XDrawPage)
156                 UnoRuntime.queryInterface(XDrawPage.class, set.getByIndex(1));
157             XNamed NPage2 = (XNamed)
158                 UnoRuntime.queryInterface(XNamed.class,page2);
159             NPage2.setName("NewSlide2");
160 
161             XExporter xEx = (XExporter)
162                 UnoRuntime.queryInterface(XExporter.class,oObj);
163             xEx.setSourceDocument(xDrawDoc);
164 
165         } catch (com.sun.star.uno.Exception e) {
166             e.printStackTrace(log) ;
167             throw new StatusException("Can't create component.", e) ;
168         }
169 
170         // create testobject here
171         log.println( "creating a new environment" );
172         TestEnvironment tEnv = new TestEnvironment( oObj );
173 
174         tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
175             new String[] {"FilterName"},
176             new Object[] {"sdraw: StarOffice XML (Draw)"}));
177         tEnv.addObjRelation("SourceDocument",xDrawDoc);
178         tEnv.addObjRelation("XFilter.Checker", filter);
179         return tEnv;
180     }
181 
182     /**
183      * This class checks the XML for tags and data required and returns
184      * checking result to <code>XFilter</code> interface test. All
185      * the information about errors occured in XML data is written
186      * to log specified.
187      * @see ifc.document._XFilter
188      */
189     protected class FilterChecker extends XMLTools.XMLChecker
190         implements ifc.document._XFilter.FilterChecker {
191 
192         /**
193          * Creates a class which will write information
194          * into log specified.
195          */
FilterChecker(PrintWriter log)196         public FilterChecker(PrintWriter log) {
197             super(log,true) ;
198         }
199         /**
200          * <code>_XFilter.FilterChecker</code> interface method
201          * which returns the result of XML checking.
202          * @return <code>true</code> if the XML data exported was
203          * valid (i.e. all necessary tags and character data exists),
204          * <code>false</code> if some errors occured.
205          */
checkFilter()206         public boolean checkFilter() {
207             return check();
208         }
209     }
210 
211 }
212