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