Search in sources :

Example 1 with NodeOverNodeInfo

use of net.sf.saxon.dom.NodeOverNodeInfo 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)

Aggregations

LinkedList (java.util.LinkedList)1 List (java.util.List)1 DOMNodeList (net.sf.saxon.dom.DOMNodeList)1 NodeOverNodeInfo (net.sf.saxon.dom.NodeOverNodeInfo)1 NodeInfo (net.sf.saxon.om.NodeInfo)1 FallbackConverter (org.apache.camel.FallbackConverter)1 TypeConverter (org.apache.camel.TypeConverter)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1