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._sc;
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.XIndexAccess;
36 import com.sun.star.document.XExporter;
37 import com.sun.star.lang.XComponent;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.sheet.XSpreadsheet;
40 import com.sun.star.sheet.XSpreadsheetDocument;
41 import com.sun.star.sheet.XSpreadsheets;
42 import com.sun.star.table.XCell;
43 import com.sun.star.uno.Any;
44 import com.sun.star.uno.AnyConverter;
45 import com.sun.star.uno.Type;
46 import com.sun.star.uno.UnoRuntime;
47 import com.sun.star.uno.XInterface;
48 import com.sun.star.xml.sax.XDocumentHandler;
49 
50 /**
51  * Test for object which is represented by service
52  * <code>com.sun.star.comp.Calc.XMLContentExporter</code>. <p>
53  * Object implements the following interfaces :
54  * <ul>
55  *  <li><code>com::sun::star::lang::XInitialization</code></li>
56  *  <li><code>com::sun::star::document::ExportFilter</code></li>
57  *  <li><code>com::sun::star::document::XFilter</code></li>
58  *  <li><code>com::sun::star::document::XExporter</code></li>
59  *  <li><code>com::sun::star::beans::XPropertySet</code></li>
60  * </ul>
61  * @see com.sun.star.lang.XInitialization
62  * @see com.sun.star.document.ExportFilter
63  * @see com.sun.star.document.XFilter
64  * @see com.sun.star.document.XExporter
65  * @see com.sun.star.beans.XPropertySet
66  * @see ifc.lang._XInitialization
67  * @see ifc.document._ExportFilter
68  * @see ifc.document._XFilter
69  * @see ifc.document._XExporter
70  * @see ifc.beans._XPropertySet
71  */
72 public class XMLContentExporter extends TestCase {
73 
74     static XComponent xSheetDoc;
75     static ContentFilterChecker Filter;
76 
77     /**
78      * New spreadsheet document created.
79      */
initialize( TestParameters tParam, PrintWriter log )80     protected void initialize( TestParameters tParam, PrintWriter log ) {
81         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
82         try {
83             log.println( "creating a calc document" );
84             xSheetDoc = SOF.openDoc("scalc","_blank");
85 
86         } catch ( com.sun.star.uno.Exception e ) {
87             // Some exception occures.FAILED
88             e.printStackTrace( log );
89             throw new StatusException( "Couldn't create document", e );
90         }
91     }
92 
cleanup( TestParameters tParam, PrintWriter log )93     protected void cleanup( TestParameters tParam, PrintWriter log ) {
94         log.println( "    disposing xCalcDoc " );
95         util.DesktopTools.closeDoc(xSheetDoc);
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.Calc.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 calc document is set as a source document for exporter
106     * created. A cell in the sheet is set to some value. This made
107     * for checking if this value is successfully exported within
108     * the document content.
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(TestParameters tParam, PrintWriter log) {
120 
121         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF() ;
122         XInterface oObj = null;
123         final String CELL_TEXT = "XMLContentExporter";
124 
125         ContentFilterChecker Filter = new ContentFilterChecker(log);
126 
127         Any arg = new Any(new Type(XDocumentHandler.class), Filter);
128         try {
129             oObj = (XInterface) xMSF.createInstanceWithArguments(
130                 "com.sun.star.comp.Calc.XMLContentExporter",
131                 new Object[] {arg} );
132             XExporter xEx = (XExporter) UnoRuntime.queryInterface
133                 (XExporter.class,oObj);
134             xEx.setSourceDocument(xSheetDoc);
135 
136             // Setting some string to a cell
137             XSpreadsheetDocument xSpreadsheetDoc = (XSpreadsheetDocument)
138                 UnoRuntime.queryInterface(XSpreadsheetDocument.class, xSheetDoc);
139             XSpreadsheets xSpreadsheets = xSpreadsheetDoc.getSheets();
140             XIndexAccess xSheetsIndexArray = (XIndexAccess)
141                 UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets);
142             XSpreadsheet xSheet = (XSpreadsheet) AnyConverter.toObject(
143                 new Type(XSpreadsheet.class),xSheetsIndexArray.getByIndex(0));
144             XCell xCell = xSheet.getCellByPosition(0, 0);
145             xCell.setFormula(CELL_TEXT);
146 
147             log.println("fill sheet 1 with contnet...");
148             util.CalcTools.fillCalcSheetWithContent(xSheetDoc, 1, 1, 1, 5, 5);
149 
150         } catch (com.sun.star.uno.Exception e) {
151             e.printStackTrace(log) ;
152             throw new StatusException("Can't create component.", e) ;
153         } catch (java.lang.Exception e) {
154             e.printStackTrace(log);
155             throw new StatusException("Can't create environment.", e);
156         }
157 
158         // adding tags which must be contained in XML output
159         Filter.addTag("office:document-content");
160         Filter.addTagEnclosed("office:body", "office:document-content");
161         Filter.addTagEnclosed("office:script", "office:document-content");
162         Filter.addTagEnclosed("table:table", "office:body");
163         Filter.addTagEnclosed("table:table-column", "table:table");
164         Filter.addTagEnclosed("table:table-row", "table:table");
165         Filter.addTagEnclosed("table:table-cell", "table:table-row");
166         Filter.addTagEnclosed("text:p", "table:table-cell");
167         Filter.addCharactersEnclosed(CELL_TEXT, "text:p");
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[] {"scalc: StarOffice XML (Calc)"}));
176         tEnv.addObjRelation("SourceDocument",xSheetDoc);
177         tEnv.addObjRelation("XFilter.Checker", Filter);
178         return tEnv;
179 
180     }
181 
182     /**
183      * This class checks the XML for tags and data required and returns
184      * checking result to <code>XFilter</code> interface test. All
185      * the information about errors occured in XML data is written
186      * to log specified.
187      * @see ifc.document._XFilter
188      */
189     protected class ContentFilterChecker extends XMLTools.XMLTagsChecker
190       implements ifc.document._XFilter.FilterChecker {
191 
192         /**
193          * Creates a class which will write information
194          * into log specified.
195          */
ContentFilterChecker(PrintWriter log)196         public ContentFilterChecker(PrintWriter log) {
197             super(log) ;
198         }
199 
200         /**
201          * <code>_XFilter.FilterChecker</code> interface method
202          * which returns the result of XML checking.
203          * @return <code>true</code> if the XML data exported was
204          * valid (i.e. all necessary tags and character data exists),
205          * <code>false</code> if some errors occured.
206          */
checkFilter()207         public boolean checkFilter() {
208             return checkTags();
209         }
210     }
211 }
212 
213