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