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 ifc.xml.sax;
29 
30 import java.io.PrintWriter;
31 
32 import lib.MultiMethodTest;
33 import lib.Status;
34 import lib.StatusException;
35 import util.XMLTools;
36 
37 import com.sun.star.xml.sax.SAXException;
38 import com.sun.star.xml.sax.XDocumentHandler;
39 import com.sun.star.xml.sax.XLocator;
40 
41 /**
42 * Testing <code>com.sun.star.xml.sax.XDocumentHandler</code>
43 * interface methods :
44 * <ul>
45 *  <li><code> startDocument()</code></li>
46 *  <li><code> endDocument()</code></li>
47 *  <li><code> startElement()</code></li>
48 *  <li><code> endElement()</code></li>
49 *  <li><code> characters()</code></li>
50 *  <li><code> ignorableWhitespace()</code></li>
51 *  <li><code> processingInstruction()</code></li>
52 *  <li><code> setDocumentLocator()</code></li>
53 * </ul> <p>
54 * This test needs the following object relations :
55 * <ul>
56 *  <li> <code>'XDocumentHandler.XMLData'</code> (of type <code>String[][]
57 *   </code>):the XML data which will be passed to the handler. Each
58 *   array of strings corresponds to some handler event. The fisrt
59 *   string of event array is the type of the event they can have
60 *   the following values :
61 *   <ul>
62 *    <li>'start' : startElement() event. The string with index 1
63 *     is the name of element, the next array elements are attributes
64 *     of XML element in order Name, Type, Value, Name, Type, Value, etc.
65 *    </li>
66 *    <li>'end' : endElement() event. The string with index 1
67 *     is the name of element. </li>
68 *    <li>'chars' : characters() event. The string with index 1
69 *     is characters. </li>
70 *    <li>'spaces' : ignorableWhitespace() event. The string with index 1
71 *     is spaces. </li>
72 *    <li>'instruct' : processingInstruction() event. The string with
73 *     index 1 is the target of instruction. The string with index
74 *     2 is the data of instruction. </li>
75 *   </ul> </li>
76 *   <li> <code>'XDocumentHandler.ImportChecker'</code>
77 *    (of type <code>ifc.xml.sax._XDocumentHandler.ImportChecker</code>) :
78 *    this relation must be implementation of the interface above
79 *    ant it must check if the XML data was successfully imported to
80 *    the document. </li>
81 *  </li>
82 * Test is <b> NOT </b> multithread compilant. <p>
83 * @see com.sun.star.xml.sax.XDocumentHandler
84 */
85 public class _XDocumentHandler extends MultiMethodTest {
86 
87     private static class DocumentLocator implements XLocator {
88         public boolean aMethodCalled = false ;
89 
90         private PrintWriter log = null ;
91         public DocumentLocator(PrintWriter log) {
92             this.log = log ;
93         }
94         public int getColumnNumber() {
95             log.println("getColumnNumber() method called.") ;
96             aMethodCalled = true ;
97             return 10 ;
98         }
99         public int getLineNumber() {
100             log.println("getLineNumber() method called.") ;
101             aMethodCalled = true ;
102             return 9 ;
103         }
104         public String getPublicId() {
105             log.println("getPublicId() method called.") ;
106             aMethodCalled = true ;
107             return "file://d:/file.txt";
108         }
109         public String getSystemId() {
110             log.println("getSystemId() method called.") ;
111             aMethodCalled = true ;
112             return "system";
113         }
114     }
115 
116     /**
117     * This interface implementation must be passed by component test
118     * for checking the whole import process.
119     */
120     public static interface ImportChecker {
121         /**
122         * Returns <code>true</code> if the XML data was successfully
123         * imported, <code>false</code> in other case.
124         */
125         boolean checkImport() ;
126     }
127 
128     /**
129     * This interface implementation must be passed by component test
130     * for setting a target document to the import process
131     */
132     public static interface TargetDocumentSetter {
133 
134         void setTargetDocument();
135     }
136 
137     public XDocumentHandler oObj = null;
138     private String[][] xmlData = null ;
139     private DocumentLocator locator = null ;
140     private ImportChecker checker = null ;
141     private boolean locatorResult = true ;
142     private SAXException locatorException = null ;
143     private boolean ToBeSkipped = false;
144 
145     /**
146     * Retrieves object relations.
147     * @throws StatusException If one of relations not found.
148     */
149     public void before() {
150         locator = new DocumentLocator(log) ;
151         if (tEnv.getTestCase().getObjectName().equals("XMLSettingsImporter")) {
152             log.println("Settings can't be imported in the current Implementation");
153             ToBeSkipped = true;
154         }
155         xmlData = (String[][])tEnv.getObjRelation("XDocumentHandler.XMLData") ;
156         checker = (ImportChecker)
157             tEnv.getObjRelation("XDocumentHandler.ImportChecker") ;
158 
159         TargetDocumentSetter targetDocSet = (TargetDocumentSetter)
160             tEnv.getObjRelation("XDocumentHandler.TargetDocumentSetter");
161 
162         if (xmlData == null || checker == null) throw new StatusException
163             (Status.failed("Relation wasn't found")) ;
164 
165         if (targetDocSet != null){
166 
167         }else{
168             log.println("object realtion 'XDocumentHandler.TargetDocumentSetter' not used.");
169             log.println("be shure that the test have a target to write throu");
170         }
171     }
172 
173     /**
174       * Sets document locator to dummy locator implementation and
175          * calls the <code>startDocument</code> method. <p>
176          *
177      * Has <b> OK </b> status if no runtime exceptions occured.
178      */
179     public void _startDocument() {
180         if (ToBeSkipped) {
181             tRes.tested("startDocument()", Status.skipped(true));
182             return;
183         }
184 
185         try {
186             oObj.setDocumentLocator(locator) ;
187         } catch (SAXException e) {
188             locatorException = e ;
189             locatorResult = false ;
190         }
191 
192         boolean result = true ;
193         try {
194             oObj.startDocument() ;
195         } catch (SAXException e) {
196             e.printStackTrace(log) ;
197             log.println("Wrapped exception :" + e.WrappedException) ;
198             result = false ;
199         }
200 
201         tRes.tested("startDocument()", result) ;
202     }
203 
204         /**
205          * This test is finally executed. It finishes XML data
206          * transfering with <code>endDocument</code> method call. <p>
207          *
208          * Has <b>OK</b> status if no exceptions occured during
209          * the whole transfering and if the appropriate changes
210          * occured in the document where XML data was trnsfered to.
211          * This check is performed by checker relation.
212          */
213     public void _endDocument() {
214         if (ToBeSkipped) {
215             tRes.tested("endDocument()", Status.skipped(true));
216             return;
217         }
218         requiredMethod("startElement()") ;
219         executeMethod("endElement()") ;
220         executeMethod("characters()") ;
221         executeMethod("ignorableWhitespace()") ;
222         executeMethod("processingInstruction()") ;
223 
224         boolean result = true ;
225         try {
226             oObj.endDocument() ;
227         } catch (SAXException e) {
228             e.printStackTrace(log) ;
229             log.println("Wrapped exception :" + e.WrappedException) ;
230             result = false ;
231         }
232 
233         log.println("Check if import was successful ...") ;
234         result &= checker.checkImport() ;
235 
236         tRes.tested("endDocument()", result) ;
237     }
238 
239         /**
240          * Transfers XML data obtained from relation
241          * <code>'XDocumentHandler.XMLData'</code>. <p>
242          *
243          * Has <b>OK</b> status if no exceptions occured during XML data
244          * transfering in <code>startDocument</code> and
245          * <code>startElement</code> method tests. <p>
246          *
247          * Exact checking of XML transfer is made in <code>endDocument</code>
248          */
249     public void _startElement() {
250         if (ToBeSkipped) {
251             tRes.tested("startElement()", Status.skipped(true));
252             return;
253         }
254         boolean result = true ;
255 
256         try {
257             log.println("StartElement Processing XML data ...") ;
258             for(int i = 0; i < xmlData.length; i++) {
259                 String[] elem = xmlData[i] ;
260                 String xmlTag = "" ;
261                 if ("start".equals(elem[0])) {
262                     xmlTag += "<" ;
263                     String tagName = elem[1] ;
264                     xmlTag += tagName ;
265                     XMLTools.AttributeList attr = new XMLTools.AttributeList() ;
266                     for (int j = 2; j < elem.length; j+=3) {
267                         attr.add(elem[j], elem[j+1], elem[j+2]);
268                         xmlTag += " " + elem[j] + "(" + elem[j+1] +
269                             ")=\"" + elem[j+2] + "\"" ;
270                     }
271                     xmlTag += ">" ;
272 
273                     log.println(xmlTag) ;
274                     oObj.startElement(tagName, attr) ;
275                 } else
276                 if ("end".equals(elem[0])) {
277                     log.println("</" + elem[1] + ">") ;
278                     oObj.endElement(elem[1]) ;
279                 } else
280                 if ("chars".equals(elem[0])) {
281                     log.println("'" + elem[1] + "'") ;
282                     oObj.characters(elem[1]) ;
283                 } else
284                 if ("spaces".equals(elem[0])) {
285                     log.println("(spaces)'" + elem[1] + "'") ;
286                     oObj.ignorableWhitespace(elem[1]) ;
287                 } else
288                 if ("instruct".equals(elem[0])) {
289                     log.println("<?" + elem[1] + " " + elem[2] + "?>") ;
290                     oObj.processingInstruction(elem[1], elem[2]) ;
291                 } else {
292                     log.println("!!! Bad object relation !!!") ;
293                     throw new StatusException(Status.failed("Bad relation")) ;
294                 }
295             }
296         } catch (SAXException e) {
297             e.printStackTrace(log) ;
298             log.println("Wrapped exception :" + e.WrappedException) ;
299             result = false ;
300         }
301 
302         tRes.tested("startElement()", result) ;
303     }
304 
305         /**
306          * Does nothing. <p>
307          *
308          * Has <b>OK</b> status if no exceptions occured during XML data
309          * transfering in <code>startDocument</code> and
310          * <code>startElement</code> method tests.
311          */
312     public void _endElement() {
313         if (ToBeSkipped) {
314             tRes.tested("endElement()", Status.skipped(true));
315             return;
316         }
317         requiredMethod("startElement()") ;
318 
319         boolean result = true ;
320 
321         tRes.tested("endElement()", result) ;
322     }
323 
324         /**
325          * Does nothing. <p>
326          *
327          * Has <b>OK</b> status if no exceptions occured during XML data
328          * transfering in <code>startDocument</code> and
329          * <code>startElement</code> method tests.
330          */
331     public void _characters() {
332         if (ToBeSkipped) {
333             tRes.tested("characters()", Status.skipped(true));
334             return;
335         }
336         requiredMethod("startElement()") ;
337 
338         boolean result = true ;
339 
340         tRes.tested("characters()", result) ;
341     }
342 
343         /**
344          * Does nothing. <p>
345          *
346          * Has <b>OK</b> status if no exceptions occured during XML data
347          * transfering in <code>startDocument</code> and
348          * <code>startElement</code> method tests.
349          */
350     public void _ignorableWhitespace() {
351         if (ToBeSkipped) {
352             tRes.tested("ignorableWhitespace()", Status.skipped(true));
353             return;
354         }
355         requiredMethod("startElement()") ;
356 
357         boolean result = true ;
358 
359         tRes.tested("ignorableWhitespace()", result) ;
360     }
361 
362         /**
363          * Does nothing. <p>
364          *
365          * Has <b>OK</b> status if no exceptions occured during XML data
366          * transfering in <code>startDocument</code> and
367          * <code>startElement</code> method tests.
368          */
369     public void _processingInstruction() {
370         if (ToBeSkipped) {
371             tRes.tested("processingInstruction()", Status.skipped(true));
372             return;
373         }
374         requiredMethod("startElement()") ;
375 
376         boolean result = true ;
377 
378         tRes.tested("processingInstruction()", result) ;
379     }
380 
381         /**
382          * Does nothing. <p>
383          *
384          * Has <b>OK</b> status if no exceptions occured during XML data
385          * transfering in <code>startDocument</code> and
386          * <code>startElement</code> method tests.
387          */
388     public void _setDocumentLocator() {
389         if (ToBeSkipped) {
390             tRes.tested("setDocumentLocator()", Status.skipped(true));
391             return;
392         }
393         executeMethod("endDocument()") ;
394 
395         boolean result = locatorResult ;
396         if (locatorException != null) {
397             log.println("Exception occured during setDocumentLocator() call:") ;
398             locatorException.printStackTrace(log) ;
399             log.println("Wrapped exception :"
400                 + locatorException.WrappedException) ;
401             result = false ;
402         }
403 
404         tRes.tested("setDocumentLocator()", result) ;
405     }
406 
407 }
408 
409