2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import java
.io
.PrintWriter
;
23 import lib
.StatusException
;
25 import lib
.TestEnvironment
;
26 import lib
.TestParameters
;
27 import util
.SOfficeFactory
;
30 import com
.sun
.star
.document
.XExporter
;
31 import com
.sun
.star
.lang
.XMultiServiceFactory
;
32 import com
.sun
.star
.text
.XSimpleText
;
33 import com
.sun
.star
.text
.XTextCursor
;
34 import com
.sun
.star
.text
.XTextDocument
;
35 import com
.sun
.star
.uno
.Any
;
36 import com
.sun
.star
.uno
.Type
;
37 import com
.sun
.star
.uno
.UnoRuntime
;
38 import com
.sun
.star
.uno
.XInterface
;
39 import com
.sun
.star
.xml
.sax
.XDocumentHandler
;
42 * Test for object which is represented by service
43 * <code>com.sun.star.comp.Calc.XMLExporter</code>. <p>
44 * Object implements the following interfaces :
46 * <li><code>com::sun::star::lang::XInitialization</code></li>
47 * <li><code>com::sun::star::document::ExportFilter</code></li>
48 * <li><code>com::sun::star::document::XFilter</code></li>
49 * <li><code>com::sun::star::document::XExporter</code></li>
50 * <li><code>com::sun::star::beans::XPropertySet</code></li>
52 * @see com.sun.star.lang.XInitialization
53 * @see com.sun.star.document.ExportFilter
54 * @see com.sun.star.document.XFilter
55 * @see com.sun.star.document.XExporter
56 * @see com.sun.star.beans.XPropertySet
57 * @see ifc.lang._XInitialization
58 * @see ifc.document._ExportFilter
59 * @see ifc.document._XFilter
60 * @see ifc.document._XExporter
61 * @see ifc.beans._XPropertySet
63 public class XMLExporter
extends TestCase
{
64 XTextDocument xTextDoc
;
68 * New text document created.
71 protected void initialize( TestParameters tParam
, PrintWriter log
) {
72 SOfficeFactory SOF
= SOfficeFactory
.getFactory( tParam
.getMSF() );
75 log
.println( "creating a textdocument" );
76 xTextDoc
= SOF
.createTextDoc( null );
77 } catch ( com
.sun
.star
.uno
.Exception e
) {
78 // Some exception occurs.FAILED
79 e
.printStackTrace( log
);
80 throw new StatusException( "Couldn't create document", e
);
85 * Document disposed here.
88 protected void cleanup( TestParameters tParam
, PrintWriter log
) {
89 log
.println( " disposing xTextDoc " );
90 util
.DesktopTools
.closeDoc(xTextDoc
);
94 * Creating a Testenvironment for the interfaces to be tested.
95 * Creates an instance of the service
96 * <code>com.sun.star.comp.Calc.XMLExporter</code> with
97 * argument which is an implementation of <code>XDocumentHandler</code>
98 * and which can check if required tags and character data is
100 * The text document is set as a source document for exporter
101 * created. A string inserted into document. This made
102 * for checking if this string is successfully exported within
103 * the document content.
104 * Object relations created :
106 * <li> <code>'MediaDescriptor'</code> for
107 * {@link ifc.document._XFilter} interface </li>
108 * <li> <code>'XFilter.Checker'</code> for
109 * {@link ifc.document._XFilter} interface </li>
110 * <li> <code>'SourceDocument'</code> for
111 * {@link ifc.document._XExporter} interface </li>
115 public synchronized TestEnvironment createTestEnvironment
116 (TestParameters tParam
, PrintWriter log
) {
118 final String TEST_STR
= "XMLExporter";
120 XMultiServiceFactory xMSF
= tParam
.getMSF() ;
121 XInterface oObj
= null;
123 Filter
= new FilterChecker(log
);
124 Any arg
= new Any(new Type(XDocumentHandler
.class),Filter
);
127 oObj
= (XInterface
) xMSF
.createInstanceWithArguments(
128 "com.sun.star.comp.Writer.XMLExporter", new Object
[] {arg
});
129 XExporter xEx
= UnoRuntime
.queryInterface
130 (XExporter
.class,oObj
);
131 xEx
.setSourceDocument(xTextDoc
);
133 XSimpleText aText
= xTextDoc
.getText();
134 XTextCursor curs
= aText
.createTextCursor();
135 aText
.insertString(curs
, TEST_STR
, false);
136 } catch (com
.sun
.star
.uno
.Exception e
) {
137 e
.printStackTrace(log
) ;
138 throw new StatusException("Can't create component.", e
) ;
141 // adding tags which must be contained in XML output
142 Filter
.addTag("office:document") ;
143 Filter
.addTagEnclosed("office:meta", "office:document") ;
144 Filter
.addTagEnclosed("office:settings", "office:document") ;
145 Filter
.addTagEnclosed("office:script", "office:document") ;
146 Filter
.addTagEnclosed("office:styles", "office:document") ;
147 Filter
.addTagEnclosed("office:body", "office:document") ;
148 Filter
.addCharactersEnclosed(TEST_STR
, "text:p") ;
150 // create testobject here
151 log
.println( "creating a new environment" );
152 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 log
.println("Implementation Name: "+util
.utils
.getImplName(oObj
));
162 } // finish method getTestEnvironment
165 * This class checks the XML for tags and data required and returns
166 * checking result to <code>XFilter</code> interface test. All
167 * the information about errors occurred in XML data is written
169 * @see ifc.document._XFilter
171 private class FilterChecker
extends XMLTools
.XMLTagsChecker
172 implements ifc
.document
._XFilter
.FilterChecker
{
175 * Creates a class which will write information
176 * into log specified.
178 private FilterChecker(PrintWriter log
) {
182 * <code>_XFilter.FilterChecker</code> interface method
183 * which returns the result of XML checking.
184 * @return <code>true</code> if the XML data exported was
185 * valid (i.e. all necessary tags and character data exists),
186 * <code>false</code> if some errors occurred.
188 public boolean checkFilter() {