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