merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / mod / _xmloff / Draw / XMLStylesExporter.java
blobdd3936da17f80a14fe6c3819cc5a842cd5aef89f
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: XMLStylesExporter.java,v $
10 * $Revision: 1.5 $
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._xmloff.Draw;
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.container.XNameAccess;
43 import com.sun.star.container.XNameContainer;
44 import com.sun.star.document.XExporter;
45 import com.sun.star.lang.XComponent;
46 import com.sun.star.lang.XMultiServiceFactory;
47 import com.sun.star.style.XStyle;
48 import com.sun.star.style.XStyleFamiliesSupplier;
49 import com.sun.star.uno.Any;
50 import com.sun.star.uno.Exception;
51 import com.sun.star.uno.Type;
52 import com.sun.star.uno.UnoRuntime;
53 import com.sun.star.uno.XInterface;
54 import com.sun.star.xml.sax.XDocumentHandler;
56 /**
57 * Test for object which is represented by service
58 * <code>com.sun.star.comp.Draw.XMLStylesExporter</code>. <p>
59 * Object implements the following interfaces :
60 * <ul>
61 * <li><code>com::sun::star::lang::XInitialization</code></li>
62 * <li><code>com::sun::star::document::ExportFilter</code></li>
63 * <li><code>com::sun::star::document::XFilter</code></li>
64 * <li><code>com::sun::star::document::XExporter</code></li>
65 * <li><code>com::sun::star::beans::XPropertySet</code></li>
66 * </ul>
67 * @see com.sun.star.lang.XInitialization
68 * @see com.sun.star.document.ExportFilter
69 * @see com.sun.star.document.XFilter
70 * @see com.sun.star.document.XExporter
71 * @see com.sun.star.beans.XPropertySet
72 * @see ifc.lang._XInitialization
73 * @see ifc.document._ExportFilter
74 * @see ifc.document._XFilter
75 * @see ifc.document._XExporter
76 * @see ifc.beans._XPropertySet
78 public class XMLStylesExporter extends TestCase {
79 XComponent xDrawDoc = null;
80 protected static int counter = 0;
82 /**
83 * New draw document created.
85 protected void initialize( TestParameters tParam, PrintWriter log ) {
87 // get a soffice factory object
88 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF());
90 try {
91 log.println( "creating a drawdocument" );
92 xDrawDoc = SOF.createDrawDoc(null);
93 } catch ( Exception e ) {
94 // Some exception occures.FAILED
95 e.printStackTrace( log );
96 throw new StatusException( "Couldn't create document", e );
101 * Document disposed here.
103 protected void cleanup( TestParameters tParam, PrintWriter log ) {
104 log.println( " disposing xDrawDoc " );
105 xDrawDoc.dispose();
109 * Creating a Testenvironment for the interfaces to be tested.
110 * Creates an instance of the service
111 * <code>com.sun.star.comp.Draw.XMLStylesExporter</code> with
112 * argument which is an implementation of <code>XDocumentHandler</code>
113 * and which can check if required tags and character data is
114 * exported. <p>
115 * The draw document is set as a source document for exporter
116 * created. New style is added to the style family 'graphics' with unique name
117 * every time. This made for checking if this style is successfully exported
118 * within the document styles information.
119 * Object relations created :
120 * <ul>
121 * <li> <code>'MediaDescriptor'</code> for
122 * {@link ifc.document._XFilter} interface </li>
123 * <li> <code>'XFilter.Checker'</code> for
124 * {@link ifc.document._XFilter} interface </li>
125 * <li> <code>'SourceDocument'</code> for
126 * {@link ifc.document._XExporter} interface </li>
127 * </ul>
129 public synchronized TestEnvironment createTestEnvironment(
130 TestParameters tParam, PrintWriter log) throws StatusException {
132 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
133 XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
134 XInterface oObj = null;
135 FilterChecker filter = new FilterChecker(log) ;
136 Any arg = new Any(new Type(XDocumentHandler.class),filter);
137 final String STYLE_NAME = "New style" + counter++ ;
139 try {
140 oObj = (XInterface) xMSF.createInstanceWithArguments(
141 "com.sun.star.comp.Draw.XMLStylesExporter", new Object[] {arg});
142 XExporter xEx = (XExporter)
143 UnoRuntime.queryInterface(XExporter.class,oObj);
144 xEx.setSourceDocument(xDrawDoc);
146 //obtain style families
147 XStyleFamiliesSupplier styleSup = (XStyleFamiliesSupplier)
148 UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, xDrawDoc);
149 XNameAccess StyleFamilies = styleSup.getStyleFamilies();
150 //obtain all style family names
151 String[] styleFamiliesNames = StyleFamilies.getElementNames();
152 String styleFamilyName = styleFamiliesNames[0];
153 //obtain style family with name[0]
154 Object objectStyle = StyleFamilies.getByName(styleFamilyName);
155 XNameContainer xStyleFamilyName = (XNameContainer)
156 UnoRuntime.queryInterface(XNameContainer.class, objectStyle);
157 //creat new style
158 Object SC = SOF.createInstance(xDrawDoc, "com.sun.star.style.Style");
159 XStyle Style = (XStyle) UnoRuntime.queryInterface(XStyle.class,SC);
160 //add new style to style familiy with name[0]
161 xStyleFamilyName.insertByName(STYLE_NAME,Style);
162 } catch (com.sun.star.uno.Exception e) {
163 e.printStackTrace(log) ;
164 throw new StatusException("Can't create component.", e) ;
167 // Checking Head Tag existance and that property has changed
168 filter.addTag(new XMLTools.Tag ("office:document-styles"));
169 filter.addTag(new XMLTools.Tag ("office:styles"));
170 filter.addTagEnclosed(
171 new XMLTools.Tag("style:style"),
172 new XMLTools.Tag("office:styles") );
173 filter.addTag(new XMLTools.Tag("style:style", "style:name", STYLE_NAME));
175 // create testobject here
176 log.println( "creating a new environment" );
177 TestEnvironment tEnv = new TestEnvironment( oObj );
179 tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
180 new String[] {"FilterName"},
181 new Object[] {"sdraw: StarOffice XML (Draw)"}));
182 tEnv.addObjRelation("SourceDocument",xDrawDoc);
183 tEnv.addObjRelation("XFilter.Checker", filter);
184 return tEnv;
188 * This class checks the XML for tags and data required and returns
189 * checking result to <code>XFilter</code> interface test. All
190 * the information about errors occured in XML data is written
191 * to log specified.
192 * @see ifc.document._XFilter
194 protected class FilterChecker extends XMLTools.XMLChecker
195 implements ifc.document._XFilter.FilterChecker {
198 * Creates a class which will write information
199 * into log specified.
201 public FilterChecker(PrintWriter log) {
202 super(log, false) ;
206 * <code>_XFilter.FilterChecker</code> interface method
207 * which returns the result of XML checking.
208 * @return <code>true</code> if the XML data exported was
209 * valid (i.e. all necessary tags and character data exists),
210 * <code>false</code> if some errors occured.
212 public boolean checkFilter() {
213 return check();