1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 package mod._xmloff.Impress; 29 30 import java.io.PrintWriter; 31 32 import lib.StatusException; 33 import lib.TestCase; 34 import lib.TestEnvironment; 35 import lib.TestParameters; 36 import util.SOfficeFactory; 37 import util.XMLTools; 38 39 import com.sun.star.container.XNameAccess; 40 import com.sun.star.container.XNameContainer; 41 import com.sun.star.document.XExporter; 42 import com.sun.star.lang.XComponent; 43 import com.sun.star.lang.XMultiServiceFactory; 44 import com.sun.star.style.XStyle; 45 import com.sun.star.style.XStyleFamiliesSupplier; 46 import com.sun.star.uno.Any; 47 import com.sun.star.uno.Exception; 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; 52 53 /** 54 * Test for object which is represented by service 55 * <code>com.sun.star.comp.Impress.XMLStylesExporter</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 74 */ 75 public class XMLStylesExporter extends TestCase { 76 XComponent xImpressDoc = null; 77 protected static int counter = 0; 78 79 /** 80 * New spreadsheet document created. 81 */ 82 protected void initialize( TestParameters tParam, PrintWriter log ) { 83 84 // get a soffice factory object 85 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF()); 86 87 try { 88 log.println( "creating an impress document" ); 89 xImpressDoc = SOF.createImpressDoc(null);; 90 } catch ( Exception e ) { 91 // Some exception occures.FAILED 92 e.printStackTrace( log ); 93 throw new StatusException( "Couldn't create document", e ); 94 } 95 } 96 97 /** 98 * Spreadsheet document disposed 99 */ 100 protected void cleanup( TestParameters tParam, PrintWriter log ) { 101 log.println( " disposing xImpressDoc " ); 102 xImpressDoc.dispose(); 103 } 104 105 /** 106 * Creating a Testenvironment for the interfaces to be tested. 107 * Creates an instance of the service 108 * <code>com.sun.star.comp.Impress.XMLStylesExporter</code> with 109 * argument which is an implementation of <code>XDocumentHandler</code> 110 * and which can check if required tags and character data is 111 * exported. <p> 112 * The Impress document is set as a source document for exporter 113 * created. New style 'NewStyle' added to style family "Graphics". 114 * This made for checking if this style is successfully exported within 115 * the document styles information. 116 * Object relations created : 117 * <ul> 118 * <li> <code>'MediaDescriptor'</code> for 119 * {@link ifc.document._XFilter} interface </li> 120 * <li> <code>'XFilter.Checker'</code> for 121 * {@link ifc.document._XFilter} interface </li> 122 * <li> <code>'SourceDocument'</code> for 123 * {@link ifc.document._XExporter} interface </li> 124 * </ul> 125 */ 126 public synchronized TestEnvironment createTestEnvironment 127 (TestParameters tParam, PrintWriter log) throws StatusException { 128 129 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() ); 130 XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ; 131 XInterface oObj = null; 132 FilterChecker filter = new FilterChecker(log); 133 Any arg = new Any(new Type(XDocumentHandler.class),filter); 134 135 String newName = "NewGraphicsStyle" + counter++; 136 // Adding tags for checking existance of head tag and property value 137 filter.addTag(new XMLTools.Tag("office:document-styles")); 138 filter.addTag(new XMLTools.Tag("office:styles")); 139 filter.addTag(new XMLTools.Tag("style:style","style:name", newName)); 140 141 try { 142 oObj = (XInterface) xMSF.createInstanceWithArguments( 143 "com.sun.star.comp.Impress.XMLStylesExporter", 144 new Object[] {arg}); 145 XExporter xEx = (XExporter) 146 UnoRuntime.queryInterface(XExporter.class,oObj); 147 xEx.setSourceDocument(xImpressDoc); 148 149 // Obtaining and changing property values 150 XStyleFamiliesSupplier styleSup = (XStyleFamiliesSupplier) 151 UnoRuntime.queryInterface 152 (XStyleFamiliesSupplier.class, xImpressDoc) ; 153 XNameAccess styleFamilies = styleSup.getStyleFamilies(); 154 String[] styleFamiliesNames = styleFamilies.getElementNames(); 155 XNameContainer StyleFamilyName = (XNameContainer) 156 UnoRuntime.queryInterface(XNameContainer.class, 157 styleFamilies.getByName(styleFamiliesNames[0])); 158 Object SC = SOF.createInstance 159 (xImpressDoc, "com.sun.star.style.Style"); 160 XStyle StylePage = (XStyle) 161 UnoRuntime.queryInterface(XStyle.class,SC); 162 StyleFamilyName.insertByName(newName, StylePage); 163 164 } catch (com.sun.star.uno.Exception e) { 165 e.printStackTrace(log) ; 166 throw new StatusException("Can't create component.", e) ; 167 } 168 169 // create testobject here 170 log.println( "creating a new environment" ); 171 TestEnvironment tEnv = new TestEnvironment( oObj ); 172 173 tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor( 174 new String[] {"FilterName"}, 175 new Object[] {"simpress: StarOffice XML (Impress)"})); 176 tEnv.addObjRelation("SourceDocument",xImpressDoc); 177 tEnv.addObjRelation("XFilter.Checker", filter); 178 return tEnv; 179 } 180 181 /** 182 * This class checks the XML for tags and data required and returns 183 * checking result to <code>XFilter</code> interface test. All 184 * the information about errors occured in XML data is written 185 * to log specified. 186 * @see ifc.document._XFilter 187 */ 188 protected class FilterChecker extends XMLTools.XMLChecker 189 implements ifc.document._XFilter.FilterChecker { 190 191 /** 192 * Creates a class which will write information 193 * into log specified. 194 */ 195 public FilterChecker(PrintWriter log) { 196 super(log, false) ; 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 */ 205 public boolean checkFilter() { 206 return check(); 207 } 208 } 209 } 210