1 package net.sf.saxon.dom; 2 3 import net.sf.saxon.Configuration; 4 import net.sf.saxon.event.PipelineConfiguration; 5 import net.sf.saxon.event.Receiver; 6 import net.sf.saxon.event.Sender; 7 import net.sf.saxon.expr.XPathContext; 8 import net.sf.saxon.om.*; 9 import net.sf.saxon.trans.DynamicError; 10 import net.sf.saxon.trans.XPathException; 11 import net.sf.saxon.value.SequenceExtent; 12 import net.sf.saxon.value.Value; 13 import org.w3c.dom.Node ; 14 import org.w3c.dom.NodeList ; 15 16 import javax.xml.transform.Result ; 17 import javax.xml.transform.Source ; 18 import javax.xml.transform.dom.DOMSource ; 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.HashSet ; 22 import java.util.List ; 23 24 29 30 33 public class DOMEnvelope implements ExternalObjectModel, Serializable { 34 35 public DOMEnvelope() {} 36 37 40 41 public boolean isRecognizedNode(Object object) { 42 return object instanceof NodeOverNodeInfo; 43 } 44 45 52 53 public boolean isRecognizedNodeClass(Class nodeClass) { 54 return NodeOverNodeInfo.class.isAssignableFrom(nodeClass); 55 } 56 57 64 65 public boolean isRecognizedNodeListClass(Class nodeClass) { 66 return NodeList .class.isAssignableFrom(nodeClass); 67 } 68 69 77 78 public Receiver getDocumentBuilder(Result result) throws XPathException { 79 return null; 80 } 81 82 90 91 public boolean sendSource(Source source, Receiver receiver, PipelineConfiguration pipe) throws XPathException { 92 if (source instanceof DOMSource ) { 93 Node startNode = ((DOMSource )source).getNode(); 94 if (startNode instanceof NodeOverNodeInfo) { 95 NodeInfo base = ((NodeOverNodeInfo)startNode).getUnderlyingNodeInfo(); 96 Sender driver = new Sender(pipe); 97 driver.send(base, receiver); 98 return true; 99 } 100 } 101 return false; 102 } 103 104 108 109 public NodeInfo unravel(Source source, Configuration config) { 110 111 if (source instanceof DOMSource ) { 112 NodeInfo start; 113 Node dsnode = ((DOMSource )source).getNode(); 114 if (dsnode instanceof NodeOverNodeInfo) { 115 start = ((NodeOverNodeInfo)dsnode).getUnderlyingNodeInfo(); 117 return start; 118 } 119 } 120 return null; 121 } 122 123 129 130 public Value convertObjectToXPathValue(Object object, Configuration config) throws XPathException { 131 if (object instanceof NodeList ) { 132 NodeList list = ((NodeList )object); 133 NodeInfo[] nodes = new NodeInfo[list.getLength()]; 134 for (int i=0; i<list.getLength(); i++) { 135 if (list.item(i) instanceof NodeOverNodeInfo) { 136 nodes[i] = ((NodeOverNodeInfo)list.item(i)).getUnderlyingNodeInfo(); 137 } else { 138 return null; 139 } 140 } 141 return new SequenceExtent(nodes); 142 143 } else if (object instanceof NodeOverNodeInfo) { 146 return Value.asValue(((NodeOverNodeInfo)object).getUnderlyingNodeInfo()); 147 } else { 148 return null; 149 } 150 } 151 152 161 162 public Object convertXPathValueToObject(Value value, Class target, XPathContext context) throws XPathException { 163 boolean requireDOM = 167 (Node .class.isAssignableFrom(target) || (target == NodeList .class) || 168 (target.isArray() && Node .class.isAssignableFrom(target.getComponentType()))); 169 170 173 boolean allowDOM = 174 (target == Object .class || target.isAssignableFrom(ArrayList .class) || 175 target.isAssignableFrom(HashSet .class) || 176 (target.isArray() && target.getComponentType() == Object .class)); 177 if (!(requireDOM || allowDOM)) { 178 return null; 179 } 180 List nodes = new ArrayList (20); 181 182 SequenceIterator iter = value.iterate(context); 183 while (true) { 184 Item item = iter.next(); 185 if (item == null) { 186 break; 187 } 188 if (item instanceof VirtualNode) { 189 Object o = ((VirtualNode)item).getUnderlyingNode(); 190 if (o instanceof Node ) { 191 nodes.add(o); 192 } else { 193 if (requireDOM) { 194 DynamicError err = new DynamicError("Extension function required class " + target.getName() + 195 "; supplied value of class " + item.getClass().getName() + 196 " could not be converted"); 197 throw err; 198 }; 199 } 200 } else if (requireDOM) { 201 if (item instanceof NodeInfo) { 202 nodes.add(NodeOverNodeInfo.wrap((NodeInfo)item)); 203 } else { 204 DynamicError err = new DynamicError("Extension function required class " + target.getName() + 205 "; supplied value of class " + item.getClass().getName() + 206 " could not be converted"); 207 throw err; 208 } 209 } else { 210 return null; } 212 } 213 214 if (nodes.size() == 0 && !requireDOM) { 215 return null; } 217 if (Node .class.isAssignableFrom(target)) { 218 if (nodes.size() != 1) { 219 DynamicError err = new DynamicError("Extension function requires a single DOM Node" + 220 "; supplied value contains " + nodes.size() + " nodes"); 221 throw err; 222 } 223 return nodes.get(0); 224 } else if (target == NodeList .class) { 226 return new DOMNodeList(nodes); 227 } else if (target.isArray() && target.getComponentType() == Node .class) { 228 Node [] array = new Node [nodes.size()]; 229 nodes.toArray(array); 230 return array; 231 } else if (target.isAssignableFrom(ArrayList .class)) { 232 return nodes; 233 } else if (target.isAssignableFrom(HashSet .class)) { 234 return new HashSet (nodes); 235 } else { 236 return null; 238 } 239 } 240 241 250 251 public DocumentInfo wrapDocument(Object node, String baseURI, Configuration config) { 252 if (node instanceof DocumentOverNodeInfo) { 253 return (DocumentInfo)((DocumentOverNodeInfo)node).getUnderlyingNodeInfo(); 254 } 255 if (node instanceof NodeOverNodeInfo) { 256 return ((NodeOverNodeInfo)node).getUnderlyingNodeInfo().getDocumentRoot(); 257 } 258 throw new IllegalArgumentException ("Unknown node class " + node.getClass()); 259 } 260 261 270 271 public NodeInfo wrapNode(DocumentInfo document, Object node) { 272 if (node instanceof NodeOverNodeInfo) { 273 return ((NodeOverNodeInfo)node).getUnderlyingNodeInfo(); 274 } 275 throw new IllegalArgumentException ("Unknown node class " + node.getClass()); 276 } 277 278 285 286 public Object convertToNodeList(SequenceExtent extent) { 287 return null; 288 } 289 } 290 291 292 | Popular Tags |