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.Draw; 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.Draw.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 xDrawDoc = null; 77 protected static int counter = 0; 78 79 /** 80 * New draw 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 a drawdocument" ); 89 xDrawDoc = SOF.createDrawDoc(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 * Document disposed here. 99 */ 100 protected void cleanup( TestParameters tParam, PrintWriter log ) { 101 log.println( " disposing xDrawDoc " ); 102 xDrawDoc.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.Draw.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 draw document is set as a source document for exporter 113 * created. New style is added to the style family 'graphics' with unique name 114 * every time. This made for checking if this style is successfully exported 115 * within 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 final String STYLE_NAME = "New style" + counter++ ; 135 136 try { 137 oObj = (XInterface) xMSF.createInstanceWithArguments( 138 "com.sun.star.comp.Draw.XMLStylesExporter", new Object[] {arg}); 139 XExporter xEx = (XExporter) 140 UnoRuntime.queryInterface(XExporter.class,oObj); 141 xEx.setSourceDocument(xDrawDoc); 142 143 //obtain style families 144 XStyleFamiliesSupplier styleSup = (XStyleFamiliesSupplier) 145 UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, xDrawDoc); 146 XNameAccess StyleFamilies = styleSup.getStyleFamilies(); 147 //obtain all style family names 148 String[] styleFamiliesNames = StyleFamilies.getElementNames(); 149 String styleFamilyName = styleFamiliesNames[0]; 150 //obtain style family with name[0] 151 Object objectStyle = StyleFamilies.getByName(styleFamilyName); 152 XNameContainer xStyleFamilyName = (XNameContainer) 153 UnoRuntime.queryInterface(XNameContainer.class, objectStyle); 154 //creat new style 155 Object SC = SOF.createInstance(xDrawDoc, "com.sun.star.style.Style"); 156 XStyle Style = (XStyle) UnoRuntime.queryInterface(XStyle.class,SC); 157 //add new style to style familiy with name[0] 158 xStyleFamilyName.insertByName(STYLE_NAME,Style); 159 } catch (com.sun.star.uno.Exception e) { 160 e.printStackTrace(log) ; 161 throw new StatusException("Can't create component.", e) ; 162 } 163 164 // Checking Head Tag existance and that property has changed 165 filter.addTag(new XMLTools.Tag ("office:document-styles")); 166 filter.addTag(new XMLTools.Tag ("office:styles")); 167 filter.addTagEnclosed( 168 new XMLTools.Tag("style:style"), 169 new XMLTools.Tag("office:styles") ); 170 filter.addTag(new XMLTools.Tag("style:style", "style:name", STYLE_NAME)); 171 172 // create testobject here 173 log.println( "creating a new environment" ); 174 TestEnvironment tEnv = new TestEnvironment( oObj ); 175 176 tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor( 177 new String[] {"FilterName"}, 178 new Object[] {"sdraw: StarOffice XML (Draw)"})); 179 tEnv.addObjRelation("SourceDocument",xDrawDoc); 180 tEnv.addObjRelation("XFilter.Checker", filter); 181 return tEnv; 182 } 183 184 /** 185 * This class checks the XML for tags and data required and returns 186 * checking result to <code>XFilter</code> interface test. All 187 * the information about errors occured in XML data is written 188 * to log specified. 189 * @see ifc.document._XFilter 190 */ 191 protected class FilterChecker extends XMLTools.XMLChecker 192 implements ifc.document._XFilter.FilterChecker { 193 194 /** 195 * Creates a class which will write information 196 * into log specified. 197 */ 198 public FilterChecker(PrintWriter log) { 199 super(log, false) ; 200 } 201 202 /** 203 * <code>_XFilter.FilterChecker</code> interface method 204 * which returns the result of XML checking. 205 * @return <code>true</code> if the XML data exported was 206 * valid (i.e. all necessary tags and character data exists), 207 * <code>false</code> if some errors occured. 208 */ 209 public boolean checkFilter() { 210 return check(); 211 } 212 } 213 } 214 215