merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / runner / util / XMLTools.java
blob04e48b60c463265adaa9b9220cfd46b43ede8854
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XMLTools.java,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package util;
33 import java.io.PrintWriter ;
34 import java.util.Vector ;
35 import java.util.Hashtable ;
36 import java.util.Enumeration ;
37 import java.util.HashSet ;
39 // access the implementations via names
40 import com.sun.star.uno.XInterface;
41 import com.sun.star.io.XOutputStream;
42 import com.sun.star.io.XInputStream;
43 import com.sun.star.io.XActiveDataSource;
44 import com.sun.star.ucb.XSimpleFileAccess;
45 import com.sun.star.lang.XMultiServiceFactory;
46 import com.sun.star.xml.sax.XDocumentHandler;
47 import com.sun.star.uno.Any;
48 import com.sun.star.uno.Type;
49 import com.sun.star.uno.UnoRuntime;
50 import com.sun.star.beans.PropertyValue;
51 import com.sun.star.xml.sax.XLocator;
52 import com.sun.star.xml.sax.XAttributeList;
53 import com.sun.star.xml.sax.XParser ;
54 import com.sun.star.xml.sax.InputSource ;
55 import com.sun.star.lang.XComponent;
56 import com.sun.star.document.XExporter;
57 import com.sun.star.document.XImporter;
58 import com.sun.star.document.XFilter;
61 public class XMLTools {
63 /**
64 * The implementation of <code>com.sun.star.xml.sax.XAttributeList</code>
65 * where attributes and their values can be added.
67 public static class AttributeList implements XAttributeList {
68 private static class Attribute {
69 public String Name ;
70 public String Type ;
71 public String Value ;
73 private Hashtable attrByName = new Hashtable() ;
74 private Vector attributes = new Vector() ;
75 private PrintWriter log = null ;
77 /**
78 * Creates a class instance.
80 public AttributeList() {}
82 /**
83 * Constructs a list which will report to <code>log</code>
84 * specified about each <code>XDocumentHandler</code> method
85 * call.
87 public AttributeList(PrintWriter log) {
88 this.log = log ;
91 public AttributeList(XAttributeList list) {
92 if (list == null) return ;
93 for (short i = 0; i < list.getLength(); i++) {
94 add(list.getNameByIndex(i), list.getTypeByIndex(i),
95 list.getValueByIndex(i)) ;
99 /**
100 * Adds an attribute with type and value specified.
101 * @param name The attribute name.
102 * @param type Value type (usually 'CDATA' used).
103 * @param value Attribute value.
105 public void add(String name, String type, String value) {
106 Attribute attr = new Attribute() ;
107 attr.Name = name ;
108 attr.Type = type ;
109 attr.Value = value ;
110 attributes.add(attr) ;
111 attrByName.put(attr.Name, attr) ;
115 * Adds an attribute with value specified. As a type of
116 * value 'CDATA' string specified.
117 * @param name The attribute name.
118 * @param value Attribute value.
120 public void add(String name, String value) {
121 add(name, "CDATA", value) ;
125 * Clears all attributes added before.
127 public void clear() {
128 attrByName.clear() ;
129 attributes.clear() ;
132 /***************************************
133 * XAttributeList methods
134 ****************************************/
136 public short getLength() {
137 if (log != null)
138 log.println("getLength() called -> " + attributes.size()) ;
139 return (short) attributes.size() ;
142 public String getNameByIndex(short idx) {
143 String name = ((Attribute) attributes.get(idx)).Name ;
144 if (log != null)
145 log.println("getNameByIndex(" + idx + ") called -> '" +
146 name + "'") ;
147 return name ;
150 public String getTypeByIndex(short idx) {
151 String type = ((Attribute) attributes.get(idx)).Type ;
152 if (log != null)
153 log.println("getTypeByIndex(" + idx + ") called -> '" +
154 type + "'") ;
155 return type;
158 public String getTypeByName(String name) {
159 String type = ((Attribute) attrByName.get(name)).Type ;
160 if (log != null)
161 log.println("getTypeByName('" + name + "') called -> '" +
162 type + "'") ;
163 return type;
165 public String getValueByIndex(short idx) {
166 String value = ((Attribute) attributes.get(idx)).Value ;
167 if (log != null)
168 log.println("getValueByIndex(" + idx + ") called -> '" +
169 value + "'") ;
170 return value;
173 public String getValueByName(String name) {
174 String value = ((Attribute) attrByName.get(name)).Value ;
175 if (log != null)
176 log.println("getValueByName('" + name + "') called -> '" +
177 value + "'") ;
178 return value;
183 * This class writes all XML data handled into a stream specified
184 * in the constructor.
186 public static class XMLWriter implements XDocumentHandler {
187 private PrintWriter _log = null ;
188 private String align = "" ;
191 * Creates a SAX handler which writes all XML data
192 * handled into a <code>log</code> stream specified.
194 public XMLWriter(PrintWriter log) {
195 _log = log ;
199 * Creates a SAX handler which does nothing.
201 public XMLWriter() {
204 public void processingInstruction(String appl, String data) {
205 if (_log == null) return ;
206 _log.println(align + "<?" + appl + " " + data + "?>") ;
208 public void startDocument() {
209 if (_log == null) return ;
210 _log.println("START DOCUMENT:") ;
212 public void endDocument() {
213 if (_log == null) return ;
214 _log.println("END DOCUMENT:") ;
216 public void setDocumentLocator(XLocator loc) {
217 if (_log == null) return ;
218 _log.println("DOCUMENT LOCATOR: ('" + loc.getPublicId() +
219 "','" + loc.getSystemId() + "')") ;
221 public void startElement(String name, XAttributeList attr) {
222 if (_log == null) return ;
223 _log.print(align + "<" + name + " ") ;
224 if (attr != null) {
225 short attrLen = attr.getLength() ;
226 for (short i = 0; i < attrLen; i++) {
227 if (i != 0) _log.print(align + " ") ;
228 _log.print(attr.getNameByIndex(i) + "[" +
229 attr.getTypeByIndex(i) + "]=\"" +
230 attr.getValueByIndex(i) + "\"") ;
231 if (i+1 != attrLen) {
232 _log.println() ;
236 _log.println(">") ;
238 align += " " ;
241 public void endElement(String name) {
242 if (_log == null) return ;
243 align = align.substring(3) ;
244 _log.println(align + "</" + name + ">") ;
247 public void characters(String chars) {
248 if (_log == null) return ;
249 _log.println(align + chars) ;
251 public void ignorableWhitespace(String sp) {
252 if (_log == null) return ;
253 _log.println(sp) ;
258 * Checks if the XML structure is well formed (i.e. all tags opened must be
259 * closed and all tags opened inside a tag must be closed
260 * inside the same tag). It also checks parameters passed.
261 * If any collisions found appropriate error message is
262 * output into a stream specified. No XML data output, i.e.
263 * no output will be performed if no errors occur.<p>
264 * After document is completed there is a way to cehck
265 * if the XML data and structure was valid.
267 public static class XMLWellFormChecker extends XMLWriter {
268 protected boolean docStarted = false ;
269 protected boolean docEnded = false ;
270 protected Vector tagStack = new Vector() ;
271 protected boolean wellFormed = true ;
272 protected boolean noOtherErrors = true ;
273 protected PrintWriter log = null ;
274 protected boolean printXMLData = false ;
276 public XMLWellFormChecker(PrintWriter log) {
277 super() ;
278 this.log = log ;
281 public XMLWellFormChecker(PrintWriter log_, boolean printXMLData) {
282 super(printXMLData ? log_ : null) ;
283 this.printXMLData = printXMLData ;
284 this.log = log_ ;
288 * Reset all values. This is important e.g. for test of XFilter
289 * interface, where 'filter()' method istbstarted twice.
291 public void reset() {
292 docStarted = false ;
293 docEnded = false ;
294 tagStack = new Vector() ;
295 wellFormed = true ;
296 noOtherErrors = true ;
297 PrintWriter log = null ;
298 printXMLData = false ;
301 public void startDocument() {
302 super.startDocument();
304 if (docStarted) {
305 printError("Document is started twice.") ;
306 wellFormed = false ;
309 docStarted = true ;
311 public void endDocument() {
312 super.endDocument();
313 if (!docStarted) {
314 wellFormed = false ;
315 printError("Document ended but not started.") ;
317 docEnded = true ;
319 public void startElement(String name, XAttributeList attr) {
320 super.startElement(name, attr);
321 if (attr == null) {
322 printError("attribute list passed as parameter to startElement()"+
323 " method has null value for tag <" + name + ">") ;
324 noOtherErrors = false ;
326 tagStack.add(0, name) ;
328 public void endElement(String name) {
329 super.endElement(name);
330 if (wellFormed) {
331 if (tagStack.size() == 0) {
332 wellFormed = false ;
333 printError("No tags to close (bad closing tag </" + name + ">)") ;
334 } else {
335 String startTag = (String) tagStack.elementAt(0) ;
336 tagStack.remove(0) ;
337 if (!startTag.equals(name)) {
338 wellFormed = false ;
339 printError("Bad closing tag: </" + name +
340 ">; tag expected: </" + startTag + ">");
347 * Checks if there were no errors during document handling.
348 * I.e. startDocument() and endDocument() must be called,
349 * XML must be well formed, paramters must be valid.
351 public boolean isWellFormed() {
352 if (!docEnded) {
353 printError("Document was not ended.") ;
354 wellFormed = false ;
357 return wellFormed && noOtherErrors ;
361 * Prints error message and all tags where error occured inside.
362 * Also prints "Tag trace" in case if the full XML data isn't
363 * printed.
365 public void printError(String msg) {
366 log.println("!!! Error: " + msg) ;
367 if (printXMLData) return ;
368 log.println(" Tag trace :") ;
369 for (int i = 0; i < tagStack.size(); i++) {
370 String tag = (String) tagStack.elementAt(i) ;
371 log.println(" <" + tag + ">") ;
377 * Beside structure of XML this class also can check existence
378 * of tags, inner tags, and character data. After document
379 * completion there is a way to check if required tags and
380 * character data was found. If there any error occurs an
381 * appropriate message is output.
383 public static class XMLTagsChecker extends XMLWellFormChecker {
384 protected Hashtable tags = new Hashtable() ;
385 protected Hashtable chars = new Hashtable() ;
386 protected boolean allOK = true ;
388 public XMLTagsChecker(PrintWriter log) {
389 super(log) ;
393 * Adds a tag name which must be contained in the XML data.
395 public void addTag(String tag) {
396 tags.put(tag, "") ;
399 * Adds a tag name which must be contained in the XML data and
400 * must be inside the tag with name <code>outerTag</code>.
402 public void addTagEnclosed(String tag, String outerTag) {
403 tags.put(tag, outerTag) ;
406 * Adds a character data which must be contained in the XML data.
408 public void addCharacters(String ch) {
409 chars.put(ch, "") ;
412 * Adds a character data which must be contained in the XML data and
413 * must be inside the tag with name <code>outerTag</code>.
415 public void addCharactersEnclosed(String ch, String outerTag) {
416 chars.put(ch, outerTag) ;
419 public void startElement(String name, XAttributeList attrs) {
420 super.startElement(name, attrs) ;
421 if (tags.containsKey(name)) {
422 String outerTag = (String) tags.get(name);
423 if (!outerTag.equals("")) {
424 boolean isInTag = false ;
425 for (int i = 0; i < tagStack.size(); i++) {
426 if (outerTag.equals((String) tagStack.elementAt(i))) {
427 isInTag = true ;
428 break ;
431 if (!isInTag) {
432 printError("Required tag <" + name + "> found, but is not enclosed in tag <" +
433 outerTag + ">") ;
434 allOK = false ;
437 tags.remove(name) ;
441 public void characters(String ch) {
442 super.characters(ch) ;
444 if (chars.containsKey(ch)) {
445 String outerTag = (String) chars.get(ch);
446 if (!outerTag.equals("")) {
447 boolean isInTag = false ;
448 for (int i = 0; i < tagStack.size(); i++) {
449 if (outerTag.equals((String) tagStack.elementAt(i))) {
450 isInTag = true ;
451 break ;
454 if (!isInTag) {
455 printError("Required characters '" + ch + "' found, but are not enclosed in tag <" +
456 outerTag + ">") ;
457 allOK = false ;
460 chars.remove(ch) ;
465 * Checks if the XML data was valid and well formed and if
466 * all necessary tags and character data was found.
468 public boolean checkTags() {
469 allOK &= isWellFormed() ;
471 Enumeration badTags = tags.keys() ;
472 Enumeration badChars = chars.keys() ;
474 if (badTags.hasMoreElements()) {
475 allOK = false ;
476 log.println("Required tags were not found in export :") ;
477 while(badTags.hasMoreElements()) {
478 log.println(" <" + ((String) badTags.nextElement()) + ">") ;
481 if (badChars.hasMoreElements()) {
482 allOK = false ;
483 log.println("Required characters were not found in export :") ;
484 while(badChars.hasMoreElements()) {
485 log.println(" <" + ((String) badChars.nextElement()) + ">") ;
488 reset();
489 return allOK ;
494 * Represents an XML tag which must be found in XML data written.
495 * This tag can contain only its name or tag name and attribute
496 * name, or attribute value additionally.
498 public static class Tag {
499 private String name = null;
500 private String[][] attrList = new String[0][3] ;
503 * Creates tag which has only a name. Attributes don't make sense.
504 * @param tagName The name of the tag.
506 public Tag(String tagName) {
507 name = tagName ;
511 * Creates a tag with the name specified, which must have an
512 * attribute with name specified. The value of this attribute
513 * doesn't make sense.
514 * @param tagName The name of the tag.
515 * @param attrName The name of attribute which must be contained
516 * in the tag.
518 public Tag(String tagName, String attrName) {
519 name = tagName ;
520 attrList = new String[1][3] ;
521 attrList[0][0] = attrName ;
525 * Creates a tag with the name specified, which must have an
526 * attribute with the value specified. The type of value
527 * assumed to be 'CDATA'.
528 * @param tagName The name of the tag.
529 * @param attrName The name of attribute which must be contained
530 * in the tag.
531 * @param attrValue Attribute value.
533 public Tag(String tagName, String attrName, String attrValue) {
534 name = tagName ;
535 attrList = new String[1][3] ;
536 attrList[0][0] = attrName ;
537 attrList[0][1] = "CDATA" ;
538 attrList[0][2] = attrValue ;
542 * Creates a tag with the name specified, which must have
543 * attributes specified. The value of thesee attributes
544 * doesn't make sense.
545 * @param tagName The name of the tag.
546 * @param attrNames Array with names of attributes which must
547 * be contained in the tag.
549 public Tag(String tagName, String[] attrNames) {
550 name = tagName ;
551 attrList = new String[attrNames.length][3] ;
552 for (int i = 0; i < attrNames.length; i++) {
553 attrList[i][0] = attrNames[i] ;
558 * Creates a tag with the name specified, which must have an
559 * attributes with their values specified. The type of all values
560 * assumed to be 'CDATA'.
561 * @param tagName The name of the tag.
562 * @param attrValues An array with attribute names and their values.
563 * <code>attrValues[N][0]</code> element contains the name of Nth
564 * attribute, and <code>attrValues[N][1]</code> element contains
565 * value of Nth attribute, if value is <code>null</code> then the
566 * attribute value can be any.
568 public Tag(String tagName, String[][] attrValues) {
569 name = tagName ;
570 attrList = new String[attrValues.length][3] ;
571 for (int i = 0; i < attrValues.length; i++) {
572 attrList[i][0] = attrValues[i][0] ;
573 attrList[i][1] = "CDATA" ;
574 attrList[i][2] = attrValues[i][1] ;
579 * Gets tag String description.
581 public String toString() {
582 String ret = "<" + name ;
583 for (int i = 0; i < attrList.length; i++) {
584 ret += " " + attrList[i][0] + "=";
585 if (attrList[i][2] == null) {
586 ret += "(not specified)";
587 } else {
588 ret += "\"" + attrList[i][2] + "\"";
591 ret += ">";
593 return ret ;
596 protected boolean checkAttr(int attrListIdx, XAttributeList list) {
597 short j = 0 ;
598 int listLen = list.getLength();
599 while(j < listLen) {
600 if (attrList[attrListIdx][0].equals(list.getNameByIndex(j))) {
601 if (attrList[attrListIdx][2] == null) return true ;
602 return attrList[attrListIdx][2].equals(list.getValueByIndex(j)) ;
604 j++ ;
606 return false ;
610 * Checks if this tag matches tag passed in parameters.
611 * I.e. if tag specifies only it's name it mathes if names
612 * are equal (attributes don't make sense). If there are
613 * some attributes names specified in this tag method checks
614 * if all names present in attribute list <code>list</code>
615 * (attributes' values don't make sense). If attributes specified
616 * with values method checks if these attributes exist and
617 * have appropriate values.
619 public boolean isMatchTo(String tagName, XAttributeList list) {
620 if (!name.equals(tagName)) return false;
621 boolean result = true ;
622 for (int i = 0; i < attrList.length; i++) {
623 result &= checkAttr(i, list) ;
625 return result ;
630 * Class realises extended XML data checking. It has possibilities
631 * to check if a tag exists, if it has some attributes with
632 * values, and if this tag is contained in another tag (which
633 * also can specify any attributes). It can check if some
634 * character data exists inside any tag specified.
636 public static class XMLChecker extends XMLWellFormChecker {
637 protected HashSet tagSet = new HashSet() ;
638 protected Vector tags = new Vector() ;
639 protected Vector chars = new Vector() ;
640 protected Vector tagStack = new Vector() ;
641 protected Vector attrStack = new Vector() ;
643 public XMLChecker(PrintWriter log, boolean writeXML) {
644 super(log, writeXML) ;
647 public void addTag(Tag tag) {
648 tags.add(new Tag[] {tag, null}) ;
649 tagSet.add(tag.name) ;
652 public void addTagEnclosed(Tag tag, Tag outerTag) {
653 tags.add(new Tag[] {tag, outerTag}) ;
654 tagSet.add(tag.name) ;
657 public void addCharacters(String ch) {
658 chars.add(new Object[] {ch.trim(), null}) ;
661 public void addCharactersEnclosed(String ch, Tag outerTag) {
662 chars.add(new Object[] {ch.trim(), outerTag}) ;
665 public void startElement(String name, XAttributeList attr) {
666 try {
667 super.startElement(name, attr);
669 if (tagSet.contains(name)) {
670 for (int i = 0; i < tags.size(); i++) {
671 Tag[] tag = (Tag[]) tags.elementAt(i);
672 if (tag[0].isMatchTo(name, attr)) {
673 if (tag[1] == null) {
674 tags.remove(i--);
675 } else {
676 boolean isInStack = false ;
677 for (int j = 0; j < tagStack.size(); j++) {
678 if (tag[1].isMatchTo((String) tagStack.elementAt(j),
679 (XAttributeList) attrStack.elementAt(j))) {
681 isInStack = true ;
682 break ;
685 if (isInStack) {
686 tags.remove(i--) ;
693 tagStack.add(0, name) ;
694 attrStack.add(0, new AttributeList(attr));
695 } catch (Exception e) {
696 e.printStackTrace(log);
700 public void characters(String ch) {
701 super.characters(ch) ;
702 for (int i = 0; i < chars.size(); i++) {
703 Object[] chr = (Object[]) chars.elementAt(i);
704 if (((String) chr[0]).equals(ch)) {
705 if (chr[1] == null) {
706 chars.remove(i--);
707 } else {
708 boolean isInStack = false ;
709 for (int j = 0; j < tagStack.size(); j++) {
710 if (((Tag) chr[1]).isMatchTo((String) tagStack.elementAt(j),
711 (XAttributeList) attrStack.elementAt(j))) {
713 isInStack = true ;
714 break ;
717 if (isInStack) {
718 chars.remove(i--) ;
725 public void endElement(String name) {
726 try {
727 super.endElement(name);
729 if (tagStack.size() > 0) {
730 tagStack.remove(0) ;
731 attrStack.remove(0) ;
733 } catch(Exception e) {
734 e.printStackTrace(log) ;
738 public boolean check() {
739 if (tags.size()> 0) {
740 log.println("!!! Error: Some tags were not found :") ;
741 for (int i = 0; i < tags.size(); i++) {
742 Tag[] tag = (Tag[]) tags.elementAt(i) ;
743 log.println(" Tag " + tag[0] + " was not found");
744 if (tag[1] != null)
745 log.println(" inside tag " + tag[1]) ;
748 if (chars.size() > 0) {
749 log.println("!!! Error: Some character data blocks were not found :") ;
750 for (int i = 0; i < chars.size(); i++) {
751 Object[] ch = (Object[]) chars.elementAt(i) ;
752 log.println(" Character data \"" + ch[0] + "\" was not found ") ;
753 if (ch[1] != null)
754 log.println(" inside tag " + ch[1]) ;
758 if (!isWellFormed())
759 log.println("!!! Some errors were found in XML structure") ;
761 boolean result = tags.size() == 0 && chars.size() == 0 && isWellFormed();
762 reset();
763 return result;
768 * Creates <code>XDocumentHandler</code> implementation in form
769 * of <code>com.sun.star.xml.sax.Writer</code> service, which
770 * writes XML data into a <code>com.sun.star.io.Pipe</code>
771 * created.
772 * @return Single element array which contains the handler
773 * contained in <code>Any</code> structure.
775 public static Object[] getDocumentHandler(XMultiServiceFactory xMSF) {
776 Object[] ret = new Object[1];
777 try {
778 XInterface Writer = (XInterface) xMSF.createInstance(
779 "com.sun.star.xml.sax.Writer");
780 XInterface oPipe = (XInterface) xMSF.createInstance
781 ( "com.sun.star.io.Pipe" );
782 XOutputStream xPipeOutput = (XOutputStream) UnoRuntime.
783 queryInterface(XOutputStream.class, oPipe) ;
785 XActiveDataSource xADS = (XActiveDataSource)
786 UnoRuntime.queryInterface(XActiveDataSource.class,Writer);
787 xADS.setOutputStream(xPipeOutput);
788 XDocumentHandler handler = (XDocumentHandler)
789 UnoRuntime.queryInterface(XDocumentHandler.class,Writer);
791 Any arg = new Any(new Type(XDocumentHandler.class),handler);
793 ret[0] = arg;
794 } catch (com.sun.star.uno.Exception e) {
795 e.printStackTrace();
797 return ret;
800 public static PropertyValue[] createMediaDescriptor(String[] propNames, Object[] values) {
801 PropertyValue[] props = new PropertyValue[propNames.length] ;
803 for (int i = 0; i < props.length; i++) {
804 props[i] = new PropertyValue() ;
805 props[i].Name = propNames[i] ;
806 if (values != null && i < values.length) {
807 props[i].Value = values[i] ;
811 return props ;
815 * Gets the hanlder, which writes all the XML data passed to the
816 * file specified.
817 * @param xMSF Soffice <code>ServiceManager</code> factory.
818 * @param fileURL The file URL (in form file:///<path>) to which
819 * XML data is written.
820 * @return SAX handler to which XML data has to be written.
822 public static XDocumentHandler getFileXMLWriter(XMultiServiceFactory xMSF, String fileURL)
823 throws com.sun.star.uno.Exception
825 XInterface oFacc = (XInterface)xMSF.createInstance(
826 "com.sun.star.comp.ucb.SimpleFileAccess");
827 XSimpleFileAccess xFacc = (XSimpleFileAccess)UnoRuntime.queryInterface
828 (XSimpleFileAccess.class, oFacc) ;
830 XInterface oWriter = (XInterface)xMSF.createInstance(
831 "com.sun.star.xml.sax.Writer");
832 XActiveDataSource xWriterDS = (XActiveDataSource)
833 UnoRuntime.queryInterface(XActiveDataSource.class, oWriter);
834 XDocumentHandler xDocHandWriter = (XDocumentHandler) UnoRuntime.queryInterface
835 (XDocumentHandler.class, oWriter) ;
837 if (xFacc.exists(fileURL))
838 xFacc.kill(fileURL);
839 XOutputStream fOut = xFacc.openFileWrite(fileURL) ;
840 xWriterDS.setOutputStream(fOut);
842 return xDocHandWriter ;
846 * Parses XML file and passes its data to the SAX handler specified.
847 * @param xMSF Soffice <code>ServiceManager</code> factory.
848 * @param fileURL XML file name (in form file:///<path>) to be parsed.
849 * @param handler SAX handler to which XML data from file will
850 * be transferred.
852 public static void parseXMLFile(XMultiServiceFactory xMSF,
853 String fileURL, XDocumentHandler handler) throws com.sun.star.uno.Exception
855 XInterface oFacc = (XInterface)xMSF.createInstance(
856 "com.sun.star.comp.ucb.SimpleFileAccess");
857 XSimpleFileAccess xFacc = (XSimpleFileAccess)UnoRuntime.queryInterface
858 (XSimpleFileAccess.class, oFacc) ;
859 XInputStream oIn = xFacc.openFileRead(fileURL) ;
861 XInterface oParser = (XInterface)xMSF.createInstance(
862 "com.sun.star.xml.sax.Parser");
863 XParser xParser = (XParser) UnoRuntime.queryInterface(XParser.class, oParser);
865 xParser.setDocumentHandler(handler) ;
866 InputSource inSrc = new InputSource() ;
867 inSrc.aInputStream = oIn ;
868 xParser.parseStream(inSrc) ;
870 oIn.closeInput();
874 * Exports document (the whole or a part) into the file specified
875 * in XML format.
876 * @param xMSF Soffice <code>ServiceManager</code> factory.
877 * @param xDoc Document to be exported.
878 * @param docType Type of document (for example 'Calc', 'Writer', 'Draw')
879 * The type must start with <b>capital</b> letter.
880 * @param exportType The type of export specifies if the whole
881 * document will be exported or one of its parts (Meta info, Styles, etc.).
882 * The following types supported (it also depends of document type) :
883 * "" (empty string) - for the whole document ;
884 * "Content" - only content exported ;
885 * "Meta" - meta document info exported ;
886 * "Settings" - view settings of document exported ;
887 * "Styles" - document styles exported ;
888 * @param fileURL XML file name (in form file:///<path>) to be exported to.
890 public static void exportDocument(XMultiServiceFactory xMSF, XComponent xDoc,
891 String docType, String exportType, String fileURL)
892 throws com.sun.star.uno.Exception {
894 XDocumentHandler xDocHandWriter = XMLTools.getFileXMLWriter(xMSF, fileURL) ;
896 Any arg = new Any(new Type(XDocumentHandler.class), xDocHandWriter);
897 XInterface oExp = (XInterface)xMSF.createInstanceWithArguments(
898 "com.sun.star.comp." + docType + ".XML" + exportType + "Exporter",
899 new Object[] {arg});
901 XExporter xExp = (XExporter) UnoRuntime.queryInterface
902 (XExporter.class, oExp) ;
903 xExp.setSourceDocument(xDoc) ;
905 XFilter filter = (XFilter) UnoRuntime.queryInterface(XFilter.class, oExp) ;
906 filter.filter(XMLTools.createMediaDescriptor(
907 new String[] {"FilterName"},
908 new Object[] {"Custom filter"})) ;
912 * Imports document (the whole or a part) from the file specified
913 * in XML format.
914 * @param xMSF Soffice <code>ServiceManager</code> factory.
915 * @param xDoc Target document to be imported.
916 * @param docType Type of document (for example 'Calc', 'Writer', 'Draw')
917 * The type must start with <b>capital</b> letter.
918 * @param exportType The type of export specifies if the whole
919 * document will be exported or one of its parts (Meta info, Styles, etc.).
920 * The following types supported (it hardly depends of XML data in file) :
921 * "" (empty string) - for the whole document ;
922 * "Content" - only content exported ;
923 * "Meta" - meta document info exported ;
924 * "Settings" - view settings of document exported ;
925 * "Styles" - document styles exported ;
926 * @param fileURL XML file name (in form file:///<path>) to be imported from.
928 public static void importDocument(XMultiServiceFactory xMSF, XComponent xDoc,
929 String docType, String importType, String fileURL)
930 throws com.sun.star.uno.Exception {
932 XInterface oImp = (XInterface)xMSF.createInstance(
933 "com.sun.star.comp." + docType + ".XML" + importType + "Importer");
934 XImporter xImp = (XImporter) UnoRuntime.queryInterface
935 (XImporter.class, oImp) ;
936 XDocumentHandler xDocHandImp = (XDocumentHandler) UnoRuntime.queryInterface
937 (XDocumentHandler.class, oImp) ;
939 xImp.setTargetDocument(xDoc) ;
940 parseXMLFile(xMSF, fileURL, xDocHandImp) ;