Changeset 14053


Ignore:
Timestamp:
2007-05-03T09:44:55+12:00 (17 years ago)
Author:
xiao
Message:

add some new xml methods.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/util/XMLTools.java

    r13325 r14053  
    1111import org.xml.sax.*;
    1212
     13import java.io.FileReader;
     14import java.io.IOException;
     15import java.io.StringReader;
     16
     17// SAX
     18import org.xml.sax.XMLReader;
     19import org.xml.sax.SAXException;
     20import org.xml.sax.SAXParseException;
     21import org.xml.sax.helpers.DefaultHandler;
     22import org.xml.sax.InputSource;
     23
     24// JAXP
     25import javax.xml.parsers.FactoryConfigurationError;
     26import javax.xml.parsers.ParserConfigurationException;
     27import javax.xml.parsers.SAXParser;
     28import javax.xml.parsers.SAXParserFactory;
    1329
    1430/** This class is a static class containing useful XML functions */
    15 public class XMLTools
    16 {
     31public class XMLTools {
     32    /** extracts the text out of a node */
     33    public static Node getNodeTextNode (Element param) {
     34        param.normalize ();
     35        Node n = param.getFirstChild ();
     36        while (n!=null && n.getNodeType () !=Node.TEXT_NODE) {
     37            n=n.getNextSibling ();
     38        }
     39        return n;
     40    }
     41   
     42    /** extracts the text out of a node */
     43    public static String getNodeText (Element param) {
     44        Node text_node = getNodeTextNode (param);
     45        if (text_node == null) {
     46            return "";
     47        }
     48        return text_node.getNodeValue ();
     49    }
     50    public static void setNodeText (Element elem, String text) {
     51        Node old_text_node = getNodeTextNode (elem);
     52        if (old_text_node != null) {
     53            elem.removeChild (old_text_node);
     54        }
     55        Text t = elem.getOwnerDocument ().createTextNode (text);
     56        elem.appendChild (t);
     57    }
     58    /** returns the (first) child element with the given name */
     59    public static Node getChildByTagName (Node n, String name) {
     60       
     61        Node child = n.getFirstChild ();
     62        while (child!=null) {
     63            if (child.getNodeName ().equals (name)) {
     64                return child;
     65            }
     66            child = child.getNextSibling ();
     67        }
     68        return null; //not found
     69    }
     70   
     71    /** returns the (nth) child element with the given name
     72     * index numbers start at 0 */
     73    public static Node getChildByTagNameIndexed (Node n, String name, int index) {
     74        if (index == -1) {
     75            return getChildByTagName (n, name);
     76        }
     77        int count = 0;
     78        Node child = n.getFirstChild ();
     79        while (child!=null) {
     80            if (child.getNodeName ().equals (name)) {
     81                if (count == index) {
     82                    return child;
     83                } else {
     84                    count++;
     85                }
     86            }
     87            child = child.getNextSibling ();
     88        }
     89        return null; //not found
     90    }
     91   
     92    /** returns the element parent/node_name[@attribute_name='attribute_value']
     93     */
     94    public static Element getNamedElement (Element parent, String node_name,
     95    String attribute_name,
     96    String attribute_value) {
     97       
     98        NodeList children = parent.getChildNodes ();
     99        for (int i=0; i<children.getLength (); i++) {
     100            Node child = children.item (i);
     101            //logger.debug("getnamed elem, node nmae="+child.getNodeName());
     102            if (child.getNodeName ().equals (node_name)) {
     103                if (((Element)child).getAttribute (attribute_name).equals (attribute_value))
     104                    return (Element)child;
     105            }
     106        }
     107        // not found
     108        return null;
     109    }
     110    /** returns a list of elements parent/node_name[@attribute_name='attribute_value']
     111     */
     112    public static ArrayList getNamedElementList (Element parent, String node_name,
     113    String attribute_name,
     114    String attribute_value) {
     115        ArrayList elements = new ArrayList ();
     116        NodeList children = parent.getChildNodes ();
     117        for (int i=0; i<children.getLength (); i++) {
     118            //System.out.println("getNamedElementList");
     119            Node child = children.item (i);
     120            //logger.debug("getnamed elem, node nmae="+child.getNodeName());
     121            if (child.getNodeName ().equals (node_name)) {
     122                if (((Element)child).getAttribute (attribute_name).equals (attribute_value))
     123                    elements.add ((Element)child);
     124            }
     125        }
     126        // not found
     127        if (elements.size () == 0) {
     128            elements = null;
     129        }
     130        return elements;
     131    }
     132    public static void copyAllChildren (Element to, Element from) {
     133       
     134        Document to_doc = to.getOwnerDocument ();
     135        Node child = from.getFirstChild ();
     136        while (child != null) {
     137            to.appendChild (to_doc.importNode (child, true));
     138            child = child.getNextSibling ();
     139        }
     140    }
     141    /** Duplicates an element */
     142    public static Element duplicateElement (Document owner, Element element, boolean with_attributes) {
     143        return duplicateElementNS (owner, element, null, with_attributes);
     144    }
     145   
     146    /** Duplicates an element */
     147    public static Element duplicateElementNS (Document owner,
     148    Element element,
     149    String namespace_uri,
     150    boolean with_attributes) {
     151        Element duplicate;
     152        if (namespace_uri == null) {
     153            duplicate = owner.createElement (element.getTagName ());
     154        } else {
     155            duplicate = owner.createElementNS (namespace_uri, element.getTagName ());
     156        }
     157        // Copy element attributes
     158        if (with_attributes) {
     159            NamedNodeMap attributes = element.getAttributes ();
     160            for (int i = 0; i < attributes.getLength (); i++) {
     161                Node attribute = attributes.item (i);
     162                duplicate.setAttribute (attribute.getNodeName (), attribute.getNodeValue ());
     163            }
     164        }
     165       
     166        // Copy element children
     167        NodeList children = element.getChildNodes ();
     168        for (int i = 0; i < children.getLength (); i++) {
     169            Node child = children.item (i);
     170            duplicate.appendChild (owner.importNode (child, true));
     171        }
     172       
     173        return duplicate;
     174    }
     175   
     176   
    17177    /** Remove all of the child nodes from a certain node. */
    18     static final public void clear(Node node)
    19     {
    20     while (node.hasChildNodes()) {
    21         node.removeChild(node.getFirstChild());
    22     }
    23     }
    24 
    25 
    26     static public ArrayList getChildElementsByTagName(Element parent_element, String element_name)
    27     {
    28     ArrayList child_elements = new ArrayList();
    29 
    30     NodeList children_nodelist = parent_element.getChildNodes();
    31     for (int i = 0; i < children_nodelist.getLength(); i++) {
    32         Node child_node = children_nodelist.item(i);
    33         if (child_node.getNodeType() == Node.ELEMENT_NODE && child_node.getNodeName().equals(element_name)) {
    34         child_elements.add(child_node);
    35         }
    36     }
    37 
    38     return child_elements;
    39     }
    40 
    41 
    42     static public String getElementTextValue(Element element)
    43     {
    44     // Find the first text node child
    45     NodeList children_nodelist = element.getChildNodes();
    46     for (int i = 0; i < children_nodelist.getLength(); i++) {
    47         Node child_node = children_nodelist.item(i);
    48         if (child_node.getNodeType() == Node.TEXT_NODE) {
    49         return child_node.getNodeValue();
    50         }
    51     }
    52 
    53     // None found
    54     return "";
    55     }
    56 
    57 
     178    static final public void clear (Node node) {
     179        while (node.hasChildNodes ()) {
     180            node.removeChild (node.getFirstChild ());
     181        }
     182    }
     183   
     184   
     185    static public ArrayList getChildElementsByTagName (Element parent_element, String element_name) {
     186        ArrayList child_elements = new ArrayList ();
     187       
     188        NodeList children_nodelist = parent_element.getChildNodes ();
     189        for (int i = 0; i < children_nodelist.getLength (); i++) {
     190            Node child_node = children_nodelist.item (i);
     191            if (child_node.getNodeType () == Node.ELEMENT_NODE && child_node.getNodeName ().equals (element_name)) {
     192                child_elements.add (child_node);
     193            }
     194        }
     195       
     196        return child_elements;
     197    }
     198   
     199   
     200    static public String getElementTextValue (Element element) {
     201        // Find the first text node child
     202        NodeList children_nodelist = element.getChildNodes ();
     203        for (int i = 0; i < children_nodelist.getLength (); i++) {
     204            Node child_node = children_nodelist.item (i);
     205            if (child_node.getNodeType () == Node.TEXT_NODE) {
     206                return child_node.getNodeValue ();
     207            }
     208        }
     209       
     210        // None found
     211        return "";
     212    }
     213   
     214   
    58215    /** Method to retrieve the value of a given node.
    59216     * @param element The <strong>Element</strong> whose value we wish to find.
    60217     * Soon to be deprecated!
    61218     */
    62     static final public String getValue(Node element) {
    63     if (element == null) {
    64         return "";
    65     }
    66     // If we've been given a subject node first retrieve its value node.
    67     if(element.getNodeName().equals("Subject")) {
    68         element = getNodeFromNamed(element, "Value");
    69     }
    70     // If we've got a value node, then reconstruct the text. Remember that DOM will split text over 256 characters into several text nodes
    71     if(element != null && element.hasChildNodes()) {
    72         StringBuffer text_buffer = new StringBuffer();
    73         NodeList text_nodes = element.getChildNodes();
    74         for(int i = 0; i < text_nodes.getLength(); i++) {
    75         Node possible_text = text_nodes.item(i);
    76         if(possible_text.getNodeName().equals(StaticStrings.TEXT_NODE)) {
    77             text_buffer.append(possible_text.getNodeValue());
    78         }
    79         }
    80         return text_buffer.toString();
    81     }
    82     return "";
    83     }
    84 
    85 
     219    static final public String getValue (Node element) {
     220        if (element == null) {
     221            return "";
     222        }
     223        // If we've been given a subject node first retrieve its value node.
     224        if(element.getNodeName ().equals ("Subject")) {
     225            element = getNodeFromNamed (element, "Value");
     226        }
     227        // If we've got a value node, then reconstruct the text. Remember that DOM will split text over 256 characters into several text nodes
     228        if(element != null && element.hasChildNodes ()) {
     229            StringBuffer text_buffer = new StringBuffer ();
     230            NodeList text_nodes = element.getChildNodes ();
     231            for(int i = 0; i < text_nodes.getLength (); i++) {
     232                Node possible_text = text_nodes.item (i);
     233                if(possible_text.getNodeName ().equals (StaticStrings.TEXT_NODE)) {
     234                    text_buffer.append (possible_text.getNodeValue ());
     235                }
     236            }
     237            return text_buffer.toString ();
     238        }
     239        return "";
     240    }
     241   
     242   
    86243    /** Method to retrieve from the node given, a certain child node with the specified name.
    87244     * @param parent The <strong>Node</strong> whose children should be searched.
     
    89246     * @return The requested <strong>Node</strong> if it is found, <i>null</i> otherwise.
    90247     * Soon to be deprecated!
    91      */
    92     static final public Node getNodeFromNamed(Node parent, String name) {
    93     Node child = null;
    94     for(Node i = parent.getFirstChild(); i != null && child == null;
    95         i = i.getNextSibling()) {
    96         if(i.getNodeName().equals(name)) {
    97         child = i;
    98         }
    99     }
    100     return child;
    101     }
    102 
     248     */
     249    static final public Node getNodeFromNamed (Node parent, String name) {
     250        Node child = null;
     251        for(Node i = parent.getFirstChild (); i != null && child == null;
     252        i = i.getNextSibling ()) {
     253            if(i.getNodeName ().equals (name)) {
     254                child = i;
     255            }
     256        }
     257        return child;
     258    }
     259   
     260    static final public String WELLFORMED= "well-formed !";
     261    static final public String NOTWELLFORMED= "not well-formed";
     262    static final private String HEADER = "<?xml version='1.0' encoding='UTF-8'?><collectionConfig xmlns:gsf='http://www.greenstone.org/greenstone3/schema/ConfigFormat' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>";
     263    static final private String FOOTER = "</collectionConfig>";
     264       
     265public static String parse (String xml_str) {
     266        String validation_msg = WELLFORMED;
     267        xml_str = HEADER + xml_str + FOOTER;
     268        try {
     269            SAXParserFactory factory = SAXParserFactory.newInstance ();
     270            factory.setNamespaceAware (true);
     271            //factory.setValidating (true);
     272            SAXParser parser = factory.newSAXParser ();
     273            InputSource iSource = new InputSource ( new StringReader ( xml_str ) );
     274//              parser.parse (iSource, new DefaultHandler ());
     275           
     276            org.xml.sax.XMLReader reader = parser.getXMLReader ();
     277            reader.setContentHandler(new DefaultHandler());
     278            reader.setErrorHandler(new DefaultHandler());
     279            reader.parse(iSource);
     280        } catch (FactoryConfigurationError e) {
     281            validation_msg = "unable to get a document builder factory";
     282        } catch (ParserConfigurationException e) {
     283            validation_msg = "unable to configure parser";
     284        } catch (SAXParseException e) {
     285            validation_msg = NOTWELLFORMED + getLocationString(e) + e.getMessage ();
     286        } catch (SAXException e) {
     287            validation_msg += " Fatal error: " + e.toString ();
     288        } catch (IOException e) {
     289            validation_msg = "Unable to read the input, i/o error";
     290        }
     291       
     292        return validation_msg;
     293    }
     294//In this method, the parsed string xml_str is not wrapped by the header and footer strings.
     295public static String parseDOM (String xml_str) {
     296        String validation_msg = WELLFORMED;
     297       
     298        try {
     299            SAXParserFactory factory = SAXParserFactory.newInstance ();
     300            factory.setNamespaceAware (true);
     301            //factory.setValidating (true);
     302            SAXParser parser = factory.newSAXParser ();
     303            InputSource iSource = new InputSource ( new StringReader ( xml_str ) );
     304//              parser.parse (iSource, new DefaultHandler ());
     305           
     306            org.xml.sax.XMLReader reader = parser.getXMLReader ();
     307            reader.setContentHandler(new DefaultHandler());
     308            reader.setErrorHandler(new DefaultHandler());
     309            reader.parse(iSource);
     310        } catch (FactoryConfigurationError e) {
     311            validation_msg = "unable to get a document builder factory";
     312        } catch (ParserConfigurationException e) {
     313            validation_msg = "unable to configure parser";
     314        } catch (SAXParseException e) {
     315            validation_msg = NOTWELLFORMED + getLocationString(e) + e.getMessage ();
     316        } catch (SAXException e) {
     317            validation_msg += " " + e.toString ();
     318        } catch (IOException e) {
     319            validation_msg = "Unable to read the input, i/o error";
     320        }
     321       
     322        return validation_msg;
     323    }
     324
     325public static String parse (File xml_file) {
     326        String validation_msg = WELLFORMED;
     327
     328        try {
     329            SAXParserFactory factory = SAXParserFactory.newInstance ();
     330            factory.setNamespaceAware (true);
     331            //factory.setValidating (true);
     332            SAXParser parser = factory.newSAXParser ();
     333            FileReader r = new FileReader(xml_file);
     334            InputSource iSource = new InputSource(r);
     335            XMLReader reader = parser.getXMLReader ();
     336            reader.setContentHandler(new DefaultHandler());
     337            reader.setErrorHandler(new DefaultHandler());
     338            reader.parse(iSource);
     339        } catch (FactoryConfigurationError e) {
     340            validation_msg = "unable to get a document builder factory";
     341        } catch (ParserConfigurationException e) {
     342            validation_msg = "unable to configure parser";
     343        } catch (SAXParseException e) {
     344            validation_msg = NOTWELLFORMED + getLocationString(e) + e.getMessage ();
     345        } catch (SAXException e) {
     346            validation_msg += " Fatal error: " + e.toString ();
     347        } catch (IOException e) {
     348            validation_msg = "Unable to read the input, i/o error";
     349        }
     350       
     351        return validation_msg;
     352    }   
     353    /** Returns a string of the location. */
     354    private static String getLocationString(SAXParseException ex) {
     355        StringBuffer str = new StringBuffer();
     356
     357        String systemId = ex.getSystemId();
     358        if (systemId != null) {
     359            int index = systemId.lastIndexOf('/');
     360            if (index != -1)
     361                systemId = systemId.substring(index + 1);
     362            str.append(systemId);
     363        }
     364        str.append("(line ");
     365        str.append(ex.getLineNumber()-1);
     366        str.append(", column ");
     367        str.append(ex.getColumnNumber());
     368        str.append("): ");
     369
     370        return str.toString();
     371
     372    } // getLocationString(SAXParseException):String
    103373
    104374    /** Parse an XML document from a given file path */
    105     static public Document parseXMLFile(String xml_file_path, boolean use_class_loader)
    106     {
    107     if (use_class_loader == true) {
    108         InputStream is = JarTools.getResourceAsStream("/" + xml_file_path);
    109         if (is != null) {
    110         return parseXML(is);
    111         }
    112     }
    113 
    114     // Try the file outside the classes directory
    115     return parseXMLFile(new File(xml_file_path));
    116     }
    117 
    118 
     375    static public Document parseXMLFile (String xml_file_path, boolean use_class_loader) {
     376        if (use_class_loader == true) {
     377            InputStream is = JarTools.getResourceAsStream ("/" + xml_file_path);
     378            if (is != null) {
     379                return parseXML (is);
     380            }
     381        }
     382       
     383        // Try the file outside the classes directory
     384        return parseXMLFile (new File (xml_file_path));
     385    }
     386   
     387   
    119388    /** Parse an XML document from a given file */
    120     static public Document parseXMLFile(File xml_file)
    121     {
    122     // No file? No point trying!
    123     if (xml_file.exists() == false) {
    124         return null;
    125     }
    126 
    127     try {
    128         return parseXML(new FileInputStream(xml_file));
    129     }
    130     catch (Exception exception) {
    131         DebugStream.printStackTrace(exception);
    132         return null;
    133     }
    134     }
    135 
    136 
     389    static public Document parseXMLFile (File xml_file) {
     390        // No file? No point trying!
     391        if (xml_file.exists () == false) {
     392            return null;
     393        }
     394       
     395        try {
     396            return parseXML (new FileInputStream (xml_file));
     397        }
     398        catch (Exception exception) {
     399            DebugStream.printStackTrace (exception);
     400            return null;
     401        }
     402    }
     403   
     404   
    137405    /** Parse an XML document from a given input stream */
    138     static public Document parseXML(InputStream xml_input_stream)
    139     {
    140     Document document = null;
    141 
    142     try {
    143         InputStreamReader isr = new InputStreamReader(xml_input_stream, "UTF-8");
    144         Reader xml_reader = new BufferedReader(isr);
    145         document = parseXML(xml_reader);
    146         isr.close();
    147         xml_input_stream.close();
    148     }
    149     catch (Exception exception) {
    150         DebugStream.printStackTrace(exception);
    151     }
    152 
    153     return document;
    154     }
    155 
    156 
     406    static public Document parseXML (InputStream xml_input_stream) {
     407        Document document = null;
     408       
     409        try {
     410            InputStreamReader isr = new InputStreamReader (xml_input_stream, "UTF-8");
     411            Reader xml_reader = new BufferedReader (isr);
     412            document = parseXML (xml_reader);
     413            isr.close ();
     414            xml_input_stream.close ();
     415        }
     416        catch (Exception exception) {
     417            DebugStream.printStackTrace (exception);
     418        }
     419       
     420        return document;
     421    }
     422   
     423   
    157424    /** Parse an XML document from a given reader */
    158     static public Document parseXML(Reader xml_reader)
    159     {
    160     Document document = null;
    161 
    162     try {
    163         InputSource isc       = new InputSource(xml_reader);
    164         DOMParser parser      = new DOMParser();
    165         parser.setFeature("http://xml.org/sax/features/validation", false);
    166         parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    167         // May or may not be ignored, the documentation for Xerces is contradictory. If it works then parsing -should- be faster.
    168         parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
    169         parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
    170         parser.parse(isc);
    171         document = parser.getDocument();
    172     }
    173     catch (SAXException exception) {
    174         System.err.println("SAX exception: " + exception.getMessage());
    175         DebugStream.printStackTrace(exception);
    176     }
    177     catch (Exception exception) {
    178         DebugStream.printStackTrace(exception);
    179     }
    180 
    181     return document;
    182     }
    183 
    184 
    185     static public StringBuffer readXMLStream(InputStream input_stream)
    186     {
    187     StringBuffer xml = new StringBuffer("");
    188 
    189     try {
    190         InputStreamReader isr = new InputStreamReader(input_stream, "UTF-8");
    191         BufferedReader buffered_in = new BufferedReader(isr);
    192 
    193         String line = "";
    194         boolean xml_content = false;
    195         while((line = buffered_in.readLine()) != null) {
    196         if(xml_content) {
    197             xml.append(line);
    198             xml.append("\n");
    199         }
    200         else if(line.trim().startsWith("<?xml")) {
    201             xml_content = true;
    202             xml.append(line);
    203             xml.append("\n");
    204         }
    205         }
    206         buffered_in = null;
    207     }
    208     catch (Exception error) {
    209         System.err.println("Failed when trying to parse XML stream");
    210         error.printStackTrace();
    211     }
    212 
    213     return xml;
    214     }
    215 
    216 
     425    static public Document parseXML (Reader xml_reader) {
     426        Document document = null;
     427       
     428        try {
     429            InputSource isc       = new InputSource (xml_reader);
     430            DOMParser parser      = new DOMParser ();
     431            parser.setFeature ("http://xml.org/sax/features/validation", false);
     432            parser.setFeature ("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
     433            // May or may not be ignored, the documentation for Xerces is contradictory. If it works then parsing -should- be faster.
     434            parser.setFeature ("http://apache.org/xml/features/dom/defer-node-expansion", true);
     435            parser.setFeature ("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
     436            parser.parse (isc);
     437            document = parser.getDocument ();
     438        }
     439        catch (SAXException exception) {
     440            System.err.println ("SAX exception: " + exception.getMessage ());
     441            DebugStream.printStackTrace (exception);
     442        }
     443        catch (Exception exception) {
     444            DebugStream.printStackTrace (exception);
     445        }
     446       
     447        return document;
     448    }
     449   
     450   
     451    static public StringBuffer readXMLStream (InputStream input_stream) {
     452        StringBuffer xml = new StringBuffer ("");
     453       
     454        try {
     455            InputStreamReader isr = new InputStreamReader (input_stream, "UTF-8");
     456            BufferedReader buffered_in = new BufferedReader (isr);
     457           
     458            String line = "";
     459            boolean xml_content = false;
     460            while((line = buffered_in.readLine ()) != null) {
     461                if(xml_content) {
     462                    xml.append (line);
     463                    xml.append ("\n");
     464                }
     465                else if(line.trim ().startsWith ("<?xml")) {
     466                    xml_content = true;
     467                    xml.append (line);
     468                    xml.append ("\n");
     469                }
     470            }
     471            buffered_in = null;
     472        }
     473        catch (Exception error) {
     474            System.err.println ("Failed when trying to parse XML stream");
     475            error.printStackTrace ();
     476        }
     477       
     478        return xml;
     479    }
     480   
     481   
    217482    /** Removes characters that are invalid in XML (see http://www.w3.org/TR/2000/REC-xml-20001006#charsets) */
    218     static public String removeInvalidCharacters(String text)
    219     {
    220     char[] safe_characters = new char[text.length()];
    221     int j = 0;
    222 
    223     char[] raw_characters = new char[text.length()];
    224     text.getChars(0, text.length(), raw_characters, 0);
    225     for (int i = 0; i < raw_characters.length; i++) {
    226         char character = raw_characters[i];
    227         if ((character >= 0x20 && character <= 0xD7FF) || character == 0x09 || character == 0x0A || character == 0x0D || (character >= 0xE000 && character <= 0xFFFD) || (character >= 0x10000 && character <= 0x10FFFF)) {
    228         safe_characters[j] = character;
    229         j++;
    230         }
    231     }
    232 
    233     return new String(safe_characters, 0, j);
    234     }
    235 
    236 
    237     static public void setElementTextValue(Element element, String text)
    238     {
    239     // Remove all text node children
    240     NodeList children_nodelist = element.getChildNodes();
    241     for (int i = children_nodelist.getLength() - 1; i >= 0; i--) {
    242         Node child_node = children_nodelist.item(i);
    243         if (child_node.getNodeType() == Node.TEXT_NODE) {
    244         element.removeChild(child_node);
    245         }
    246     }
    247 
    248     // Add a new text node
    249     if (text != null) {
    250         element.appendChild(element.getOwnerDocument().createTextNode(text));
    251     }
    252     }
    253 
    254 
     483    static public String removeInvalidCharacters (String text) {
     484        char[] safe_characters = new char[text.length ()];
     485        int j = 0;
     486       
     487        char[] raw_characters = new char[text.length ()];
     488        text.getChars (0, text.length (), raw_characters, 0);
     489        for (int i = 0; i < raw_characters.length; i++) {
     490            char character = raw_characters[i];
     491            if ((character >= 0x20 && character <= 0xD7FF) || character == 0x09 || character == 0x0A || character == 0x0D || (character >= 0xE000 && character <= 0xFFFD) || (character >= 0x10000 && character <= 0x10FFFF)) {
     492                safe_characters[j] = character;
     493                j++;
     494            }
     495        }
     496       
     497        return new String (safe_characters, 0, j);
     498    }
     499   
     500   
     501    static public void setElementTextValue (Element element, String text) {
     502        // Remove all text node children
     503        NodeList children_nodelist = element.getChildNodes ();
     504        for (int i = children_nodelist.getLength () - 1; i >= 0; i--) {
     505            Node child_node = children_nodelist.item (i);
     506            if (child_node.getNodeType () == Node.TEXT_NODE) {
     507                element.removeChild (child_node);
     508            }
     509        }
     510       
     511        // Add a new text node
     512        if (text != null) {
     513            element.appendChild (element.getOwnerDocument ().createTextNode (text));
     514        }
     515    }
     516   
     517   
    255518    /** Set the #text node value of some element.
    256519     * @param element the Element whose value we wish to set
     
    258521     * Soon to be deprecated!
    259522     */
    260     static final public void setValue(Element element, String value) {
    261     // Remove any existing child node(s)
    262     clear(element);
    263     // Add new text node.
    264     if (value != null) {
    265         element.appendChild(element.getOwnerDocument().createTextNode(value));
    266     }
    267     }
    268 
    269 
     523    static final public void setValue (Element element, String value) {
     524        // Remove any existing child node(s)
     525        clear (element);
     526        // Add new text node.
     527        if (value != null) {
     528            element.appendChild (element.getOwnerDocument ().createTextNode (value));
     529        }
     530    }
     531   
     532    /** Write an XML document to a given file with the text node of the specified element unescaped*/
     533    static public void writeXMLFile (File xml_file, Document document, String[] nonEscapingTagNames) {
     534        try {
     535            OutputStream os = new FileOutputStream (xml_file);
     536            // Create an output format for our document.
     537            OutputFormat f = new OutputFormat (document);
     538            f.setEncoding ("UTF-8");
     539            f.setIndenting (true);
     540            f.setLineWidth (0); // Why isn't this working!
     541            f.setPreserveSpace (false);
     542           
     543            f.setNonEscapingElements (nonEscapingTagNames);
     544            // Create the necessary writer stream for serialization.
     545            OutputStreamWriter osw = new OutputStreamWriter (os, "UTF-8");
     546            Writer w               = new BufferedWriter (osw);
     547            // Generate a new serializer from the above.
     548            XMLSerializer s        = new XMLSerializer (w, f);
     549            s.asDOMSerializer ();
     550            // Finally serialize the document to file.
     551            s.serialize (document);
     552            // And close.
     553            os.close ();
     554        }
     555        catch (Exception exception) {
     556            DebugStream.printStackTrace (exception);
     557        }
     558    }
     559   
    270560    /** Write an XML document to a given file */
    271     static public void writeXMLFile(File xml_file, Document document)
    272     {
    273     try {
    274         OutputStream os = new FileOutputStream(xml_file);
    275         // Create an output format for our document.
    276         OutputFormat f = new OutputFormat(document);
    277         f.setEncoding("UTF-8");
    278         f.setIndenting(true);
    279         f.setLineWidth(0); // Why isn't this working!
    280         f.setPreserveSpace(false);
    281         // Create the necessary writer stream for serialization.
    282         OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
    283         Writer w               = new BufferedWriter(osw);
    284         // Generate a new serializer from the above.
    285         XMLSerializer s        = new XMLSerializer(w, f);
    286         s.asDOMSerializer();
    287         // Finally serialize the document to file.
    288         s.serialize(document);
    289         // And close.
    290         os.close();
    291     }
    292     catch (Exception exception) {
    293         DebugStream.printStackTrace(exception);
    294     }
    295     }
    296 
    297     public static void printXMLNode(Node e) {
    298     printXMLNode(e, 0) ;
    299     }
    300  
    301  public static void printXMLNode(Node e, int depth) { //recursive method call using DOM API...
    302    
    303     for (int i=0 ; i<depth ; i++)
    304         System.out.print(' ') ;
    305 
    306     if (e.getNodeType() == Node.TEXT_NODE){
    307         System.out.println("text") ;
    308         //System.out.println(e.getNodeValue()) ;
    309         return ;
    310     }
    311    
    312     System.out.print('<');
    313     System.out.print(e.getNodeName());
    314     NamedNodeMap attrs = e.getAttributes();
    315     for (int i = 0; i < attrs.getLength(); i++) {
    316         Node attr = attrs.item(i);
    317         System.out.print(' '); 
    318         System.out.print(attr.getNodeName());
    319         System.out.print("=\"");
    320         System.out.print(attr.getNodeValue());   
    321         System.out.print('"');
    322     }
    323 
    324     NodeList children = e.getChildNodes();
    325 
    326     if (children == null || children.getLength() == 0)
    327         System.out.println("/>") ;
    328     else {
    329        
    330         System.out.println('>') ;
    331        
    332         int len = children.getLength();
    333         for (int i = 0; i < len; i++) {
    334         printXMLNode(children.item(i), depth + 1);
    335         }
    336        
    337         for (int i=0 ; i<depth ; i++)
    338         System.out.print(' ') ;
    339        
    340         System.out.println("</" + e.getNodeName() + ">");
    341     }   
    342 
    343     }
     561    static public void writeXMLFile (File xml_file, Document document) {
     562        try {
     563            OutputStream os = new FileOutputStream (xml_file);
     564            // Create an output format for our document.
     565            OutputFormat f = new OutputFormat (document);
     566            f.setEncoding ("UTF-8");
     567            f.setIndenting (true);
     568            f.setLineWidth (0); // Why isn't this working!
     569            f.setPreserveSpace (false);
     570            // Create the necessary writer stream for serialization.
     571            OutputStreamWriter osw = new OutputStreamWriter (os, "UTF-8");
     572            Writer w               = new BufferedWriter (osw);
     573            // Generate a new serializer from the above.
     574            XMLSerializer s        = new XMLSerializer (w, f);
     575            s.asDOMSerializer ();
     576            // Finally serialize the document to file.
     577            s.serialize (document);
     578            // And close.
     579            os.close ();
     580        }
     581        catch (Exception exception) {
     582            DebugStream.printStackTrace (exception);
     583        }
     584    }
     585   
     586    public static void printXMLNode (Node e) {
     587        printXMLNode (e, 0) ;
     588    }
     589   
     590    public static void printXMLNode (Node e, int depth) { //recursive method call using DOM API...
     591       
     592        for (int i=0 ; i<depth ; i++)
     593            System.out.print (' ') ;
     594       
     595        if (e.getNodeType () == Node.TEXT_NODE){
     596            //System.out.println("text") ;
     597            if (e.getNodeValue () != "") {
     598                System.out.println (e.getNodeValue ()) ;
     599            }
     600            return ;
     601        }
     602       
     603        System.out.print ('<');
     604        System.out.print (e.getNodeName ());
     605        NamedNodeMap attrs = e.getAttributes ();
     606        if (attrs != null) {
     607            for (int i = 0; i < attrs.getLength (); i++) {
     608                Node attr = attrs.item (i);
     609                System.out.print (' ');
     610                System.out.print (attr.getNodeName ());
     611                System.out.print ("=\"");
     612                System.out.print (attr.getNodeValue ());
     613                System.out.print ('"');
     614            }
     615        }
     616        NodeList children = e.getChildNodes ();
     617       
     618        if (children == null || children.getLength () == 0)
     619            System.out.println ("/>") ;
     620        else {
     621           
     622            System.out.println ('>') ;
     623           
     624            int len = children.getLength ();
     625            for (int i = 0; i < len; i++) {
     626                printXMLNode (children.item (i), depth + 1);
     627            }
     628           
     629            for (int i=0 ; i<depth ; i++)
     630                System.out.print (' ') ;
     631           
     632            System.out.println ("</" + e.getNodeName () + ">");
     633        }
     634       
     635    }
     636    public static String xmlNodeToString (Node e){
     637        StringBuffer sb = new StringBuffer ("");
     638        xmlNodeToString (sb,e,0);
     639        return sb.toString ();
     640    }
     641   
     642    private static void xmlNodeToString (StringBuffer sb, Node e, int depth){
     643       
     644        for (int i=0 ; i<depth ; i++)
     645            sb.append (' ') ;
     646       
     647        if (e.getNodeType () == Node.TEXT_NODE){
     648            if (e.getNodeValue () != "") {
     649                sb.append (e.getNodeValue ()) ;
     650            }
     651            return ;
     652        }
     653       
     654        sb.append ('<');
     655        sb.append (e.getNodeName ());
     656        NamedNodeMap attrs = e.getAttributes ();
     657        if (attrs != null) {
     658            for (int i = 0; i < attrs.getLength (); i++) {
     659                Node attr = attrs.item (i);
     660                sb.append (' ');
     661                sb.append (attr.getNodeName ());
     662                sb.append ("=\"");
     663                sb.append (attr.getNodeValue ());
     664                sb.append ('"');
     665            }
     666        }
     667        NodeList children = e.getChildNodes ();
     668       
     669        if (children == null || children.getLength () == 0)
     670            sb.append ("/>\n") ;
     671        else {
     672           
     673            sb.append (">\n") ;
     674           
     675            int len = children.getLength ();
     676            for (int i = 0; i < len; i++) {
     677                xmlNodeToString (sb,children.item (i), depth + 1);
     678            }
     679           
     680            for (int i=0 ; i<depth ; i++)
     681                sb.append (' ') ;
     682           
     683            sb.append ("</" + e.getNodeName () + ">\n");
     684        }
     685       
     686       
     687    }
     688    public static String xmlNodeToStringWithoutNewline (Node e){
     689        StringBuffer sb = new StringBuffer ("");
     690        xmlNodeToStringWithoutNewline (sb,e,0);
     691        return sb.toString ();
     692    }
     693   
     694    private static void xmlNodeToStringWithoutNewline (StringBuffer sb, Node e, int depth){
     695       
     696        for (int i=0 ; i<depth ; i++)
     697            sb.append (' ') ;
     698       
     699        if (e.getNodeType () == Node.TEXT_NODE){
     700            if (e.getNodeValue () != "") {
     701                sb.append (e.getNodeValue ()) ;
     702            }
     703            return ;
     704        }
     705       
     706        sb.append ('<');
     707        sb.append (e.getNodeName ());
     708        NamedNodeMap attrs = e.getAttributes ();
     709        if (attrs != null) {
     710            for (int i = 0; i < attrs.getLength (); i++) {
     711                Node attr = attrs.item (i);
     712                sb.append (' ');
     713                sb.append (attr.getNodeName ());
     714                sb.append ("=\"");
     715                sb.append (attr.getNodeValue ());
     716                sb.append ('"');
     717            }
     718        }
     719        NodeList children = e.getChildNodes ();
     720       
     721        if (children == null || children.getLength () == 0)
     722            sb.append ("/>") ;
     723        else {
     724           
     725            sb.append (">") ;
     726           
     727            int len = children.getLength ();
     728            for (int i = 0; i < len; i++) {
     729                xmlNodeToStringWithoutNewline (sb,children.item (i), depth + 1);
     730            }
     731           
     732            for (int i=0 ; i<depth ; i++)
     733                sb.append (' ') ;
     734           
     735            sb.append ("</" + e.getNodeName () + ">");
     736        }
     737       
     738       
     739    }
     740   
    344741}
Note: See TracChangeset for help on using the changeset viewer.