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.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 xDrawDoc = null;
77 
78     /**
79     * New text 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 a drawdocument" );
88             xDrawDoc = SOF.createDrawDoc(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 xDrawDoc " );
101         xDrawDoc.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.Draw.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 draw document is set as a source document for exporter
112     * created. Then a new page (Slide) created in this document and
113     * both two pages renamed to 'NewSlide1' and 'NewSlide2'. After this
114     * filter checks that new tags exist in the XML output.
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         FilterChecker filter = new FilterChecker(log);
131         Any arg = new Any(new Type(XDocumentHandler.class),filter);
132 
133         // Adding tags for checking existance of head tag and other tags
134         filter.addTag(new XMLTools.Tag("office:document"));
135         filter.addTag(new XMLTools.Tag("office:body"));
136         filter.addTagEnclosed(
137             new XMLTools.Tag("draw:page"),
138             new XMLTools.Tag("office:body"));
139         filter.addTag(new XMLTools.Tag("draw:page","draw:name","NewSlide1"));
140         filter.addTag(new XMLTools.Tag("draw:page","draw:name","NewSlide2"));
141 
142         try {
143             oObj = (XInterface) xMSF.createInstanceWithArguments(
144                 "com.sun.star.comp.Draw.XMLExporter", new Object[] {arg});
145 
146 
147             XDrawPagesSupplier supp = (XDrawPagesSupplier)
148                 UnoRuntime.queryInterface(XDrawPagesSupplier.class, xDrawDoc);
149             XDrawPages set = supp.getDrawPages();
150 
151             // This is an XML-export BUG (new slide named "NewSlide2" can not be exported to XML)
152             set.insertNewByIndex(1);
153 
154             XDrawPage page1 = (XDrawPage)
155                 UnoRuntime.queryInterface(XDrawPage.class, set.getByIndex(0));
156             XNamed NPage1 = (XNamed)
157                 UnoRuntime.queryInterface(XNamed.class,page1);
158             NPage1.setName("NewSlide1");
159             XDrawPage page2 = (XDrawPage)
160                 UnoRuntime.queryInterface(XDrawPage.class, set.getByIndex(1));
161             XNamed NPage2 = (XNamed)
162                 UnoRuntime.queryInterface(XNamed.class,page2);
163             NPage2.setName("NewSlide2");
164 
165             XExporter xEx = (XExporter)
166                 UnoRuntime.queryInterface(XExporter.class,oObj);
167             xEx.setSourceDocument(xDrawDoc);
168 
169         } catch (com.sun.star.uno.Exception e) {
170             e.printStackTrace(log) ;
171             throw new StatusException("Can't create component.", e) ;
172         }
173 
174         // create testobject here
175         log.println( "creating a new environment" );
176         TestEnvironment tEnv = new TestEnvironment( oObj );
177 
178         tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
179             new String[] {"FilterName"},
180             new Object[] {"sdraw: StarOffice XML (Draw)"}));
181         tEnv.addObjRelation("SourceDocument",xDrawDoc);
182         tEnv.addObjRelation("XFilter.Checker", filter);
183         return tEnv;
184     }
185 
186     /**
187      * This class checks the XML for tags and data required and returns
188      * checking result to <code>XFilter</code> interface test. All
189      * the information about errors occured in XML data is written
190      * to log specified.
191      * @see ifc.document._XFilter
192      */
193     protected class FilterChecker extends XMLTools.XMLChecker
194         implements ifc.document._XFilter.FilterChecker {
195 
196         /**
197          * Creates a class which will write information
198          * into log specified.
199          */
200         public FilterChecker(PrintWriter log) {
201             super(log,true) ;
202         }
203         /**
204          * <code>_XFilter.FilterChecker</code> interface method
205          * which returns the result of XML checking.
206          * @return <code>true</code> if the XML data exported was
207          * valid (i.e. all necessary tags and character data exists),
208          * <code>false</code> if some errors occured.
209          */
210         public boolean checkFilter() {
211             return check();
212         }
213     }
214 
215 }
216