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.XNamed; 36 import com.sun.star.document.XExporter; 37 import com.sun.star.drawing.XDrawPage; 38 import com.sun.star.drawing.XDrawPages; 39 import com.sun.star.drawing.XDrawPagesSupplier; 40 import com.sun.star.lang.XComponent; 41 import com.sun.star.lang.XMultiServiceFactory; 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.XMLContentExporter</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 XMLContentExporter extends TestCase { 72 XComponent xDrawDoc = null; 73 74 /** 75 * New text document created. 76 */ initialize( TestParameters tParam, PrintWriter log )77 protected void initialize( TestParameters tParam, PrintWriter log ) { 78 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF()); 79 80 try { 81 log.println( "creating a drawdocument" ); 82 xDrawDoc = SOF.createDrawDoc(null); 83 } catch ( Exception e ) { 84 // Some exception occures.FAILED 85 e.printStackTrace( log ); 86 throw new StatusException( "Couldn't create document", e ); 87 } 88 } 89 90 /** 91 * Document disposed here. 92 */ cleanup( TestParameters tParam, PrintWriter log )93 protected void cleanup( TestParameters tParam, PrintWriter log ) { 94 log.println( " disposing xDrawDoc " ); 95 xDrawDoc.dispose(); 96 } 97 98 /** 99 * Creating a Testenvironment for the interfaces to be tested. 100 * Creates an instance of the service 101 * <code>com.sun.star.comp.Draw.XMLContentExporter</code> with 102 * argument which is an implementation of <code>XDocumentHandler</code> 103 * and which can check if required tags and character data is 104 * exported. <p> 105 * The draw document is set as a source document for exporter 106 * created. Then a new page (Slide) created in this document and 107 * both two pages renamed to 'NewSlide1' and 'NewSlide2'. After this 108 * filter checks that new tags exist in the XML output. 109 * Object relations created : 110 * <ul> 111 * <li> <code>'MediaDescriptor'</code> for 112 * {@link ifc.document._XFilter} interface </li> 113 * <li> <code>'XFilter.Checker'</code> for 114 * {@link ifc.document._XFilter} interface </li> 115 * <li> <code>'SourceDocument'</code> for 116 * {@link ifc.document._XExporter} interface </li> 117 * </ul> 118 */ createTestEnvironment(TestParameters tParam, PrintWriter log)119 protected synchronized TestEnvironment createTestEnvironment 120 (TestParameters tParam, PrintWriter log) { 121 122 XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ; 123 XInterface oObj = null; 124 FilterChecker filter = new FilterChecker(log); 125 Any arg = new Any(new Type(XDocumentHandler.class),filter); 126 127 // Adding tags for checking existance of head tag and other tags 128 filter.addTag(new XMLTools.Tag("office:document-content")); 129 filter.addTag(new XMLTools.Tag("office:body")); 130 filter.addTagEnclosed( 131 new XMLTools.Tag("draw:page"), 132 new XMLTools.Tag("office:body")); 133 filter.addTag(new XMLTools.Tag("draw:page","draw:name","NewSlide1")); 134 filter.addTag(new XMLTools.Tag("draw:page","draw:name","NewSlide2")); 135 136 try { 137 oObj = (XInterface) xMSF.createInstanceWithArguments( 138 "com.sun.star.comp.Draw.XMLContentExporter", 139 new Object[] {arg}); 140 XExporter xEx = (XExporter) 141 UnoRuntime.queryInterface(XExporter.class,oObj); 142 //xEx.setSourceDocument(xDrawDoc); 143 144 XDrawPagesSupplier supp = (XDrawPagesSupplier) 145 UnoRuntime.queryInterface(XDrawPagesSupplier.class, xDrawDoc); 146 XDrawPages set = supp.getDrawPages(); 147 148 // This is an XML-export BUG (new slide named "NewSlide2" 149 // can not be exported to XML) 150 set.insertNewByIndex(1); 151 152 XDrawPage page1 = (XDrawPage) 153 UnoRuntime.queryInterface(XDrawPage.class, set.getByIndex(0)); 154 XNamed NPage1 = (XNamed) 155 UnoRuntime.queryInterface(XNamed.class,page1); 156 NPage1.setName("NewSlide1"); 157 XDrawPage page2 = (XDrawPage) 158 UnoRuntime.queryInterface(XDrawPage.class, set.getByIndex(1)); 159 XNamed NPage2 = (XNamed) 160 UnoRuntime.queryInterface(XNamed.class,page2); 161 NPage2.setName("NewSlide2"); 162 xEx.setSourceDocument(xDrawDoc); 163 } catch (com.sun.star.uno.Exception e) { 164 e.printStackTrace(log) ; 165 throw new StatusException("Can't create component.", e) ; 166 } 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 /** 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 */ FilterChecker(PrintWriter log)195 public FilterChecker(PrintWriter log) { 196 super(log,true) ; 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 212