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.Impress; 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.Impress.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 xImpressDoc = null; 73 protected static int counter = 0; 74 75 /** 76 * New spreadsheet 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 an impress document" ); 85 xImpressDoc = SOF.createImpressDoc(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 * Spreadsheet document disposed 95 */ cleanup( TestParameters tParam, PrintWriter log )96 protected void cleanup( TestParameters tParam, PrintWriter log ) { 97 log.println( " disposing xImpressDoc " ); 98 xImpressDoc.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.Impress.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 Impress document is set as a source document for exporter 109 * created. New style 'NewStyle' added to style family "Graphics". 110 * This made for checking if this style is successfully exported within 111 * 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 131 String newName = "NewGraphicsStyle" + counter++; 132 // Adding tags for checking existance of head tag and property value 133 filter.addTag(new XMLTools.Tag("office:document-styles")); 134 filter.addTag(new XMLTools.Tag("office:styles")); 135 filter.addTag(new XMLTools.Tag("style:style","style:name", newName)); 136 137 try { 138 oObj = (XInterface) xMSF.createInstanceWithArguments( 139 "com.sun.star.comp.Impress.XMLStylesExporter", 140 new Object[] {arg}); 141 XExporter xEx = (XExporter) 142 UnoRuntime.queryInterface(XExporter.class,oObj); 143 xEx.setSourceDocument(xImpressDoc); 144 145 // Obtaining and changing property values 146 XStyleFamiliesSupplier styleSup = (XStyleFamiliesSupplier) 147 UnoRuntime.queryInterface 148 (XStyleFamiliesSupplier.class, xImpressDoc) ; 149 XNameAccess styleFamilies = styleSup.getStyleFamilies(); 150 String[] styleFamiliesNames = styleFamilies.getElementNames(); 151 XNameContainer StyleFamilyName = (XNameContainer) 152 UnoRuntime.queryInterface(XNameContainer.class, 153 styleFamilies.getByName(styleFamiliesNames[0])); 154 Object SC = SOF.createInstance 155 (xImpressDoc, "com.sun.star.style.Style"); 156 XStyle StylePage = (XStyle) 157 UnoRuntime.queryInterface(XStyle.class,SC); 158 StyleFamilyName.insertByName(newName, StylePage); 159 160 } catch (com.sun.star.uno.Exception e) { 161 e.printStackTrace(log) ; 162 throw new StatusException("Can't create component.", e) ; 163 } 164 165 // create testobject here 166 log.println( "creating a new environment" ); 167 TestEnvironment tEnv = new TestEnvironment( oObj ); 168 169 tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor( 170 new String[] {"FilterName"}, 171 new Object[] {"simpress: StarOffice XML (Impress)"})); 172 tEnv.addObjRelation("SourceDocument",xImpressDoc); 173 tEnv.addObjRelation("XFilter.Checker", filter); 174 return tEnv; 175 } 176 177 /** 178 * This class checks the XML for tags and data required and returns 179 * checking result to <code>XFilter</code> interface test. All 180 * the information about errors occured in XML data is written 181 * to log specified. 182 * @see ifc.document._XFilter 183 */ 184 protected class FilterChecker extends XMLTools.XMLChecker 185 implements ifc.document._XFilter.FilterChecker { 186 187 /** 188 * Creates a class which will write information 189 * into log specified. 190 */ FilterChecker(PrintWriter log)191 public FilterChecker(PrintWriter log) { 192 super(log, false) ; 193 } 194 /** 195 * <code>_XFilter.FilterChecker</code> interface method 196 * which returns the result of XML checking. 197 * @return <code>true</code> if the XML data exported was 198 * valid (i.e. all necessary tags and character data exists), 199 * <code>false</code> if some errors occured. 200 */ checkFilter()201 public boolean checkFilter() { 202 return check(); 203 } 204 } 205 } 206