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();
}
}
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();
}
}
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;
}
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);
}
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();
}
Aggregations