merge the formfield patch from ooo-build
[ooovba.git] / transex3 / source / filter / utils / XMLHelper.java
blob795b71db3194af569b8ae909472450be45adc71d
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: XMLHelper.java,v $
10 * $Revision: 1.7 $
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 com.sun.star.filter.config.tools.utils;
33 //_______________________________________________
35 import java.lang.*;
36 import java.util.*;
37 import java.io.*;
39 //_______________________________________________
41 /**
42 * It provides some constant values and some static helper routines
43 * which are neccessary to work with a xml file - especialy
44 * the filter configuration.
48 public class XMLHelper
50 //___________________________________________
51 // public const
53 /** its a possible value of the xml attribute "oor:type" and identify an integer type. */
54 public static final java.lang.String XMLTYPE_INTEGER = "xs:int";
56 /** its a possible value of the xml attribute "oor:type" and identify an boolean type. */
57 public static final java.lang.String XMLTYPE_BOOLEAN = "xs:boolean";
59 /** its a possible value of the xml attribute "oor:type" and identify an string type. */
60 public static final java.lang.String XMLTYPE_STRING = "xs:string";
62 /** its a possible value of the xml attribute "oor:type" and identify an string list type. */
63 public static final java.lang.String XMLTYPE_STRINGLIST = "oor:string-list";
65 /** its a xml attribute, which specify a property name. */
66 public static final java.lang.String XMLATTRIB_OOR_NAME = "oor:name";
68 /** its a xml attribute, which specify a property type. */
69 public static final java.lang.String XMLATTRIB_OOR_TYPE = "oor:type";
71 /** its a xml attribute, which specify a list separator. */
72 public static final java.lang.String XMLATTRIB_OOR_SEPARATOR = "oor:separator";
74 /** its a xml attribute, which specify a localized value. */
75 public static final java.lang.String XMLATTRIB_OOR_LOCALIZED = "oor:localized";
77 /** its a xml attribute, which specify a merge operation for cfg layering. */
78 public static final java.lang.String XMLATTRIB_OOR_OP = "oor:op";
80 /** can be used as value for XMLATTRIB_OOR_OP. */
81 public static final java.lang.String XMLATTRIB_OP_REPLACE = "replace";
83 /** its a xml attribute, which specify a locale value. */
84 public static final java.lang.String XMLATTRIB_XML_LANG = "xml:lang";
86 /** its the tag name of a <value ...> entry. */
87 public static final java.lang.String XMLTAG_VALUE = "value";
89 /** its the tag name of a <prop ...> entry. */
90 public static final java.lang.String XMLTAG_PROP = "prop";
92 /** its the tag name of a <node ...> entry. */
93 public static final java.lang.String XMLTAG_NODE = "node";
95 //___________________________________________
96 // private const
98 /** a static list of all possible separators, which can be used for configuration type string-list. */
99 private static final java.lang.String[] DELIMS = {" ", ",", ";", ".", ":", "-", "_", "#", "'", "+", "*", "~", "=", "?"};
101 /** index of the default separator inside list DELIMS.
102 * Its neccessary to know such default separator; because it can
103 * be supressed as xml attribute of the corresponding value tag. */
104 private static final int DEFAULT_SEPARATOR = 0;
106 //___________________________________________
108 /** analyze the structures of the given XML node and
109 * return a property set of all found sub nodes.
111 * Such properties are organized as [name, value] pairs.
112 * The type of a xml node will be detected automaticly.
113 * Following types are supported:
114 * xs:int => java.lang.Integer
115 * xs:bool => java.lang.Boolean
116 * xs:string => java.lang.String
117 * oor:string-list => java.util.LinkedList[java.lang.String]
118 * oor:set => java.util.Vector[java.lang.Object]
119 * oor:localized => java.util.HashMap[java.lang.Object]
121 * @param aNode
122 * points directly to the xml node, where we should analyze
123 * the children nodes.
125 * @return [java.util.HashMap]
126 * contains every node name as key and its string(!) as value.
128 public static java.util.HashMap convertNodeToPropSet(org.w3c.dom.Node aNode)
129 throws java.lang.Exception
131 java.util.HashMap aPropSet = new java.util.HashMap();
133 // get all child nodes, which seems to be properties
134 java.util.Vector lChildNodes = XMLHelper.extractChildNodesByTagName(aNode, XMLTAG_PROP);
135 java.util.Enumeration en1 = lChildNodes.elements();
136 while(en1.hasMoreElements())
138 org.w3c.dom.Node aChildNode = (org.w3c.dom.Node)en1.nextElement();
140 // read its name
141 java.lang.String sChildName = XMLHelper.extractNodeAttribByName(aChildNode, XMLATTRIB_OOR_NAME);
142 if (sChildName == null)
143 throw new java.io.IOException("unsupported format: could not extract child node name");
145 // read its type info
146 java.lang.String sChildType = XMLHelper.extractNodeAttribByName(aChildNode, XMLATTRIB_OOR_TYPE);
147 if (sChildType == null)
149 /** Special patch:
150 * If an xml tag has no type information set ... we can restore it
151 * by analyzing the already readed tag name :-)
152 * Not very nice - but it can help to read stripped xml files too. */
153 sChildType = XMLHelper.getTypeForTag(sChildName);
154 if (sChildType == null)
155 throw new java.io.IOException("unsupported format: could not extract child node type");
158 // read its value(s?)
159 java.util.Vector lChildValues = XMLHelper.extractChildNodesByTagName(aChildNode, XMLTAG_VALUE);
160 java.util.Enumeration en2 = lChildValues.elements();
161 int nValue = 0;
162 java.lang.Object aValue = null;
163 while(en2.hasMoreElements())
165 org.w3c.dom.Node aValueNode = (org.w3c.dom.Node)en2.nextElement();
166 java.lang.String sChildLocale = XMLHelper.extractNodeAttribByName(aValueNode, XMLATTRIB_XML_LANG);
167 boolean bLocalized = (sChildLocale != null);
169 ++nValue;
171 if (sChildType.equals(XMLTYPE_INTEGER))
173 if (!bLocalized && nValue > 1)
174 throw new java.io.IOException("unsupported format: more then one value for non localized but atomic type detected");
175 java.lang.String sData = ((org.w3c.dom.CharacterData)aValueNode.getFirstChild()).getData();
176 aValue = new java.lang.Integer(sData);
178 else
179 if (sChildType.equals(XMLTYPE_BOOLEAN))
181 if (!bLocalized && nValue > 1)
182 throw new java.io.IOException("unsupported format: more then one value for non localized but atomic type detected");
183 java.lang.String sData = ((org.w3c.dom.CharacterData)aValueNode.getFirstChild()).getData();
184 aValue = new java.lang.Boolean(sData);
186 else
187 if (sChildType.equals(XMLTYPE_STRING))
189 if (!bLocalized && nValue > 1)
190 throw new java.io.IOException("unsupported format: more then one value for non localized but atomic type detected");
192 java.lang.String sData = ((org.w3c.dom.CharacterData)aValueNode.getFirstChild()).getData();
194 java.util.HashMap lLocalized = null;
195 if (bLocalized)
197 if (aValue == null)
198 aValue = new java.util.HashMap();
199 lLocalized = (java.util.HashMap)aValue;
200 lLocalized.put(sChildLocale, sData);
202 else
203 aValue = sData;
205 else
206 if (sChildType.equals(XMLTYPE_STRINGLIST))
208 if (!bLocalized && nValue > 1)
209 throw new java.io.IOException("unsupported format: more then one value for non localized but atomic type detected");
211 java.lang.String sSeparator = XMLHelper.extractNodeAttribByName(aChildNode, XMLATTRIB_OOR_SEPARATOR);
212 if (sSeparator == null)
213 sSeparator = " ";
215 java.lang.String sData = ((org.w3c.dom.CharacterData)aValueNode.getFirstChild()).getData();
216 sData = sData.replace('\t', ' ');
217 sData = sData.replace('\n', ' ');
218 java.util.StringTokenizer aTokenizer = new java.util.StringTokenizer(sData, sSeparator);
219 java.util.Vector lList = new java.util.Vector();
220 while(aTokenizer.hasMoreTokens())
222 java.lang.String sToken = (java.lang.String)aTokenizer.nextToken();
223 sToken.trim();
224 if (sToken.length() < 1)
225 continue;
226 lList.add(sToken);
228 aValue = lList;
231 aPropSet.put(sChildName, aValue);
235 return aPropSet;
238 //___________________________________________
240 private static java.lang.String getTypeForTag(java.lang.String sTag)
242 java.lang.String sType = null;
244 if (
245 (sTag.equals(Cache.PROPNAME_DATA )) ||
246 (sTag.equals(Cache.PROPNAME_NAME )) ||
247 (sTag.equals(Cache.PROPNAME_UINAME )) ||
248 (sTag.equals(Cache.PROPNAME_MEDIATYPE )) ||
249 (sTag.equals(Cache.PROPNAME_CLIPBOARDFORMAT )) ||
250 (sTag.equals(Cache.PROPNAME_PREFERREDFILTER )) ||
251 (sTag.equals(Cache.PROPNAME_DETECTSERVICE )) ||
252 (sTag.equals(Cache.PROPNAME_FRAMELOADER )) ||
253 (sTag.equals(Cache.PROPNAME_CONTENTHANDLER )) ||
254 (sTag.equals(Cache.PROPNAME_DOCUMENTSERVICE )) ||
255 (sTag.equals(Cache.PROPNAME_FILTERSERVICE )) ||
256 (sTag.equals(Cache.PROPNAME_TEMPLATENAME )) ||
257 (sTag.equals(Cache.PROPNAME_TYPE )) ||
258 (sTag.equals(Cache.PROPNAME_UICOMPONENT ))
260 sType = XMLTYPE_STRING;
261 else
262 if (
263 (sTag.equals(Cache.PROPNAME_PREFERRED )) ||
264 (sTag.equals("Installed" ))
266 sType = XMLTYPE_BOOLEAN;
267 else
268 if (
269 (sTag.equals(Cache.PROPNAME_UIORDER )) ||
270 (sTag.equals(Cache.PROPNAME_DOCUMENTICONID )) ||
271 (sTag.equals(Cache.PROPNAME_FILEFORMATVERSION))
273 sType = XMLTYPE_INTEGER;
274 else
275 if (
276 (sTag.equals(Cache.PROPNAME_URLPATTERN )) ||
277 (sTag.equals(Cache.PROPNAME_EXTENSIONS )) ||
278 (sTag.equals(Cache.PROPNAME_USERDATA )) ||
279 (sTag.equals(Cache.PROPNAME_FLAGS )) ||
280 (sTag.equals(Cache.PROPNAME_TYPES ))
282 sType = XMLTYPE_STRINGLIST;
284 if (sType == null)
285 System.err.println("getTypeForTag("+sTag+") = "+sType);
287 return sType;
290 //___________________________________________
292 /** return a xml representation of the given property set.
294 * @param aPropSet
295 * a set of <name,value> pairs, which should be translated to xml
297 * @return [java.lang.String]
298 * the xml string representation.
300 * @throws [java.lang.Exception]
301 * if anything during convertion fill fail.
303 public static java.lang.String convertPropSetToXML(java.util.HashMap aPropSet ,
304 int nPrettyTabs)
305 throws java.lang.Exception
307 java.lang.StringBuffer sXML = new java.lang.StringBuffer(256);
309 java.util.Iterator it1 = aPropSet.keySet().iterator();
310 while(it1.hasNext())
312 java.lang.String sProp = (java.lang.String)it1.next();
313 java.lang.Object aVal = aPropSet.get(sProp);
315 sProp = encodeHTMLSigns(sProp);
317 // is it a simple type?
318 if (
319 (aVal instanceof java.lang.Integer) ||
320 (aVal instanceof java.lang.Boolean) ||
321 (aVal instanceof java.lang.String )
324 sXML.append(XMLHelper.convertSimpleObjectToXML(sProp, aVal, nPrettyTabs));
325 continue;
328 // no!
329 // is it a list value?
330 if (aVal instanceof java.util.Vector)
332 java.util.Vector lVal = (java.util.Vector)aVal;
333 sXML.append(XMLHelper.convertListToXML(sProp, lVal, nPrettyTabs));
334 continue;
337 // its a localized value?
338 if (aVal instanceof java.util.HashMap)
340 java.util.HashMap lVal = (java.util.HashMap)aVal;
341 sXML.append(XMLHelper.convertLocalizedValueToXML(sProp, lVal, nPrettyTabs));
342 continue;
345 // unknown type!
346 java.lang.StringBuffer sMsg = new java.lang.StringBuffer(256);
347 sMsg.append("unsupported object type detected.");
348 sMsg.append("\ttype ? : \""+sProp+"\" = "+aVal);
349 sMsg.append("\tprop set: \""+aPropSet );
350 throw new java.lang.Exception(sMsg.toString());
353 return sXML.toString();
356 public static java.lang.String encodeHTMLSigns(java.lang.String sValue)
358 java.lang.StringBuffer sSource = new java.lang.StringBuffer(sValue);
359 java.lang.StringBuffer sDestination = new java.lang.StringBuffer(1000 );
361 for (int i=0; i<sSource.length(); ++i)
363 char c = sSource.charAt(i);
364 if (c == '&')
365 sDestination.append("&amp;");
366 else
367 sDestination.append(c);
370 java.lang.String sReturn = sDestination.toString();
371 if (!sReturn.equals(sValue))
372 System.out.println("encode \""+sValue+"\" => \""+sReturn+"\"");
374 return sReturn;
377 //___________________________________________
379 /** return a xml representation of an atomic property.
381 * Atomic property types are e.g. Integer, Boolean, String.
383 * @param sName
384 * the name of the property.
386 * @param aValue
387 * the value of the property.
389 * @param nPrettyTabs
390 * count of tab signs for pretty format the xml code :-)
392 * @return [java.lang.String]
393 * the xml string representation.
395 * @throws [java.lang.Exception]
396 * if anything during convertion fill fail.
398 private static java.lang.String convertSimpleObjectToXML(java.lang.String sName ,
399 java.lang.Object aValue ,
400 int nPrettyTabs)
401 throws java.lang.Exception
403 java.lang.StringBuffer sXML = new java.lang.StringBuffer(256);
404 for (int t=0; t<nPrettyTabs; ++t)
405 sXML.append("\t");
407 if (aValue instanceof java.lang.Integer)
409 sXML.append("<prop "+XMLATTRIB_OOR_NAME+"=\""+sName+"\">");
410 sXML.append("<value>"+aValue.toString()+"</value>");
411 sXML.append("</prop>\n");
413 else
414 if (aValue instanceof java.lang.Boolean)
416 sXML.append("<prop "+XMLATTRIB_OOR_NAME+"=\""+sName+"\">");
417 sXML.append("<value>"+aValue.toString()+"</value>");
418 sXML.append("</prop>\n");
420 else
421 if (aValue instanceof java.lang.String)
423 sXML.append("<prop "+XMLATTRIB_OOR_NAME+"=\""+sName+"\"");
424 java.lang.String sValue = (java.lang.String)aValue;
426 sValue = encodeHTMLSigns(sValue);
428 if (sValue.length() < 1)
429 sXML.append("/>\n");
430 else
432 sXML.append("><value>"+sValue+"</value>");
433 sXML.append("</prop>\n");
436 else
438 System.err.println("name = "+sName);
439 System.err.println("value = "+aValue);
440 // ! can be used outside to detect - that it was not a simple type :-)
441 throw new java.lang.Exception("not an atomic type.");
444 return sXML.toString();
447 //___________________________________________
449 /** return a xml representation of a string-list property.
451 * @param sName
452 * the name of the property.
454 * @param aValue
455 * the value of the property.
457 * @param nPrettyTabs
458 * count of tab signs for pretty format the xml code :-)
460 * @return [java.lang.String]
461 * the xml string representation.
463 * @throws [java.lang.Exception]
464 * if anything during convertion fill fail.
466 private static java.lang.String convertListToXML(java.lang.String sName ,
467 java.util.Vector aValue ,
468 int nPrettyTabs)
469 throws java.lang.Exception
471 java.lang.StringBuffer sXML = new java.lang.StringBuffer(256);
473 for (int t=0; t<nPrettyTabs; ++t)
474 sXML.append("\t");
476 int c = aValue.size();
477 if (c < 1)
479 sXML.append("<prop "+XMLATTRIB_OOR_NAME+"=\""+sName+"\"/>\n");
480 return sXML.toString();
483 // step over all list items and add it to a string buffer
484 // Every item will be separated by a default separator "\n" first.
485 // Because "\n" is not a valid separator at all and can`t occure inside
486 // our list items. During we step over all items, we check if our current separator
487 // (we use a list of possible ones!) clash with an item.
488 // If it clash - we step to the next possible separator.
489 // If our list of possible separator values runs out of range we throw
490 // an exception :-) Its better then generating of wrong values
491 // If we found a valid seperator - we use it to replace our "\n" place holder
492 // at the end of the following loop ...
494 int d = 0;
495 java.lang.StringBuffer sValBuff = new java.lang.StringBuffer(256);
496 for (int i=0; i<c; ++i)
498 // get the next list item
499 java.lang.Object aItem = aValue.get(i);
500 if (!(aItem instanceof java.lang.String))
501 throw new java.lang.Exception("Current implementation supports string-list only!");
503 java.lang.String sValue = (java.lang.String)aItem;
505 sValue = encodeHTMLSigns(sValue);
507 // append item with default separator, which isn a valid separator at all
508 // But supress adding of the separator if last element is reached.
509 sValBuff.append(sValue);
510 if (i<(c-1))
511 sValBuff.append("\n");
513 // check for delim clash
514 // Attention: An empty (means default) element forbid using
515 // of a whitespace character as separator!
516 while(true)
518 if (d >= DELIMS.length)
519 throw new java.lang.Exception("No valid separator found for a string list item.");
520 if (sValue.length() < 1 && DELIMS[d].equals(" "))
522 ++d;
523 continue;
525 if (sValue.indexOf(DELIMS[d]) != -1)
527 ++d;
528 continue;
530 break;
534 // replace default separator with right one
535 System.out.println("TODO: must be adapted to java 1.3 :-(");
536 System.exit(-1);
537 //TODO_JAVA java.lang.String sListVal = sValBuff.toString().replaceAll("\n", DELIMS[d]);
538 java.lang.String sListVal = null;
540 sXML.append("<prop "+XMLATTRIB_OOR_NAME+"=\""+sName+"\">");
541 if (d == DEFAULT_SEPARATOR)
542 sXML.append("<value>");
543 else
544 sXML.append("<value "+XMLATTRIB_OOR_SEPARATOR+"=\""+DELIMS[d]+"\">");
545 sXML.append(sListVal);
546 sXML.append("</value>");
547 sXML.append("</prop>\n");
549 return sXML.toString();
552 //___________________________________________
554 /** return a xml representation of a localized property.
556 * @param sName
557 * the name of the property.
559 * @param aValue
560 * the value of the property.
562 * @param nPrettyTabs
563 * count of tab signs for pretty format the xml code :-)
565 * @return [java.lang.String]
566 * the xml string representation.
568 * @throws [java.lang.Exception]
569 * if anything during convertion fill fail.
571 private static java.lang.String convertLocalizedValueToXML(java.lang.String sName ,
572 java.util.HashMap aValue ,
573 int nPrettyTabs)
574 throws java.lang.Exception
576 java.lang.StringBuffer sXML = new java.lang.StringBuffer(256);
578 int c = aValue.size();
579 if (c < 1)
580 throw new java.lang.Exception("Cant detect type of localized values. Because the given list is empty.");
582 for (int t=0; t<nPrettyTabs; ++t)
583 sXML.append("\t");
584 // !Our localized values must be formated at a deeper coloum
585 // then its property name!
586 ++nPrettyTabs;
588 sXML.append("<prop "+XMLATTRIB_OOR_NAME+"=\""+sName+"\">\n");
589 java.util.Iterator it = aValue.keySet().iterator();
590 // boolean bTypeKnown = false;
591 while(it.hasNext())
593 java.lang.String sLocale = (java.lang.String)it.next();
594 java.lang.Object aLocalizedValue = aValue.get(sLocale);
596 if (!bTypeKnown)
598 bTypeKnown = true;
599 if (aLocalizedValue instanceof java.lang.Integer)
600 sXML.append(" "+XMLATTRIB_OOR_TYPE+"=\""+XMLTYPE_INTEGER+"\">\n");
601 else
602 if (aLocalizedValue instanceof java.lang.Boolean)
603 sXML.append(" "+XMLATTRIB_OOR_TYPE+"=\""+XMLTYPE_BOOLEAN+"\">\n");
604 else
605 if (aLocalizedValue instanceof java.lang.String)
606 sXML.append(" "+XMLATTRIB_OOR_TYPE+"=\""+XMLTYPE_STRING+"\">\n");
607 else
608 throw new java.lang.Exception("Unsupported type for localized value detected.");
611 java.lang.String sLocValue = aLocalizedValue.toString();
612 java.lang.String sValue = encodeHTMLSigns(sLocValue);
614 for (int t=0; t<nPrettyTabs; ++t)
615 sXML.append("\t");
616 sXML.append("<value "+XMLATTRIB_XML_LANG+"=\""+sLocale+"\">"+sValue+"</value>\n");
618 --nPrettyTabs;
619 for (int t=0; t<nPrettyTabs; ++t)
620 sXML.append("\t");
621 sXML.append("</prop>\n");
623 return sXML.toString();
626 //___________________________________________
628 /** returns the value of an attribute of the given node.
630 * If the given node represent an lement node, may it supports some attributes.
631 * Then this method search for an attribute with the specified name and return it's value.
632 * If nothing could be found ... or the given node isn't a suitable node ... it returns null.
634 * @param aNode
635 * the node, which should be analyzed.
637 * @param sAttrib
638 * name of the attribute, which should be searched.
640 * @return The value of the specified attribute if it could be found at the given node.
641 * Can be null if node doesn't support attributes or the searched one does not exist there.
643 public static java.lang.String extractNodeAttribByName(org.w3c.dom.Node aNode ,
644 java.lang.String sAttrib)
645 throws java.lang.Exception
647 // We can get valid attributes for element nodes only!
648 if (aNode.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE)
650 // System.err.println("not an element node");
651 return null;
654 // may it supports attributes in general ... but doesn't have anyone realy.
655 org.w3c.dom.NamedNodeMap lAttribs = aNode.getAttributes();
656 if (lAttribs==null)
658 // System.err.println("no attributes at all");
659 return null;
662 // step over the attribute list and search for the requested one
663 for (int i=0; i<lAttribs.getLength(); ++i)
665 org.w3c.dom.Attr aAttrib = (org.w3c.dom.Attr)lAttribs.item(i);
666 if (aAttrib.getName().equals(sAttrib))
668 java.lang.String sValue = aAttrib.getValue();
669 return sValue;
673 // the searched attribute was not found!
674 // System.err.println("required attribute \""+sAttrib+"\" does not exist for node ["+aNode.toString()+"]");
675 return null;
678 //___________________________________________
680 /** returns a list of childs, which are ELEMENT_NODES and have the right tag name.
682 * It analyze the list of all possible child nodes. Only ELEMENT_NODES are candidates.
683 * All other ones will be ignored. Further these element nodes are compared by it's tag
684 * names. If it match with the specified value it's added to the return list.
685 * So the return list includes references to the DOM tree nodes only, which are child
686 * element nodes with the right tag name.
688 * @param aNode
689 * provides access to the child nodes, which should be analyzed
691 * @param sTag
692 * the searched tag name.
694 * @return A list of child nodes, which are element nodes and have the right tag name.
696 public static java.util.Vector extractChildNodesByTagName(org.w3c.dom.Node aNode,
697 java.lang.String sTag )
699 // extract first all ELEMENT_NODES of he given parent
700 // Such nodes only provide tag names.
701 java.util.Vector lChilds = XMLHelper.extractChildNodesByType(aNode,org.w3c.dom.Node.ELEMENT_NODE);
702 java.util.Vector lExtractedChilds = new java.util.Vector(lChilds.size());
704 // step over the list and search for the right tags using the specified name
705 java.util.Enumeration en = lChilds.elements();
706 while (en.hasMoreElements())
708 org.w3c.dom.Node aChild = (org.w3c.dom.Node)en.nextElement();
709 if (aChild.getNodeName().equals(sTag))
710 lExtractedChilds.add(aChild);
713 // pack(!) and return the list
714 lExtractedChilds.trimToSize();
715 return lExtractedChilds;
718 //___________________________________________
720 /** returns a list of childs, which supports the right node type.
722 * It analyze the list of all possible child nodes. If a node represent the right node type
723 * it is added to the return list. Otherwhise it will be ignored.
725 * @param aNode
726 * provides access to the list of possible children nodes.
728 * @param nType
729 * represent the searched node type.
730 * Possible values are constant fields of a org.w3c.dom.Node - e.g. org.w3c.dom.Node.ELEMENT_NODE.
732 * @return A list of child nodes, which provides the right node type.
734 public static java.util.Vector extractChildNodesByType(org.w3c.dom.Node aNode,
735 short nType)
737 // get list of all possibe childs and reserve enough space for our return list
738 // Attention: A null pointer is not allowed for return! (means lExtractedChilds)
739 org.w3c.dom.NodeList lChilds = aNode.getChildNodes();
740 int c = lChilds.getLength();
741 java.util.Vector lExtractedChilds = new java.util.Vector(c);
743 // step of these childs and select only needed ones
744 for (int i=0; i<c; ++i)
746 org.w3c.dom.Node aChild = lChilds.item(i);
747 if (aChild.getNodeType() == nType)
748 lExtractedChilds.add(aChild);
751 // pack(!) and return the list
752 lExtractedChilds.trimToSize();
753 return lExtractedChilds;
756 //___________________________________________
758 /** generates an xml header, using parameters.
760 * @param sVersion
761 * number of the xml version.
763 * @param sEncoding
764 * used file encoding.
766 * @param sPath
767 * name of the configuration root.
769 * @param sPackage
770 * name of the configuration package.
772 * @param bLanguagepack
773 * force creation of a special header,
774 * which is needed for language packs only.
776 * @return [java.lang.String]
777 * the generated xml header.
780 public static java.lang.String generateHeader(java.lang.String sVersion ,
781 java.lang.String sEncoding ,
782 java.lang.String sPath ,
783 java.lang.String sPackage ,
784 boolean bLanguagePack)
786 java.lang.StringBuffer sHeader = new java.lang.StringBuffer(256);
788 if (bLanguagePack)
790 sHeader.append("<?xml version=\"");
791 sHeader.append(sVersion);
792 sHeader.append("\" encoding=\"");
793 sHeader.append(sEncoding);
794 sHeader.append("\"?>\n");
795 sHeader.append("<oor:component-data oor:package=\"");
796 sHeader.append(sPath);
797 sHeader.append("\" oor:name=\"");
798 sHeader.append(sPackage);
799 sHeader.append("\" xmlns:install=\"http://openoffice.org/2004/installation\"");
800 sHeader.append(" xmlns:oor=\"http://openoffice.org/2001/registry\"");
801 sHeader.append(" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"");
802 sHeader.append(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n");
804 else
806 sHeader.append("<?xml version=\"");
807 sHeader.append(sVersion);
808 sHeader.append("\" encoding=\"");
809 sHeader.append(sEncoding);
810 sHeader.append("\"?>\n<!DOCTYPE oor:component-data SYSTEM \"../../../../component-update.dtd\">\n");
811 sHeader.append("<oor:component-data xmlns:oor=\"http://openoffice.org/2001/registry\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" oor:package=\"");
812 sHeader.append(sPath);
813 sHeader.append("\" oor:name=\"");
814 sHeader.append(sPackage);
815 sHeader.append("\">\n");
818 return sHeader.toString();
821 public static java.lang.String generateFooter()
823 return "</oor:component-data>\n";