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.document.XExporter;
41 import com.sun.star.lang.XComponent;
42 import com.sun.star.lang.XMultiServiceFactory;
43 import com.sun.star.sheet.XSpreadsheet;
44 import com.sun.star.sheet.XSpreadsheetDocument;
45 import com.sun.star.sheet.XSpreadsheets;
46 import com.sun.star.table.XCell;
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.XMLContentExporter</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 XMLContentExporter extends TestCase {
77 
78     static XComponent xSheetDoc;
79     static ContentFilterChecker Filter;
80 
81     /**
82      * New spreadsheet document created.
83      */
84     protected void initialize( TestParameters tParam, PrintWriter log ) {
85         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
86         try {
87             log.println( "creating a calc document" );
88             xSheetDoc = SOF.openDoc("scalc","_blank");
89 
90         } catch ( com.sun.star.uno.Exception e ) {
91             // Some exception occures.FAILED
92             e.printStackTrace( log );
93             throw new StatusException( "Couldn't create document", e );
94         }
95     }
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.XMLContentExporter</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. A cell in the sheet is set to some value. This made
111     * for checking if this value is successfully exported within
112     * the document content.
113     *     Object relations created :
114     * <ul>
115     *  <li> <code>'MediaDescriptor'</code> for
116     *      {@link ifc.document._XFilter} interface </li>
117     *  <li> <code>'XFilter.Checker'</code> for
118     *      {@link ifc.document._XFilter} interface </li>
119     *  <li> <code>'SourceDocument'</code> for
120     *      {@link ifc.document._XExporter} interface </li>
121     * </ul>
122     */
123     protected synchronized TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
124 
125         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
126         XInterface oObj = null;
127         final String CELL_TEXT = "XMLContentExporter";
128 
129         ContentFilterChecker Filter = new ContentFilterChecker(log);
130 
131         Any arg = new Any(new Type(XDocumentHandler.class), Filter);
132         try {
133             oObj = (XInterface) xMSF.createInstanceWithArguments(
134                 "com.sun.star.comp.Calc.XMLContentExporter",
135                 new Object[] {arg} );
136             XExporter xEx = (XExporter) UnoRuntime.queryInterface
137                 (XExporter.class,oObj);
138             xEx.setSourceDocument(xSheetDoc);
139 
140             // Setting some string to a cell
141             XSpreadsheetDocument xSpreadsheetDoc = (XSpreadsheetDocument)
142                 UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSheetDoc);
143             XSpreadsheets xSpreadsheets = xSpreadsheetDoc.getSheets();
144             XIndexAccess xSheetsIndexArray = (XIndexAccess)
145                 UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
146             XSpreadsheet xSheet = (XSpreadsheet) AnyConverter.toObject(
147                 new Type(XSpreadsheet.class),xSheetsIndexArray.getByIndex(0));
148             XCell xCell = xSheet.getCellByPosition(0, 0);
149             xCell.setFormula(CELL_TEXT);
150 
151             log.println("fill sheet 1 with contnet...");
152             util.CalcTools.fillCalcSheetWithContent(xSheetDoc, 1, 1, 1, 5, 5);
153 
154         } catch (com.sun.star.uno.Exception e) {
155             e.printStackTrace(log) ;
156             throw new StatusException("Can't create component.", e) ;
157         } catch (java.lang.Exception e) {
158             e.printStackTrace(log);
159             throw new StatusException("Can't create environment.", e);
160         }
161 
162         // adding tags which must be contained in XML output
163         Filter.addTag("office:document-content");
164         Filter.addTagEnclosed("office:body", "office:document-content");
165         Filter.addTagEnclosed("office:script", "office:document-content");
166         Filter.addTagEnclosed("table:table", "office:body");
167         Filter.addTagEnclosed("table:table-column", "table:table");
168         Filter.addTagEnclosed("table:table-row", "table:table");
169         Filter.addTagEnclosed("table:table-cell", "table:table-row");
170         Filter.addTagEnclosed("text:p", "table:table-cell");
171         Filter.addCharactersEnclosed(CELL_TEXT, "text:p");
172 
173         // create testobject here
174         log.println( "creating a new environment" );
175         TestEnvironment tEnv = new TestEnvironment( oObj );
176 
177         tEnv.addObjRelation("MediaDescriptor", XMLTools.createMediaDescriptor(
178             new String[] {"FilterName"},
179             new Object[] {"scalc: StarOffice XML (Calc)"}));
180         tEnv.addObjRelation("SourceDocument",xSheetDoc);
181         tEnv.addObjRelation("XFilter.Checker", Filter);
182         return tEnv;
183 
184     }
185 
186     /**
187      * This class checks the XML for tags and data required and returns
188      * checking result to <code>XFilter</code> interface test. All
189      * the information about errors occured in XML data is written
190      * to log specified.
191      * @see ifc.document._XFilter
192      */
193     protected class ContentFilterChecker extends XMLTools.XMLTagsChecker
194       implements ifc.document._XFilter.FilterChecker {
195 
196         /**
197          * Creates a class which will write information
198          * into log specified.
199          */
200         public ContentFilterChecker(PrintWriter log) {
201             super(log) ;
202         }
203 
204         /**
205          * <code>_XFilter.FilterChecker</code> interface method
206          * which returns the result of XML checking.
207          * @return <code>true</code> if the XML data exported was
208          * valid (i.e. all necessary tags and character data exists),
209          * <code>false</code> if some errors occured.
210          */
211         public boolean checkFilter() {
212             return checkTags();
213         }
214     }
215 }
216 
217