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: DocumentHandler.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 ************************************************************************/
30 package basicrunner
.basichelper
;
32 import com
.sun
.star
.lang
.XInitialization
;
33 import com
.sun
.star
.lang
.XSingleServiceFactory
;
34 import com
.sun
.star
.lang
.XServiceInfo
;
35 import com
.sun
.star
.lang
.XTypeProvider
;
36 import com
.sun
.star
.uno
.Type
;
37 import com
.sun
.star
.xml
.sax
.XDocumentHandler
;
38 import com
.sun
.star
.container
.XNameAccess
;
39 import com
.sun
.star
.container
.NoSuchElementException
;
40 import java
.util
.Vector
;
41 import util
.XMLTools
.Tag
;
43 import java
.io
.StringWriter
;
44 import java
.io
.PrintWriter
;
47 * This class provides a handler of the BASIC test document.
48 * @see com.sun.star.lang.XSingleServiceFactory
49 * @see com.sun.star.lang.XServiceInfo
51 public class DocumentHandler
implements XServiceInfo
, XSingleServiceFactory
{
52 /** The service name of this class **/
53 static final String __serviceName
= "basichelper.DocumentHandler";
54 /** The actual handler of the document **/
55 static DocumentHandlerImpl oDocumentHandler
= null;
56 /** A string writer **/
57 private StringWriter writer
;
58 /** The log writer (just a wrapper around <code>writer</code>) **/
59 private PrintWriter log
;
62 * Create an instance of the document handler.
63 * @param args A boolean value as <codde>args[0]</code> determines,
64 * if checked XML data is printed to the log.
66 * @return The document handler
68 public Object
createInstanceWithArguments(Object
[] args
) {
69 boolean printXML
= false;
70 if (args
!= null && args
.length
!=0 && args
[0] instanceof Boolean
)
71 printXML
= ((Boolean
)args
[0]).booleanValue();
72 writer
= new StringWriter();
73 log
= new PrintWriter(writer
);
74 oDocumentHandler
= new DocumentHandlerImpl(log
, printXML
, writer
);
75 return oDocumentHandler
;
79 * Create an instance of the document handler.
80 * @return The document handler
82 public Object
createInstance() {
83 return createInstanceWithArguments(null);
86 /** Get the unique id for this implementation
89 public byte[] getImplementationId() {
90 return toString().getBytes();
93 /** Get all implemented types.
94 * @return The implemented UNO types.
96 public Type
[] getTypes() {
97 Class interfaces
[] = getClass().getInterfaces();
98 Type types
[] = new Type
[interfaces
.length
];
99 for(int i
= 0; i
< interfaces
.length
; ++ i
)
100 types
[i
] = new Type(interfaces
[i
]);
104 /** Is this servioce supported?
105 * @param name The service name.
106 * @return True, if the service is supported.
108 public boolean supportsService(String name
) {
109 return __serviceName
.equals(name
);
113 * Get all supported service names.
114 * @return All supported servcices.
116 public String
[] getSupportedServiceNames() {
117 return new String
[] {__serviceName
};
121 * Get the implementation name of this class.
122 * @return The implementation name.
124 public String
getImplementationName() {
125 return getClass().getName();
130 * The actual implementation of the document handler
131 * @see util.XMLTools.XMLChecker
132 * @see com.sun.star.lang.XInitialization
133 * @see com.sun.star.xml.sax.XDocumentHandler
134 * @see com.sun.star.container.XNameAccess
135 * @see com.sun.star.lang.XTypeProvider
137 class DocumentHandlerImpl
extends XMLTools
.XMLChecker
138 implements XInitialization
, XDocumentHandler
,
139 XNameAccess
, XTypeProvider
{
140 /** A string writer **/
141 private StringWriter writer
;
145 * @param log_ A log writer.
146 * @param printXML Should XML data be printed to the log?
147 * @param logWriter A wrapper around <code>log_</code> for convenience.
149 public DocumentHandlerImpl(PrintWriter log_
,
150 boolean printXML
, StringWriter logWriter
) {
151 super(log_
, printXML
);
156 * Initialize this class with rules.
157 * @param parm1 An array of filter rules:
158 * <code>processAction()</code> is called for every rule.
159 * @throws com.sun.star.uno.Exception for an incorrect rule.
161 public void initialize(Object
[] parm1
) throws com
.sun
.star
.uno
.Exception
{
162 if (!(parm1
[0] instanceof Object
[])) return;
163 for (int i
=0; i
<parm1
.length
; i
++) {
164 processActionForXMLChecker((Object
[])parm1
[i
]);
169 * Method processes all filters received from basic tests.
170 * Called by initialize().
171 * @param filterRule An array building one filter rule.
172 * @throws com.sun.star.uno.Exception for an incorrect rule.
174 private void processActionForXMLChecker(Object
[] filterRule
)
175 throws com
.sun
.star
.uno
.Exception
{
176 int arrLen
= filterRule
.length
;
180 int tagsNum
= arrLen
-1;
181 Vector allTags
= new Vector();
185 // First element of rule is RuleName and should be String
186 if (!(filterRule
[0] instanceof String
)) {
187 throw new com
.sun
.star
.uno
.Exception("Error: incorrect filter rule "+
188 "received from basic test! Rule name must be a String.");
190 action
= (String
) filterRule
[0];
193 // Searching for character data and defining amount of tags received.
194 for (int j
=1; j
<arrLen
; j
++) {
195 if ( (filterRule
[j
] instanceof String
) && (j
!= 1) ) {
196 CDATA
= (String
) filterRule
[j
];
201 // Adding received tags to internal array.
202 oTags
= new Object
[tagsNum
];
203 for (int j
=1; j
<=tagsNum
; j
++) {
204 if (filterRule
[j
] instanceof Object
[]) {
205 oTags
[j
-1] = (Object
[]) filterRule
[j
];
209 // Process all received tags for a given filter rule
210 for (int i
=0; i
<oTags
.length
; i
++) {
211 if (oTags
[i
] instanceof Object
[]) {
212 oTag
= (Object
[]) oTags
[i
];
213 oTagName
= (String
) oTag
[0];
214 } else if (oTags
[i
] instanceof Object
) {
215 oTag
= new Object
[1];
216 oTag
[0] = (Object
) oTags
[i
];
217 oTagName
= (String
) oTag
[0];
219 throw new com
.sun
.star
.uno
.Exception("Error: invalid tag "+
220 "received from basic test! Check tag "
221 +i
+" in rule '"+action
+"'.");
224 // Action for constructor Tag(TagName, attrName, attrValue)
225 if (oTag
.length
== 3) {
226 if ((oTag
[1] instanceof String
)&&(oTag
[2] instanceof String
)) {
227 allTags
.add(new Tag(oTagName
,
228 (String
) oTag
[1], (String
) oTag
[2]));
230 throw new com
.sun
.star
.uno
.Exception("Error: invalid tag '"+
231 oTagName
+"' received from basic test!");
234 // Action for constructors:
235 // Tag(TagName, String[][] attrValues )
236 // Tag(TagName, String[] attrNames)
237 // Tag(TagName, String attrName)
239 } else if (oTag
.length
== 2) {
240 if (oTag
[1] instanceof String
[][]) {
241 allTags
.add(new Tag(oTagName
, (String
[][]) oTag
[1]));
242 } else if (oTag
[1] instanceof String
[]) {
243 allTags
.add(new Tag(oTagName
, (String
[]) oTag
[1]));
244 } else if (oTag
[1] instanceof String
) {
245 allTags
.add(new Tag(oTagName
, (String
) oTag
[1]));
247 throw new com
.sun
.star
.uno
.Exception("Error: invalid tag '"+
248 oTagName
+"' received from basic test!");
251 // Action for constructor Tag(TagName)
252 } else if (oTag
.length
== 1) {
253 if (oTag
[0] instanceof String
) {
254 allTags
.add(new Tag(oTagName
));
256 throw new com
.sun
.star
.uno
.Exception("Error: invalid tag '"+
257 oTagName
+"' received from basic test!");
260 throw new com
.sun
.star
.uno
.Exception("Error: invalid tag '"+
261 oTagName
+"' received from basic test!");
265 // Adding tags to XMLChecker
266 if ( action
.equals((String
)"TagExists") ) {
267 for (int i
=0; i
<allTags
.size(); i
++) {
268 addTag((Tag
)allTags
.get(i
));
270 } else if (action
.equals((String
)"TagEnclosed")) {
271 addTagEnclosed((Tag
) allTags
.get(0), (Tag
) allTags
.get(1));
272 } else if (action
.equals((String
)"CharsEnclosed")) {
273 addCharactersEnclosed(CDATA
, (Tag
) allTags
.get(0));
275 throw new com
.sun
.star
.uno
.Exception("Error: incorrect rule name '"+
276 action
+"' received from basic test!");
281 * Get the names of the elements.
282 * @return element names.
284 public String
[] getElementNames() {
285 return new String
[]{"XMLCode", "XMLIsCorrect"};
289 * Is this an element?
290 * @param name Element name.
291 * @return true, if <code>name>/code> is the name of an element.
293 public boolean hasByName(String name
) {
294 return (name
.equals("XMLCode") || name
.equals("XMLIsCorrect"));
298 * Get an element by its name.
299 * @param name The element name.
300 * @return The element with the specified <code>name</code>.
301 * @throws NoSuchElementException Is thrown, if name does not exist.
303 public Object
getByName(String name
) throws NoSuchElementException
{
304 if (name
.equals("XMLIsCorrect"))
305 return new Boolean(this.check());
306 else if (name
.equals("XMLCode")) {
307 return writer
.getBuffer().toString();
309 throw new NoSuchElementException();
313 * Are there any elements?
314 * @return Always true.
316 public boolean hasElements() {
321 * Get the element type.
324 public Type
getElementType() {
325 return new Type(Object
.class);
329 * Get a unique id for this implementation.
332 public byte[] getImplementationId() {
333 return toString().getBytes();
337 * Return all implemented types of this class.
338 * @return The implemented UNO types.
340 public Type
[] getTypes() {
341 Class interfaces
[] = getClass().getInterfaces();
342 Type types
[] = new Type
[interfaces
.length
];
343 for(int i
= 0; i
< interfaces
.length
; ++ i
)
344 types
[i
] = new Type(interfaces
[i
]);