xref: /AOO41X/main/qadevOOo/runner/util/XMLTools.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package util;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import java.io.PrintWriter ;
31*cdf0e10cSrcweir import java.util.Vector ;
32*cdf0e10cSrcweir import java.util.Hashtable ;
33*cdf0e10cSrcweir import java.util.Enumeration ;
34*cdf0e10cSrcweir import java.util.HashSet ;
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir // access the implementations via names
37*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
38*cdf0e10cSrcweir import com.sun.star.io.XOutputStream;
39*cdf0e10cSrcweir import com.sun.star.io.XInputStream;
40*cdf0e10cSrcweir import com.sun.star.io.XActiveDataSource;
41*cdf0e10cSrcweir import com.sun.star.ucb.XSimpleFileAccess;
42*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
43*cdf0e10cSrcweir import com.sun.star.xml.sax.XDocumentHandler;
44*cdf0e10cSrcweir import com.sun.star.uno.Any;
45*cdf0e10cSrcweir import com.sun.star.uno.Type;
46*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
47*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
48*cdf0e10cSrcweir import com.sun.star.xml.sax.XLocator;
49*cdf0e10cSrcweir import com.sun.star.xml.sax.XAttributeList;
50*cdf0e10cSrcweir import com.sun.star.xml.sax.XParser ;
51*cdf0e10cSrcweir import com.sun.star.xml.sax.InputSource ;
52*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
53*cdf0e10cSrcweir import com.sun.star.document.XExporter;
54*cdf0e10cSrcweir import com.sun.star.document.XImporter;
55*cdf0e10cSrcweir import com.sun.star.document.XFilter;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir public class XMLTools {
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir     /**
61*cdf0e10cSrcweir      * The implementation of <code>com.sun.star.xml.sax.XAttributeList</code>
62*cdf0e10cSrcweir      * where attributes and their values can be added.
63*cdf0e10cSrcweir      */
64*cdf0e10cSrcweir     public static class AttributeList implements XAttributeList {
65*cdf0e10cSrcweir         private static class Attribute {
66*cdf0e10cSrcweir             public String Name ;
67*cdf0e10cSrcweir             public String Type ;
68*cdf0e10cSrcweir             public String Value ;
69*cdf0e10cSrcweir         }
70*cdf0e10cSrcweir         private Hashtable attrByName = new Hashtable() ;
71*cdf0e10cSrcweir         private Vector attributes = new Vector() ;
72*cdf0e10cSrcweir         private PrintWriter log = null ;
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir         /**
75*cdf0e10cSrcweir          * Creates a class instance.
76*cdf0e10cSrcweir          */
77*cdf0e10cSrcweir         public AttributeList() {}
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir         /**
80*cdf0e10cSrcweir          * Constructs a list which will report to <code>log</code>
81*cdf0e10cSrcweir          * specified about each <code>XDocumentHandler</code> method
82*cdf0e10cSrcweir          * call.
83*cdf0e10cSrcweir          */
84*cdf0e10cSrcweir         public AttributeList(PrintWriter log) {
85*cdf0e10cSrcweir             this.log = log ;
86*cdf0e10cSrcweir         }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir         public AttributeList(XAttributeList list) {
89*cdf0e10cSrcweir             if (list == null) return ;
90*cdf0e10cSrcweir             for (short i = 0; i < list.getLength(); i++) {
91*cdf0e10cSrcweir                 add(list.getNameByIndex(i), list.getTypeByIndex(i),
92*cdf0e10cSrcweir                     list.getValueByIndex(i)) ;
93*cdf0e10cSrcweir             }
94*cdf0e10cSrcweir         }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir         /**
97*cdf0e10cSrcweir          * Adds an attribute with type and value specified.
98*cdf0e10cSrcweir          * @param name The attribute name.
99*cdf0e10cSrcweir          * @param type Value type (usually 'CDATA' used).
100*cdf0e10cSrcweir          * @param value Attribute value.
101*cdf0e10cSrcweir          */
102*cdf0e10cSrcweir         public void add(String name, String type, String value) {
103*cdf0e10cSrcweir             Attribute attr = new Attribute() ;
104*cdf0e10cSrcweir             attr.Name = name ;
105*cdf0e10cSrcweir             attr.Type = type ;
106*cdf0e10cSrcweir             attr.Value = value ;
107*cdf0e10cSrcweir             attributes.add(attr) ;
108*cdf0e10cSrcweir             attrByName.put(attr.Name, attr) ;
109*cdf0e10cSrcweir         }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir         /**
112*cdf0e10cSrcweir          * Adds an attribute with value specified. As a type of
113*cdf0e10cSrcweir          * value 'CDATA' string specified.
114*cdf0e10cSrcweir          * @param name The attribute name.
115*cdf0e10cSrcweir          * @param value Attribute value.
116*cdf0e10cSrcweir          */
117*cdf0e10cSrcweir         public void add(String name, String value) {
118*cdf0e10cSrcweir             add(name, "CDATA", value) ;
119*cdf0e10cSrcweir         }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir         /**
122*cdf0e10cSrcweir          * Clears all attributes added before.
123*cdf0e10cSrcweir          */
124*cdf0e10cSrcweir         public void clear() {
125*cdf0e10cSrcweir             attrByName.clear() ;
126*cdf0e10cSrcweir             attributes.clear() ;
127*cdf0e10cSrcweir         }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir         /***************************************
130*cdf0e10cSrcweir         * XAttributeList methods
131*cdf0e10cSrcweir         ****************************************/
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir         public short getLength() {
134*cdf0e10cSrcweir             if (log != null)
135*cdf0e10cSrcweir                 log.println("getLength() called -> " + attributes.size()) ;
136*cdf0e10cSrcweir             return (short) attributes.size() ;
137*cdf0e10cSrcweir         }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir         public String getNameByIndex(short idx) {
140*cdf0e10cSrcweir             String name = ((Attribute) attributes.get(idx)).Name ;
141*cdf0e10cSrcweir             if (log != null)
142*cdf0e10cSrcweir                 log.println("getNameByIndex(" + idx + ") called -> '" +
143*cdf0e10cSrcweir                 name + "'") ;
144*cdf0e10cSrcweir             return name ;
145*cdf0e10cSrcweir         }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir         public String getTypeByIndex(short idx) {
148*cdf0e10cSrcweir             String type = ((Attribute) attributes.get(idx)).Type  ;
149*cdf0e10cSrcweir             if (log != null)
150*cdf0e10cSrcweir                 log.println("getTypeByIndex(" + idx + ") called -> '" +
151*cdf0e10cSrcweir                     type + "'") ;
152*cdf0e10cSrcweir             return type;
153*cdf0e10cSrcweir         }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir         public String getTypeByName(String name) {
156*cdf0e10cSrcweir             String type = ((Attribute) attrByName.get(name)).Type ;
157*cdf0e10cSrcweir             if (log != null)
158*cdf0e10cSrcweir                 log.println("getTypeByName('" + name + "') called -> '" +
159*cdf0e10cSrcweir                     type + "'") ;
160*cdf0e10cSrcweir             return type;
161*cdf0e10cSrcweir         }
162*cdf0e10cSrcweir         public String getValueByIndex(short idx) {
163*cdf0e10cSrcweir             String value = ((Attribute) attributes.get(idx)).Value ;
164*cdf0e10cSrcweir             if (log != null)
165*cdf0e10cSrcweir                 log.println("getValueByIndex(" + idx + ") called -> '" +
166*cdf0e10cSrcweir                     value + "'") ;
167*cdf0e10cSrcweir             return  value;
168*cdf0e10cSrcweir         }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir         public String getValueByName(String name) {
171*cdf0e10cSrcweir             String value = ((Attribute) attrByName.get(name)).Value ;
172*cdf0e10cSrcweir             if (log != null)
173*cdf0e10cSrcweir                 log.println("getValueByName('" + name + "') called -> '" +
174*cdf0e10cSrcweir                     value + "'") ;
175*cdf0e10cSrcweir             return value;
176*cdf0e10cSrcweir         }
177*cdf0e10cSrcweir     }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir     /**
180*cdf0e10cSrcweir     * This class writes all XML data handled into a stream specified
181*cdf0e10cSrcweir     * in the constructor.
182*cdf0e10cSrcweir     */
183*cdf0e10cSrcweir     public static class XMLWriter implements XDocumentHandler {
184*cdf0e10cSrcweir         private PrintWriter _log = null ;
185*cdf0e10cSrcweir         private String align = "" ;
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir         /**
188*cdf0e10cSrcweir         * Creates a SAX handler which writes all XML data
189*cdf0e10cSrcweir         * handled into a <code>log</code> stream specified.
190*cdf0e10cSrcweir         */
191*cdf0e10cSrcweir         public XMLWriter(PrintWriter log) {
192*cdf0e10cSrcweir             _log = log ;
193*cdf0e10cSrcweir         }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir         /**
196*cdf0e10cSrcweir         * Creates a SAX handler which does nothing.
197*cdf0e10cSrcweir         */
198*cdf0e10cSrcweir         public XMLWriter() {
199*cdf0e10cSrcweir         }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir         public void processingInstruction(String appl, String data) {
202*cdf0e10cSrcweir             if (_log == null) return ;
203*cdf0e10cSrcweir             _log.println(align + "<?" + appl + " " + data + "?>") ;
204*cdf0e10cSrcweir         }
205*cdf0e10cSrcweir         public void startDocument() {
206*cdf0e10cSrcweir             if (_log == null) return ;
207*cdf0e10cSrcweir             _log.println("START DOCUMENT:") ;
208*cdf0e10cSrcweir         }
209*cdf0e10cSrcweir         public void endDocument() {
210*cdf0e10cSrcweir             if (_log == null) return ;
211*cdf0e10cSrcweir             _log.println("END DOCUMENT:") ;
212*cdf0e10cSrcweir         }
213*cdf0e10cSrcweir         public void setDocumentLocator(XLocator loc) {
214*cdf0e10cSrcweir             if (_log == null) return ;
215*cdf0e10cSrcweir             _log.println("DOCUMENT LOCATOR: ('" + loc.getPublicId() +
216*cdf0e10cSrcweir                 "','" + loc.getSystemId() + "')") ;
217*cdf0e10cSrcweir         }
218*cdf0e10cSrcweir         public void startElement(String name, XAttributeList attr) {
219*cdf0e10cSrcweir             if (_log == null) return ;
220*cdf0e10cSrcweir             _log.print(align + "<" + name + " ") ;
221*cdf0e10cSrcweir             if (attr != null) {
222*cdf0e10cSrcweir                 short attrLen = attr.getLength() ;
223*cdf0e10cSrcweir                 for (short i = 0; i < attrLen; i++) {
224*cdf0e10cSrcweir                     if (i != 0) _log.print(align + "       ") ;
225*cdf0e10cSrcweir                     _log.print(attr.getNameByIndex(i) + "[" +
226*cdf0e10cSrcweir                         attr.getTypeByIndex(i) + "]=\"" +
227*cdf0e10cSrcweir                         attr.getValueByIndex(i) + "\"") ;
228*cdf0e10cSrcweir                     if (i+1 != attrLen) {
229*cdf0e10cSrcweir                         _log.println() ;
230*cdf0e10cSrcweir                     }
231*cdf0e10cSrcweir                 }
232*cdf0e10cSrcweir             }
233*cdf0e10cSrcweir             _log.println(">") ;
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir             align += "   " ;
236*cdf0e10cSrcweir         }
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir         public void endElement(String name) {
239*cdf0e10cSrcweir             if (_log == null) return ;
240*cdf0e10cSrcweir             align = align.substring(3) ;
241*cdf0e10cSrcweir             _log.println(align + "</" + name + ">") ;
242*cdf0e10cSrcweir         }
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir         public void characters(String chars) {
245*cdf0e10cSrcweir             if (_log == null) return ;
246*cdf0e10cSrcweir             _log.println(align + chars) ;
247*cdf0e10cSrcweir         }
248*cdf0e10cSrcweir         public void ignorableWhitespace(String sp) {
249*cdf0e10cSrcweir             if (_log == null) return ;
250*cdf0e10cSrcweir             _log.println(sp) ;
251*cdf0e10cSrcweir         }
252*cdf0e10cSrcweir     }
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir     /**
255*cdf0e10cSrcweir     * Checks if the XML structure is well formed (i.e. all tags opened must be
256*cdf0e10cSrcweir     * closed and all tags opened inside a tag must be closed
257*cdf0e10cSrcweir     * inside the same tag). It also checks parameters passed.
258*cdf0e10cSrcweir     * If any collisions found appropriate error message is
259*cdf0e10cSrcweir     * output into a stream specified. No XML data output, i.e.
260*cdf0e10cSrcweir     * no output will be performed if no errors occur.<p>
261*cdf0e10cSrcweir     * After document is completed there is a way to cehck
262*cdf0e10cSrcweir     * if the XML data and structure was valid.
263*cdf0e10cSrcweir     */
264*cdf0e10cSrcweir     public static class XMLWellFormChecker extends XMLWriter {
265*cdf0e10cSrcweir         protected boolean docStarted = false ;
266*cdf0e10cSrcweir         protected boolean docEnded = false ;
267*cdf0e10cSrcweir         protected Vector tagStack = new Vector() ;
268*cdf0e10cSrcweir         protected boolean wellFormed = true ;
269*cdf0e10cSrcweir         protected boolean noOtherErrors = true ;
270*cdf0e10cSrcweir         protected PrintWriter log = null ;
271*cdf0e10cSrcweir         protected boolean printXMLData = false ;
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir         public XMLWellFormChecker(PrintWriter log) {
274*cdf0e10cSrcweir             super() ;
275*cdf0e10cSrcweir             this.log = log ;
276*cdf0e10cSrcweir         }
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir         public XMLWellFormChecker(PrintWriter log_, boolean printXMLData) {
279*cdf0e10cSrcweir             super(printXMLData ? log_ : null) ;
280*cdf0e10cSrcweir             this.printXMLData = printXMLData ;
281*cdf0e10cSrcweir             this.log = log_ ;
282*cdf0e10cSrcweir         }
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir         /**
285*cdf0e10cSrcweir          * Reset all values. This is important e.g. for test of XFilter
286*cdf0e10cSrcweir          * interface, where 'filter()' method istbstarted twice.
287*cdf0e10cSrcweir          */
288*cdf0e10cSrcweir         public void reset() {
289*cdf0e10cSrcweir             docStarted = false ;
290*cdf0e10cSrcweir             docEnded = false ;
291*cdf0e10cSrcweir             tagStack = new Vector() ;
292*cdf0e10cSrcweir             wellFormed = true ;
293*cdf0e10cSrcweir             noOtherErrors = true ;
294*cdf0e10cSrcweir             PrintWriter log = null ;
295*cdf0e10cSrcweir             printXMLData = false ;
296*cdf0e10cSrcweir         }
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir         public void startDocument() {
299*cdf0e10cSrcweir             super.startDocument();
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir             if (docStarted) {
302*cdf0e10cSrcweir                 printError("Document is started twice.") ;
303*cdf0e10cSrcweir                 wellFormed = false ;
304*cdf0e10cSrcweir             }
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir             docStarted = true ;
307*cdf0e10cSrcweir         }
308*cdf0e10cSrcweir         public void endDocument() {
309*cdf0e10cSrcweir             super.endDocument();
310*cdf0e10cSrcweir             if (!docStarted) {
311*cdf0e10cSrcweir                 wellFormed = false ;
312*cdf0e10cSrcweir                 printError("Document ended but not started.") ;
313*cdf0e10cSrcweir             }
314*cdf0e10cSrcweir             docEnded = true ;
315*cdf0e10cSrcweir         }
316*cdf0e10cSrcweir         public void startElement(String name, XAttributeList attr) {
317*cdf0e10cSrcweir             super.startElement(name, attr);
318*cdf0e10cSrcweir             if (attr == null) {
319*cdf0e10cSrcweir                 printError("attribute list passed as parameter to startElement()"+
320*cdf0e10cSrcweir                     " method has null value for tag <" + name + ">") ;
321*cdf0e10cSrcweir                 noOtherErrors = false ;
322*cdf0e10cSrcweir             }
323*cdf0e10cSrcweir             tagStack.add(0, name) ;
324*cdf0e10cSrcweir         }
325*cdf0e10cSrcweir         public void endElement(String name) {
326*cdf0e10cSrcweir             super.endElement(name);
327*cdf0e10cSrcweir             if (wellFormed) {
328*cdf0e10cSrcweir                 if (tagStack.size() == 0) {
329*cdf0e10cSrcweir                     wellFormed = false ;
330*cdf0e10cSrcweir                     printError("No tags to close (bad closing tag </" + name + ">)") ;
331*cdf0e10cSrcweir                 } else {
332*cdf0e10cSrcweir                     String startTag = (String) tagStack.elementAt(0) ;
333*cdf0e10cSrcweir                     tagStack.remove(0) ;
334*cdf0e10cSrcweir                     if (!startTag.equals(name)) {
335*cdf0e10cSrcweir                         wellFormed = false ;
336*cdf0e10cSrcweir                         printError("Bad closing tag: </" + name +
337*cdf0e10cSrcweir                             ">; tag expected: </" + startTag + ">");
338*cdf0e10cSrcweir                     }
339*cdf0e10cSrcweir                 }
340*cdf0e10cSrcweir             }
341*cdf0e10cSrcweir         }
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir         /**
344*cdf0e10cSrcweir         * Checks if there were no errors during document handling.
345*cdf0e10cSrcweir         * I.e. startDocument() and endDocument() must be called,
346*cdf0e10cSrcweir         * XML must be well formed, paramters must be valid.
347*cdf0e10cSrcweir         */
348*cdf0e10cSrcweir         public boolean isWellFormed() {
349*cdf0e10cSrcweir             if (!docEnded) {
350*cdf0e10cSrcweir                 printError("Document was not ended.") ;
351*cdf0e10cSrcweir                 wellFormed = false ;
352*cdf0e10cSrcweir             }
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir             return wellFormed && noOtherErrors ;
355*cdf0e10cSrcweir         }
356*cdf0e10cSrcweir 
357*cdf0e10cSrcweir         /**
358*cdf0e10cSrcweir         * Prints error message and all tags where error occured inside.
359*cdf0e10cSrcweir         * Also prints "Tag trace" in case if the full XML data isn't
360*cdf0e10cSrcweir         * printed.
361*cdf0e10cSrcweir         */
362*cdf0e10cSrcweir         public void printError(String msg) {
363*cdf0e10cSrcweir             log.println("!!! Error: " + msg) ;
364*cdf0e10cSrcweir             if (printXMLData) return ;
365*cdf0e10cSrcweir             log.println("   Tag trace :") ;
366*cdf0e10cSrcweir             for (int i = 0; i < tagStack.size(); i++) {
367*cdf0e10cSrcweir                 String tag = (String) tagStack.elementAt(i) ;
368*cdf0e10cSrcweir                 log.println("      <" + tag + ">") ;
369*cdf0e10cSrcweir             }
370*cdf0e10cSrcweir         }
371*cdf0e10cSrcweir     }
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir     /**
374*cdf0e10cSrcweir     * Beside structure of XML this class also can check existence
375*cdf0e10cSrcweir     * of tags, inner tags, and character data. After document
376*cdf0e10cSrcweir     * completion there is a way to check if required tags and
377*cdf0e10cSrcweir     * character data was found. If there any error occurs an
378*cdf0e10cSrcweir     * appropriate message is output.
379*cdf0e10cSrcweir     */
380*cdf0e10cSrcweir     public static class XMLTagsChecker extends XMLWellFormChecker {
381*cdf0e10cSrcweir         protected Hashtable tags = new Hashtable() ;
382*cdf0e10cSrcweir         protected Hashtable chars = new Hashtable() ;
383*cdf0e10cSrcweir         protected boolean allOK = true ;
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir         public XMLTagsChecker(PrintWriter log) {
386*cdf0e10cSrcweir             super(log) ;
387*cdf0e10cSrcweir         }
388*cdf0e10cSrcweir 
389*cdf0e10cSrcweir         /**
390*cdf0e10cSrcweir         * Adds a tag name which must be contained in the XML data.
391*cdf0e10cSrcweir         */
392*cdf0e10cSrcweir         public void addTag(String tag) {
393*cdf0e10cSrcweir             tags.put(tag, "") ;
394*cdf0e10cSrcweir         }
395*cdf0e10cSrcweir         /**
396*cdf0e10cSrcweir         * Adds a tag name which must be contained in the XML data and
397*cdf0e10cSrcweir         * must be inside the tag with name <code>outerTag</code>.
398*cdf0e10cSrcweir         */
399*cdf0e10cSrcweir         public void addTagEnclosed(String tag, String outerTag) {
400*cdf0e10cSrcweir             tags.put(tag, outerTag) ;
401*cdf0e10cSrcweir         }
402*cdf0e10cSrcweir         /**
403*cdf0e10cSrcweir         * Adds a character data which must be contained in the XML data.
404*cdf0e10cSrcweir         */
405*cdf0e10cSrcweir         public void addCharacters(String ch) {
406*cdf0e10cSrcweir             chars.put(ch, "") ;
407*cdf0e10cSrcweir         }
408*cdf0e10cSrcweir         /**
409*cdf0e10cSrcweir         * Adds a character data which must be contained in the XML data and
410*cdf0e10cSrcweir         * must be inside the tag with name <code>outerTag</code>.
411*cdf0e10cSrcweir         */
412*cdf0e10cSrcweir         public void addCharactersEnclosed(String ch, String outerTag) {
413*cdf0e10cSrcweir             chars.put(ch, outerTag) ;
414*cdf0e10cSrcweir         }
415*cdf0e10cSrcweir 
416*cdf0e10cSrcweir         public void startElement(String name, XAttributeList attrs) {
417*cdf0e10cSrcweir             super.startElement(name, attrs) ;
418*cdf0e10cSrcweir             if (tags.containsKey(name)) {
419*cdf0e10cSrcweir                 String outerTag = (String) tags.get(name);
420*cdf0e10cSrcweir                 if (!outerTag.equals("")) {
421*cdf0e10cSrcweir                     boolean isInTag = false ;
422*cdf0e10cSrcweir                     for (int i = 0; i < tagStack.size(); i++) {
423*cdf0e10cSrcweir                         if (outerTag.equals((String) tagStack.elementAt(i))) {
424*cdf0e10cSrcweir                             isInTag = true ;
425*cdf0e10cSrcweir                             break ;
426*cdf0e10cSrcweir                         }
427*cdf0e10cSrcweir                     }
428*cdf0e10cSrcweir                     if (!isInTag) {
429*cdf0e10cSrcweir                         printError("Required tag <" + name + "> found, but is not enclosed in tag <" +
430*cdf0e10cSrcweir                             outerTag + ">") ;
431*cdf0e10cSrcweir                         allOK = false ;
432*cdf0e10cSrcweir                     }
433*cdf0e10cSrcweir                 }
434*cdf0e10cSrcweir                 tags.remove(name) ;
435*cdf0e10cSrcweir             }
436*cdf0e10cSrcweir         }
437*cdf0e10cSrcweir 
438*cdf0e10cSrcweir         public void characters(String ch) {
439*cdf0e10cSrcweir             super.characters(ch) ;
440*cdf0e10cSrcweir 
441*cdf0e10cSrcweir             if (chars.containsKey(ch)) {
442*cdf0e10cSrcweir                 String outerTag = (String) chars.get(ch);
443*cdf0e10cSrcweir                 if (!outerTag.equals("")) {
444*cdf0e10cSrcweir                     boolean isInTag = false ;
445*cdf0e10cSrcweir                     for (int i = 0; i < tagStack.size(); i++) {
446*cdf0e10cSrcweir                         if (outerTag.equals((String) tagStack.elementAt(i))) {
447*cdf0e10cSrcweir                             isInTag = true ;
448*cdf0e10cSrcweir                             break ;
449*cdf0e10cSrcweir                         }
450*cdf0e10cSrcweir                     }
451*cdf0e10cSrcweir                     if (!isInTag) {
452*cdf0e10cSrcweir                         printError("Required characters '" + ch + "' found, but are not enclosed in tag <" +
453*cdf0e10cSrcweir                             outerTag + ">") ;
454*cdf0e10cSrcweir                         allOK = false ;
455*cdf0e10cSrcweir                     }
456*cdf0e10cSrcweir                 }
457*cdf0e10cSrcweir                 chars.remove(ch) ;
458*cdf0e10cSrcweir             }
459*cdf0e10cSrcweir         }
460*cdf0e10cSrcweir 
461*cdf0e10cSrcweir         /**
462*cdf0e10cSrcweir         * Checks if the XML data was valid and well formed and if
463*cdf0e10cSrcweir         * all necessary tags and character data was found.
464*cdf0e10cSrcweir         */
465*cdf0e10cSrcweir         public boolean checkTags() {
466*cdf0e10cSrcweir             allOK &= isWellFormed() ;
467*cdf0e10cSrcweir 
468*cdf0e10cSrcweir             Enumeration badTags = tags.keys() ;
469*cdf0e10cSrcweir             Enumeration badChars = chars.keys() ;
470*cdf0e10cSrcweir 
471*cdf0e10cSrcweir             if (badTags.hasMoreElements()) {
472*cdf0e10cSrcweir                 allOK = false ;
473*cdf0e10cSrcweir                 log.println("Required tags were not found in export :") ;
474*cdf0e10cSrcweir                 while(badTags.hasMoreElements()) {
475*cdf0e10cSrcweir                     log.println("   <" + ((String) badTags.nextElement()) + ">") ;
476*cdf0e10cSrcweir                 }
477*cdf0e10cSrcweir             }
478*cdf0e10cSrcweir             if (badChars.hasMoreElements()) {
479*cdf0e10cSrcweir                 allOK = false ;
480*cdf0e10cSrcweir                 log.println("Required characters were not found in export :") ;
481*cdf0e10cSrcweir                 while(badChars.hasMoreElements()) {
482*cdf0e10cSrcweir                     log.println("   <" + ((String) badChars.nextElement()) + ">") ;
483*cdf0e10cSrcweir                 }
484*cdf0e10cSrcweir             }
485*cdf0e10cSrcweir             reset();
486*cdf0e10cSrcweir             return allOK ;
487*cdf0e10cSrcweir         }
488*cdf0e10cSrcweir     }
489*cdf0e10cSrcweir 
490*cdf0e10cSrcweir     /**
491*cdf0e10cSrcweir      * Represents an XML tag which must be found in XML data written.
492*cdf0e10cSrcweir      * This tag can contain only its name or tag name and attribute
493*cdf0e10cSrcweir      * name, or attribute value additionally.
494*cdf0e10cSrcweir      */
495*cdf0e10cSrcweir     public static class Tag {
496*cdf0e10cSrcweir         private String name = null;
497*cdf0e10cSrcweir         private String[][] attrList = new String[0][3] ;
498*cdf0e10cSrcweir 
499*cdf0e10cSrcweir         /**
500*cdf0e10cSrcweir          * Creates tag which has only a name. Attributes don't make sense.
501*cdf0e10cSrcweir          * @param tagName The name of the tag.
502*cdf0e10cSrcweir          */
503*cdf0e10cSrcweir         public Tag(String tagName) {
504*cdf0e10cSrcweir             name = tagName ;
505*cdf0e10cSrcweir         }
506*cdf0e10cSrcweir 
507*cdf0e10cSrcweir         /**
508*cdf0e10cSrcweir          * Creates a tag with the name specified, which must have an
509*cdf0e10cSrcweir          * attribute with name specified. The value of this attribute
510*cdf0e10cSrcweir          * doesn't make sense.
511*cdf0e10cSrcweir          * @param tagName The name of the tag.
512*cdf0e10cSrcweir          * @param attrName The name of attribute which must be contained
513*cdf0e10cSrcweir          * in the tag.
514*cdf0e10cSrcweir          */
515*cdf0e10cSrcweir         public Tag(String tagName, String attrName) {
516*cdf0e10cSrcweir             name = tagName ;
517*cdf0e10cSrcweir             attrList = new String[1][3] ;
518*cdf0e10cSrcweir             attrList[0][0] = attrName ;
519*cdf0e10cSrcweir         }
520*cdf0e10cSrcweir 
521*cdf0e10cSrcweir         /**
522*cdf0e10cSrcweir          * Creates a tag with the name specified, which must have an
523*cdf0e10cSrcweir          * attribute with the value specified. The type of value
524*cdf0e10cSrcweir          * assumed to be 'CDATA'.
525*cdf0e10cSrcweir          * @param tagName The name of the tag.
526*cdf0e10cSrcweir          * @param attrName The name of attribute which must be contained
527*cdf0e10cSrcweir          * in the tag.
528*cdf0e10cSrcweir          * @param attrValue Attribute value.
529*cdf0e10cSrcweir          */
530*cdf0e10cSrcweir         public Tag(String tagName, String attrName, String attrValue) {
531*cdf0e10cSrcweir             name = tagName ;
532*cdf0e10cSrcweir             attrList = new String[1][3] ;
533*cdf0e10cSrcweir             attrList[0][0] = attrName ;
534*cdf0e10cSrcweir             attrList[0][1] = "CDATA" ;
535*cdf0e10cSrcweir             attrList[0][2] = attrValue ;
536*cdf0e10cSrcweir         }
537*cdf0e10cSrcweir 
538*cdf0e10cSrcweir         /**
539*cdf0e10cSrcweir          * Creates a tag with the name specified, which must have
540*cdf0e10cSrcweir          * attributes specified. The value of thesee attributes
541*cdf0e10cSrcweir          * doesn't make sense.
542*cdf0e10cSrcweir          * @param tagName The name of the tag.
543*cdf0e10cSrcweir          * @param attrNames Array with names of attributes which must
544*cdf0e10cSrcweir          * be contained in the tag.
545*cdf0e10cSrcweir          */
546*cdf0e10cSrcweir         public Tag(String tagName, String[] attrNames) {
547*cdf0e10cSrcweir             name = tagName ;
548*cdf0e10cSrcweir             attrList = new String[attrNames.length][3] ;
549*cdf0e10cSrcweir             for (int i = 0; i < attrNames.length; i++) {
550*cdf0e10cSrcweir                 attrList[i][0] = attrNames[i] ;
551*cdf0e10cSrcweir             }
552*cdf0e10cSrcweir         }
553*cdf0e10cSrcweir 
554*cdf0e10cSrcweir         /**
555*cdf0e10cSrcweir          * Creates a tag with the name specified, which must have an
556*cdf0e10cSrcweir          * attributes with their values specified. The type of all values
557*cdf0e10cSrcweir          * assumed to be 'CDATA'.
558*cdf0e10cSrcweir          * @param tagName The name of the tag.
559*cdf0e10cSrcweir          * @param attrValues An array with attribute names and their values.
560*cdf0e10cSrcweir          * <code>attrValues[N][0]</code> element contains the name of Nth
561*cdf0e10cSrcweir          * attribute, and <code>attrValues[N][1]</code> element contains
562*cdf0e10cSrcweir          * value of Nth attribute, if value is <code>null</code> then the
563*cdf0e10cSrcweir          * attribute value can be any.
564*cdf0e10cSrcweir          */
565*cdf0e10cSrcweir         public Tag(String tagName, String[][] attrValues) {
566*cdf0e10cSrcweir             name = tagName ;
567*cdf0e10cSrcweir             attrList = new String[attrValues.length][3] ;
568*cdf0e10cSrcweir             for (int i = 0; i < attrValues.length; i++) {
569*cdf0e10cSrcweir                 attrList[i][0] = attrValues[i][0] ;
570*cdf0e10cSrcweir                 attrList[i][1] = "CDATA" ;
571*cdf0e10cSrcweir                 attrList[i][2] = attrValues[i][1] ;
572*cdf0e10cSrcweir             }
573*cdf0e10cSrcweir         }
574*cdf0e10cSrcweir 
575*cdf0e10cSrcweir         /**
576*cdf0e10cSrcweir          * Gets tag String description.
577*cdf0e10cSrcweir          */
578*cdf0e10cSrcweir         public String toString() {
579*cdf0e10cSrcweir             String ret = "<" + name ;
580*cdf0e10cSrcweir             for (int i = 0; i < attrList.length; i++) {
581*cdf0e10cSrcweir                 ret += " " + attrList[i][0] + "=";
582*cdf0e10cSrcweir                 if (attrList[i][2] == null) {
583*cdf0e10cSrcweir                     ret += "(not specified)";
584*cdf0e10cSrcweir                 } else {
585*cdf0e10cSrcweir                     ret += "\"" + attrList[i][2] + "\"";
586*cdf0e10cSrcweir                 }
587*cdf0e10cSrcweir             }
588*cdf0e10cSrcweir             ret += ">";
589*cdf0e10cSrcweir 
590*cdf0e10cSrcweir             return ret ;
591*cdf0e10cSrcweir         }
592*cdf0e10cSrcweir 
593*cdf0e10cSrcweir         protected boolean checkAttr(int attrListIdx, XAttributeList list) {
594*cdf0e10cSrcweir             short j  = 0 ;
595*cdf0e10cSrcweir             int listLen = list.getLength();
596*cdf0e10cSrcweir             while(j < listLen) {
597*cdf0e10cSrcweir                 if (attrList[attrListIdx][0].equals(list.getNameByIndex(j))) {
598*cdf0e10cSrcweir                     if (attrList[attrListIdx][2] == null) return true ;
599*cdf0e10cSrcweir                     return attrList[attrListIdx][2].equals(list.getValueByIndex(j)) ;
600*cdf0e10cSrcweir                 }
601*cdf0e10cSrcweir                 j++ ;
602*cdf0e10cSrcweir             }
603*cdf0e10cSrcweir             return false ;
604*cdf0e10cSrcweir         }
605*cdf0e10cSrcweir 
606*cdf0e10cSrcweir         /**
607*cdf0e10cSrcweir          * Checks if this tag matches tag passed in parameters.
608*cdf0e10cSrcweir          * I.e. if tag specifies only it's name it mathes if names
609*cdf0e10cSrcweir          * are equal (attributes don't make sense). If there are
610*cdf0e10cSrcweir          * some attributes names specified in this tag method checks
611*cdf0e10cSrcweir          * if all names present in attribute list <code>list</code>
612*cdf0e10cSrcweir          * (attributes' values don't make sense). If attributes specified
613*cdf0e10cSrcweir          * with values method checks if these attributes exist and
614*cdf0e10cSrcweir          * have appropriate values.
615*cdf0e10cSrcweir          */
616*cdf0e10cSrcweir         public boolean isMatchTo(String tagName, XAttributeList list) {
617*cdf0e10cSrcweir             if (!name.equals(tagName)) return false;
618*cdf0e10cSrcweir             boolean result = true ;
619*cdf0e10cSrcweir             for (int i = 0; i < attrList.length; i++) {
620*cdf0e10cSrcweir                 result &= checkAttr(i, list) ;
621*cdf0e10cSrcweir             }
622*cdf0e10cSrcweir             return result ;
623*cdf0e10cSrcweir         }
624*cdf0e10cSrcweir     }
625*cdf0e10cSrcweir 
626*cdf0e10cSrcweir     /**
627*cdf0e10cSrcweir      * Class realises extended XML data checking. It has possibilities
628*cdf0e10cSrcweir      * to check if a tag exists, if it has some attributes with
629*cdf0e10cSrcweir      * values, and if this tag is contained in another tag (which
630*cdf0e10cSrcweir      * also can specify any attributes). It can check if some
631*cdf0e10cSrcweir      * character data exists inside any tag specified.
632*cdf0e10cSrcweir      */
633*cdf0e10cSrcweir     public static class XMLChecker extends XMLWellFormChecker {
634*cdf0e10cSrcweir         protected HashSet tagSet = new HashSet() ;
635*cdf0e10cSrcweir         protected Vector tags = new Vector() ;
636*cdf0e10cSrcweir         protected Vector chars = new Vector() ;
637*cdf0e10cSrcweir         protected Vector tagStack = new Vector() ;
638*cdf0e10cSrcweir         protected Vector attrStack = new Vector() ;
639*cdf0e10cSrcweir 
640*cdf0e10cSrcweir         public XMLChecker(PrintWriter log, boolean writeXML) {
641*cdf0e10cSrcweir             super(log, writeXML) ;
642*cdf0e10cSrcweir         }
643*cdf0e10cSrcweir 
644*cdf0e10cSrcweir         public void addTag(Tag tag) {
645*cdf0e10cSrcweir             tags.add(new Tag[] {tag, null}) ;
646*cdf0e10cSrcweir             tagSet.add(tag.name) ;
647*cdf0e10cSrcweir         }
648*cdf0e10cSrcweir 
649*cdf0e10cSrcweir         public void addTagEnclosed(Tag tag, Tag outerTag) {
650*cdf0e10cSrcweir             tags.add(new Tag[] {tag, outerTag}) ;
651*cdf0e10cSrcweir             tagSet.add(tag.name) ;
652*cdf0e10cSrcweir         }
653*cdf0e10cSrcweir 
654*cdf0e10cSrcweir         public void addCharacters(String ch) {
655*cdf0e10cSrcweir             chars.add(new Object[] {ch.trim(), null}) ;
656*cdf0e10cSrcweir         }
657*cdf0e10cSrcweir 
658*cdf0e10cSrcweir         public void addCharactersEnclosed(String ch, Tag outerTag) {
659*cdf0e10cSrcweir             chars.add(new Object[] {ch.trim(), outerTag}) ;
660*cdf0e10cSrcweir         }
661*cdf0e10cSrcweir 
662*cdf0e10cSrcweir         public void startElement(String name, XAttributeList attr) {
663*cdf0e10cSrcweir             try {
664*cdf0e10cSrcweir             super.startElement(name, attr);
665*cdf0e10cSrcweir 
666*cdf0e10cSrcweir             if (tagSet.contains(name)) {
667*cdf0e10cSrcweir                 for (int i = 0; i < tags.size(); i++) {
668*cdf0e10cSrcweir                     Tag[] tag = (Tag[]) tags.elementAt(i);
669*cdf0e10cSrcweir                     if (tag[0].isMatchTo(name, attr)) {
670*cdf0e10cSrcweir                         if (tag[1] == null) {
671*cdf0e10cSrcweir                             tags.remove(i--);
672*cdf0e10cSrcweir                         } else {
673*cdf0e10cSrcweir                             boolean isInStack = false ;
674*cdf0e10cSrcweir                             for (int j = 0; j < tagStack.size(); j++) {
675*cdf0e10cSrcweir                                 if (tag[1].isMatchTo((String) tagStack.elementAt(j),
676*cdf0e10cSrcweir                                     (XAttributeList) attrStack.elementAt(j))) {
677*cdf0e10cSrcweir 
678*cdf0e10cSrcweir                                     isInStack = true ;
679*cdf0e10cSrcweir                                     break ;
680*cdf0e10cSrcweir                                 }
681*cdf0e10cSrcweir                             }
682*cdf0e10cSrcweir                             if (isInStack) {
683*cdf0e10cSrcweir                                 tags.remove(i--) ;
684*cdf0e10cSrcweir                             }
685*cdf0e10cSrcweir                         }
686*cdf0e10cSrcweir                     }
687*cdf0e10cSrcweir                 }
688*cdf0e10cSrcweir             }
689*cdf0e10cSrcweir 
690*cdf0e10cSrcweir             tagStack.add(0, name) ;
691*cdf0e10cSrcweir             attrStack.add(0, new AttributeList(attr));
692*cdf0e10cSrcweir             } catch (Exception e) {
693*cdf0e10cSrcweir                 e.printStackTrace(log);
694*cdf0e10cSrcweir             }
695*cdf0e10cSrcweir         }
696*cdf0e10cSrcweir 
697*cdf0e10cSrcweir         public void characters(String ch) {
698*cdf0e10cSrcweir             super.characters(ch) ;
699*cdf0e10cSrcweir             for (int i = 0; i < chars.size(); i++) {
700*cdf0e10cSrcweir                 Object[] chr = (Object[]) chars.elementAt(i);
701*cdf0e10cSrcweir                 if (((String) chr[0]).equals(ch)) {
702*cdf0e10cSrcweir                     if (chr[1] == null) {
703*cdf0e10cSrcweir                         chars.remove(i--);
704*cdf0e10cSrcweir                     } else {
705*cdf0e10cSrcweir                         boolean isInStack = false ;
706*cdf0e10cSrcweir                         for (int j = 0; j < tagStack.size(); j++) {
707*cdf0e10cSrcweir                             if (((Tag) chr[1]).isMatchTo((String) tagStack.elementAt(j),
708*cdf0e10cSrcweir                                 (XAttributeList) attrStack.elementAt(j))) {
709*cdf0e10cSrcweir 
710*cdf0e10cSrcweir                                 isInStack = true ;
711*cdf0e10cSrcweir                                 break ;
712*cdf0e10cSrcweir                             }
713*cdf0e10cSrcweir                         }
714*cdf0e10cSrcweir                         if (isInStack) {
715*cdf0e10cSrcweir                             chars.remove(i--) ;
716*cdf0e10cSrcweir                         }
717*cdf0e10cSrcweir                     }
718*cdf0e10cSrcweir                 }
719*cdf0e10cSrcweir             }
720*cdf0e10cSrcweir         }
721*cdf0e10cSrcweir 
722*cdf0e10cSrcweir         public void endElement(String name) {
723*cdf0e10cSrcweir             try {
724*cdf0e10cSrcweir             super.endElement(name);
725*cdf0e10cSrcweir 
726*cdf0e10cSrcweir             if (tagStack.size() > 0) {
727*cdf0e10cSrcweir                 tagStack.remove(0) ;
728*cdf0e10cSrcweir                 attrStack.remove(0) ;
729*cdf0e10cSrcweir             }
730*cdf0e10cSrcweir             } catch(Exception e) {
731*cdf0e10cSrcweir                 e.printStackTrace(log) ;
732*cdf0e10cSrcweir             }
733*cdf0e10cSrcweir         }
734*cdf0e10cSrcweir 
735*cdf0e10cSrcweir         public boolean check() {
736*cdf0e10cSrcweir             if (tags.size()> 0) {
737*cdf0e10cSrcweir                 log.println("!!! Error: Some tags were not found :") ;
738*cdf0e10cSrcweir                 for (int i = 0; i < tags.size(); i++) {
739*cdf0e10cSrcweir                     Tag[] tag = (Tag[]) tags.elementAt(i) ;
740*cdf0e10cSrcweir                     log.println("   Tag " + tag[0] + " was not found");
741*cdf0e10cSrcweir                     if (tag[1] != null)
742*cdf0e10cSrcweir                         log.println("      inside tag " + tag[1]) ;
743*cdf0e10cSrcweir                 }
744*cdf0e10cSrcweir             }
745*cdf0e10cSrcweir             if (chars.size() > 0) {
746*cdf0e10cSrcweir                 log.println("!!! Error: Some character data blocks were not found :") ;
747*cdf0e10cSrcweir                 for (int i = 0; i < chars.size(); i++) {
748*cdf0e10cSrcweir                     Object[] ch = (Object[]) chars.elementAt(i) ;
749*cdf0e10cSrcweir                     log.println("   Character data \"" + ch[0] + "\" was not found ") ;
750*cdf0e10cSrcweir                     if (ch[1] != null)
751*cdf0e10cSrcweir                         log.println("      inside tag " + ch[1]) ;
752*cdf0e10cSrcweir                 }
753*cdf0e10cSrcweir             }
754*cdf0e10cSrcweir 
755*cdf0e10cSrcweir             if (!isWellFormed())
756*cdf0e10cSrcweir                 log.println("!!! Some errors were found in XML structure") ;
757*cdf0e10cSrcweir 
758*cdf0e10cSrcweir             boolean result = tags.size() == 0 && chars.size() == 0 && isWellFormed();
759*cdf0e10cSrcweir             reset();
760*cdf0e10cSrcweir             return result;
761*cdf0e10cSrcweir         }
762*cdf0e10cSrcweir     }
763*cdf0e10cSrcweir 
764*cdf0e10cSrcweir     /**
765*cdf0e10cSrcweir     * Creates <code>XDocumentHandler</code> implementation in form
766*cdf0e10cSrcweir     * of <code>com.sun.star.xml.sax.Writer</code> service, which
767*cdf0e10cSrcweir     * writes XML data into a <code>com.sun.star.io.Pipe</code>
768*cdf0e10cSrcweir     * created.
769*cdf0e10cSrcweir     * @return Single element array which contains the handler
770*cdf0e10cSrcweir     * contained in <code>Any</code> structure.
771*cdf0e10cSrcweir     */
772*cdf0e10cSrcweir     public static Object[] getDocumentHandler(XMultiServiceFactory xMSF) {
773*cdf0e10cSrcweir         Object[] ret = new Object[1];
774*cdf0e10cSrcweir         try {
775*cdf0e10cSrcweir             XInterface Writer = (XInterface) xMSF.createInstance(
776*cdf0e10cSrcweir                                     "com.sun.star.xml.sax.Writer");
777*cdf0e10cSrcweir             XInterface oPipe = (XInterface) xMSF.createInstance
778*cdf0e10cSrcweir                 ( "com.sun.star.io.Pipe" );
779*cdf0e10cSrcweir             XOutputStream xPipeOutput = (XOutputStream) UnoRuntime.
780*cdf0e10cSrcweir                 queryInterface(XOutputStream.class, oPipe) ;
781*cdf0e10cSrcweir 
782*cdf0e10cSrcweir             XActiveDataSource xADS = (XActiveDataSource)
783*cdf0e10cSrcweir                 UnoRuntime.queryInterface(XActiveDataSource.class,Writer);
784*cdf0e10cSrcweir             xADS.setOutputStream(xPipeOutput);
785*cdf0e10cSrcweir             XDocumentHandler handler = (XDocumentHandler)
786*cdf0e10cSrcweir                 UnoRuntime.queryInterface(XDocumentHandler.class,Writer);
787*cdf0e10cSrcweir 
788*cdf0e10cSrcweir             Any arg = new Any(new Type(XDocumentHandler.class),handler);
789*cdf0e10cSrcweir 
790*cdf0e10cSrcweir             ret[0] = arg;
791*cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
792*cdf0e10cSrcweir             e.printStackTrace();
793*cdf0e10cSrcweir         }
794*cdf0e10cSrcweir         return ret;
795*cdf0e10cSrcweir     }
796*cdf0e10cSrcweir 
797*cdf0e10cSrcweir     public static PropertyValue[] createMediaDescriptor(String[] propNames, Object[] values) {
798*cdf0e10cSrcweir         PropertyValue[] props = new PropertyValue[propNames.length] ;
799*cdf0e10cSrcweir 
800*cdf0e10cSrcweir         for (int i = 0; i < props.length; i++) {
801*cdf0e10cSrcweir             props[i] = new PropertyValue() ;
802*cdf0e10cSrcweir             props[i].Name = propNames[i] ;
803*cdf0e10cSrcweir             if (values != null && i < values.length) {
804*cdf0e10cSrcweir                 props[i].Value = values[i] ;
805*cdf0e10cSrcweir             }
806*cdf0e10cSrcweir         }
807*cdf0e10cSrcweir 
808*cdf0e10cSrcweir         return props ;
809*cdf0e10cSrcweir     }
810*cdf0e10cSrcweir 
811*cdf0e10cSrcweir     /**
812*cdf0e10cSrcweir      * Gets the hanlder, which writes all the XML data passed to the
813*cdf0e10cSrcweir      * file specified.
814*cdf0e10cSrcweir      * @param xMSF Soffice <code>ServiceManager</code> factory.
815*cdf0e10cSrcweir      * @param fileURL The file URL (in form file:///<path>) to which
816*cdf0e10cSrcweir      * XML data is written.
817*cdf0e10cSrcweir      * @return SAX handler to which XML data has to be written.
818*cdf0e10cSrcweir      */
819*cdf0e10cSrcweir     public static XDocumentHandler getFileXMLWriter(XMultiServiceFactory xMSF, String fileURL)
820*cdf0e10cSrcweir         throws com.sun.star.uno.Exception
821*cdf0e10cSrcweir     {
822*cdf0e10cSrcweir         XInterface oFacc = (XInterface)xMSF.createInstance(
823*cdf0e10cSrcweir             "com.sun.star.comp.ucb.SimpleFileAccess");
824*cdf0e10cSrcweir         XSimpleFileAccess xFacc = (XSimpleFileAccess)UnoRuntime.queryInterface
825*cdf0e10cSrcweir             (XSimpleFileAccess.class, oFacc) ;
826*cdf0e10cSrcweir 
827*cdf0e10cSrcweir         XInterface oWriter = (XInterface)xMSF.createInstance(
828*cdf0e10cSrcweir             "com.sun.star.xml.sax.Writer");
829*cdf0e10cSrcweir         XActiveDataSource xWriterDS = (XActiveDataSource)
830*cdf0e10cSrcweir             UnoRuntime.queryInterface(XActiveDataSource.class, oWriter);
831*cdf0e10cSrcweir         XDocumentHandler xDocHandWriter = (XDocumentHandler) UnoRuntime.queryInterface
832*cdf0e10cSrcweir             (XDocumentHandler.class, oWriter) ;
833*cdf0e10cSrcweir 
834*cdf0e10cSrcweir         if (xFacc.exists(fileURL))
835*cdf0e10cSrcweir             xFacc.kill(fileURL);
836*cdf0e10cSrcweir         XOutputStream fOut = xFacc.openFileWrite(fileURL) ;
837*cdf0e10cSrcweir         xWriterDS.setOutputStream(fOut);
838*cdf0e10cSrcweir 
839*cdf0e10cSrcweir         return xDocHandWriter ;
840*cdf0e10cSrcweir     }
841*cdf0e10cSrcweir 
842*cdf0e10cSrcweir     /**
843*cdf0e10cSrcweir      * Parses XML file and passes its data to the SAX handler specified.
844*cdf0e10cSrcweir      * @param xMSF Soffice <code>ServiceManager</code> factory.
845*cdf0e10cSrcweir      * @param fileURL XML file name (in form file:///<path>) to be parsed.
846*cdf0e10cSrcweir      * @param handler SAX handler to which XML data from file will
847*cdf0e10cSrcweir      * be transferred.
848*cdf0e10cSrcweir      */
849*cdf0e10cSrcweir     public static void parseXMLFile(XMultiServiceFactory xMSF,
850*cdf0e10cSrcweir         String fileURL, XDocumentHandler handler) throws com.sun.star.uno.Exception
851*cdf0e10cSrcweir     {
852*cdf0e10cSrcweir         XInterface oFacc = (XInterface)xMSF.createInstance(
853*cdf0e10cSrcweir             "com.sun.star.comp.ucb.SimpleFileAccess");
854*cdf0e10cSrcweir         XSimpleFileAccess xFacc = (XSimpleFileAccess)UnoRuntime.queryInterface
855*cdf0e10cSrcweir             (XSimpleFileAccess.class, oFacc) ;
856*cdf0e10cSrcweir         XInputStream oIn = xFacc.openFileRead(fileURL) ;
857*cdf0e10cSrcweir 
858*cdf0e10cSrcweir         XInterface oParser = (XInterface)xMSF.createInstance(
859*cdf0e10cSrcweir             "com.sun.star.xml.sax.Parser");
860*cdf0e10cSrcweir         XParser xParser = (XParser) UnoRuntime.queryInterface(XParser.class, oParser);
861*cdf0e10cSrcweir 
862*cdf0e10cSrcweir         xParser.setDocumentHandler(handler) ;
863*cdf0e10cSrcweir         InputSource inSrc = new InputSource() ;
864*cdf0e10cSrcweir         inSrc.aInputStream = oIn ;
865*cdf0e10cSrcweir         xParser.parseStream(inSrc) ;
866*cdf0e10cSrcweir 
867*cdf0e10cSrcweir         oIn.closeInput();
868*cdf0e10cSrcweir     }
869*cdf0e10cSrcweir 
870*cdf0e10cSrcweir     /**
871*cdf0e10cSrcweir      * Exports document (the whole or a part) into the file specified
872*cdf0e10cSrcweir      * in XML format.
873*cdf0e10cSrcweir      * @param xMSF Soffice <code>ServiceManager</code> factory.
874*cdf0e10cSrcweir      * @param xDoc Document to be exported.
875*cdf0e10cSrcweir      * @param docType Type of document (for example 'Calc', 'Writer', 'Draw')
876*cdf0e10cSrcweir      * The type must start with <b>capital</b> letter.
877*cdf0e10cSrcweir      * @param exportType The type of export specifies if the whole
878*cdf0e10cSrcweir      * document will be exported or one of its parts (Meta info, Styles, etc.).
879*cdf0e10cSrcweir      * The following types supported (it also depends of document type) :
880*cdf0e10cSrcweir      *  "" (empty string) - for the whole document ;
881*cdf0e10cSrcweir      *  "Content" - only content exported ;
882*cdf0e10cSrcweir      *  "Meta" - meta document info exported ;
883*cdf0e10cSrcweir      *  "Settings" - view settings of document exported ;
884*cdf0e10cSrcweir      *  "Styles" - document styles exported ;
885*cdf0e10cSrcweir      * @param fileURL XML file name (in form file:///<path>) to be exported to.
886*cdf0e10cSrcweir      */
887*cdf0e10cSrcweir     public static void exportDocument(XMultiServiceFactory xMSF, XComponent xDoc,
888*cdf0e10cSrcweir         String docType, String exportType, String fileURL)
889*cdf0e10cSrcweir         throws com.sun.star.uno.Exception {
890*cdf0e10cSrcweir 
891*cdf0e10cSrcweir         XDocumentHandler xDocHandWriter = XMLTools.getFileXMLWriter(xMSF, fileURL) ;
892*cdf0e10cSrcweir 
893*cdf0e10cSrcweir         Any arg = new Any(new Type(XDocumentHandler.class), xDocHandWriter);
894*cdf0e10cSrcweir         XInterface oExp = (XInterface)xMSF.createInstanceWithArguments(
895*cdf0e10cSrcweir             "com.sun.star.comp." + docType + ".XML" + exportType + "Exporter",
896*cdf0e10cSrcweir             new Object[] {arg});
897*cdf0e10cSrcweir 
898*cdf0e10cSrcweir         XExporter xExp = (XExporter) UnoRuntime.queryInterface
899*cdf0e10cSrcweir             (XExporter.class, oExp) ;
900*cdf0e10cSrcweir         xExp.setSourceDocument(xDoc) ;
901*cdf0e10cSrcweir 
902*cdf0e10cSrcweir         XFilter filter = (XFilter) UnoRuntime.queryInterface(XFilter.class, oExp) ;
903*cdf0e10cSrcweir         filter.filter(XMLTools.createMediaDescriptor(
904*cdf0e10cSrcweir             new String[] {"FilterName"},
905*cdf0e10cSrcweir             new Object[] {"Custom filter"})) ;
906*cdf0e10cSrcweir     }
907*cdf0e10cSrcweir 
908*cdf0e10cSrcweir     /**
909*cdf0e10cSrcweir      * Imports document (the whole or a part) from the file specified
910*cdf0e10cSrcweir      * in XML format.
911*cdf0e10cSrcweir      * @param xMSF Soffice <code>ServiceManager</code> factory.
912*cdf0e10cSrcweir      * @param xDoc Target document to be imported.
913*cdf0e10cSrcweir      * @param docType Type of document (for example 'Calc', 'Writer', 'Draw')
914*cdf0e10cSrcweir      * The type must start with <b>capital</b> letter.
915*cdf0e10cSrcweir      * @param exportType The type of export specifies if the whole
916*cdf0e10cSrcweir      * document will be exported or one of its parts (Meta info, Styles, etc.).
917*cdf0e10cSrcweir      * The following types supported (it hardly depends of XML data in file) :
918*cdf0e10cSrcweir      *  "" (empty string) - for the whole document ;
919*cdf0e10cSrcweir      *  "Content" - only content exported ;
920*cdf0e10cSrcweir      *  "Meta" - meta document info exported ;
921*cdf0e10cSrcweir      *  "Settings" - view settings of document exported ;
922*cdf0e10cSrcweir      *  "Styles" - document styles exported ;
923*cdf0e10cSrcweir      * @param fileURL XML file name (in form file:///<path>) to be imported from.
924*cdf0e10cSrcweir      */
925*cdf0e10cSrcweir     public static void importDocument(XMultiServiceFactory xMSF, XComponent xDoc,
926*cdf0e10cSrcweir         String docType, String importType, String fileURL)
927*cdf0e10cSrcweir         throws com.sun.star.uno.Exception {
928*cdf0e10cSrcweir 
929*cdf0e10cSrcweir         XInterface oImp = (XInterface)xMSF.createInstance(
930*cdf0e10cSrcweir             "com.sun.star.comp." + docType + ".XML" + importType + "Importer");
931*cdf0e10cSrcweir         XImporter xImp = (XImporter) UnoRuntime.queryInterface
932*cdf0e10cSrcweir             (XImporter.class, oImp) ;
933*cdf0e10cSrcweir         XDocumentHandler xDocHandImp = (XDocumentHandler) UnoRuntime.queryInterface
934*cdf0e10cSrcweir             (XDocumentHandler.class, oImp) ;
935*cdf0e10cSrcweir 
936*cdf0e10cSrcweir         xImp.setTargetDocument(xDoc) ;
937*cdf0e10cSrcweir         parseXMLFile(xMSF, fileURL, xDocHandImp) ;
938*cdf0e10cSrcweir     }
939*cdf0e10cSrcweir }