bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / runner / util / XMLTools.java
blobb658aa58f1856334b567ed040258e9077310dd5a
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package util;
21 import java.io.PrintWriter;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Iterator;
27 import com.sun.star.beans.PropertyValue;
28 import com.sun.star.document.XExporter;
29 import com.sun.star.document.XFilter;
30 import com.sun.star.document.XImporter;
31 import com.sun.star.io.XActiveDataSource;
32 import com.sun.star.io.XInputStream;
33 import com.sun.star.io.XOutputStream;
34 import com.sun.star.lang.XComponent;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.ucb.XSimpleFileAccess;
37 import com.sun.star.uno.Any;
38 import com.sun.star.uno.Type;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XInterface;
41 import com.sun.star.xml.sax.InputSource;
42 import com.sun.star.xml.sax.XAttributeList;
43 import com.sun.star.xml.sax.XDocumentHandler;
44 import com.sun.star.xml.sax.XLocator;
45 import com.sun.star.xml.sax.XParser;
48 public class XMLTools {
50 /**
51 * The implementation of <code>com.sun.star.xml.sax.XAttributeList</code>
52 * where attributes and their values can be added.
54 public static class AttributeList implements XAttributeList {
55 private static class Attribute {
56 public String Name ;
57 public String Type ;
58 public String Value ;
60 private HashMap<String, Attribute> attrByName = new HashMap<String, Attribute>() ;
61 private ArrayList<Attribute> attributes = new ArrayList<Attribute>() ;
62 private PrintWriter log = null ;
64 /**
65 * Creates a class instance.
67 public AttributeList() {}
69 /**
70 * Constructs a list which will report to <code>log</code>
71 * specified about each <code>XDocumentHandler</code> method
72 * call.
74 public AttributeList(PrintWriter log) {
75 this.log = log ;
78 public AttributeList(XAttributeList list) {
79 if (list == null) return ;
80 for (short i = 0; i < list.getLength(); i++) {
81 add(list.getNameByIndex(i), list.getTypeByIndex(i),
82 list.getValueByIndex(i)) ;
86 /**
87 * Adds an attribute with type and value specified.
88 * @param name The attribute name.
89 * @param type Value type (usually 'CDATA' used).
90 * @param value Attribute value.
92 public void add(String name, String type, String value) {
93 Attribute attr = new Attribute() ;
94 attr.Name = name ;
95 attr.Type = type ;
96 attr.Value = value ;
97 attributes.add(attr) ;
98 attrByName.put(attr.Name, attr) ;
102 * Adds an attribute with value specified. As a type of
103 * value 'CDATA' string specified.
104 * @param name The attribute name.
105 * @param value Attribute value.
107 public void add(String name, String value) {
108 add(name, "CDATA", value) ;
112 * Clears all attributes added before.
114 public void clear() {
115 attrByName.clear() ;
116 attributes.clear() ;
119 /***************************************
120 * XAttributeList methods
121 ****************************************/
123 public short getLength() {
124 if (log != null)
125 log.println("getLength() called -> " + attributes.size()) ;
126 return (short) attributes.size() ;
129 public String getNameByIndex(short idx) {
130 String name = attributes.get(idx).Name ;
131 if (log != null)
132 log.println("getNameByIndex(" + idx + ") called -> '" +
133 name + "'") ;
134 return name ;
137 public String getTypeByIndex(short idx) {
138 String type = attributes.get(idx).Type ;
139 if (log != null)
140 log.println("getTypeByIndex(" + idx + ") called -> '" +
141 type + "'") ;
142 return type;
145 public String getTypeByName(String name) {
146 String type = attrByName.get(name).Type ;
147 if (log != null)
148 log.println("getTypeByName('" + name + "') called -> '" +
149 type + "'") ;
150 return type;
152 public String getValueByIndex(short idx) {
153 String value = attributes.get(idx).Value ;
154 if (log != null)
155 log.println("getValueByIndex(" + idx + ") called -> '" +
156 value + "'") ;
157 return value;
160 public String getValueByName(String name) {
161 String value = attrByName.get(name).Value ;
162 if (log != null)
163 log.println("getValueByName('" + name + "') called -> '" +
164 value + "'") ;
165 return value;
170 * This class writes all XML data handled into a stream specified
171 * in the constructor.
173 public static class XMLWriter implements XDocumentHandler {
174 private PrintWriter _log = null ;
175 private String align = "" ;
178 * Creates a SAX handler which writes all XML data
179 * handled into a <code>log</code> stream specified.
181 public XMLWriter(PrintWriter log) {
182 _log = log ;
186 * Creates a SAX handler which does nothing.
188 public XMLWriter() {
191 public void processingInstruction(String appl, String data) {
192 if (_log == null) return ;
193 _log.println(align + "<?" + appl + " " + data + "?>") ;
195 public void startDocument() {
196 if (_log == null) return ;
197 _log.println("START DOCUMENT:") ;
199 public void endDocument() {
200 if (_log == null) return ;
201 _log.println("END DOCUMENT:") ;
203 public void setDocumentLocator(XLocator loc) {
204 if (_log == null) return ;
205 _log.println("DOCUMENT LOCATOR: ('" + loc.getPublicId() +
206 "','" + loc.getSystemId() + "')") ;
208 public void startElement(String name, XAttributeList attr) {
209 if (_log == null) return ;
210 _log.print(align + "<" + name + " ") ;
211 if (attr != null) {
212 short attrLen = attr.getLength() ;
213 for (short i = 0; i < attrLen; i++) {
214 if (i != 0) _log.print(align + " ") ;
215 _log.print(attr.getNameByIndex(i) + "[" +
216 attr.getTypeByIndex(i) + "]=\"" +
217 attr.getValueByIndex(i) + "\"") ;
218 if (i+1 != attrLen) {
219 _log.println() ;
223 _log.println(">") ;
225 align += " " ;
228 public void endElement(String name) {
229 if (_log == null) return ;
230 align = align.substring(3) ;
231 _log.println(align + "</" + name + ">") ;
234 public void characters(String chars) {
235 if (_log == null) return ;
236 _log.println(align + chars) ;
238 public void ignorableWhitespace(String sp) {
239 if (_log == null) return ;
240 _log.println(sp) ;
245 * Checks if the XML structure is well formed (i.e. all tags opened must be
246 * closed and all tags opened inside a tag must be closed
247 * inside the same tag). It also checks parameters passed.
248 * If any collisions found appropriate error message is
249 * output into a stream specified. No XML data output, i.e.
250 * no output will be performed if no errors occur.<p>
251 * After document is completed there is a way to cehck
252 * if the XML data and structure was valid.
254 public static class XMLWellFormChecker extends XMLWriter {
255 protected boolean docStarted = false ;
256 protected boolean docEnded = false ;
257 protected ArrayList<String> tagStack = new ArrayList<String>() ;
258 protected boolean wellFormed = true ;
259 protected boolean noOtherErrors = true ;
260 protected PrintWriter log = null ;
261 protected boolean printXMLData = false ;
263 public XMLWellFormChecker(PrintWriter log) {
264 super() ;
265 this.log = log ;
268 public XMLWellFormChecker(PrintWriter log_, boolean printXMLData) {
269 super(printXMLData ? log_ : null) ;
270 this.printXMLData = printXMLData ;
271 this.log = log_ ;
275 * Reset all values. This is important e.g. for test of XFilter
276 * interface, where 'filter()' method istbstarted twice.
278 public void reset() {
279 docStarted = false ;
280 docEnded = false ;
281 tagStack = new ArrayList<String>() ;
282 wellFormed = true ;
283 noOtherErrors = true ;
284 printXMLData = false ;
287 public void startDocument() {
288 super.startDocument();
290 if (docStarted) {
291 printError("Document is started twice.") ;
292 wellFormed = false ;
295 docStarted = true ;
297 public void endDocument() {
298 super.endDocument();
299 if (!docStarted) {
300 wellFormed = false ;
301 printError("Document ended but not started.") ;
303 docEnded = true ;
305 public void startElement(String name, XAttributeList attr) {
306 super.startElement(name, attr);
307 if (attr == null) {
308 printError("attribute list passed as parameter to startElement()"+
309 " method has null value for tag <" + name + ">") ;
310 noOtherErrors = false ;
312 tagStack.add(0, name) ;
314 public void endElement(String name) {
315 super.endElement(name);
316 if (wellFormed) {
317 if (tagStack.size() == 0) {
318 wellFormed = false ;
319 printError("No tags to close (bad closing tag </" + name + ">)") ;
320 } else {
321 String startTag = tagStack.get(0) ;
322 tagStack.remove(0) ;
323 if (!startTag.equals(name)) {
324 wellFormed = false ;
325 printError("Bad closing tag: </" + name +
326 ">; tag expected: </" + startTag + ">");
333 * Checks if there were no errors during document handling.
334 * I.e. startDocument() and endDocument() must be called,
335 * XML must be well formed, parameters must be valid.
337 public boolean isWellFormed() {
338 if (!docEnded) {
339 printError("Document was not ended.") ;
340 wellFormed = false ;
343 return wellFormed && noOtherErrors ;
347 * Prints error message and all tags where error occurred inside.
348 * Also prints "Tag trace" in case if the full XML data isn't
349 * printed.
351 public void printError(String msg) {
352 log.println("!!! Error: " + msg) ;
353 if (printXMLData) return ;
354 log.println(" Tag trace :") ;
355 for (int i = 0; i < tagStack.size(); i++) {
356 String tag = tagStack.get(i) ;
357 log.println(" <" + tag + ">") ;
363 * Beside structure of XML this class also can check existence
364 * of tags, inner tags, and character data. After document
365 * completion there is a way to check if required tags and
366 * character data was found. If there any error occurs an
367 * appropriate message is output.
369 public static class XMLTagsChecker extends XMLWellFormChecker {
370 protected HashMap<String,String> tags = new HashMap<String,String>() ;
371 protected HashMap<String,String> chars = new HashMap<String,String>() ;
372 protected boolean allOK = true ;
374 public XMLTagsChecker(PrintWriter log) {
375 super(log) ;
379 * Adds a tag name which must be contained in the XML data.
381 public void addTag(String tag) {
382 tags.put(tag, "") ;
385 * Adds a tag name which must be contained in the XML data and
386 * must be inside the tag with name <code>outerTag</code>.
388 public void addTagEnclosed(String tag, String outerTag) {
389 tags.put(tag, outerTag) ;
392 * Adds a character data which must be contained in the XML data.
394 public void addCharacters(String ch) {
395 chars.put(ch, "") ;
398 * Adds a character data which must be contained in the XML data and
399 * must be inside the tag with name <code>outerTag</code>.
401 public void addCharactersEnclosed(String ch, String outerTag) {
402 chars.put(ch, outerTag) ;
405 public void startElement(String name, XAttributeList attrs) {
406 super.startElement(name, attrs) ;
407 if (tags.containsKey(name)) {
408 String outerTag = tags.get(name);
409 if (!outerTag.equals("")) {
410 boolean isInTag = false ;
411 for (int i = 0; i < tagStack.size(); i++) {
412 if (outerTag.equals(tagStack.get(i))) {
413 isInTag = true ;
414 break ;
417 if (!isInTag) {
418 printError("Required tag <" + name + "> found, but is not enclosed in tag <" +
419 outerTag + ">") ;
420 allOK = false ;
423 tags.remove(name) ;
427 public void characters(String ch) {
428 super.characters(ch) ;
430 if (chars.containsKey(ch)) {
431 String outerTag = chars.get(ch);
432 if (!outerTag.equals("")) {
433 boolean isInTag = false ;
434 for (int i = 0; i < tagStack.size(); i++) {
435 if (outerTag.equals(tagStack.get(i))) {
436 isInTag = true ;
437 break ;
440 if (!isInTag) {
441 printError("Required characters '" + ch + "' found, but are not enclosed in tag <" +
442 outerTag + ">") ;
443 allOK = false ;
446 chars.remove(ch) ;
451 * Checks if the XML data was valid and well formed and if
452 * all necessary tags and character data was found.
454 public boolean checkTags() {
455 allOK &= isWellFormed() ;
457 Iterator<String> badTags = tags.keySet().iterator() ;
458 Iterator<String> badChars = chars.keySet().iterator() ;
460 if (badTags.hasNext()) {
461 allOK = false ;
462 log.println("Required tags were not found in export :") ;
463 while(badTags.hasNext()) {
464 log.println(" <" + badTags.next() + ">") ;
467 if (badChars.hasNext()) {
468 allOK = false ;
469 log.println("Required characters were not found in export :") ;
470 while(badChars.hasNext()) {
471 log.println(" <" + badChars.next() + ">") ;
474 reset();
475 return allOK ;
480 * Represents an XML tag which must be found in XML data written.
481 * This tag can contain only its name or tag name and attribute
482 * name, or attribute value additionally.
484 public static class Tag {
485 private String name = null;
486 private String[][] attrList = new String[0][3] ;
489 * Creates tag which has only a name. Attributes don't make sense.
490 * @param tagName The name of the tag.
492 public Tag(String tagName) {
493 name = tagName ;
497 * Creates a tag with the name specified, which must have an
498 * attribute with name specified. The value of this attribute
499 * doesn't make sense.
500 * @param tagName The name of the tag.
501 * @param attrName The name of attribute which must be contained
502 * in the tag.
504 public Tag(String tagName, String attrName) {
505 name = tagName ;
506 attrList = new String[1][3] ;
507 attrList[0][0] = attrName ;
511 * Creates a tag with the name specified, which must have an
512 * attribute with the value specified. The type of value
513 * assumed to be 'CDATA'.
514 * @param tagName The name of the tag.
515 * @param attrName The name of attribute which must be contained
516 * in the tag.
517 * @param attrValue Attribute value.
519 public Tag(String tagName, String attrName, String attrValue) {
520 name = tagName ;
521 attrList = new String[1][3] ;
522 attrList[0][0] = attrName ;
523 attrList[0][1] = "CDATA" ;
524 attrList[0][2] = attrValue ;
528 * Creates a tag with the name specified, which must have
529 * attributes specified. The value of thesee attributes
530 * doesn't make sense.
531 * @param tagName The name of the tag.
532 * @param attrNames Array with names of attributes which must
533 * be contained in the tag.
535 public Tag(String tagName, String[] attrNames) {
536 name = tagName ;
537 attrList = new String[attrNames.length][3] ;
538 for (int i = 0; i < attrNames.length; i++) {
539 attrList[i][0] = attrNames[i] ;
544 * Creates a tag with the name specified, which must have an
545 * attributes with their values specified. The type of all values
546 * assumed to be 'CDATA'.
547 * @param tagName The name of the tag.
548 * @param attrValues An array with attribute names and their values.
549 * <code>attrValues[N][0]</code> element contains the name of Nth
550 * attribute, and <code>attrValues[N][1]</code> element contains
551 * value of Nth attribute, if value is <code>null</code> then the
552 * attribute value can be any.
554 public Tag(String tagName, String[][] attrValues) {
555 name = tagName ;
556 attrList = new String[attrValues.length][3] ;
557 for (int i = 0; i < attrValues.length; i++) {
558 attrList[i][0] = attrValues[i][0] ;
559 attrList[i][1] = "CDATA" ;
560 attrList[i][2] = attrValues[i][1] ;
565 * Gets tag String description.
567 public String toString() {
568 String ret = "<" + name ;
569 for (int i = 0; i < attrList.length; i++) {
570 ret += " " + attrList[i][0] + "=";
571 if (attrList[i][2] == null) {
572 ret += "(not specified)";
573 } else {
574 ret += "\"" + attrList[i][2] + "\"";
577 ret += ">";
579 return ret ;
582 protected boolean checkAttr(int attrListIdx, XAttributeList list) {
583 short j = 0 ;
584 int listLen = list.getLength();
585 while(j < listLen) {
586 if (attrList[attrListIdx][0].equals(list.getNameByIndex(j))) {
587 if (attrList[attrListIdx][2] == null) return true ;
588 return attrList[attrListIdx][2].equals(list.getValueByIndex(j)) ;
590 j++ ;
592 return false ;
596 * Checks if this tag matches tag passed in parameters.
597 * I.e. if tag specifies only it's name it mathes if names
598 * are equal (attributes don't make sense). If there are
599 * some attributes names specified in this tag method checks
600 * if all names present in attribute list <code>list</code>
601 * (attributes' values don't make sense). If attributes specified
602 * with values method checks if these attributes exist and
603 * have appropriate values.
605 public boolean isMatchTo(String tagName, XAttributeList list) {
606 if (!name.equals(tagName)) return false;
607 boolean result = true ;
608 for (int i = 0; i < attrList.length; i++) {
609 result &= checkAttr(i, list) ;
611 return result ;
616 * Class realises extended XML data checking. It has possibilities
617 * to check if a tag exists, if it has some attributes with
618 * values, and if this tag is contained in another tag (which
619 * also can specify any attributes). It can check if some
620 * character data exists inside any tag specified.
622 public static class XMLChecker extends XMLWellFormChecker {
623 protected HashSet<String> tagSet = new HashSet<String>() ;
624 protected ArrayList<Tag[]> tags = new ArrayList<Tag[]>() ;
625 protected ArrayList<Object[]> chars = new ArrayList<Object[]>() ;
626 protected ArrayList<String> tagStack = new ArrayList<String>() ;
627 protected ArrayList<AttributeList> attrStack = new ArrayList<AttributeList>() ;
629 public XMLChecker(PrintWriter log, boolean writeXML) {
630 super(log, writeXML) ;
633 public void addTag(Tag tag) {
634 tags.add(new Tag[] {tag, null}) ;
635 tagSet.add(tag.name) ;
638 public void addTagEnclosed(Tag tag, Tag outerTag) {
639 tags.add(new Tag[] {tag, outerTag}) ;
640 tagSet.add(tag.name) ;
643 public void addCharacters(String ch) {
644 chars.add(new Object[] {ch.trim(), null}) ;
647 public void addCharactersEnclosed(String ch, Tag outerTag) {
648 chars.add(new Object[] {ch.trim(), outerTag}) ;
651 public void startElement(String name, XAttributeList attr) {
652 try {
653 super.startElement(name, attr);
655 if (tagSet.contains(name)) {
656 for (int i = 0; i < tags.size(); i++) {
657 Tag[] tag = tags.get(i);
658 if (tag[0].isMatchTo(name, attr)) {
659 if (tag[1] == null) {
660 tags.remove(i--);
661 } else {
662 boolean isInStack = false ;
663 for (int j = 0; j < tagStack.size(); j++) {
664 if (tag[1].isMatchTo(tagStack.get(j),
665 attrStack.get(j))) {
667 isInStack = true ;
668 break ;
671 if (isInStack) {
672 tags.remove(i--) ;
679 tagStack.add(0, name) ;
680 attrStack.add(0, new AttributeList(attr));
681 } catch (Exception e) {
682 e.printStackTrace(log);
686 public void characters(String ch) {
687 super.characters(ch) ;
688 for (int i = 0; i < chars.size(); i++) {
689 Object[] chr = chars.get(i);
690 if (((String) chr[0]).equals(ch)) {
691 if (chr[1] == null) {
692 chars.remove(i--);
693 } else {
694 boolean isInStack = false ;
695 for (int j = 0; j < tagStack.size(); j++) {
696 if (((Tag) chr[1]).isMatchTo(tagStack.get(j),
697 attrStack.get(j))) {
699 isInStack = true ;
700 break ;
703 if (isInStack) {
704 chars.remove(i--) ;
711 public void endElement(String name) {
712 try {
713 super.endElement(name);
715 if (tagStack.size() > 0) {
716 tagStack.remove(0) ;
717 attrStack.remove(0) ;
719 } catch(Exception e) {
720 e.printStackTrace(log) ;
724 public boolean check() {
725 if (tags.size()> 0) {
726 log.println("!!! Error: Some tags were not found :") ;
727 for (int i = 0; i < tags.size(); i++) {
728 Tag[] tag = tags.get(i) ;
729 log.println(" Tag " + tag[0] + " was not found");
730 if (tag[1] != null)
731 log.println(" inside tag " + tag[1]) ;
734 if (chars.size() > 0) {
735 log.println("!!! Error: Some character data blocks were not found :") ;
736 for (int i = 0; i < chars.size(); i++) {
737 Object[] ch = chars.get(i) ;
738 log.println(" Character data \"" + ch[0] + "\" was not found ") ;
739 if (ch[1] != null)
740 log.println(" inside tag " + ch[1]) ;
744 if (!isWellFormed())
745 log.println("!!! Some errors were found in XML structure") ;
747 boolean result = tags.size() == 0 && chars.size() == 0 && isWellFormed();
748 reset();
749 return result;
754 * Creates <code>XDocumentHandler</code> implementation in form
755 * of <code>com.sun.star.xml.sax.Writer</code> service, which
756 * writes XML data into a <code>com.sun.star.io.Pipe</code>
757 * created.
758 * @return Single element array which contains the handler
759 * contained in <code>Any</code> structure.
761 public static Object[] getDocumentHandler(XMultiServiceFactory xMSF) {
762 Object[] ret = new Object[1];
763 try {
764 XInterface Writer = (XInterface) xMSF.createInstance(
765 "com.sun.star.xml.sax.Writer");
766 XInterface oPipe = (XInterface) xMSF.createInstance
767 ( "com.sun.star.io.Pipe" );
768 XOutputStream xPipeOutput = UnoRuntime.
769 queryInterface(XOutputStream.class, oPipe) ;
771 XActiveDataSource xADS = UnoRuntime.queryInterface(XActiveDataSource.class,Writer);
772 xADS.setOutputStream(xPipeOutput);
773 XDocumentHandler handler = UnoRuntime.queryInterface(XDocumentHandler.class,Writer);
775 Any arg = new Any(new Type(XDocumentHandler.class),handler);
777 ret[0] = arg;
778 } catch (com.sun.star.uno.Exception e) {
779 e.printStackTrace();
781 return ret;
784 public static PropertyValue[] createMediaDescriptor(String[] propNames, Object[] values) {
785 PropertyValue[] props = new PropertyValue[propNames.length] ;
787 for (int i = 0; i < props.length; i++) {
788 props[i] = new PropertyValue() ;
789 props[i].Name = propNames[i] ;
790 if (values != null && i < values.length) {
791 props[i].Value = values[i] ;
795 return props ;
799 * Gets the hanlder, which writes all the XML data passed to the
800 * file specified.
801 * @param xMSF Soffice <code>ServiceManager</code> factory.
802 * @param fileURL The file URL (in form file:///<path>) to which
803 * XML data is written.
804 * @return SAX handler to which XML data has to be written.
806 public static XDocumentHandler getFileXMLWriter(XMultiServiceFactory xMSF, String fileURL)
807 throws com.sun.star.uno.Exception
809 XInterface oFacc = (XInterface)xMSF.createInstance(
810 "com.sun.star.comp.ucb.SimpleFileAccess");
811 XSimpleFileAccess xFacc = UnoRuntime.queryInterface
812 (XSimpleFileAccess.class, oFacc) ;
814 XInterface oWriter = (XInterface)xMSF.createInstance(
815 "com.sun.star.xml.sax.Writer");
816 XActiveDataSource xWriterDS = UnoRuntime.queryInterface(XActiveDataSource.class, oWriter);
817 XDocumentHandler xDocHandWriter = UnoRuntime.queryInterface
818 (XDocumentHandler.class, oWriter) ;
820 if (xFacc.exists(fileURL))
821 xFacc.kill(fileURL);
822 XOutputStream fOut = xFacc.openFileWrite(fileURL) ;
823 xWriterDS.setOutputStream(fOut);
825 return xDocHandWriter ;
829 * Parses XML file and passes its data to the SAX handler specified.
830 * @param xMSF Soffice <code>ServiceManager</code> factory.
831 * @param fileURL XML file name (in form file:///<path>) to be parsed.
832 * @param handler SAX handler to which XML data from file will
833 * be transferred.
835 public static void parseXMLFile(XMultiServiceFactory xMSF,
836 String fileURL, XDocumentHandler handler) throws com.sun.star.uno.Exception
838 XInterface oFacc = (XInterface)xMSF.createInstance(
839 "com.sun.star.comp.ucb.SimpleFileAccess");
840 XSimpleFileAccess xFacc = UnoRuntime.queryInterface
841 (XSimpleFileAccess.class, oFacc) ;
842 XInputStream oIn = xFacc.openFileRead(fileURL) ;
844 XInterface oParser = (XInterface)xMSF.createInstance(
845 "com.sun.star.xml.sax.Parser");
846 XParser xParser = UnoRuntime.queryInterface(XParser.class, oParser);
848 xParser.setDocumentHandler(handler) ;
849 InputSource inSrc = new InputSource() ;
850 inSrc.aInputStream = oIn ;
851 xParser.parseStream(inSrc) ;
853 oIn.closeInput();
857 * Exports document (the whole or a part) into the file specified
858 * in XML format.
859 * @param xMSF Soffice <code>ServiceManager</code> factory.
860 * @param xDoc Document to be exported.
861 * @param docType Type of document (for example 'Calc', 'Writer', 'Draw')
862 * The type must start with <b>capital</b> letter.
863 * @param exportType The type of export specifies if the whole
864 * document will be exported or one of its parts (Meta info, Styles, etc.).
865 * The following types supported (it also depends of document type) :
866 * "" (empty string) - for the whole document ;
867 * "Content" - only content exported ;
868 * "Meta" - meta document info exported ;
869 * "Settings" - view settings of document exported ;
870 * "Styles" - document styles exported ;
871 * @param fileURL XML file name (in form file:///<path>) to be exported to.
873 public static void exportDocument(XMultiServiceFactory xMSF, XComponent xDoc,
874 String docType, String exportType, String fileURL)
875 throws com.sun.star.uno.Exception {
877 XDocumentHandler xDocHandWriter = XMLTools.getFileXMLWriter(xMSF, fileURL) ;
879 Any arg = new Any(new Type(XDocumentHandler.class), xDocHandWriter);
880 XInterface oExp = (XInterface)xMSF.createInstanceWithArguments(
881 "com.sun.star.comp." + docType + ".XML" + exportType + "Exporter",
882 new Object[] {arg});
884 XExporter xExp = UnoRuntime.queryInterface
885 (XExporter.class, oExp) ;
886 xExp.setSourceDocument(xDoc) ;
888 XFilter filter = UnoRuntime.queryInterface(XFilter.class, oExp) ;
889 filter.filter(XMLTools.createMediaDescriptor(
890 new String[] {"FilterName"},
891 new Object[] {"Custom filter"})) ;
895 * Imports document (the whole or a part) from the file specified
896 * in XML format.
897 * @param xMSF Soffice <code>ServiceManager</code> factory.
898 * @param xDoc Target document to be imported.
899 * @param docType Type of document (for example 'Calc', 'Writer', 'Draw')
900 * The type must start with <b>capital</b> letter.
901 * @param importType The type of export specifies if the whole
902 * document will be exported or one of its parts (Meta info, Styles, etc.).
903 * The following types supported (it hardly depends of XML data in file) :
904 * "" (empty string) - for the whole document ;
905 * "Content" - only content exported ;
906 * "Meta" - meta document info exported ;
907 * "Settings" - view settings of document exported ;
908 * "Styles" - document styles exported ;
909 * @param fileURL XML file name (in form file:///<path>) to be imported from.
911 public static void importDocument(XMultiServiceFactory xMSF, XComponent xDoc,
912 String docType, String importType, String fileURL)
913 throws com.sun.star.uno.Exception {
915 XInterface oImp = (XInterface)xMSF.createInstance(
916 "com.sun.star.comp." + docType + ".XML" + importType + "Importer");
917 XImporter xImp = UnoRuntime.queryInterface
918 (XImporter.class, oImp) ;
919 XDocumentHandler xDocHandImp = UnoRuntime.queryInterface
920 (XDocumentHandler.class, oImp) ;
922 xImp.setTargetDocument(xDoc) ;
923 parseXMLFile(xMSF, fileURL, xDocHandImp) ;