1 package net.sf.saxon.query; 2 3 import net.sf.saxon.event.Receiver; 4 import net.sf.saxon.event.SequenceReceiver; 5 import net.sf.saxon.om.Item; 6 import net.sf.saxon.om.NamePool; 7 import net.sf.saxon.om.NamespaceConstant; 8 import net.sf.saxon.om.NodeInfo; 9 import net.sf.saxon.trans.XPathException; 10 import net.sf.saxon.type.AtomicType; 11 import net.sf.saxon.value.AtomicValue; 12 import net.sf.saxon.style.StandardNames; 13 14 19 20 public class SequenceWrapper extends SequenceReceiver { 21 22 public static final String RESULT_NS = QueryResult.RESULT_NS; 23 24 private Receiver out; 25 private int depth = 0; 26 27 private int resultSequence; 28 private int resultDocument; 29 private int resultElement; 30 private int resultAttribute; 31 private int resultText; 32 private int resultComment; 33 private int resultPI; 34 private int resultNamespace; 35 private int resultAtomicValue; 36 private int xsiType; 37 38 public SequenceWrapper(Receiver destination) { 39 this.out = destination; 40 setPipelineConfiguration(destination.getPipelineConfiguration()); 41 } 42 43 public void open() throws XPathException { 44 45 NamePool pool = getNamePool(); 46 47 resultSequence = pool.allocate("result", RESULT_NS, "sequence"); 48 resultDocument = pool.allocate("result", RESULT_NS, "document"); 49 resultElement = pool.allocate("result", RESULT_NS, "element"); 50 resultAttribute = pool.allocate("result", RESULT_NS, "attribute"); 51 resultText = pool.allocate("result", RESULT_NS, "text"); 52 resultComment = pool.allocate("result", RESULT_NS, "comment"); 53 resultPI = pool.allocate("result", RESULT_NS, "processing-instruction"); 54 resultNamespace = pool.allocate("result", RESULT_NS, "namespace"); 55 resultAtomicValue = pool.allocate("result", RESULT_NS, "atomic-value"); 56 xsiType = pool.allocate("xsi", NamespaceConstant.SCHEMA_INSTANCE, "type"); 57 58 out.open(); 59 out.startDocument(0); 60 61 out.startElement(resultSequence, StandardNames.XDT_UNTYPED, 0, 0); 62 out.namespace(pool.allocateNamespaceCode("result", RESULT_NS), 0); 63 out.namespace(pool.allocateNamespaceCode("xs", NamespaceConstant.SCHEMA), 0); 64 out.namespace(pool.allocateNamespaceCode("xsi", NamespaceConstant.SCHEMA_INSTANCE), 0); 65 out.startContent(); 66 67 } 68 69 72 73 public void startDocument(int properties) throws XPathException { 74 out.startElement(resultDocument, StandardNames.XDT_UNTYPED, 0, 0); 75 out.startContent(); 76 depth++; 77 } 78 79 82 83 public void endDocument() throws XPathException { 84 out.endElement(); 85 depth--; 86 } 87 88 95 96 public void startElement(int nameCode, int typeCode, int locationId, int properties) throws XPathException { 97 if (depth++ == 0) { 98 out.startElement(resultElement, StandardNames.XDT_UNTYPED, 0, 0); 99 out.startContent(); 100 } 101 out.startElement(nameCode, typeCode, locationId, properties); 102 } 103 104 107 108 public void endElement() throws XPathException { 109 out.endElement(); 110 if (--depth == 0) { 111 out.endElement(); } 113 } 114 115 127 128 public void attribute(int nameCode, int typeCode, CharSequence value, int locationId, int properties) throws XPathException { 129 if (depth==0) { 130 out.startElement(resultAttribute, StandardNames.XDT_UNTYPED, 0, 0); 131 out.attribute(nameCode, typeCode, value, locationId, properties); 132 out.startContent(); 133 out.endElement(); 134 } else { 135 out.attribute(nameCode, typeCode, value, locationId, properties); 136 } 137 } 138 139 152 153 public void namespace(int namespaceCode, int properties) throws XPathException { 154 if (depth == 0) { 155 out.startElement(resultNamespace, StandardNames.XDT_UNTYPED, 0, 0); 156 out.namespace(namespaceCode, properties); 157 out.startContent(); 158 out.endElement(); 159 } else { 160 out.namespace(namespaceCode, properties); 161 } 162 } 163 164 167 168 public void characters(CharSequence chars, int locationId, int properties) throws XPathException { 169 if (depth==0) { 170 out.startElement(resultText, StandardNames.XDT_UNTYPED, 0, 0); 171 out.startContent(); 172 out.characters(chars, locationId, properties); 173 out.endElement(); 174 } else { 175 out.characters(chars, locationId, properties); 176 } 177 } 178 179 182 183 public void comment(CharSequence chars, int locationId, int properties) throws XPathException { 184 if (depth==0) { 185 out.startElement(resultComment, StandardNames.XDT_UNTYPED, 0, 0); 186 out.startContent(); 187 out.comment(chars, locationId, properties); 188 out.endElement(); 189 } else { 190 out.comment(chars, locationId, properties); 191 } 192 } 193 194 197 198 public void processingInstruction(String target, CharSequence data, int locationId, int properties) throws XPathException { 199 if (depth==0) { 200 out.startElement(resultPI, StandardNames.XDT_UNTYPED, 0, 0); 201 out.startContent(); 202 out.processingInstruction(target, data, locationId, properties); 203 out.endElement(); 204 } else { 205 out.processingInstruction(target, data, locationId, properties); 206 } 207 } 208 209 212 213 public void append(Item item, int locationId, int copyNamespaces) throws XPathException { 214 if (item instanceof AtomicValue) { 215 NamePool pool = getNamePool(); 216 out.startElement(resultAtomicValue, StandardNames.XDT_UNTYPED, 0, 0); 217 AtomicType type = (AtomicType)((AtomicValue)item).getItemType(); 218 int nameCode = type.getNameCode(); 219 String prefix = pool.getPrefix(nameCode); 220 String localName = pool.getLocalName(nameCode); 221 String uri = pool.getURI(nameCode); 222 if (prefix.equals("")) { 223 prefix = pool.suggestPrefixForURI(uri); 224 if (prefix == null) { 225 prefix = "p" + uri.hashCode(); 226 } 227 } 228 int nscode = pool.allocateNamespaceCode(prefix, uri); 229 String displayName = prefix + ':' + localName; 230 out.namespace(nscode, 0); 231 out.attribute(xsiType, StandardNames.XDT_UNTYPED_ATOMIC, displayName, 0, 0); 232 out.startContent(); 233 out.characters(item.getStringValue(), 0, 0); 234 out.endElement(); 235 } else { 236 ((NodeInfo)item).copy(this, NodeInfo.ALL_NAMESPACES, true, locationId); 237 } 238 } 239 240 246 247 public void startContent() throws XPathException { 248 out.startContent(); 249 } 250 251 254 255 public void close() throws XPathException { 256 out.endElement(); out.endDocument(); 258 out.close(); 259 } 260 261 } 262 263 | Popular Tags |