Search in sources :

Example 26 with NodeInfo

use of net.sf.saxon.om.NodeInfo in project checkstyle by checkstyle.

the class DescendantIteratorTest method testWithoutSelf.

@Test
public void testWithoutSelf() {
    final NodeInfo startNode = findNode("CLASS_DEF");
    try (DescendantIterator iterator = new DescendantIterator(startNode, DescendantIterator.StartWith.CHILDREN)) {
        assertWithMessage("Invalid node").that(iterator.next()).isEqualTo(findNode("OBJBLOCK"));
        assertWithMessage("Invalid node").that(iterator.next()).isEqualTo(findNode("LCURLY"));
        assertWithMessage("Invalid node").that(iterator.next()).isEqualTo(findNode("METHOD_DEF"));
        assertWithMessage("Invalid node").that(iterator.next()).isEqualTo(findNode("RCURLY"));
        assertWithMessage("Invalid node").that(iterator.next()).isEqualTo(findNode("PARAMETERS"));
        assertWithMessage("Invalid node").that(iterator.next()).isEqualTo(findNode("SLIST"));
        assertWithMessage("Node should be null").that(iterator.next()).isNull();
    }
}
Also used : NodeInfo(net.sf.saxon.om.NodeInfo) Test(org.junit.jupiter.api.Test)

Example 27 with NodeInfo

use of net.sf.saxon.om.NodeInfo in project checkstyle by checkstyle.

the class PrecedingIteratorTest method testReverseOrderOfDescendants.

@Test
public void testReverseOrderOfDescendants() {
    final NodeInfo startNode = findNode("RCURLY");
    try (PrecedingIterator iterator = new PrecedingIterator(startNode)) {
        assertWithMessage("Invalid node").that(iterator.next()).isEqualTo(findNode("METHOD_DEF"));
        assertWithMessage("Invalid node").that(iterator.next()).isEqualTo(findNode("SLIST"));
        assertWithMessage("Invalid node").that(iterator.next()).isEqualTo(findNode("PARAMETERS"));
        assertWithMessage("Invalid node").that(iterator.next()).isEqualTo(findNode("LCURLY"));
        assertWithMessage("Invalid node").that(iterator.next()).isEqualTo(findNode("OBJBLOCK"));
        assertWithMessage("Invalid node").that(iterator.next()).isEqualTo(findNode("CLASS_DEF"));
        assertWithMessage("Invalid node").that(iterator.next()).isEqualTo(findNode("ANNOTATIONS"));
        assertWithMessage("Invalid node").that(iterator.next()).isEqualTo(findNode("ANNOTATION_DEF"));
        assertWithMessage("Invalid node").that(iterator.next()).isEqualTo(findNode("PACKAGE_DEF"));
        assertWithMessage("Invalid node").that(iterator.next()).isEqualTo(findNode("ROOT"));
        assertWithMessage("Node should be null").that(iterator.next()).isNull();
    }
}
Also used : NodeInfo(net.sf.saxon.om.NodeInfo) Test(org.junit.jupiter.api.Test)

Example 28 with NodeInfo

use of net.sf.saxon.om.NodeInfo in project camel by apache.

the class SaxonConverter method convertTo.

@FallbackConverter
public static <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
    if (NodeInfo.class.isAssignableFrom(value.getClass())) {
        // use a fallback type converter so we can convert the embedded body if the value is NodeInfo
        NodeInfo ni = (NodeInfo) value;
        // first try to find a Converter for Node
        TypeConverter tc = registry.lookup(type, Node.class);
        if (tc != null) {
            Node node = NodeOverNodeInfo.wrap(ni);
            return tc.convertTo(type, exchange, node);
        }
        // if this does not exist we can also try NodeList (there are some type converters for that) as
        // the default Xerces Node implementation also implements NodeList.
        tc = registry.lookup(type, NodeList.class);
        if (tc != null) {
            List<NodeInfo> nil = new LinkedList<NodeInfo>();
            nil.add((NodeInfo) value);
            return tc.convertTo(type, exchange, toDOMNodeList(nil));
        }
    } else if (List.class.isAssignableFrom(value.getClass())) {
        TypeConverter tc = registry.lookup(type, NodeList.class);
        if (tc != null) {
            List<NodeInfo> lion = new LinkedList<NodeInfo>();
            for (Object o : (List<?>) value) {
                if (o instanceof NodeInfo) {
                    lion.add((NodeInfo) o);
                }
            }
            if (lion.size() > 0) {
                NodeList nl = toDOMNodeList(lion);
                return tc.convertTo(type, exchange, nl);
            }
        }
    } else if (NodeOverNodeInfo.class.isAssignableFrom(value.getClass())) {
        // NodeOverNode info is a read-only Node implementation from Saxon. In contrast to the JDK
        // com.sun.org.apache.xerces.internal.dom.NodeImpl class it does not implement NodeList, but
        // many Camel type converters are based on that interface. Therefore we convert to NodeList and
        // try type conversion in the fallback type converter.
        TypeConverter tc = registry.lookup(type, NodeList.class);
        if (tc != null) {
            List<Node> domNodeList = new LinkedList<Node>();
            domNodeList.add((NodeOverNodeInfo) value);
            return tc.convertTo(type, exchange, new DOMNodeList(domNodeList));
        }
    }
    return null;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) DOMNodeList(net.sf.saxon.dom.DOMNodeList) NodeOverNodeInfo(net.sf.saxon.dom.NodeOverNodeInfo) NodeInfo(net.sf.saxon.om.NodeInfo) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) DOMNodeList(net.sf.saxon.dom.DOMNodeList) NodeList(org.w3c.dom.NodeList) DOMNodeList(net.sf.saxon.dom.DOMNodeList) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList) FallbackConverter(org.apache.camel.FallbackConverter)

Example 29 with NodeInfo

use of net.sf.saxon.om.NodeInfo in project camel by apache.

the class SaxonConverterTest method convertToNodeList.

@Test
public void convertToNodeList() throws XPathException {
    List<NodeInfo> nil = new LinkedList<NodeInfo>();
    nil.add(doc);
    NodeList nodeList = context.getTypeConverter().convertTo(NodeList.class, exchange, nil);
    assertNotNull(nodeList);
    assertEquals(1, nodeList.getLength());
    String string = context.getTypeConverter().convertTo(String.class, exchange, nodeList);
    assertEquals(CONTENT, string);
}
Also used : NodeInfo(net.sf.saxon.om.NodeInfo) NodeList(org.w3c.dom.NodeList) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 30 with NodeInfo

use of net.sf.saxon.om.NodeInfo in project sirix by sirixdb.

the class TestNodeWrapper method testCompareOrder.

@Test
public void testCompareOrder() throws XPathException, SirixException {
    final Processor proc = new Processor(false);
    final Configuration config = proc.getUnderlyingConfiguration();
    final Session session = generateSession();
    final NodeWriteTrx trx = session.beginNodeWriteTrx();
    trx.commit();
    trx.close();
    // Not the same document.
    NodeInfo node = new DocumentWrapper(session, config);
    NodeInfo other = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), config), 3);
    try {
        node.compareOrder(other);
        fail();
    } catch (final IllegalStateException e) {
    }
    // Before.
    node = new DocumentWrapper(mHolder.getSession(), config);
    other = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), config), 3);
    assertEquals(-1, node.compareOrder(other));
    // After.
    node = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), config), 3);
    other = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), config), 0);
    assertEquals(1, node.compareOrder(other));
    // Same.
    node = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), config), 3);
    other = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), config), 3);
    assertEquals(0, node.compareOrder(other));
    session.close();
    mDatabase.close();
}
Also used : Processor(net.sf.saxon.s9api.Processor) Configuration(net.sf.saxon.Configuration) DatabaseConfiguration(org.sirix.access.conf.DatabaseConfiguration) ResourceConfiguration(org.sirix.access.conf.ResourceConfiguration) SessionConfiguration(org.sirix.access.conf.SessionConfiguration) NodeInfo(net.sf.saxon.om.NodeInfo) NodeWriteTrx(org.sirix.api.NodeWriteTrx) Session(org.sirix.api.Session) NameTest(net.sf.saxon.pattern.NameTest) Test(org.junit.Test)

Aggregations

NodeInfo (net.sf.saxon.om.NodeInfo)52 Test (org.junit.Test)21 XPathExpression (javax.xml.xpath.XPathExpression)16 Test (org.junit.jupiter.api.Test)14 Processor (net.sf.saxon.s9api.Processor)8 Configuration (net.sf.saxon.Configuration)7 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)5 DocumentWrapper (org.sirix.saxon.wrapper.DocumentWrapper)5 NameTest (net.sf.saxon.pattern.NameTest)4 SaxonApiException (net.sf.saxon.s9api.SaxonApiException)4 XQueryCompiler (net.sf.saxon.s9api.XQueryCompiler)3 XQueryExecutable (net.sf.saxon.s9api.XQueryExecutable)3 AxisIterator (net.sf.saxon.tree.iter.AxisIterator)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Item (net.sf.saxon.om.Item)2 Serializer (net.sf.saxon.s9api.Serializer)2 NodeReadTrx (org.sirix.api.NodeReadTrx)2 NodeList (org.w3c.dom.NodeList)2