Search in sources :

Example 91 with Text

use of org.w3c.dom.Text in project groovy by apache.

the class DOMCategory method appendNode.

public static Element appendNode(Element self, Object name, String value) {
    Document doc = self.getOwnerDocument();
    Element newChild;
    if (name instanceof QName) {
        QName qn = (QName) name;
        newChild = doc.createElementNS(qn.getNamespaceURI(), qn.getQualifiedName());
    } else {
        newChild = doc.createElement(name.toString());
    }
    if (value != null) {
        Text text = doc.createTextNode(value);
        newChild.appendChild(text);
    }
    self.appendChild(newChild);
    return newChild;
}
Also used : QName(groovy.xml.QName) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) Document(org.w3c.dom.Document)

Example 92 with Text

use of org.w3c.dom.Text in project hadoop by apache.

the class ConfigurationUtils method parseDocument.

// Canibalized from FileSystemAccess <code>Configuration.loadResource()</code>.
private static void parseDocument(Configuration conf, Document doc) throws IOException {
    try {
        Element root = doc.getDocumentElement();
        if (!"configuration".equals(root.getTagName())) {
            throw new IOException("bad conf file: top-level element not <configuration>");
        }
        NodeList props = root.getChildNodes();
        for (int i = 0; i < props.getLength(); i++) {
            Node propNode = props.item(i);
            if (!(propNode instanceof Element)) {
                continue;
            }
            Element prop = (Element) propNode;
            if (!"property".equals(prop.getTagName())) {
                throw new IOException("bad conf file: element not <property>");
            }
            NodeList fields = prop.getChildNodes();
            String attr = null;
            String value = null;
            for (int j = 0; j < fields.getLength(); j++) {
                Node fieldNode = fields.item(j);
                if (!(fieldNode instanceof Element)) {
                    continue;
                }
                Element field = (Element) fieldNode;
                if ("name".equals(field.getTagName()) && field.hasChildNodes()) {
                    attr = ((Text) field.getFirstChild()).getData().trim();
                }
                if ("value".equals(field.getTagName()) && field.hasChildNodes()) {
                    value = ((Text) field.getFirstChild()).getData();
                }
            }
            if (attr != null && value != null) {
                conf.set(attr, value);
            }
        }
    } catch (DOMException e) {
        throw new IOException(e);
    }
}
Also used : DOMException(org.w3c.dom.DOMException) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Text(org.w3c.dom.Text) IOException(java.io.IOException)

Example 93 with Text

use of org.w3c.dom.Text in project hadoop by apache.

the class JobConfigurationParser method parse.

/**
   * Parse the job configuration file (as an input stream) and return a
   * {@link Properties} collection. The input stream will not be closed after
   * return from the call.
   * 
   * @param input
   *          The input data.
   * @return A {@link Properties} collection extracted from the job
   *         configuration xml.
   * @throws IOException
   */
static Properties parse(InputStream input) throws IOException {
    Properties result = new Properties();
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(input);
        Element root = doc.getDocumentElement();
        if (!"configuration".equals(root.getTagName())) {
            System.out.print("root is not a configuration node");
            return null;
        }
        NodeList props = root.getChildNodes();
        for (int i = 0; i < props.getLength(); ++i) {
            Node propNode = props.item(i);
            if (!(propNode instanceof Element))
                continue;
            Element prop = (Element) propNode;
            if (!"property".equals(prop.getTagName())) {
                System.out.print("bad conf file: element not <property>");
            }
            NodeList fields = prop.getChildNodes();
            String attr = null;
            String value = null;
            @SuppressWarnings("unused") boolean finalParameter = false;
            for (int j = 0; j < fields.getLength(); j++) {
                Node fieldNode = fields.item(j);
                if (!(fieldNode instanceof Element)) {
                    continue;
                }
                Element field = (Element) fieldNode;
                if ("name".equals(field.getTagName()) && field.hasChildNodes()) {
                    attr = ((Text) field.getFirstChild()).getData().trim();
                }
                if ("value".equals(field.getTagName()) && field.hasChildNodes()) {
                    value = ((Text) field.getFirstChild()).getData();
                }
                if ("final".equals(field.getTagName()) && field.hasChildNodes()) {
                    finalParameter = "true".equals(((Text) field.getFirstChild()).getData());
                }
            }
            if (attr != null && value != null) {
                result.put(attr, value);
            }
        }
    } catch (ParserConfigurationException e) {
        return null;
    } catch (SAXException e) {
        return null;
    }
    return result;
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Text(org.w3c.dom.Text) Properties(java.util.Properties) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 94 with Text

use of org.w3c.dom.Text in project robovm by robovm.

the class TextImpl method replaceWholeText.

public final Text replaceWholeText(String content) throws DOMException {
    // TODO: support entity references. This code should expand and replace
    // the child elements of entity references.
    //     http://code.google.com/p/android/issues/detail?id=6807
    Node parent = getParentNode();
    Text result = null;
    // delete all nodes in the current run of text...
    for (TextImpl n = firstTextNodeInCurrentRun(); n != null; ) {
        // ...except the current node if we have content for it
        if (n == this && content != null && content.length() > 0) {
            setData(content);
            result = this;
            n = n.nextTextNode();
        } else {
            // because removeChild() detaches siblings
            Node toRemove = n;
            n = n.nextTextNode();
            parent.removeChild(toRemove);
        }
    }
    return result;
}
Also used : Node(org.w3c.dom.Node) Text(org.w3c.dom.Text)

Example 95 with Text

use of org.w3c.dom.Text in project robovm by robovm.

the class DocumentBuilderImpl method appendText.

/**
     * @param token the XML pull parser token type, such as XmlPullParser.CDSECT
     *      or XmlPullParser.ENTITY_REF.
     */
private void appendText(DocumentImpl document, Node parent, int token, String text) {
    // Ignore empty runs.
    if (text.isEmpty()) {
        return;
    }
    // Merge with any previous text node if possible.
    if (coalescing || token != XmlPullParser.CDSECT) {
        Node lastChild = parent.getLastChild();
        if (lastChild != null && lastChild.getNodeType() == Node.TEXT_NODE) {
            Text textNode = (Text) lastChild;
            textNode.appendData(text);
            return;
        }
    }
    // Okay, we really do need a new text node
    parent.appendChild(token == XmlPullParser.CDSECT ? new CDATASectionImpl(document, text) : new TextImpl(document, text));
}
Also used : Node(org.w3c.dom.Node) Text(org.w3c.dom.Text) CDATASectionImpl(org.apache.harmony.xml.dom.CDATASectionImpl) TextImpl(org.apache.harmony.xml.dom.TextImpl)

Aggregations

Text (org.w3c.dom.Text)144 Element (org.w3c.dom.Element)82 Document (org.w3c.dom.Document)51 Node (org.w3c.dom.Node)48 NodeList (org.w3c.dom.NodeList)27 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)20 DocumentBuilder (javax.xml.parsers.DocumentBuilder)19 Attr (org.w3c.dom.Attr)12 StringReader (java.io.StringReader)10 InputSource (org.xml.sax.InputSource)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)9 DOMException (org.w3c.dom.DOMException)7 IOException (java.io.IOException)6 Test (org.junit.Test)6 HashMap (java.util.HashMap)4 DOMSource (javax.xml.transform.dom.DOMSource)4 DocumentFragment (org.w3c.dom.DocumentFragment)4 NamedNodeMap (org.w3c.dom.NamedNodeMap)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3