| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.dom; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.event.Receiver; |
| 12 |
import net.sf.saxon.event.Sender; |
| 13 |
import net.sf.saxon.expr.JPConverter; |
| 14 |
import net.sf.saxon.expr.PJConverter; |
| 15 |
import net.sf.saxon.expr.XPathContext; |
| 16 |
import net.sf.saxon.lib.ExternalObjectModel; |
| 17 |
import net.sf.saxon.om.NodeInfo; |
| 18 |
import net.sf.saxon.om.Sequence; |
| 19 |
import net.sf.saxon.pattern.AnyNodeTest; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.type.ItemType; |
| 22 |
import net.sf.saxon.value.SequenceExtent; |
| 23 |
import org.w3c.dom.Node; |
| 24 |
import org.w3c.dom.NodeList; |
| 25 |
|
| 26 |
import javax.xml.transform.Result; |
| 27 |
import javax.xml.transform.Source; |
| 28 |
import javax.xml.transform.dom.DOMSource; |
| 29 |
import javax.xml.xpath.XPathConstants; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class DOMEnvelope implements ExternalObjectModel { |
| 38 |
|
| 39 |
private static DOMEnvelope THE_INSTANCE = new DOMEnvelope(); |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public static DOMEnvelope getInstance() { |
| 48 |
return THE_INSTANCE; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public DOMEnvelope() { |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public String getDocumentClassName() { |
| 66 |
return "org.w3c.dom.Document"; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public String getIdentifyingURI() { |
| 75 |
return XPathConstants.DOM_OBJECT_MODEL; |
| 76 |
} |
| 77 |
|
| 78 |
public PJConverter getPJConverter(Class<?> targetClass) { |
| 79 |
if (NodeOverNodeInfo.class.isAssignableFrom(targetClass)) { |
| 80 |
return new PJConverter() { |
| 81 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 82 |
return DOMObjectModel.convertXPathValueToObject(value, targetClass); |
| 83 |
} |
| 84 |
}; |
| 85 |
} else if (NodeList.class.isAssignableFrom(targetClass)) { |
| 86 |
return new PJConverter() { |
| 87 |
public Object convert(Sequence<?> value, Class<?> targetClass, XPathContext context) throws XPathException { |
| 88 |
return DOMObjectModel.convertXPathValueToObject(value, targetClass); |
| 89 |
} |
| 90 |
}; |
| 91 |
} else { |
| 92 |
return null; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
public JPConverter getJPConverter(Class sourceClass, Configuration config) { |
| 97 |
if (NodeOverNodeInfo.class.isAssignableFrom(sourceClass)) { |
| 98 |
return new JPConverter() { |
| 99 |
|
| 100 |
public Sequence<?> convert(Object object, XPathContext context) { |
| 101 |
return convertObjectToXPathValue(object); |
| 102 |
} |
| 103 |
|
| 104 |
public ItemType getItemType() { |
| 105 |
return AnyNodeTest.getInstance(); |
| 106 |
} |
| 107 |
}; |
| 108 |
} else { |
| 109 |
return null; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public PJConverter getNodeListCreator(Object node) { |
| 124 |
|
| 125 |
return null; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public boolean isRecognizedNode(Object object) { |
| 136 |
return object instanceof NodeOverNodeInfo; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public boolean isRecognizedNodeClass(Class nodeClass) { |
| 148 |
return NodeOverNodeInfo.class.isAssignableFrom(nodeClass); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public Receiver getDocumentBuilder(Result result) { |
| 160 |
return null; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public boolean sendSource(Source source, Receiver receiver) throws XPathException { |
| 172 |
if (source instanceof DOMSource) { |
| 173 |
Node startNode = ((DOMSource) source).getNode(); |
| 174 |
if (startNode instanceof NodeOverNodeInfo) { |
| 175 |
NodeInfo base = ((NodeOverNodeInfo) startNode).getUnderlyingNodeInfo(); |
| 176 |
Sender.send(base, receiver, null); |
| 177 |
return true; |
| 178 |
} |
| 179 |
} |
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public NodeInfo unravel(Source source, Configuration config) { |
| 189 |
|
| 190 |
if (source instanceof DOMSource) { |
| 191 |
Node dsnode = ((DOMSource) source).getNode(); |
| 192 |
if (dsnode instanceof NodeOverNodeInfo) { |
| 193 |
|
| 194 |
return ((NodeOverNodeInfo) dsnode).getUnderlyingNodeInfo(); |
| 195 |
} |
| 196 |
} |
| 197 |
return null; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
private Sequence<?> convertObjectToXPathValue(Object object) { |
| 211 |
if (object instanceof NodeList) { |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
NodeList list = (NodeList) object; |
| 216 |
final int len = list.getLength(); |
| 217 |
if (len == 0) { |
| 218 |
return null; |
| 219 |
} |
| 220 |
NodeInfo[] nodes = new NodeInfo[len]; |
| 221 |
for (int i = 0; i < len; i++) { |
| 222 |
if (list.item(i) instanceof NodeOverNodeInfo) { |
| 223 |
nodes[i] = ((NodeOverNodeInfo) list.item(i)).getUnderlyingNodeInfo(); |
| 224 |
} else { |
| 225 |
return null; |
| 226 |
} |
| 227 |
} |
| 228 |
return new SequenceExtent(nodes); |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
} else if (object instanceof NodeOverNodeInfo) { |
| 233 |
return ((NodeOverNodeInfo) object).getUnderlyingNodeInfo(); |
| 234 |
} else { |
| 235 |
return null; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
} |
| 240 |
|
| 241 |
|