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._sc;
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.XIndexAccess;
40 import com.sun.star.container.XNamed;
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.sheet.XSpreadsheet;
45 import com.sun.star.sheet.XSpreadsheetDocument;
46 import com.sun.star.sheet.XSpreadsheets;
47 import com.sun.star.uno.Any;
48 import com.sun.star.uno.AnyConverter;
49 import com.sun.star.uno.Type;
50 import com.sun.star.uno.UnoRuntime;
51 import com.sun.star.uno.XInterface;
52 import com.sun.star.xml.sax.XDocumentHandler;
53 
54 /**
55  * Test for object which is represented by service
56  * <code>com.sun.star.comp.Calc.XMLExporter</code>. <p>
57  * Object implements the following interfaces :
58  * <ul>
59  *  <li><code>com::sun::star::lang::XInitialization</code></li>
60  *  <li><code>com::sun::star::document::ExportFilter</code></li>
61  *  <li><code>com::sun::star::document::XFilter</code></li>
62  *  <li><code>com::sun::star::document::XExporter</code></li>
63  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
64  * </ul>
65  * @see com.sun.star.lang.XInitialization
66  * @see com.sun.star.document.ExportFilter
67  * @see com.sun.star.document.XFilter
68  * @see com.sun.star.document.XExporter
69  * @see com.sun.star.beans.XPropertySet
70  * @see ifc.lang._XInitialization
71  * @see ifc.document._ExportFilter
72  * @see ifc.document._XFilter
73  * @see ifc.document._XExporter
74  * @see ifc.beans._XPropertySet
75  */
76 public class XMLExporter extends TestCase {
77     static XComponent xSheetDoc;
78 
79     /**
80      * New spreadsheet document created.
81      */
82     protected void initialize( TestParameters tParam, PrintWriter log ) {
83         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
84         try {
85             log.println( "creating a calc document" );
86             xSheetDoc = SOF.openDoc("scalc","_blank");
87         } catch ( com.sun.star.uno.Exception e ) {
88             // Some exception occures.FAILED
89             e.printStackTrace( log );
90             throw new StatusException( "Couldn't create document", e );
91         }
92     }
93 
94     /**
95      * Spreadsheet document disposed
96      */
97     protected void cleanup( TestParameters tParam, PrintWriter log ) {
98         log.println( "    disposing xCalcDoc " );
99         util.DesktopTools.closeDoc(xSheetDoc);
100     }
101 
102     /**
103     * Creating a Testenvironment for the interfaces to be tested.
104     * Creates an instance of the service
105     * <code>com.sun.star.comp.Calc.XMLExporter</code> with
106     * argument which is an implementation of <code>XDocumentHandler</code>
107     * and which can check if required tags and character data is
108     * exported. <p>
109     * The calc document is set as a source document for exporter
110     * created. Checking whether tags for table corresponding to some sheet
111     * is exported.<p>
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     */
122     protected synchronized TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
123 
124         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
125         XInterface oObj = null;
126         FilterChecker Filter = new FilterChecker(log);
127         Any arg = new Any(new Type(XDocumentHandler.class), Filter);
128         final String SHEET_NAME = "XMLExporter_SheetTestName";
129 
130         try {
131             oObj = (XInterface) xMSF.createInstanceWithArguments(
132                 "com.sun.star.comp.Calc.XMLExporter", new Object[] {arg} );
133             XExporter xEx = (XExporter) UnoRuntime.queryInterface
134                 (XExporter.class,oObj);
135             xEx.setSourceDocument(xSheetDoc);
136 
137             //set name of sheet
138             XSpreadsheetDocument xSpreadsheetDoc = (XSpreadsheetDocument)
139                 UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSheetDoc);
140             XSpreadsheets xSpreadsheets = xSpreadsheetDoc.getSheets();
141             XIndexAccess xSheetsIndexArray = (XIndexAccess)
142                 UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
143             XSpreadsheet xSheet = (XSpreadsheet) AnyConverter.toObject(
144                     new Type(XSpreadsheet.class),xSheetsIndexArray.getByIndex(0));
145             XNamed xSheetNamed = (XNamed)
146                 UnoRuntime.queryInterface(XNamed.class, xSheet);
147             xSheetNamed.setName(SHEET_NAME);
148 
149             log.println("fill sheet with contnet...");
150             util.CalcTools.fillCalcSheetWithContent(xSheet, 3, 3, 50, 50);
151 
152         } catch (com.sun.star.uno.Exception e) {
153             e.printStackTrace(log) ;
154             throw new StatusException("Can't create component.", e) ;
155         } catch (java.lang.Exception e) {
156             e.printStackTrace(log);
157             throw new StatusException("Can't create environment.", e);
158         }
159 
160         // adding tags which must be contained in XML output
161         Filter.addTag( new XMLTools.Tag("office:document") );
162         Filter.addTagEnclosed(
163             new XMLTools.Tag("office:meta"),
164             new XMLTools.Tag("office:document") );
165         Filter.addTagEnclosed(
166             new XMLTools.Tag("office:settings"),
167             new XMLTools.Tag("office:document") );
168         Filter.addTagEnclosed(
169             new XMLTools.Tag("office:script"),
170             new XMLTools.Tag("office:document") );
171         Filter.addTagEnclosed(
172             new XMLTools.Tag("office:styles"),
173             new XMLTools.Tag("office:document") );
174         Filter.addTagEnclosed(
175             new XMLTools.Tag("office:body"),
176             new XMLTools.Tag("office:document") );
177         Filter.addTagEnclosed(
178             new XMLTools.Tag("table:table"),
179             new XMLTools.Tag("office:body") );
180         Filter.addTag(
181             new XMLTools.Tag("table:table", "table:name", SHEET_NAME) );
182 
183         // create testobject here
184         log.println( "creating a new environment" );
185         TestEnvironment tEnv = new TestEnvironment( oObj );
186 
187         tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
188             new String[] {"FilterName"},
189             new Object[] {"scalc: StarOffice XML (Calc)"}));
190         tEnv.addObjRelation("SourceDocument",xSheetDoc);
191         tEnv.addObjRelation("XFilter.Checker", Filter) ;
192         return tEnv;
193     }
194 
195     /**
196      * This class checks the XML for tags and data required and returns
197      * checking result to <code>XFilter</code> interface test. All
198      * the information about errors occured in XML data is written
199      * to log specified.
200      * @see ifc.document._XFilter
201      */
202     protected class FilterChecker extends XMLTools.XMLChecker
203         implements ifc.document._XFilter.FilterChecker {
204 
205         /**
206          * Creates a class which will write information
207          * into log specified.
208          */
209         public FilterChecker(PrintWriter log) {
210             super(log, false) ;
211         }
212         /**
213          * <code>_XFilter.FilterChecker</code> interface method
214          * which returns the result of XML checking.
215          * @return <code>true</code> if the XML data exported was
216          * valid (i.e. all necessary tags and character data exists),
217          * <code>false</code> if some errors occured.
218          */
219         public boolean checkFilter() {
220             return check();
221         }
222     }
223 }
224 
225