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