Search in sources :

Example 51 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.

the class MemtreeTest method getNode.

// TODO(AR) Move DOM like tests below to somewhere more appropriate
@Test
public void getNode() throws IOException, ParserConfigurationException, SAXException {
    final DocumentImpl doc;
    try (final InputStream is = new UnsynchronizedByteArrayInputStream(XML.getBytes(UTF_8))) {
        doc = parse(is);
    }
    // the document node
    assertTrue(doc.getNode(0) instanceof DocumentImpl);
    // <!-- comment before doc 1 -->
    assertTrue(doc.getNode(1) instanceof CommentImpl);
    // <?pi-before-doc-1?>
    assertTrue(doc.getNode(2) instanceof ProcessingInstructionImpl);
    // <!-- comment before doc 2 -->
    assertTrue(doc.getNode(3) instanceof CommentImpl);
    // <?pi-before-doc-2?>
    assertTrue(doc.getNode(4) instanceof ProcessingInstructionImpl);
    // doc-element
    assertTrue(doc.getNode(5) instanceof ElementImpl);
    // doc-element/text()[1]
    assertTrue(doc.getNode(6) instanceof TextImpl);
    // <!-- comment before e1 -->
    assertTrue(doc.getNode(7) instanceof CommentImpl);
    // doc-element/text()[2]
    assertTrue(doc.getNode(8) instanceof TextImpl);
    // e1
    assertTrue(doc.getNode(9) instanceof ElementImpl);
    // e1/text()[1]
    assertTrue(doc.getNode(10) instanceof TextImpl);
    // <?pi-before-e1_1?>
    assertTrue(doc.getNode(11) instanceof ProcessingInstructionImpl);
    // e1/text()[2]
    assertTrue(doc.getNode(12) instanceof TextImpl);
    // e1_1
    assertTrue(doc.getNode(13) instanceof ElementImpl);
    // e1_1/text()[1]
    assertTrue(doc.getNode(14) instanceof TextImpl);
    // e1/text()[3]
    assertTrue(doc.getNode(15) instanceof TextImpl);
    // e1_2
    assertTrue(doc.getNode(16) instanceof ElementImpl);
    // e1_2/text()[1]
    assertTrue(doc.getNode(17) instanceof TextImpl);
    // e1/text()[4]
    assertTrue(doc.getNode(18) instanceof TextImpl);
    // doc-element/text()[3]
    assertTrue(doc.getNode(19) instanceof TextImpl);
    // <!-- comment after e1 -->
    assertTrue(doc.getNode(20) instanceof CommentImpl);
    // doc-element/text()[4]
    assertTrue(doc.getNode(21) instanceof TextImpl);
    // <?pi-after-doc-1?>
    assertTrue(doc.getNode(22) instanceof ProcessingInstructionImpl);
    // <!-- comment after doc 1 -->
    assertTrue(doc.getNode(23) instanceof CommentImpl);
    // <?pi-after-doc-2?>
    assertTrue(doc.getNode(24) instanceof ProcessingInstructionImpl);
    // <!-- comment after doc 2 -->
    assertTrue(doc.getNode(25) instanceof CommentImpl);
    // <?pi-after-doc-3?>
    assertTrue(doc.getNode(26) instanceof ProcessingInstructionImpl);
}
Also used : UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) Test(org.junit.Test)

Example 52 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.

the class MemtreeTest method getChildNodes.

@Test
public void getChildNodes() throws IOException, ParserConfigurationException, SAXException {
    final NodeImpl doc;
    try (final InputStream is = new UnsynchronizedByteArrayInputStream(XML.getBytes(UTF_8))) {
        doc = parse(is);
    }
    // children of the document node
    final NodeList docChildren = doc.getChildNodes();
    assertEquals(10, docChildren.getLength());
    // children of <!-- comment before doc 1 -->
    final Node commentBeforeDoc1 = docChildren.item(0);
    assertTrue(commentBeforeDoc1 instanceof CommentImpl);
    assertEquals(0, commentBeforeDoc1.getChildNodes().getLength());
    // children of <?pi-before-doc-1?>
    final Node piBeforeDoc1 = docChildren.item(1);
    assertTrue(piBeforeDoc1 instanceof ProcessingInstructionImpl);
    assertEquals(0, piBeforeDoc1.getChildNodes().getLength());
    // children of <!-- comment before doc 2 -->
    final Node commentBeforeDoc2 = docChildren.item(2);
    assertTrue(commentBeforeDoc2 instanceof CommentImpl);
    assertEquals(0, commentBeforeDoc2.getChildNodes().getLength());
    // children of <?pi-before-doc-2?>
    final Node piBeforeDoc2 = docChildren.item(3);
    assertTrue(piBeforeDoc2 instanceof ProcessingInstructionImpl);
    assertEquals(0, piBeforeDoc2.getChildNodes().getLength());
    // children of doc-element
    final Node docElement = docChildren.item(4);
    assertTrue(docElement instanceof ElementImpl);
    assertEquals("doc-element", docElement.getLocalName());
    final NodeList docElementChildren = docElement.getChildNodes();
    assertEquals(7, docElementChildren.getLength());
    // children of doc-element/text()[1]
    final Node docElementText1 = docElementChildren.item(0);
    assertTrue(docElementText1 instanceof TextImpl);
    assertEquals(0, docElementText1.getChildNodes().getLength());
    // children of <!-- comment before e1 -->
    final Node commentBeforeE1 = docElementChildren.item(1);
    assertTrue(commentBeforeE1 instanceof CommentImpl);
    assertEquals(0, commentBeforeE1.getChildNodes().getLength());
    // children of doc-element/text()[2]
    final Node docElementText2 = docElementChildren.item(2);
    assertTrue(docElementText2 instanceof TextImpl);
    assertEquals(0, docElementText2.getChildNodes().getLength());
    // children of e1
    final Node e1 = docElementChildren.item(3);
    assertTrue(e1 instanceof ElementImpl);
    assertEquals("e1", e1.getLocalName());
    final NodeList e1Children = e1.getChildNodes();
    assertEquals(7, e1Children.getLength());
    // children of e1/text()[1]
    final Node e1Text1 = e1Children.item(0);
    assertTrue(e1Text1 instanceof TextImpl);
    assertEquals(0, e1Text1.getChildNodes().getLength());
    // children of <?pi-before-e1_1?>
    final Node piBeforeE11 = e1Children.item(1);
    assertTrue(piBeforeE11 instanceof ProcessingInstructionImpl);
    assertEquals(0, piBeforeE11.getChildNodes().getLength());
    // children of e1/text()[2]
    final Node e1Text2 = e1Children.item(2);
    assertTrue(e1Text2 instanceof TextImpl);
    assertEquals(0, e1Text2.getChildNodes().getLength());
    // children of e1_1
    final Node e11 = e1Children.item(3);
    assertTrue(e11 instanceof ElementImpl);
    assertEquals("e1_1", e11.getLocalName());
    final NodeList e11Children = e11.getChildNodes();
    assertEquals(1, e11Children.getLength());
    // children of e1_1/text()[1]
    final Node e11Text1 = e11Children.item(0);
    assertTrue(e11Text1 instanceof TextImpl);
    assertEquals(0, e11Text1.getChildNodes().getLength());
    // children of e1/text()[2]
    final Node e1Text3 = e1Children.item(4);
    assertTrue(e1Text3 instanceof TextImpl);
    assertEquals(0, e1Text3.getChildNodes().getLength());
    // children of e1_2
    final Node e12 = e1Children.item(5);
    assertTrue(e12 instanceof ElementImpl);
    assertEquals("e1_2", e12.getLocalName());
    final NodeList e12Children = e12.getChildNodes();
    assertEquals(1, e12Children.getLength());
    // children of e1_2/text()[1]
    final Node e12Text1 = e12Children.item(0);
    assertTrue(e12Text1 instanceof TextImpl);
    assertEquals(0, e12Text1.getChildNodes().getLength());
    // children of e1/text()[4]
    final Node e1Text4 = e1Children.item(6);
    assertTrue(e1Text4 instanceof TextImpl);
    assertEquals(0, e1Text4.getChildNodes().getLength());
    // children of doc-element/text()[3]
    final Node docElementText3 = docElementChildren.item(4);
    assertTrue(docElementText3 instanceof TextImpl);
    assertEquals(0, docElementText3.getChildNodes().getLength());
    // children of <!-- comment after e1 -->
    final Node commentAfterE1 = docElementChildren.item(5);
    assertTrue(commentAfterE1 instanceof CommentImpl);
    assertEquals(0, commentAfterE1.getChildNodes().getLength());
    // children of doc-element/text()[4]
    final Node docElementText4 = docElementChildren.item(6);
    assertTrue(docElementText4 instanceof TextImpl);
    assertEquals(0, docElementText4.getChildNodes().getLength());
    // children of <?pi-after-doc-1?>
    final Node piAfterDoc1 = docChildren.item(5);
    assertTrue(piAfterDoc1 instanceof ProcessingInstructionImpl);
    assertEquals(0, piAfterDoc1.getChildNodes().getLength());
    // children of <!-- comment after doc 1 -->
    final Node commentAfterDoc1 = docChildren.item(6);
    assertTrue(commentAfterDoc1 instanceof CommentImpl);
    assertEquals(0, commentAfterDoc1.getChildNodes().getLength());
    // children of <?pi-after-doc-2?>
    final Node piAfterDoc2 = docChildren.item(7);
    assertTrue(piAfterDoc2 instanceof ProcessingInstructionImpl);
    assertEquals(0, piAfterDoc2.getChildNodes().getLength());
    // children of <!-- comment after doc 2 -->
    final Node commentAfterDoc2 = docChildren.item(8);
    assertTrue(commentAfterDoc2 instanceof CommentImpl);
    assertEquals(0, commentAfterDoc2.getChildNodes().getLength());
    // children of <?pi-after-doc-2?>
    final Node piAfterDoc3 = docChildren.item(9);
    assertTrue(piAfterDoc3 instanceof ProcessingInstructionImpl);
    assertEquals(0, piAfterDoc3.getChildNodes().getLength());
}
Also used : UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) Test(org.junit.Test)

Example 53 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.

the class MemtreeTest method getNextSiblingFor.

@Test
public void getNextSiblingFor() throws IOException, ParserConfigurationException, SAXException {
    final DocumentImpl doc;
    try (final InputStream is = new UnsynchronizedByteArrayInputStream(XML.getBytes(UTF_8))) {
        doc = parse(is);
    }
    // next sibling of the document node
    assertEquals(-1, doc.getNextSiblingFor(0));
    // next sibling of <!-- comment before doc 1 -->
    assertEquals(2, doc.getNextSiblingFor(1));
    // next sibling of <?pi-before-doc-1?>
    assertEquals(3, doc.getNextSiblingFor(2));
    // next sibling of <!-- comment before doc 2 -->
    assertEquals(4, doc.getNextSiblingFor(3));
    // next sibling of <?pi-before-doc-2?>
    assertEquals(5, doc.getNextSiblingFor(4));
    // next sibling of doc-element
    assertEquals(22, doc.getNextSiblingFor(5));
    // next sibling of doc-element/text()[1]
    assertEquals(7, doc.getNextSiblingFor(6));
    // next sibling of <!-- comment before e1 -->
    assertEquals(8, doc.getNextSiblingFor(7));
    // next sibling of doc-element/text()[2]
    assertEquals(9, doc.getNextSiblingFor(8));
    // next sibling of e1
    assertEquals(19, doc.getNextSiblingFor(9));
    // next sibling of e1/text()[1]
    assertEquals(11, doc.getNextSiblingFor(10));
    // next sibling of <?pi-before-e1_1?>
    assertEquals(12, doc.getNextSiblingFor(11));
    // next sibling of e1/text()[2]
    assertEquals(13, doc.getNextSiblingFor(12));
    // next sibling of e1_1
    assertEquals(15, doc.getNextSiblingFor(13));
    // next sibling of e1_1/text()[1]
    assertEquals(-1, doc.getNextSiblingFor(14));
    // next sibling of e1/text()[3]
    assertEquals(16, doc.getNextSiblingFor(15));
    // next sibling of e1_2
    assertEquals(18, doc.getNextSiblingFor(16));
    // next sibling of e1_2/text()[1]
    assertEquals(-1, doc.getNextSiblingFor(17));
    // next sibling of e1/text()[4]
    assertEquals(-1, doc.getNextSiblingFor(18));
    // next sibling of doc-element/text()[3]
    assertEquals(20, doc.getNextSiblingFor(19));
    // next sibling of <!-- comment after e1 -->
    assertEquals(21, doc.getNextSiblingFor(20));
    // next sibling of doc-element/text()[4]
    assertEquals(-1, doc.getNextSiblingFor(21));
    // next sibling of <?pi-after-doc-1?>
    assertEquals(23, doc.getNextSiblingFor(22));
    // next sibling of <!-- comment after doc 1 -->
    assertEquals(24, doc.getNextSiblingFor(23));
    // next sibling of <?pi-after-doc-2?>
    assertEquals(25, doc.getNextSiblingFor(24));
    // next sibling of <!-- comment after doc 2 -->
    assertEquals(26, doc.getNextSiblingFor(25));
    // next sibling of <?pi-after-doc-3?>
    assertEquals(-1, doc.getNextSiblingFor(26));
}
Also used : UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) Test(org.junit.Test)

Example 54 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.

the class MemtreeTest method getNodeType.

@Test
public void getNodeType() throws IOException, ParserConfigurationException, SAXException {
    final DocumentImpl doc;
    try (final InputStream is = new UnsynchronizedByteArrayInputStream(XML.getBytes(UTF_8))) {
        doc = parse(is);
    }
    // type of the document node
    assertEquals(Node.DOCUMENT_NODE, doc.getNodeType(0));
    // type of <!-- comment before doc 1 -->
    assertEquals(Node.COMMENT_NODE, doc.getNodeType(1));
    // type of <?pi-before-doc-1?>
    assertEquals(Node.PROCESSING_INSTRUCTION_NODE, doc.getNodeType(2));
    // type of <!-- comment before doc 2 -->
    assertEquals(Node.COMMENT_NODE, doc.getNodeType(3));
    // type of <?pi-before-doc-2?>
    assertEquals(Node.PROCESSING_INSTRUCTION_NODE, doc.getNodeType(4));
    // type of doc-element
    assertEquals(Node.ELEMENT_NODE, doc.getNodeType(5));
    // type of doc-element/text()[1]
    assertEquals(Node.TEXT_NODE, doc.getNodeType(6));
    // type of <!-- comment before e1 -->
    assertEquals(Node.COMMENT_NODE, doc.getNodeType(7));
    // type of doc-element/text()[2]
    assertEquals(Node.TEXT_NODE, doc.getNodeType(8));
    // type of e1
    assertEquals(Node.ELEMENT_NODE, doc.getNodeType(9));
    // type of e1/text()[1]
    assertEquals(Node.TEXT_NODE, doc.getNodeType(10));
    // type of <?pi-before-e1_1?>
    assertEquals(Node.PROCESSING_INSTRUCTION_NODE, doc.getNodeType(11));
    // type of e1/text()[2]
    assertEquals(Node.TEXT_NODE, doc.getNodeType(12));
    // type of e1_1
    assertEquals(Node.ELEMENT_NODE, doc.getNodeType(13));
    // type of e1_1/text()[1]
    assertEquals(Node.TEXT_NODE, doc.getNodeType(14));
    // type of e1/text()[3]
    assertEquals(Node.TEXT_NODE, doc.getNodeType(15));
    // type of e1_2
    assertEquals(Node.ELEMENT_NODE, doc.getNodeType(16));
    // type of e1_2/text()[1]
    assertEquals(Node.TEXT_NODE, doc.getNodeType(17));
    // type of e1/text()[4]
    assertEquals(Node.TEXT_NODE, doc.getNodeType(18));
    // type of doc-element/text()[3]
    assertEquals(Node.TEXT_NODE, doc.getNodeType(19));
    // type of <!-- comment after e1 -->
    assertEquals(Node.COMMENT_NODE, doc.getNodeType(20));
    // type of doc-element/text()[4]
    assertEquals(Node.TEXT_NODE, doc.getNodeType(21));
    // type of <?pi-after-doc-1?>
    assertEquals(Node.PROCESSING_INSTRUCTION_NODE, doc.getNodeType(22));
    // type of <!-- comment after doc 1 -->
    assertEquals(Node.COMMENT_NODE, doc.getNodeType(23));
    // type of <?pi-after-doc-2?>
    assertEquals(Node.PROCESSING_INSTRUCTION_NODE, doc.getNodeType(24));
    // type of <!-- comment after doc 2 -->
    assertEquals(Node.COMMENT_NODE, doc.getNodeType(25));
    // type of <?pi-after-doc-3?>
    assertEquals(Node.PROCESSING_INSTRUCTION_NODE, doc.getNodeType(26));
}
Also used : UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) Test(org.junit.Test)

Example 55 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.

the class MemtreeTest method getFirstChildFor.

@Test
public void getFirstChildFor() throws IOException, ParserConfigurationException, SAXException {
    final DocumentImpl doc;
    try (final InputStream is = new UnsynchronizedByteArrayInputStream(XML.getBytes(UTF_8))) {
        doc = parse(is);
    }
    // first child of the document node
    assertEquals(1, doc.getFirstChildFor(0));
    // first child of <!-- comment before doc 1 -->
    assertEquals(-1, doc.getFirstChildFor(1));
    // first child of <?pi-before-doc-1?>
    assertEquals(-1, doc.getFirstChildFor(2));
    // first child of <!-- comment before doc 2 -->
    assertEquals(-1, doc.getFirstChildFor(3));
    // first child of <?pi-before-doc-2?>
    assertEquals(-1, doc.getFirstChildFor(4));
    // first child of doc-element
    assertEquals(6, doc.getFirstChildFor(5));
    // first child of doc-element/text()[1]
    assertEquals(-1, doc.getFirstChildFor(6));
    // first child of <!-- comment before e1 -->
    assertEquals(-1, doc.getFirstChildFor(7));
    // first child of doc-element/text()[2]
    assertEquals(-1, doc.getFirstChildFor(8));
    // first child of e1
    assertEquals(10, doc.getFirstChildFor(9));
    // first child of e1/text()[1]
    assertEquals(-1, doc.getFirstChildFor(10));
    // first child of <?pi-before-e1_1?>
    assertEquals(-1, doc.getFirstChildFor(11));
    // first child of e1/text()[2]
    assertEquals(-1, doc.getFirstChildFor(12));
    // first child of e1_1
    assertEquals(14, doc.getFirstChildFor(13));
    // first child of e1_1/text()[1]
    assertEquals(-1, doc.getFirstChildFor(14));
    // first child of e1/text()[3]
    assertEquals(-1, doc.getFirstChildFor(15));
    // first child of e1_2
    assertEquals(17, doc.getFirstChildFor(16));
    // first child of e1_2/text()[1]
    assertEquals(-1, doc.getFirstChildFor(17));
    // first child of e1/text()[4]
    assertEquals(-1, doc.getFirstChildFor(18));
    // first child of doc-element/text()[3]
    assertEquals(-1, doc.getFirstChildFor(19));
    // first child of <!-- comment after e1 -->
    assertEquals(-1, doc.getFirstChildFor(20));
    // first child of doc-element/text()[4]
    assertEquals(-1, doc.getFirstChildFor(21));
    // first child of <?pi-after-doc-1?>
    assertEquals(-1, doc.getFirstChildFor(22));
    // first child of <!-- comment after doc 1 -->
    assertEquals(-1, doc.getFirstChildFor(23));
    // first child of <?pi-after-doc-2?>
    assertEquals(-1, doc.getFirstChildFor(24));
    // first child of <!-- comment after doc 2 -->
    assertEquals(-1, doc.getFirstChildFor(25));
    // first child of <?pi-after-doc-3?>
    assertEquals(-1, doc.getFirstChildFor(26));
}
Also used : UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) Test(org.junit.Test)

Aggregations

UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)114 InputStream (java.io.InputStream)102 Test (org.junit.Test)93 MarkShieldInputStream (org.apache.commons.io.input.MarkShieldInputStream)31 UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)10 IOException (java.io.IOException)8 FilterInputStream (java.io.FilterInputStream)7 CachingFilterInputStream (org.exist.util.io.CachingFilterInputStream)7 XMLResource (org.xmldb.api.modules.XMLResource)6 DBBroker (org.exist.storage.DBBroker)5 Txn (org.exist.storage.txn.Txn)5 Element (org.w3c.dom.Element)4 Collection (org.xmldb.api.base.Collection)4 NodeProxy (org.exist.dom.persistent.NodeProxy)3 PermissionDeniedException (org.exist.security.PermissionDeniedException)3 DigestInputStream (org.exist.util.crypto.digest.DigestInputStream)3 Base64BinaryValueType (org.exist.xquery.value.Base64BinaryValueType)3 BooleanValue (org.exist.xquery.value.BooleanValue)3 DoubleValue (org.exist.xquery.value.DoubleValue)3 StringValue (org.exist.xquery.value.StringValue)3