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