Search in sources :

Example 46 with Text

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

the class NormalizeTest method testInvalidCharactersText.

public void testInvalidCharactersText() throws Exception {
    ErrorRecorder errorRecorder = new ErrorRecorder();
    domConfiguration.setParameter("error-handler", errorRecorder);
    domConfiguration.setParameter("namespaces", false);
    Element root = document.createElement("foo");
    document.appendChild(root);
    Text text = document.createTextNode("");
    root.appendChild(text);
    for (int c = 0; c <= Character.MAX_VALUE; c++) {
        text.setData(new String(new char[] { 'A', 'B', (char) c }));
        document.normalizeDocument();
        if (isValid((char) c)) {
            assertEquals(Collections.<DOMError>emptyList(), errorRecorder.errors);
        } else {
            errorRecorder.assertAllErrors("For character " + c, DOMError.SEVERITY_ERROR, "wf-invalid-character");
        }
    }
}
Also used : Element(org.w3c.dom.Element) Text(org.w3c.dom.Text)

Example 47 with Text

use of org.w3c.dom.Text in project cdap by caskdata.

the class Configuration method loadResource.

private void loadResource(Properties properties, Object name, boolean quiet) {
    try {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        //ignore all comments inside the xml file
        docBuilderFactory.setIgnoringComments(true);
        //allow includes in the xml file
        docBuilderFactory.setNamespaceAware(true);
        try {
            docBuilderFactory.setXIncludeAware(true);
        } catch (UnsupportedOperationException e) {
            LOG.error("Failed to set setXIncludeAware(true) for parser " + docBuilderFactory + ":" + e, e);
        }
        DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
        Document doc = null;
        Element root = null;
        if (name instanceof URL) {
            // an URL resource
            URL url = (URL) name;
            if (url != null) {
                if (!quiet) {
                    LOG.info("parsing " + url);
                }
                doc = builder.parse(url.toString());
            }
        } else if (name instanceof String) {
            // a CLASSPATH resource
            URL url = getResource((String) name);
            if (url != null) {
                if (!quiet) {
                    LOG.info("parsing " + url);
                }
                doc = builder.parse(url.toString());
            }
        } else if (name instanceof InputStream) {
            try {
                doc = builder.parse((InputStream) name);
            } finally {
                ((InputStream) name).close();
            }
        } else if (name instanceof Element) {
            root = (Element) name;
        }
        if (doc == null && root == null) {
            if (quiet) {
                return;
            }
            throw new RuntimeException(name + " not found");
        }
        if (root == null) {
            root = doc.getDocumentElement();
        }
        if (!"configuration".equals(root.getTagName())) {
            LOG.error("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 ("configuration".equals(prop.getTagName())) {
                loadResource(properties, prop, quiet);
                continue;
            }
            if (!"property".equals(prop.getTagName())) {
                LOG.warn("bad conf file: element not <property>");
            }
            NodeList fields = prop.getChildNodes();
            String attr = null;
            String value = null;
            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());
                }
            }
            // Ignore this parameter if it has already been marked as 'final'
            if (attr != null) {
                if (deprecatedKeyMap.containsKey(attr)) {
                    DeprecatedKeyInfo keyInfo = deprecatedKeyMap.get(attr);
                    keyInfo.accessed = false;
                    warnOnceIfDeprecated(attr);
                    for (String key : keyInfo.newKeys) {
                        // update new keys with deprecated key's value
                        loadProperty(properties, name, key, value, finalParameter);
                    }
                } else {
                    loadProperty(properties, name, attr, value, finalParameter);
                }
            }
        }
    } catch (IOException e) {
        LOG.error("error parsing conf file.", e);
        throw new RuntimeException(e);
    } catch (DOMException e) {
        LOG.error("error parsing conf file.", e);
        throw new RuntimeException(e);
    } catch (SAXException e) {
        LOG.error("error parsing conf file.", e);
        throw new RuntimeException(e);
    } catch (ParserConfigurationException e) {
        LOG.error("error parsing conf file.", e);
        throw new RuntimeException(e);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Text(org.w3c.dom.Text) IOException(java.io.IOException) Document(org.w3c.dom.Document) URL(java.net.URL) SAXException(org.xml.sax.SAXException) DOMException(org.w3c.dom.DOMException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 48 with Text

use of org.w3c.dom.Text in project jdk8u_jdk by JetBrains.

the class XMLSignature method setSignatureValueElement.

/**
     * Base64 encodes and sets the bytes as the content of the SignatureValue
     * Node.
     *
     * @param bytes bytes to be used by SignatureValue before Base64 encoding
     */
private void setSignatureValueElement(byte[] bytes) {
    while (signatureValueElement.hasChildNodes()) {
        signatureValueElement.removeChild(signatureValueElement.getFirstChild());
    }
    String base64codedValue = Base64.encode(bytes);
    if (base64codedValue.length() > 76 && !XMLUtils.ignoreLineBreaks()) {
        base64codedValue = "\n" + base64codedValue + "\n";
    }
    Text t = this.doc.createTextNode(base64codedValue);
    signatureValueElement.appendChild(t);
}
Also used : Text(org.w3c.dom.Text)

Example 49 with Text

use of org.w3c.dom.Text in project jdk8u_jdk by JetBrains.

the class Reference method setDigestValueElement.

/**
     * Method setDigestValueElement
     *
     * @param digestValue
     */
private void setDigestValueElement(byte[] digestValue) {
    Node n = digestValueElement.getFirstChild();
    while (n != null) {
        digestValueElement.removeChild(n);
        n = n.getNextSibling();
    }
    String base64codedValue = Base64.encode(digestValue);
    Text t = this.doc.createTextNode(base64codedValue);
    digestValueElement.appendChild(t);
}
Also used : Node(org.w3c.dom.Node) Text(org.w3c.dom.Text)

Example 50 with Text

use of org.w3c.dom.Text in project jabref by JabRef.

the class OOCalcDatabase method addTableCell.

private static void addTableCell(Document doc, Element parent, String content) {
    Element cell = doc.createElement("table:table-cell");
    Element text = doc.createElement("text:p");
    Text textNode = doc.createTextNode(content);
    text.appendChild(textNode);
    //text.setTextContent(content);
    cell.appendChild(text);
    parent.appendChild(cell);
}
Also used : Element(org.w3c.dom.Element) Text(org.w3c.dom.Text)

Aggregations

Text (org.w3c.dom.Text)166 Element (org.w3c.dom.Element)93 Document (org.w3c.dom.Document)64 Node (org.w3c.dom.Node)58 NodeList (org.w3c.dom.NodeList)35 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)23 DocumentBuilder (javax.xml.parsers.DocumentBuilder)22 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)17 IOException (java.io.IOException)14 Attr (org.w3c.dom.Attr)14 InputSource (org.xml.sax.InputSource)11 StringReader (java.io.StringReader)10 SAXException (org.xml.sax.SAXException)8 ArrayList (java.util.ArrayList)7 DOMException (org.w3c.dom.DOMException)7 Test (org.junit.Test)6 DOMSource (javax.xml.transform.dom.DOMSource)5 NamedNodeMap (org.w3c.dom.NamedNodeMap)5 HashMap (java.util.HashMap)4 DocumentFragment (org.w3c.dom.DocumentFragment)4