1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XDocumentHandler.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import java
.io
.PrintWriter
;
35 import lib
.MultiMethodTest
;
37 import lib
.StatusException
;
40 import com
.sun
.star
.xml
.sax
.SAXException
;
41 import com
.sun
.star
.xml
.sax
.XDocumentHandler
;
42 import com
.sun
.star
.xml
.sax
.XLocator
;
45 * Testing <code>com.sun.star.xml.sax.XDocumentHandler</code>
48 * <li><code> startDocument()</code></li>
49 * <li><code> endDocument()</code></li>
50 * <li><code> startElement()</code></li>
51 * <li><code> endElement()</code></li>
52 * <li><code> characters()</code></li>
53 * <li><code> ignorableWhitespace()</code></li>
54 * <li><code> processingInstruction()</code></li>
55 * <li><code> setDocumentLocator()</code></li>
57 * This test needs the following object relations :
59 * <li> <code>'XDocumentHandler.XMLData'</code> (of type <code>String[][]
60 * </code>):the XML data which will be passed to the handler. Each
61 * array of strings corresponds to some handler event. The fisrt
62 * string of event array is the type of the event they can have
63 * the following values :
65 * <li>'start' : startElement() event. The string with index 1
66 * is the name of element, the next array elements are attributes
67 * of XML element in order Name, Type, Value, Name, Type, Value, etc.
69 * <li>'end' : endElement() event. The string with index 1
70 * is the name of element. </li>
71 * <li>'chars' : characters() event. The string with index 1
72 * is characters. </li>
73 * <li>'spaces' : ignorableWhitespace() event. The string with index 1
75 * <li>'instruct' : processingInstruction() event. The string with
76 * index 1 is the target of instruction. The string with index
77 * 2 is the data of instruction. </li>
79 * <li> <code>'XDocumentHandler.ImportChecker'</code>
80 * (of type <code>ifc.xml.sax._XDocumentHandler.ImportChecker</code>) :
81 * this relation must be implementation of the interface above
82 * ant it must check if the XML data was successfully imported to
85 * Test is <b> NOT </b> multithread compilant. <p>
86 * @see com.sun.star.xml.sax.XDocumentHandler
88 public class _XDocumentHandler
extends MultiMethodTest
{
90 private static class DocumentLocator
implements XLocator
{
91 public boolean aMethodCalled
= false ;
93 private PrintWriter log
= null ;
94 public DocumentLocator(PrintWriter log
) {
97 public int getColumnNumber() {
98 log
.println("getColumnNumber() method called.") ;
99 aMethodCalled
= true ;
102 public int getLineNumber() {
103 log
.println("getLineNumber() method called.") ;
104 aMethodCalled
= true ;
107 public String
getPublicId() {
108 log
.println("getPublicId() method called.") ;
109 aMethodCalled
= true ;
110 return "file://d:/file.txt";
112 public String
getSystemId() {
113 log
.println("getSystemId() method called.") ;
114 aMethodCalled
= true ;
120 * This interface implementation must be passed by component test
121 * for checking the whole import process.
123 public static interface ImportChecker
{
125 * Returns <code>true</code> if the XML data was successfully
126 * imported, <code>false</code> in other case.
128 boolean checkImport() ;
132 * This interface implementation must be passed by component test
133 * for setting a target document to the import process
135 public static interface TargetDocumentSetter
{
137 void setTargetDocument();
140 public XDocumentHandler oObj
= null;
141 private String
[][] xmlData
= null ;
142 private DocumentLocator locator
= null ;
143 private ImportChecker checker
= null ;
144 private boolean locatorResult
= true ;
145 private SAXException locatorException
= null ;
146 private boolean ToBeSkipped
= false;
149 * Retrieves object relations.
150 * @throws StatusException If one of relations not found.
152 public void before() {
153 locator
= new DocumentLocator(log
) ;
154 if (tEnv
.getTestCase().getObjectName().equals("XMLSettingsImporter")) {
155 log
.println("Settings can't be imported in the current Implementation");
158 xmlData
= (String
[][])tEnv
.getObjRelation("XDocumentHandler.XMLData") ;
159 checker
= (ImportChecker
)
160 tEnv
.getObjRelation("XDocumentHandler.ImportChecker") ;
162 TargetDocumentSetter targetDocSet
= (TargetDocumentSetter
)
163 tEnv
.getObjRelation("XDocumentHandler.TargetDocumentSetter");
165 if (xmlData
== null || checker
== null) throw new StatusException
166 (Status
.failed("Relation wasn't found")) ;
168 if (targetDocSet
!= null){
171 log
.println("object realtion 'XDocumentHandler.TargetDocumentSetter' not used.");
172 log
.println("be shure that the test have a target to write throu");
177 * Sets document locator to dummy locator implementation and
178 * calls the <code>startDocument</code> method. <p>
180 * Has <b> OK </b> status if no runtime exceptions occured.
182 public void _startDocument() {
184 tRes
.tested("startDocument()", Status
.skipped(true));
189 oObj
.setDocumentLocator(locator
) ;
190 } catch (SAXException e
) {
191 locatorException
= e
;
192 locatorResult
= false ;
195 boolean result
= true ;
197 oObj
.startDocument() ;
198 } catch (SAXException e
) {
199 e
.printStackTrace(log
) ;
200 log
.println("Wrapped exception :" + e
.WrappedException
) ;
204 tRes
.tested("startDocument()", result
) ;
208 * This test is finally executed. It finishes XML data
209 * transfering with <code>endDocument</code> method call. <p>
211 * Has <b>OK</b> status if no exceptions occured during
212 * the whole transfering and if the appropriate changes
213 * occured in the document where XML data was trnsfered to.
214 * This check is performed by checker relation.
216 public void _endDocument() {
218 tRes
.tested("endDocument()", Status
.skipped(true));
221 requiredMethod("startElement()") ;
222 executeMethod("endElement()") ;
223 executeMethod("characters()") ;
224 executeMethod("ignorableWhitespace()") ;
225 executeMethod("processingInstruction()") ;
227 boolean result
= true ;
230 } catch (SAXException e
) {
231 e
.printStackTrace(log
) ;
232 log
.println("Wrapped exception :" + e
.WrappedException
) ;
236 log
.println("Check if import was successful ...") ;
237 result
&= checker
.checkImport() ;
239 tRes
.tested("endDocument()", result
) ;
243 * Transfers XML data obtained from relation
244 * <code>'XDocumentHandler.XMLData'</code>. <p>
246 * Has <b>OK</b> status if no exceptions occured during XML data
247 * transfering in <code>startDocument</code> and
248 * <code>startElement</code> method tests. <p>
250 * Exact checking of XML transfer is made in <code>endDocument</code>
252 public void _startElement() {
254 tRes
.tested("startElement()", Status
.skipped(true));
257 boolean result
= true ;
260 log
.println("StartElement Processing XML data ...") ;
261 for(int i
= 0; i
< xmlData
.length
; i
++) {
262 String
[] elem
= xmlData
[i
] ;
264 if ("start".equals(elem
[0])) {
266 String tagName
= elem
[1] ;
268 XMLTools
.AttributeList attr
= new XMLTools
.AttributeList() ;
269 for (int j
= 2; j
< elem
.length
; j
+=3) {
270 attr
.add(elem
[j
], elem
[j
+1], elem
[j
+2]);
271 xmlTag
+= " " + elem
[j
] + "(" + elem
[j
+1] +
272 ")=\"" + elem
[j
+2] + "\"" ;
276 log
.println(xmlTag
) ;
277 oObj
.startElement(tagName
, attr
) ;
279 if ("end".equals(elem
[0])) {
280 log
.println("</" + elem
[1] + ">") ;
281 oObj
.endElement(elem
[1]) ;
283 if ("chars".equals(elem
[0])) {
284 log
.println("'" + elem
[1] + "'") ;
285 oObj
.characters(elem
[1]) ;
287 if ("spaces".equals(elem
[0])) {
288 log
.println("(spaces)'" + elem
[1] + "'") ;
289 oObj
.ignorableWhitespace(elem
[1]) ;
291 if ("instruct".equals(elem
[0])) {
292 log
.println("<?" + elem
[1] + " " + elem
[2] + "?>") ;
293 oObj
.processingInstruction(elem
[1], elem
[2]) ;
295 log
.println("!!! Bad object relation !!!") ;
296 throw new StatusException(Status
.failed("Bad relation")) ;
299 } catch (SAXException e
) {
300 e
.printStackTrace(log
) ;
301 log
.println("Wrapped exception :" + e
.WrappedException
) ;
305 tRes
.tested("startElement()", result
) ;
311 * Has <b>OK</b> status if no exceptions occured during XML data
312 * transfering in <code>startDocument</code> and
313 * <code>startElement</code> method tests.
315 public void _endElement() {
317 tRes
.tested("endElement()", Status
.skipped(true));
320 requiredMethod("startElement()") ;
322 boolean result
= true ;
324 tRes
.tested("endElement()", result
) ;
330 * Has <b>OK</b> status if no exceptions occured during XML data
331 * transfering in <code>startDocument</code> and
332 * <code>startElement</code> method tests.
334 public void _characters() {
336 tRes
.tested("characters()", Status
.skipped(true));
339 requiredMethod("startElement()") ;
341 boolean result
= true ;
343 tRes
.tested("characters()", result
) ;
349 * Has <b>OK</b> status if no exceptions occured during XML data
350 * transfering in <code>startDocument</code> and
351 * <code>startElement</code> method tests.
353 public void _ignorableWhitespace() {
355 tRes
.tested("ignorableWhitespace()", Status
.skipped(true));
358 requiredMethod("startElement()") ;
360 boolean result
= true ;
362 tRes
.tested("ignorableWhitespace()", result
) ;
368 * Has <b>OK</b> status if no exceptions occured during XML data
369 * transfering in <code>startDocument</code> and
370 * <code>startElement</code> method tests.
372 public void _processingInstruction() {
374 tRes
.tested("processingInstruction()", Status
.skipped(true));
377 requiredMethod("startElement()") ;
379 boolean result
= true ;
381 tRes
.tested("processingInstruction()", result
) ;
387 * Has <b>OK</b> status if no exceptions occured during XML data
388 * transfering in <code>startDocument</code> and
389 * <code>startElement</code> method tests.
391 public void _setDocumentLocator() {
393 tRes
.tested("setDocumentLocator()", Status
.skipped(true));
396 executeMethod("endDocument()") ;
398 boolean result
= locatorResult
;
399 if (locatorException
!= null) {
400 log
.println("Exception occured during setDocumentLocator() call:") ;
401 locatorException
.printStackTrace(log
) ;
402 log
.println("Wrapped exception :"
403 + locatorException
.WrappedException
) ;
407 tRes
.tested("setDocumentLocator()", result
) ;