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