1 package net.sf.saxon.event; 2 3 import net.sf.saxon.trans.XPathException; 4 import net.sf.saxon.tinytree.TinyBuilder; 5 import net.sf.saxon.om.*; 6 import net.sf.saxon.type.Type; 7 import net.sf.saxon.value.AtomicValue; 8 9 21 22 public abstract class SequenceWriter extends SequenceReceiver { 23 private String systemId; 24 private Receiver tree = null; 25 private TinyBuilder builder = null; 26 private int level = 0; 27 private boolean inStartTag = false; 28 private static final int[] treeSizeParameters = {50, 10, 5, 200}; 29 30 31 32 35 36 public abstract void write(Item item) throws XPathException; 37 38 42 43 public void setSystemId(String systemId) { 44 this.systemId = systemId; 45 } 46 47 51 52 public String getSystemId() { 53 return systemId; 54 } 55 56 59 60 public boolean hasOpenNodes() { 61 return level != 0; 62 } 63 64 67 68 public void startDocument(int properties) throws XPathException { 69 if (tree==null) { 70 createTree(); 71 } 72 if (level++ == 0) { 73 tree.startDocument(properties); 74 } 75 } 76 77 81 82 private void createTree() throws XPathException { 83 builder = new TinyBuilder(); 84 builder.setPipelineConfiguration(getPipelineConfiguration()); 85 builder.setSizeParameters(treeSizeParameters); 86 87 NamespaceReducer reducer = new NamespaceReducer(); 88 reducer.setUnderlyingReceiver(builder); 89 reducer.setPipelineConfiguration(getPipelineConfiguration()); 90 91 ComplexContentOutputter complex = new ComplexContentOutputter(); 92 complex.setPipelineConfiguration(getPipelineConfiguration()); 93 complex.setReceiver(reducer); 94 tree = complex; 95 96 tree.setSystemId(systemId); 97 tree.setPipelineConfiguration(getPipelineConfiguration()); 98 tree.open(); 99 } 100 101 104 105 public void endDocument() throws XPathException { 106 if (--level == 0) { 107 tree.endDocument(); 108 DocumentInfo doc = (DocumentInfo)builder.getCurrentRoot(); 109 append(doc, 0, NodeInfo.ALL_NAMESPACES); 111 } 112 previousAtomic = false; 113 } 114 115 122 123 public void startElement(int nameCode, int typeCode, int locationId, int properties) throws XPathException { 124 125 if (inStartTag) { 126 startContent(); 127 } 128 129 if (tree==null) { 130 createTree(); 131 } 132 133 tree.startElement(nameCode, typeCode, locationId, properties); 134 level++; 135 inStartTag = true; 136 previousAtomic = false; 137 } 138 139 142 143 public void endElement() throws XPathException { 144 if (inStartTag) { 145 startContent(); 146 } 147 tree.endElement(); 148 if (--level == 0) { 149 NodeInfo element = builder.getCurrentRoot(); 150 append(element, 0, NodeInfo.ALL_NAMESPACES); 151 } 152 previousAtomic = false; 153 } 154 155 166 167 public void namespace(int nscode, int properties) 168 throws XPathException { 169 if (level == 0) { 170 NamePool namePool = getNamePool(); 171 Orphan o = new Orphan(getConfiguration()); 172 o.setNodeKind(Type.NAMESPACE); 173 o.setNameCode(namePool.allocate("", "", namePool.getPrefixFromNamespaceCode(nscode))); 174 o.setStringValue(namePool.getURIFromNamespaceCode(nscode)); 175 append(o, 0, NodeInfo.ALL_NAMESPACES); 176 } else { 177 tree.namespace(nscode, properties); 178 } 179 previousAtomic = false; 180 } 181 182 193 194 public void attribute(int nameCode, int typeCode, CharSequence value, int locationId, int properties) 195 throws XPathException { 196 if (level == 0) { 197 Orphan o = new Orphan(getConfiguration()); 198 o.setNodeKind(Type.ATTRIBUTE); 199 o.setNameCode(nameCode); 200 o.setStringValue(value); 201 o.setTypeAnnotation(typeCode); 202 append(o, locationId, NodeInfo.ALL_NAMESPACES); 203 } else { 204 tree.attribute(nameCode, typeCode, value, locationId, properties); 205 } 206 previousAtomic = false; 207 } 208 209 214 215 public void startContent() throws XPathException { 216 inStartTag = false; 217 tree.startContent(); 218 previousAtomic = false; 219 } 220 221 227 228 public void characters(CharSequence s, int locationId, int properties) throws XPathException { 229 if (level == 0) { 230 Orphan o = new Orphan(getConfiguration()); 231 o.setNodeKind(Type.TEXT); 232 o.setStringValue(s); 233 append(o, locationId, NodeInfo.ALL_NAMESPACES); 234 } else { 235 if (s.length() > 0) { 236 if (inStartTag) { 237 startContent(); 238 } 239 tree.characters(s, locationId, properties); 240 } 241 } 242 previousAtomic = false; 243 } 244 245 248 249 public void comment(CharSequence comment, int locationId, int properties) throws XPathException { 250 if (inStartTag) { 251 startContent(); 252 } 253 if (level == 0) { 254 Orphan o = new Orphan(getConfiguration()); 255 o.setNodeKind(Type.COMMENT); 256 o.setStringValue(comment); 257 append(o, locationId, NodeInfo.ALL_NAMESPACES); 258 } else { 259 tree.comment(comment, locationId, properties); 260 } 261 previousAtomic = false; 262 } 263 264 268 269 public void processingInstruction(String target, CharSequence data, int locationId, int properties) throws XPathException { 270 if (inStartTag) { 271 startContent(); 272 } 273 if (level == 0) { 274 Orphan o = new Orphan(getConfiguration()); 275 o.setNameCode(getNamePool().allocate("", "", target)); 276 o.setNodeKind(Type.PROCESSING_INSTRUCTION); 277 o.setStringValue(data); 278 append(o, locationId, NodeInfo.ALL_NAMESPACES); 279 } else { 280 tree.processingInstruction(target, data, locationId, properties); 281 } 282 previousAtomic = false; 283 } 284 285 288 289 public void close() throws XPathException { 290 previousAtomic = false; 291 if (tree != null) { 292 tree.close(); 293 } 294 } 295 296 299 300 public void append(Item item, int locationId, int copyNamespaces) throws XPathException { 301 302 if (item==null) { 303 return; 304 } 305 306 if (level==0) { 307 write(item); 308 previousAtomic = false; 309 } else { 310 if (item instanceof AtomicValue) { 311 if (previousAtomic) { 314 tree.characters(" ", 0, 0); 315 } 316 tree.characters(item.getStringValueCS(), 0, 0); 317 previousAtomic = true; 318 } else { 319 ((NodeInfo)item).copy(tree, NodeInfo.ALL_NAMESPACES, true, locationId); 320 previousAtomic = false; 321 } 322 } 323 } 324 } 325 326 | Popular Tags |