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