Search in sources :

Example 41 with Text

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

the class NodeNormalize method testNormalize.

/**
     * Runs the test case.
     *
     * @throws Throwable
     *             Any uncaught exception causes test to fail
     */
public void testNormalize() throws Throwable {
    Document doc;
    Document newDoc;
    DOMImplementation domImpl;
    DocumentType docTypeNull = null;
    Element documentElement;
    Element element1;
    Element element2;
    Element element3;
    Element element4;
    Element element5;
    Element element6;
    Element element7;
    Text text1;
    Text text2;
    Text text3;
    ProcessingInstruction pi;
    CDATASection cData;
    Comment comment;
    EntityReference entRef;
    NodeList elementList;
    doc = (Document) load("staffNS", builder);
    domImpl = doc.getImplementation();
    newDoc = domImpl.createDocument("http://www.w3.org/DOM/Test", "dom:root", docTypeNull);
    element1 = newDoc.createElement("element1");
    element2 = newDoc.createElement("element2");
    element3 = newDoc.createElement("element3");
    element4 = newDoc.createElement("element4");
    element5 = newDoc.createElement("element5");
    element6 = newDoc.createElement("element6");
    element7 = newDoc.createElement("element7");
    text1 = newDoc.createTextNode("text1");
    text2 = newDoc.createTextNode("text2");
    text3 = newDoc.createTextNode("text3");
    cData = newDoc.createCDATASection("Cdata");
    comment = newDoc.createComment("comment");
    pi = newDoc.createProcessingInstruction("PITarget", "PIData");
    entRef = newDoc.createEntityReference("EntRef");
    assertNotNull("createdEntRefNotNull", entRef);
    documentElement = newDoc.getDocumentElement();
    documentElement.appendChild(element1);
    element2.appendChild(text1);
    element2.appendChild(text2);
    element2.appendChild(text3);
    element1.appendChild(element2);
    text1 = (Text) text1.cloneNode(false);
    text2 = (Text) text2.cloneNode(false);
    element3.appendChild(entRef);
    element3.appendChild(text1);
    element3.appendChild(text2);
    element1.appendChild(element3);
    text1 = (Text) text1.cloneNode(false);
    text2 = (Text) text2.cloneNode(false);
    element4.appendChild(cData);
    element4.appendChild(text1);
    element4.appendChild(text2);
    element1.appendChild(element4);
    text2 = (Text) text2.cloneNode(false);
    text3 = (Text) text3.cloneNode(false);
    element5.appendChild(comment);
    element5.appendChild(text2);
    element5.appendChild(text3);
    element1.appendChild(element5);
    text2 = (Text) text2.cloneNode(false);
    text3 = (Text) text3.cloneNode(false);
    element6.appendChild(pi);
    element6.appendChild(text2);
    element6.appendChild(text3);
    element1.appendChild(element6);
    entRef = (EntityReference) entRef.cloneNode(false);
    text1 = (Text) text1.cloneNode(false);
    text2 = (Text) text2.cloneNode(false);
    text3 = (Text) text3.cloneNode(false);
    element7.appendChild(entRef);
    element7.appendChild(text1);
    element7.appendChild(text2);
    element7.appendChild(text3);
    element1.appendChild(element7);
    elementList = element1.getChildNodes();
    assertEquals("nodeNormalize01_1Bef", 6, elementList.getLength());
    elementList = element2.getChildNodes();
    assertEquals("nodeNormalize01_2Bef", 3, elementList.getLength());
    elementList = element3.getChildNodes();
    assertEquals("nodeNormalize01_3Bef", 3, elementList.getLength());
    elementList = element4.getChildNodes();
    assertEquals("nodeNormalize01_4Bef", 3, elementList.getLength());
    elementList = element5.getChildNodes();
    assertEquals("nodeNormalize01_5Bef", 3, elementList.getLength());
    elementList = element6.getChildNodes();
    assertEquals("nodeNormalize01_6Bef", 3, elementList.getLength());
    elementList = element7.getChildNodes();
    assertEquals("nodeNormalize01_7Bef", 4, elementList.getLength());
    newDoc.normalize();
    elementList = element1.getChildNodes();
    assertEquals("nodeNormalize01_1Aft", 6, elementList.getLength());
    elementList = element2.getChildNodes();
    assertEquals("nodeNormalize01_2Aft", 1, elementList.getLength());
    elementList = element3.getChildNodes();
    assertEquals("nodeNormalize01_3Aft", 2, elementList.getLength());
    elementList = element4.getChildNodes();
    assertEquals("nodeNormalize01_4Aft", 2, elementList.getLength());
    elementList = element5.getChildNodes();
    assertEquals("nodeNormalize01_5Aft", 2, elementList.getLength());
    elementList = element6.getChildNodes();
    assertEquals("nodeNormalize01_6Aft", 2, elementList.getLength());
    elementList = element7.getChildNodes();
    assertEquals("nodeNormalize01_7Aft", 2, elementList.getLength());
}
Also used : Comment(org.w3c.dom.Comment) CDATASection(org.w3c.dom.CDATASection) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) EntityReference(org.w3c.dom.EntityReference) DOMImplementation(org.w3c.dom.DOMImplementation) DocumentType(org.w3c.dom.DocumentType) Text(org.w3c.dom.Text) Document(org.w3c.dom.Document) ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Example 42 with Text

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

the class ImportNode method _testImportNode1.

/**
     * Runs the test case.
     *
     * @throws Throwable
     *             Any uncaught exception causes test to fail
     */
public void _testImportNode1() throws Throwable {
    Document doc;
    Document aNewDoc;
    Attr newAttr;
    Text importedChild;
    Node aNode;
    Document ownerDocument;
    Element attrOwnerElement;
    DocumentType docType;
    String system;
    boolean specified;
    NodeList childList;
    String nodeName;
    Node child;
    String childValue;
    List<String> expectedResult = new ArrayList<String>();
    expectedResult.add("elem:attr1");
    expectedResult.add("importedText");
    doc = (Document) load("staffNS", builder);
    aNewDoc = (Document) load("staffNS", builder);
    newAttr = aNewDoc.createAttribute("elem:attr1");
    importedChild = aNewDoc.createTextNode("importedText");
    aNode = newAttr.appendChild(importedChild);
    aNode = doc.importNode(newAttr, false);
    ownerDocument = aNode.getOwnerDocument();
    docType = ownerDocument.getDoctype();
    system = docType.getSystemId();
    assertNotNull("aNode", aNode);
    assertURIEquals("systemId", null, null, null, "staffNS.dtd", null, null, null, null, system);
    attrOwnerElement = ((Attr) /* Node */
    aNode).getOwnerElement();
    assertNull("ownerElement", attrOwnerElement);
    specified = ((Attr) /* Node */
    aNode).getSpecified();
    assertTrue("specified", specified);
    childList = aNode.getChildNodes();
    assertEquals("childList", 1, childList.getLength());
    nodeName = aNode.getNodeName();
    assertEquals("nodeName", "elem:attr1", nodeName);
    child = aNode.getFirstChild();
    childValue = child.getNodeValue();
    assertEquals("childValue", "importedText", childValue);
}
Also used : Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) DocumentType(org.w3c.dom.DocumentType) Text(org.w3c.dom.Text) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr)

Example 43 with Text

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

the class TextImpl method splitText.

public final Text splitText(int offset) throws DOMException {
    Text newText = document.createTextNode(substringData(offset, getLength() - offset));
    deleteData(0, offset);
    Node refNode = getNextSibling();
    if (refNode == null) {
        getParentNode().appendChild(newText);
    } else {
        getParentNode().insertBefore(newText, refNode);
    }
    return this;
}
Also used : Node(org.w3c.dom.Node) Text(org.w3c.dom.Text)

Example 44 with Text

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

the class DOMBuilder method characters.

/**
   * Receive notification of character data.
   *
   * <p>The Parser will call this method to report each chunk of
   * character data.  SAX parsers may return all contiguous character
   * data in a single chunk, or they may split it into several
   * chunks; however, all of the characters in any single event
   * must come from the same external entity, so that the Locator
   * provides useful information.</p>
   *
   * <p>The application must not attempt to read from the array
   * outside of the specified range.</p>
   *
   * <p>Note that some parsers will report whitespace using the
   * ignorableWhitespace() method rather than this one (validating
   * parsers must do so).</p>
   *
   * @param ch The characters from the XML document.
   * @param start The start position in the array.
   * @param length The number of characters to read from the array.
   * @see #ignorableWhitespace
   * @see org.xml.sax.Locator
   */
public void characters(char[] ch, int start, int length) throws org.xml.sax.SAXException {
    if (isOutsideDocElem() && org.apache.xml.utils.XMLCharacterRecognizer.isWhiteSpace(ch, start, length))
        // avoid DOM006 Hierarchy request error
        return;
    if (m_inCData) {
        cdata(ch, start, length);
        return;
    }
    String s = new String(ch, start, length);
    Node childNode;
    childNode = m_currentNode != null ? m_currentNode.getLastChild() : null;
    if (childNode != null && childNode.getNodeType() == Node.TEXT_NODE) {
        ((Text) childNode).appendData(s);
    } else {
        Text text = m_doc.createTextNode(s);
        append(text);
    }
}
Also used : Node(org.w3c.dom.Node) Text(org.w3c.dom.Text)

Example 45 with Text

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

the class SimpleBuilderTest method getTextContent.

private String getTextContent(Node node) {
    String result = (node instanceof Text ? ((Text) node).getData() : "");
    Node child = node.getFirstChild();
    while (child != null) {
        result = result + getTextContent(child);
        child = child.getNextSibling();
    }
    return result;
}
Also used : Node(org.w3c.dom.Node) 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