Ignore:
Timestamp:
2004-08-20T13:15:03+12:00 (20 years ago)
Author:
mdewsnip
Message:

Moved the getValue() and getNodeFromNamed() functions from MSMUtils into here. They will soon be deprecated.

File:
1 edited

Legend:

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

    r7979 r8014  
    1313public class XMLTools
    1414{
    15    static public ArrayList getChildElementsByTagName(Element parent_element, String element_name)
    16    {
    17       ArrayList child_elements = new ArrayList();
     15    static public ArrayList getChildElementsByTagName(Element parent_element, String element_name)
     16    {
     17    ArrayList child_elements = new ArrayList();
    1818
    19       NodeList children_nodelist = parent_element.getChildNodes();
    20       for (int i = 0; i < children_nodelist.getLength(); i++) {
    21      Node child_node = children_nodelist.item(i);
    22      if (child_node.getNodeType() == Node.ELEMENT_NODE && child_node.getNodeName().equals(element_name)) {
    23         child_elements.add(child_node);
    24      }
    25       }
     19    NodeList children_nodelist = parent_element.getChildNodes();
     20    for (int i = 0; i < children_nodelist.getLength(); i++) {
     21        Node child_node = children_nodelist.item(i);
     22        if (child_node.getNodeType() == Node.ELEMENT_NODE && child_node.getNodeName().equals(element_name)) {
     23        child_elements.add(child_node);
     24        }
     25    }
    2626
    27       return child_elements;
    28    }
     27    return child_elements;
     28    }
    2929
    3030
    31    static public String getElementTextValue(Element element)
    32    {
    33       // Find the first text node child
    34       NodeList children_nodelist = element.getChildNodes();
    35       for (int i = 0; i < children_nodelist.getLength(); i++) {
    36      Node child_node = children_nodelist.item(i);
    37      if (child_node.getNodeType() == Node.TEXT_NODE) {
    38         return child_node.getNodeValue();
    39      }
    40       }
     31    static public String getElementTextValue(Element element)
     32    {
     33    // Find the first text node child
     34    NodeList children_nodelist = element.getChildNodes();
     35    for (int i = 0; i < children_nodelist.getLength(); i++) {
     36        Node child_node = children_nodelist.item(i);
     37        if (child_node.getNodeType() == Node.TEXT_NODE) {
     38        return child_node.getNodeValue();
     39        }
     40    }
    4141
    42       // None found
    43       return null;
    44    }
     42    // None found
     43    return null;
     44    }
    4545
    4646
    47    /** Parse an XML document from a given file */
    48    static public Document parseXMLFile(File xml_file)
    49    {
    50       Document document = null;
    51 
    52       try {
    53      FileInputStream fis   = new FileInputStream(xml_file);
    54      InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
    55      Reader r              = new BufferedReader(isr);
    56      InputSource isc       = new InputSource(r);
    57      DOMParser parser      = new DOMParser();
    58      parser.setFeature("http://xml.org/sax/features/validation", false);
    59      parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    60      // May or may not be ignored, the documentation for Xerces is contradictory. If it works then parsing -should- be faster.
    61      parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
    62      parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
    63      parser.parse(isc);
    64      document = parser.getDocument();
    65      isr.close();
    66      fis.close();
    67       }
    68       catch (Exception exception) {
    69      exception.printStackTrace();
    70       }
    71 
    72       return document;
    73    }
     47    /** Method to retrieve the value of a given node.
     48     * @param element The <strong>Element</strong> whose value we wish to find.
     49     * Soon to be deprecated!
     50     */
     51    static final public String getValue(Node element) {
     52    // If we've been given a subject node first retrieve its value node.
     53    if(element.getNodeName().equals("Subject")) {
     54        element = getNodeFromNamed(element, "Value");
     55    }
     56    // If we've got a value node, then reconstruct the text. Remember that DOM will split text over 256 characters into several text nodes
     57    if(element != null && element.hasChildNodes()) {
     58        StringBuffer text_buffer = new StringBuffer();
     59        NodeList text_nodes = element.getChildNodes();
     60        for(int i = 0; i < text_nodes.getLength(); i++) {
     61        Node possible_text = text_nodes.item(i);
     62        if(possible_text.getNodeName().equals(StaticStrings.TEXT_NODE)) {
     63            text_buffer.append(possible_text.getNodeValue());
     64        }
     65        }
     66        return text_buffer.toString();
     67    }
     68    return "";
     69    }
    7470
    7571
    76    /** Write an XML document to a given file */
    77    static public void writeXMLFile(File xml_file, Document document)
    78    {
    79       try {
    80      OutputStream os = new FileOutputStream(xml_file);
    81      // Create an output format for our document.
    82      OutputFormat f = new OutputFormat(document);
    83      f.setEncoding("UTF-8");
    84      f.setIndenting(true);
    85      f.setLineWidth(0); // Why isn't this working!
    86      f.setPreserveSpace(false);
    87      // Create the necessary writer stream for serialization.
    88      OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
    89      Writer w               = new BufferedWriter(osw);
    90      // Generate a new serializer from the above.
    91      XMLSerializer s        = new XMLSerializer(w, f);
    92      s.asDOMSerializer();
    93      // Finally serialize the document to file.
    94      s.serialize(document);
    95      // And close.
    96      os.close();
    97       }
    98       catch (Exception exception) {
    99      exception.printStackTrace();
    100       }
    101    }
     72    /** Method to retrieve from the node given, a certain child node with the specified name.
     73     * @param parent The <strong>Node</strong> whose children should be searched.
     74     * @param name The required nodes name as a <strong>String</strong>.
     75     * @return The requested <strong>Node</strong> if it is found, <i>null</i> otherwise.
     76     * Soon to be deprecated!
     77     */
     78    static final public Node getNodeFromNamed(Node parent, String name) {
     79    Node child = null;
     80    for(Node i = parent.getFirstChild(); i != null && child == null;
     81        i = i.getNextSibling()) {
     82        if(i.getNodeName().equals(name)) {
     83        child = i;
     84        }
     85    }
     86    return child;
     87    }
     88
     89
     90    /** Parse an XML document from a given file */
     91    static public Document parseXMLFile(File xml_file)
     92    {
     93    Document document = null;
     94
     95    try {
     96        FileInputStream fis   = new FileInputStream(xml_file);
     97        InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
     98        Reader r              = new BufferedReader(isr);
     99        InputSource isc       = new InputSource(r);
     100        DOMParser parser      = new DOMParser();
     101        parser.setFeature("http://xml.org/sax/features/validation", false);
     102        parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
     103        // May or may not be ignored, the documentation for Xerces is contradictory. If it works then parsing -should- be faster.
     104        parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
     105        parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
     106        parser.parse(isc);
     107        document = parser.getDocument();
     108        isr.close();
     109        fis.close();
     110    }
     111    catch (Exception exception) {
     112        exception.printStackTrace();
     113    }
     114
     115    return document;
     116    }
     117
     118
     119    /** Write an XML document to a given file */
     120    static public void writeXMLFile(File xml_file, Document document)
     121    {
     122    try {
     123        OutputStream os = new FileOutputStream(xml_file);
     124        // Create an output format for our document.
     125        OutputFormat f = new OutputFormat(document);
     126        f.setEncoding("UTF-8");
     127        f.setIndenting(true);
     128        f.setLineWidth(0); // Why isn't this working!
     129        f.setPreserveSpace(false);
     130        // Create the necessary writer stream for serialization.
     131        OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
     132        Writer w               = new BufferedWriter(osw);
     133        // Generate a new serializer from the above.
     134        XMLSerializer s        = new XMLSerializer(w, f);
     135        s.asDOMSerializer();
     136        // Finally serialize the document to file.
     137        s.serialize(document);
     138        // And close.
     139        os.close();
     140    }
     141    catch (Exception exception) {
     142        exception.printStackTrace();
     143    }
     144    }
    102145}
    103 
Note: See TracChangeset for help on using the changeset viewer.