xref: /AOO41X/main/qadevOOo/tests/java/mod/_xmloff/Impress/XMLExporter.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.Impress;
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.Impress.XMLExporter</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 XMLExporter extends TestCase {
76     XComponent xImpressDoc = null;
77 
78     /**
79      * New impress document created.
80      */
81     protected void initialize( TestParameters tParam, PrintWriter log ) {
82 
83         // get a soffice factory object
84         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
85 
86         try {
87             log.println( "creating an impress document" );
88             xImpressDoc = SOF.createImpressDoc(null);;
89         } catch ( Exception e ) {
90             // Some exception occures.FAILED
91             e.printStackTrace( log );
92             throw new StatusException( "Couldn't create document", e );
93         }
94     }
95 
96     /**
97      * Document disposed here.
98      */
99     protected void cleanup( TestParameters tParam, PrintWriter log ) {
100         log.println( "    disposing xImpressDoc " );
101         xImpressDoc.dispose();
102     }
103 
104     /**
105     * Creating a Testenvironment for the interfaces to be tested.
106     * Creates an instance of the service
107     * <code>com.sun.star.comp.Impress.XMLExporter</code> with
108     * argument which is an implementation of <code>XDocumentHandler</code>
109     * and which can check if required tags and character data is
110     * exported. <p>
111     * The impress document is set as a source document for exporter
112     * created. A new draw page with specific name created. This made
113     * for checking if this draw page is successfully exported within
114     * the document.
115     *     Object relations created :
116     * <ul>
117     *  <li> <code>'MediaDescriptor'</code> for
118     *      {@link ifc.document._XFilter} interface </li>
119     *  <li> <code>'XFilter.Checker'</code> for
120     *      {@link ifc.document._XFilter} interface </li>
121     *  <li> <code>'SourceDocument'</code> for
122     *      {@link ifc.document._XExporter} interface </li>
123     * </ul>
124     */
125     protected synchronized TestEnvironment createTestEnvironment
126             (TestParameters tParam, PrintWriter log) {
127 
128         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
129         XInterface oObj = null;
130 
131         FilterChecker Filter = new FilterChecker(log);
132         Any arg = new Any(new Type(XDocumentHandler.class), Filter);
133 
134         final String NAME = "XMLExporter";
135 
136         try {
137             oObj = (XInterface) xMSF.createInstanceWithArguments(
138                 "com.sun.star.comp.Impress.XMLExporter", new Object[] {arg});
139 
140             //get draw pages
141             XDrawPagesSupplier drawPagesSupplier = (XDrawPagesSupplier)
142                 UnoRuntime.queryInterface(XDrawPagesSupplier.class, xImpressDoc);
143             XDrawPages drawPages = drawPagesSupplier.getDrawPages();
144             //insert new draw page
145             XDrawPage newDrawPage = drawPages.insertNewByIndex(0);
146             //set specific test name
147             XNamed newPageNamed = (XNamed)
148                 UnoRuntime.queryInterface(XNamed.class, newDrawPage);
149             newPageNamed.setName(NAME);
150             XExporter xEx = (XExporter)
151                 UnoRuntime.queryInterface(XExporter.class,oObj);
152             xEx.setSourceDocument(xImpressDoc);
153 
154         } catch (com.sun.star.uno.Exception e) {
155             e.printStackTrace(log) ;
156             throw new StatusException("Can't create component.", e) ;
157         }
158 
159         // adding tags which must be contained in XML output
160         Filter.addTag( new XMLTools.Tag("office:document") );
161         Filter.addTagEnclosed(
162             new XMLTools.Tag("office:body"),
163             new XMLTools.Tag("office:document") );
164         Filter.addTagEnclosed(
165             new XMLTools.Tag("draw:page", "draw:name", NAME),
166             new XMLTools.Tag("office:body") );
167 
168 
169         // create testobject here
170         log.println( "creating a new environment" );
171         TestEnvironment tEnv = new TestEnvironment( oObj );
172 
173         tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
174             new String[] {"FilterName"},
175             new Object[] {"simpress: StarOffice XML (Impress)"}));
176         tEnv.addObjRelation("SourceDocument",xImpressDoc);
177         tEnv.addObjRelation("XFilter.Checker", Filter) ;
178 
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          */
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          */
206         public boolean checkFilter() {
207             return check();
208         }
209     }
210 }
211