1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package basicrunner.basichelper;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.lang.XInitialization;
26cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
27cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
28cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider;
29cdf0e10cSrcweir import com.sun.star.uno.Type;
30cdf0e10cSrcweir import com.sun.star.xml.sax.XDocumentHandler;
31cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
32cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException;
33cdf0e10cSrcweir import java.util.Vector;
34cdf0e10cSrcweir import util.XMLTools.Tag;
35cdf0e10cSrcweir import util.XMLTools;
36cdf0e10cSrcweir import java.io.StringWriter;
37cdf0e10cSrcweir import java.io.PrintWriter;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir /**
40cdf0e10cSrcweir  * This class provides a handler of the BASIC test document.
41cdf0e10cSrcweir  * @see com.sun.star.lang.XSingleServiceFactory
42cdf0e10cSrcweir  * @see com.sun.star.lang.XServiceInfo
43cdf0e10cSrcweir  */
44cdf0e10cSrcweir public class DocumentHandler implements XServiceInfo, XSingleServiceFactory {
45cdf0e10cSrcweir     /** The service name of this class **/
46cdf0e10cSrcweir     static final String __serviceName = "basichelper.DocumentHandler";
47cdf0e10cSrcweir     /** The actual handler of the document **/
48cdf0e10cSrcweir     static DocumentHandlerImpl oDocumentHandler = null;
49cdf0e10cSrcweir     /** A string writer **/
50cdf0e10cSrcweir     private StringWriter writer;
51cdf0e10cSrcweir     /** The log writer (just a wrapper around <code>writer</code>) **/
52cdf0e10cSrcweir     private PrintWriter log;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     /**
55cdf0e10cSrcweir      * Create an instance of the document handler.
56cdf0e10cSrcweir      * @param args A boolean value as <codde>args[0]</code> determines,
57cdf0e10cSrcweir      *             if checked XML data is printed to the log.
58cdf0e10cSrcweir      *             Default is false.
59cdf0e10cSrcweir      * @return The document handler
60cdf0e10cSrcweir      */
createInstanceWithArguments(Object[] args)61cdf0e10cSrcweir     public Object createInstanceWithArguments(Object[] args) {
62cdf0e10cSrcweir         boolean printXML = false;
63cdf0e10cSrcweir         if (args != null && args.length!=0 && args[0] instanceof Boolean)
64cdf0e10cSrcweir             printXML = ((Boolean)args[0]).booleanValue();
65cdf0e10cSrcweir         writer = new StringWriter();
66cdf0e10cSrcweir         log = new PrintWriter(writer);
67cdf0e10cSrcweir         oDocumentHandler = new DocumentHandlerImpl(log, printXML, writer);
68cdf0e10cSrcweir         return oDocumentHandler;
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     /**
72cdf0e10cSrcweir      * Create an instance of the document handler.
73cdf0e10cSrcweir      * @return The document handler
74cdf0e10cSrcweir      */
createInstance()75cdf0e10cSrcweir     public Object createInstance() {
76cdf0e10cSrcweir         return createInstanceWithArguments(null);
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     /** Get the unique id for this implementation
80cdf0e10cSrcweir      * @return The id.
81cdf0e10cSrcweir      */
getImplementationId()82cdf0e10cSrcweir     public byte[] getImplementationId() {
83cdf0e10cSrcweir         return toString().getBytes();
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     /** Get all implemented types.
87cdf0e10cSrcweir      * @return The implemented UNO types.
88cdf0e10cSrcweir      */
getTypes()89cdf0e10cSrcweir     public Type[] getTypes() {
90cdf0e10cSrcweir         Class interfaces[] = getClass().getInterfaces();
91cdf0e10cSrcweir         Type types[] = new Type[interfaces.length];
92cdf0e10cSrcweir         for(int i = 0; i < interfaces.length; ++ i)
93cdf0e10cSrcweir             types[i] = new Type(interfaces[i]);
94cdf0e10cSrcweir         return types;
95cdf0e10cSrcweir     }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     /** Is this servioce supported?
98cdf0e10cSrcweir      * @param name The service name.
99cdf0e10cSrcweir      * @return True, if the service is supported.
100cdf0e10cSrcweir      */
supportsService(String name)101cdf0e10cSrcweir     public boolean supportsService(String name) {
102cdf0e10cSrcweir         return __serviceName.equals(name);
103cdf0e10cSrcweir     }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     /**
106cdf0e10cSrcweir      * Get all supported service names.
107cdf0e10cSrcweir      * @return All supported servcices.
108cdf0e10cSrcweir      */
getSupportedServiceNames()109cdf0e10cSrcweir     public String[] getSupportedServiceNames() {
110cdf0e10cSrcweir         return new String[] {__serviceName};
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     /**
114cdf0e10cSrcweir      * Get the implementation name of this class.
115cdf0e10cSrcweir      * @return The implementation name.
116cdf0e10cSrcweir      */
getImplementationName()117cdf0e10cSrcweir     public String getImplementationName() {
118cdf0e10cSrcweir         return getClass().getName();
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
122cdf0e10cSrcweir /**
123cdf0e10cSrcweir  * The actual implementation of the document handler
124cdf0e10cSrcweir  * @see util.XMLTools.XMLChecker
125cdf0e10cSrcweir  * @see com.sun.star.lang.XInitialization
126cdf0e10cSrcweir  * @see com.sun.star.xml.sax.XDocumentHandler
127cdf0e10cSrcweir  * @see com.sun.star.container.XNameAccess
128cdf0e10cSrcweir  * @see com.sun.star.lang.XTypeProvider
129cdf0e10cSrcweir  */
130cdf0e10cSrcweir class DocumentHandlerImpl extends XMLTools.XMLChecker
131cdf0e10cSrcweir                         implements XInitialization, XDocumentHandler,
132cdf0e10cSrcweir                                                 XNameAccess, XTypeProvider {
133cdf0e10cSrcweir     /** A string writer **/
134cdf0e10cSrcweir     private StringWriter writer;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     /**
137cdf0e10cSrcweir      * Constructor
138cdf0e10cSrcweir      * @param log_ A log writer.
139cdf0e10cSrcweir      * @param printXML Should XML data be printed to the log?
140cdf0e10cSrcweir      * @param logWriter A wrapper around <code>log_</code> for convenience.
141cdf0e10cSrcweir      */
DocumentHandlerImpl(PrintWriter log_, boolean printXML, StringWriter logWriter)142cdf0e10cSrcweir     public DocumentHandlerImpl(PrintWriter log_,
143cdf0e10cSrcweir                                     boolean printXML, StringWriter logWriter) {
144cdf0e10cSrcweir         super(log_, printXML);
145cdf0e10cSrcweir         writer = logWriter;
146cdf0e10cSrcweir     }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     /**
149cdf0e10cSrcweir      * Initialize this class with rules.
150cdf0e10cSrcweir      * @param parm1 An array of filter rules:
151cdf0e10cSrcweir      *              <code>processAction()</code> is called for every rule.
152cdf0e10cSrcweir      * @throws com.sun.star.uno.Exception for an incorrect rule.
153cdf0e10cSrcweir      */
initialize(Object[] parm1)154cdf0e10cSrcweir     public void initialize(Object[] parm1) throws com.sun.star.uno.Exception {
155cdf0e10cSrcweir         if (!(parm1[0] instanceof Object[])) return;
156cdf0e10cSrcweir         for (int i=0; i<parm1.length; i++) {
157cdf0e10cSrcweir             processActionForXMLChecker((Object[])parm1[i]);
158cdf0e10cSrcweir         }
159cdf0e10cSrcweir     }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir     /**
162cdf0e10cSrcweir     * Method processes all filters received from basic tests.
163cdf0e10cSrcweir     * Called by initialize().
164cdf0e10cSrcweir     * @param filterRule An array building one filter rule.
165cdf0e10cSrcweir     * @throws com.sun.star.uno.Exception for an incorrect rule.
166cdf0e10cSrcweir     */
processActionForXMLChecker(Object[] filterRule)167cdf0e10cSrcweir     private void processActionForXMLChecker(Object[] filterRule)
168cdf0e10cSrcweir                                         throws com.sun.star.uno.Exception {
169cdf0e10cSrcweir         int arrLen = filterRule.length;
170cdf0e10cSrcweir         String oTagName;
171cdf0e10cSrcweir         Object[] oTags;
172cdf0e10cSrcweir         Object[] oTag;
173cdf0e10cSrcweir         int tagsNum = arrLen-1;
174cdf0e10cSrcweir         Vector allTags = new Vector();
175cdf0e10cSrcweir         String CDATA = "";
176cdf0e10cSrcweir         String action = "";
177cdf0e10cSrcweir 
178cdf0e10cSrcweir         // First element of rule is RuleName and should be String
179cdf0e10cSrcweir         if (!(filterRule[0] instanceof String)) {
180cdf0e10cSrcweir             throw new com.sun.star.uno.Exception("Error: incorrect filter rule "+
181cdf0e10cSrcweir             "received from basic test! Rule name must be a String.");
182cdf0e10cSrcweir         } else {
183cdf0e10cSrcweir             action = (String) filterRule[0];
184cdf0e10cSrcweir         }
185cdf0e10cSrcweir 
186cdf0e10cSrcweir         // Searching for character data and defining amount of tags received.
187cdf0e10cSrcweir         for (int j=1; j<arrLen; j++) {
188cdf0e10cSrcweir             if ( (filterRule[j] instanceof String) && (j != 1) ) {
189cdf0e10cSrcweir                 CDATA = (String) filterRule[j];
190cdf0e10cSrcweir                 tagsNum--;
191cdf0e10cSrcweir             }
192cdf0e10cSrcweir         }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir         // Adding received tags to internal array.
195cdf0e10cSrcweir         oTags = new Object[tagsNum];
196cdf0e10cSrcweir         for (int j=1; j<=tagsNum; j++) {
197cdf0e10cSrcweir             if (filterRule[j] instanceof Object[]) {
198cdf0e10cSrcweir                 oTags[j-1] = (Object[]) filterRule[j];
199cdf0e10cSrcweir             }
200cdf0e10cSrcweir         }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir         // Process all received tags for a given filter rule
203cdf0e10cSrcweir         for (int i=0; i<oTags.length; i++) {
204cdf0e10cSrcweir             if (oTags[i] instanceof Object[]) {
205cdf0e10cSrcweir                 oTag = (Object[]) oTags[i];
206cdf0e10cSrcweir                 oTagName = (String) oTag[0];
207cdf0e10cSrcweir             } else if (oTags[i] instanceof Object) {
208cdf0e10cSrcweir                 oTag = new Object[1];
209cdf0e10cSrcweir                 oTag[0] = (Object) oTags[i];
210cdf0e10cSrcweir                 oTagName = (String) oTag[0];
211cdf0e10cSrcweir             } else {
212cdf0e10cSrcweir                 throw new com.sun.star.uno.Exception("Error: invalid tag "+
213cdf0e10cSrcweir                                     "received from basic test! Check tag "
214cdf0e10cSrcweir                                     +i+" in rule '"+action+"'.");
215cdf0e10cSrcweir             }
216cdf0e10cSrcweir 
217cdf0e10cSrcweir             // Action for constructor Tag(TagName, attrName, attrValue)
218cdf0e10cSrcweir             if (oTag.length == 3) {
219cdf0e10cSrcweir                 if ((oTag[1] instanceof String)&&(oTag[2] instanceof String)) {
220cdf0e10cSrcweir                     allTags.add(new Tag(oTagName,
221cdf0e10cSrcweir                                     (String) oTag[1], (String) oTag[2]));
222cdf0e10cSrcweir                 } else {
223cdf0e10cSrcweir                     throw new com.sun.star.uno.Exception("Error: invalid tag '"+
224cdf0e10cSrcweir                                     oTagName+"' received from basic test!");
225cdf0e10cSrcweir                 }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir             // Action for constructors:
228cdf0e10cSrcweir             // Tag(TagName, String[][] attrValues )
229cdf0e10cSrcweir             // Tag(TagName, String[] attrNames)
230cdf0e10cSrcweir             // Tag(TagName, String attrName)
231cdf0e10cSrcweir             //
232cdf0e10cSrcweir             } else if (oTag.length == 2) {
233cdf0e10cSrcweir                 if (oTag[1] instanceof String[][]) {
234cdf0e10cSrcweir                     allTags.add(new Tag(oTagName, (String[][]) oTag[1]));
235cdf0e10cSrcweir                 } else if (oTag[1] instanceof String[]) {
236cdf0e10cSrcweir                     allTags.add(new Tag(oTagName, (String[]) oTag[1]));
237cdf0e10cSrcweir                 } else if (oTag[1] instanceof String) {
238cdf0e10cSrcweir                     allTags.add(new Tag(oTagName, (String) oTag[1]));
239cdf0e10cSrcweir                 } else {
240cdf0e10cSrcweir                     throw new com.sun.star.uno.Exception("Error: invalid tag '"+
241cdf0e10cSrcweir                     oTagName+"' received from basic test!");
242cdf0e10cSrcweir                 }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir             // Action for constructor Tag(TagName)
245cdf0e10cSrcweir             } else if (oTag.length == 1) {
246cdf0e10cSrcweir                 if (oTag[0] instanceof String) {
247cdf0e10cSrcweir                     allTags.add(new Tag(oTagName));
248cdf0e10cSrcweir                 } else {
249cdf0e10cSrcweir                     throw new com.sun.star.uno.Exception("Error: invalid tag '"+
250cdf0e10cSrcweir                     oTagName+"' received from basic test!");
251cdf0e10cSrcweir                 }
252cdf0e10cSrcweir             } else {
253cdf0e10cSrcweir                 throw new com.sun.star.uno.Exception("Error: invalid tag '"+
254cdf0e10cSrcweir                 oTagName+"' received from basic test!");
255cdf0e10cSrcweir             }
256cdf0e10cSrcweir         }
257cdf0e10cSrcweir 
258cdf0e10cSrcweir         // Adding tags to XMLChecker
259cdf0e10cSrcweir         if ( action.equals((String)"TagExists") ) {
260cdf0e10cSrcweir             for (int i=0; i<allTags.size(); i++) {
261cdf0e10cSrcweir                 addTag((Tag)allTags.get(i));
262cdf0e10cSrcweir             }
263cdf0e10cSrcweir         } else if (action.equals((String)"TagEnclosed")) {
264cdf0e10cSrcweir             addTagEnclosed((Tag) allTags.get(0), (Tag) allTags.get(1));
265cdf0e10cSrcweir         } else if (action.equals((String)"CharsEnclosed")) {
266cdf0e10cSrcweir             addCharactersEnclosed(CDATA, (Tag) allTags.get(0));
267cdf0e10cSrcweir         } else {
268cdf0e10cSrcweir             throw new com.sun.star.uno.Exception("Error: incorrect rule name '"+
269cdf0e10cSrcweir             action+"' received from basic test!");
270cdf0e10cSrcweir         }
271cdf0e10cSrcweir     }
272cdf0e10cSrcweir 
273cdf0e10cSrcweir     /**
274cdf0e10cSrcweir      * Get the names of the elements.
275cdf0e10cSrcweir      * @return element names.
276cdf0e10cSrcweir      */
getElementNames()277cdf0e10cSrcweir     public String[] getElementNames() {
278cdf0e10cSrcweir         return new String[]{"XMLCode", "XMLIsCorrect"};
279cdf0e10cSrcweir     }
280cdf0e10cSrcweir 
281cdf0e10cSrcweir     /**
282cdf0e10cSrcweir      * Is this an element?
283cdf0e10cSrcweir      * @param name Element name.
284cdf0e10cSrcweir      * @return true, if <code>name>/code> is the name of an element.
285cdf0e10cSrcweir      */
hasByName(String name)286cdf0e10cSrcweir     public boolean hasByName(String name) {
287cdf0e10cSrcweir         return (name.equals("XMLCode") || name.equals("XMLIsCorrect"));
288cdf0e10cSrcweir     }
289cdf0e10cSrcweir 
290cdf0e10cSrcweir     /**
291cdf0e10cSrcweir      * Get an element by its name.
292cdf0e10cSrcweir      * @param name The element name.
293cdf0e10cSrcweir      * @return The element with the specified <code>name</code>.
294cdf0e10cSrcweir      * @throws NoSuchElementException Is thrown, if name does not exist.
295cdf0e10cSrcweir      */
getByName(String name)296cdf0e10cSrcweir     public Object getByName(String name) throws NoSuchElementException{
297cdf0e10cSrcweir         if (name.equals("XMLIsCorrect"))
298cdf0e10cSrcweir             return new Boolean(this.check());
299cdf0e10cSrcweir         else if (name.equals("XMLCode")) {
300cdf0e10cSrcweir             return writer.getBuffer().toString();
301cdf0e10cSrcweir         } else
302cdf0e10cSrcweir             throw new NoSuchElementException();
303cdf0e10cSrcweir     }
304cdf0e10cSrcweir 
305cdf0e10cSrcweir     /**
306cdf0e10cSrcweir      * Are there any elements?
307cdf0e10cSrcweir      * @return Always true.
308cdf0e10cSrcweir      */
hasElements()309cdf0e10cSrcweir     public boolean hasElements() {
310cdf0e10cSrcweir         return true;
311cdf0e10cSrcweir     }
312cdf0e10cSrcweir 
313cdf0e10cSrcweir     /**
314cdf0e10cSrcweir      * Get the element type.
315cdf0e10cSrcweir      * @return The type.
316cdf0e10cSrcweir      */
getElementType()317cdf0e10cSrcweir     public Type getElementType() {
318cdf0e10cSrcweir         return new Type(Object.class);
319cdf0e10cSrcweir     }
320cdf0e10cSrcweir 
321cdf0e10cSrcweir     /**
322cdf0e10cSrcweir      * Get a unique id for this implementation.
323cdf0e10cSrcweir      * @return The id.
324cdf0e10cSrcweir      */
getImplementationId()325cdf0e10cSrcweir     public byte[] getImplementationId() {
326cdf0e10cSrcweir         return toString().getBytes();
327cdf0e10cSrcweir     }
328cdf0e10cSrcweir 
329cdf0e10cSrcweir     /**
330cdf0e10cSrcweir      * Return all implemented types of this class.
331cdf0e10cSrcweir      * @return The implemented UNO types.
332cdf0e10cSrcweir      */
getTypes()333cdf0e10cSrcweir     public Type[] getTypes() {
334cdf0e10cSrcweir         Class interfaces[] = getClass().getInterfaces();
335cdf0e10cSrcweir         Type types[] = new Type[interfaces.length];
336cdf0e10cSrcweir         for(int i = 0; i < interfaces.length; ++ i)
337cdf0e10cSrcweir             types[i] = new Type(interfaces[i]);
338cdf0e10cSrcweir         return types;
339cdf0e10cSrcweir     }
340cdf0e10cSrcweir }
341