| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.query; |
| 9 |
|
| 10 |
import net.sf.saxon.event.Receiver; |
| 11 |
import net.sf.saxon.event.SequenceReceiver; |
| 12 |
import net.sf.saxon.expr.parser.ExplicitLocation; |
| 13 |
import net.sf.saxon.expr.parser.Location; |
| 14 |
import net.sf.saxon.lib.NamespaceConstant; |
| 15 |
import net.sf.saxon.ma.arrays.ArrayItem; |
| 16 |
import net.sf.saxon.ma.map.MapItem; |
| 17 |
import net.sf.saxon.om.*; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
import net.sf.saxon.type.*; |
| 20 |
import net.sf.saxon.value.AtomicValue; |
| 21 |
import net.sf.saxon.value.ObjectValue; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class SequenceWrapper extends SequenceReceiver { |
| 30 |
|
| 31 |
public static final String RESULT_NS = QueryResult.RESULT_NS; |
| 32 |
|
| 33 |
private Receiver out; |
| 34 |
private int depth = 0; |
| 35 |
|
| 36 |
private FingerprintedQName resultDocument; |
| 37 |
private FingerprintedQName resultElement; |
| 38 |
private FingerprintedQName resultAttribute; |
| 39 |
private FingerprintedQName resultText; |
| 40 |
private FingerprintedQName resultComment; |
| 41 |
private FingerprintedQName resultPI; |
| 42 |
private FingerprintedQName resultNamespace; |
| 43 |
private FingerprintedQName resultAtomicValue; |
| 44 |
private FingerprintedQName resultFunction; |
| 45 |
private FingerprintedQName resultArray; |
| 46 |
private FingerprintedQName resultMap; |
| 47 |
private FingerprintedQName resultExternalValue; |
| 48 |
private FingerprintedQName xsiType; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public SequenceWrapper(Receiver destination) { |
| 58 |
super(destination.getPipelineConfiguration()); |
| 59 |
out = destination; |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
public Receiver getDestination() { |
| 64 |
return out; |
| 65 |
} |
| 66 |
|
| 67 |
public void open() throws XPathException { |
| 68 |
|
| 69 |
|
| 70 |
FingerprintedQName resultSequence = new FingerprintedQName("result", RESULT_NS, "sequence"); |
| 71 |
resultDocument = new FingerprintedQName("result", RESULT_NS, "document"); |
| 72 |
resultElement = new FingerprintedQName("result", RESULT_NS, "element"); |
| 73 |
resultAttribute = new FingerprintedQName("result", RESULT_NS, "attribute"); |
| 74 |
resultText = new FingerprintedQName("result", RESULT_NS, "text"); |
| 75 |
resultComment = new FingerprintedQName("result", RESULT_NS, "comment"); |
| 76 |
resultPI = new FingerprintedQName("result", RESULT_NS, "processing-instruction"); |
| 77 |
resultNamespace = new FingerprintedQName("result", RESULT_NS, "namespace"); |
| 78 |
resultAtomicValue = new FingerprintedQName("result", RESULT_NS, "atomic-value"); |
| 79 |
resultFunction = new FingerprintedQName("result", RESULT_NS, "function"); |
| 80 |
resultArray = new FingerprintedQName("result", RESULT_NS, "array"); |
| 81 |
resultMap = new FingerprintedQName("result", RESULT_NS, "map"); |
| 82 |
resultExternalValue = new FingerprintedQName("result", RESULT_NS, "external-object"); |
| 83 |
xsiType = new FingerprintedQName("xsi", NamespaceConstant.SCHEMA_INSTANCE, "type"); |
| 84 |
|
| 85 |
out.open(); |
| 86 |
out.startDocument(0); |
| 87 |
|
| 88 |
out.startElement(resultSequence, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 89 |
out.namespace(new NamespaceBinding("result", RESULT_NS), 0); |
| 90 |
out.namespace(new NamespaceBinding("xs", NamespaceConstant.SCHEMA), 0); |
| 91 |
out.namespace(new NamespaceBinding("xsi", NamespaceConstant.SCHEMA_INSTANCE), 0); |
| 92 |
out.startContent(); |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public void startDocument(int properties) throws XPathException { |
| 101 |
out.startElement(resultDocument, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 102 |
out.startContent(); |
| 103 |
depth++; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public void endDocument() throws XPathException { |
| 111 |
out.endElement(); |
| 112 |
depth--; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public void startElement(NodeName nameCode, SchemaType typeCode, Location location, int properties) throws XPathException { |
| 124 |
if (depth++ == 0) { |
| 125 |
out.startElement(resultElement, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 126 |
out.startContent(); |
| 127 |
} |
| 128 |
out.startElement(nameCode, typeCode, location, properties); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public void endElement() throws XPathException { |
| 136 |
out.endElement(); |
| 137 |
if (--depth == 0) { |
| 138 |
out.endElement(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public void attribute(NodeName attName, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException { |
| 159 |
if (depth == 0) { |
| 160 |
out.startElement(resultAttribute, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 161 |
if (!attName.hasURI("")) { |
| 162 |
out.namespace(attName.getNamespaceBinding(), 0); |
| 163 |
} |
| 164 |
out.attribute(attName, typeCode, value, locationId, properties); |
| 165 |
out.startContent(); |
| 166 |
out.endElement(); |
| 167 |
} else { |
| 168 |
out.attribute(attName, typeCode, value, locationId, properties); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public void namespace(NamespaceBindingSet namespaceBindings, int properties) throws XPathException { |
| 184 |
if (depth == 0) { |
| 185 |
out.startElement(resultNamespace, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 186 |
out.namespace(namespaceBindings, properties); |
| 187 |
out.startContent(); |
| 188 |
out.endElement(); |
| 189 |
} else { |
| 190 |
out.namespace(namespaceBindings, properties); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public void characters(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 199 |
if (depth == 0) { |
| 200 |
out.startElement(resultText, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 201 |
out.startContent(); |
| 202 |
out.characters(chars, locationId, properties); |
| 203 |
out.endElement(); |
| 204 |
} else { |
| 205 |
out.characters(chars, locationId, properties); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public void comment(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 214 |
if (depth == 0) { |
| 215 |
out.startElement(resultComment, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 216 |
out.startContent(); |
| 217 |
out.comment(chars, locationId, properties); |
| 218 |
out.endElement(); |
| 219 |
} else { |
| 220 |
out.comment(chars, locationId, properties); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException { |
| 229 |
if (depth == 0) { |
| 230 |
out.startElement(resultPI, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 231 |
out.startContent(); |
| 232 |
out.processingInstruction(target, data, locationId, properties); |
| 233 |
out.endElement(); |
| 234 |
} else { |
| 235 |
out.processingInstruction(target, data, locationId, properties); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public void append( Item item, Location locationId, int copyNamespaces) throws XPathException { |
| 244 |
if (item instanceof AtomicValue) { |
| 245 |
final NamePool pool = getNamePool(); |
| 246 |
out.startElement(resultAtomicValue, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 247 |
AtomicType type = ((AtomicValue) item).getItemType(); |
| 248 |
StructuredQName name = type.getStructuredQName(); |
| 249 |
String prefix = name.getPrefix(); |
| 250 |
String localName = name.getLocalPart(); |
| 251 |
String uri = name.getURI(); |
| 252 |
if (prefix.isEmpty()) { |
| 253 |
prefix = pool.suggestPrefixForURI(uri); |
| 254 |
if (prefix == null) { |
| 255 |
prefix = "p" + uri.hashCode(); |
| 256 |
} |
| 257 |
} |
| 258 |
String displayName = prefix + ':' + localName; |
| 259 |
out.namespace(new NamespaceBinding(prefix, uri), 0); |
| 260 |
out.attribute(xsiType, BuiltInAtomicType.UNTYPED_ATOMIC, displayName, locationId, 0); |
| 261 |
out.startContent(); |
| 262 |
out.characters(item.getStringValue(), locationId, 0); |
| 263 |
out.endElement(); |
| 264 |
} else if (item instanceof NodeInfo) { |
| 265 |
((NodeInfo) item).copy(this, CopyOptions.ALL_NAMESPACES | CopyOptions.TYPE_ANNOTATIONS, locationId); |
| 266 |
} else if (item instanceof Function) { |
| 267 |
if (item instanceof MapItem) { |
| 268 |
out.startElement(resultMap, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 269 |
out.startContent(); |
| 270 |
out.characters(item.toShortString(), locationId, 0); |
| 271 |
out.endElement(); |
| 272 |
} else if (item instanceof ArrayItem) { |
| 273 |
out.startElement(resultArray, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 274 |
out.startContent(); |
| 275 |
out.characters(item.toShortString(), locationId, 0); |
| 276 |
out.endElement(); |
| 277 |
} else { |
| 278 |
out.startElement(resultFunction, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 279 |
out.startContent(); |
| 280 |
out.characters(((Function)item).getDescription(), locationId, 0); |
| 281 |
out.endElement(); |
| 282 |
} |
| 283 |
} else if (item instanceof ObjectValue) { |
| 284 |
Object obj = ((ObjectValue)item).getObject(); |
| 285 |
out.startElement(resultExternalValue, Untyped.getInstance(), ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 286 |
out.attribute(new NoNamespaceName("class"), BuiltInAtomicType.UNTYPED_ATOMIC, |
| 287 |
obj.getClass().getName(), ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 288 |
out.startContent(); |
| 289 |
out.characters(obj.toString(), locationId, 0); |
| 290 |
out.endElement(); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
public void startContent() throws XPathException { |
| 302 |
out.startContent(); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
public void close() throws XPathException { |
| 310 |
out.endElement(); |
| 311 |
out.endDocument(); |
| 312 |
out.close(); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
public boolean usesTypeAnnotations() { |
| 324 |
return true; |
| 325 |
} |
| 326 |
} |
| 327 |
|