Update ooo320-m1
[ooovba.git] / qadevOOo / tests / java / mod / _sw / XMLContentExporter.java
blobdc57188c25e763092a4abe983232b18d6383517e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XMLContentExporter.java,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package mod._sw;
33 import java.io.PrintWriter;
35 import lib.StatusException;
36 import lib.TestCase;
37 import lib.TestEnvironment;
38 import lib.TestParameters;
39 import util.SOfficeFactory;
40 import util.XMLTools;
42 import com.sun.star.document.XExporter;
43 import com.sun.star.lang.XMultiServiceFactory;
44 import com.sun.star.text.XSimpleText;
45 import com.sun.star.text.XTextCursor;
46 import com.sun.star.text.XTextDocument;
47 import com.sun.star.uno.Any;
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;
53 /**
54 * Test for object which is represented by service
55 * <code>com.sun.star.comp.Calc.XMLContentExporter</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
75 public class XMLContentExporter extends TestCase {
76 XTextDocument xTextDoc;
77 ContentFilterChecker Filter;
79 /**
80 * New text document created.
82 protected void initialize( TestParameters tParam, PrintWriter log ) {
83 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
85 try {
86 log.println( "creating a textdocument" );
87 xTextDoc = SOF.createTextDoc( null );
88 } catch ( com.sun.star.uno.Exception e ) {
89 // Some exception occures.FAILED
90 e.printStackTrace( log );
91 throw new StatusException( "Couldn't create document", e );
95 /**
96 * Document disposed here.
98 protected void cleanup( TestParameters tParam, PrintWriter log ) {
99 log.println( " disposing xTextDoc " );
100 util.DesktopTools.closeDoc(xTextDoc);
104 * Creating a Testenvironment for the interfaces to be tested.
105 * Creates an instance of the service
106 * <code>com.sun.star.comp.Calc.XMLContentExporter</code> with
107 * argument which is an implementation of <code>XDocumentHandler</code>
108 * and which can check if required tags and character data is
109 * exported. <p>
110 * The text document is set as a source document for exporter
111 * created. A string inserted into document. This made
112 * for checking if this string is successfully exported within
113 * the document content.
114 * Object relations created :
115 * <ul>
116 * <li> <code>'MediaDescriptor'</code> for
117 * {@link ifc.document._XFilter} interface </li>
118 * <li> <code>'XFilter.Checker'</code> for
119 * {@link ifc.document._XFilter} interface </li>
120 * <li> <code>'SourceDocument'</code> for
121 * {@link ifc.document._XExporter} interface </li>
122 * </ul>
124 public synchronized TestEnvironment createTestEnvironment
125 ( TestParameters tParam, PrintWriter log ) throws StatusException {
127 final String CONTENT = "XMLContentExporter";
128 XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
129 XInterface oObj = null;
131 Filter = new ContentFilterChecker(log);
132 Any arg = new Any(new Type(XDocumentHandler.class), Filter);
134 try {
135 oObj = (XInterface) xMSF.createInstanceWithArguments(
136 "com.sun.star.comp.Writer.XMLContentExporter",
137 new Object[] {arg});
138 XExporter xEx = (XExporter) UnoRuntime.queryInterface
139 (XExporter.class,oObj);
140 xEx.setSourceDocument(xTextDoc);
142 // text added to the document
143 XSimpleText aText = xTextDoc.getText();
144 XTextCursor curs = (XTextCursor) aText.createTextCursor();
145 aText.insertString(curs, CONTENT, false);
146 } catch (com.sun.star.uno.Exception e) {
147 e.printStackTrace(log) ;
148 throw new StatusException("Can't create component.", e) ;
151 // adding tags which must be contained in XML output
152 Filter.addTag("office:document-content") ;
153 Filter.addTagEnclosed("office:body", "office:document-content") ;
154 Filter.addTagEnclosed("office:script", "office:document-content") ;
155 Filter.addTagEnclosed("office:body", "office:document-content") ;
156 Filter.addCharactersEnclosed(CONTENT, "text:p") ;
158 // create testobject here
159 log.println( "creating a new environment" );
160 TestEnvironment tEnv = new TestEnvironment( oObj );
161 tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
162 new String[] {"FilterName"},
163 new Object[] {"swriter: StarOffice XML (Writer)"}));
164 tEnv.addObjRelation("SourceDocument",xTextDoc);
165 tEnv.addObjRelation("XFilter.Checker", Filter) ;
166 return tEnv;
170 * This class checks the XML for tags and data required and returns
171 * checking result to <code>XFilter</code> interface test. All
172 * the information about errors occured in XML data is written
173 * to log specified.
174 * @see ifc.document._XFilter
176 protected class ContentFilterChecker extends XMLTools.XMLTagsChecker
177 implements ifc.document._XFilter.FilterChecker {
180 * Creates a class which will write information
181 * into log specified.
183 public ContentFilterChecker(PrintWriter log) {
184 super(log) ;
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.
194 public boolean checkFilter() {
195 return checkTags();