1 package net.sf.saxon.event; 2 3 import net.sf.saxon.om.*; 4 import net.sf.saxon.trans.XPathException; 5 import net.sf.saxon.value.AtomicValue; 6 7 14 15 public class TreeReceiver extends SequenceReceiver { 16 private Receiver baseReceiver; 17 private String systemId; 18 private boolean contentStarted = true; 19 20 public TreeReceiver(Receiver nextInChain) { 21 baseReceiver = nextInChain; 22 previousAtomic = false; 23 setPipelineConfiguration(nextInChain.getPipelineConfiguration()); 24 } 25 26 public void setSystemId(String systemId) { 27 if (systemId != this.systemId) { 28 this.systemId = systemId; 29 if (baseReceiver != null) { 30 baseReceiver.setSystemId(systemId); 31 } 32 } 33 } 34 35 public String getSystemId() { 36 return systemId; 37 } 38 39 42 43 public Receiver getUnderlyingReceiver() { 44 return baseReceiver; 45 } 46 47 50 51 public void open() throws XPathException { 52 if (baseReceiver == null) { 53 throw new IllegalStateException ("TreeReceiver.open(): no underlying receiver provided"); 54 } 55 baseReceiver.open(); 56 previousAtomic = false; 57 } 58 59 62 63 public void close() throws XPathException { 64 if (baseReceiver != null) { 65 baseReceiver.close(); 66 } 67 previousAtomic = false; 68 } 69 70 73 74 public void startDocument(int properties) throws XPathException { 75 baseReceiver.startDocument(properties); 77 } 79 80 83 84 public void endDocument() throws XPathException { 85 baseReceiver.endDocument(); 87 } 89 90 96 97 public void startElement(int nameCode, int typeCode, int locationId, int properties) throws XPathException { 98 if (!contentStarted) { 99 startContent(); 100 } 101 contentStarted = false; 102 baseReceiver.startElement(nameCode, typeCode, locationId, properties); 104 previousAtomic = false; 106 } 107 108 120 121 public void namespace(int namespaceCode, int properties) throws XPathException { 122 baseReceiver.namespace(namespaceCode, properties); 124 previousAtomic = false; 126 } 127 128 139 140 public void attribute(int nameCode, int typeCode, CharSequence value, int locationId, int properties) 141 throws XPathException { 142 baseReceiver.attribute(nameCode, typeCode, value, locationId, properties); 144 previousAtomic = false; 146 } 147 148 154 155 156 public void startContent() throws XPathException { 157 contentStarted = true; 158 baseReceiver.startContent(); 160 previousAtomic = false; 162 } 163 164 167 168 public void endElement() throws XPathException { 169 if (!contentStarted) { 170 startContent(); 171 } 172 baseReceiver.endElement(); 174 previousAtomic = false; 176 } 177 178 181 182 public void characters(CharSequence chars, int locationId, int properties) throws XPathException { 183 if (!contentStarted) { 184 startContent(); 185 } 186 baseReceiver.characters(chars, locationId, properties); 188 previousAtomic = false; 190 } 191 192 193 196 197 public void processingInstruction(String target, CharSequence data, int locationId, int properties) throws XPathException { 198 if (!contentStarted) { 199 startContent(); 200 } 201 baseReceiver.processingInstruction(target, data, locationId, properties); 203 previousAtomic = false; 205 } 206 207 210 211 public void comment(CharSequence chars, int locationId, int properties) throws XPathException { 212 if (!contentStarted) { 213 startContent(); 214 } 215 baseReceiver.comment(chars, locationId, properties); 217 previousAtomic = false; 219 } 220 221 222 225 226 public void setUnparsedEntity(String name, String uri, String publicId) throws XPathException { 227 baseReceiver.setUnparsedEntity(name, uri, publicId); 229 } 231 232 235 236 public void append(Item item, int locationId, int copyNamespaces) throws XPathException { 237 if (item instanceof AtomicValue) { 238 if (previousAtomic) { 239 characters(" ", locationId, 0); 240 } 241 characters(item.getStringValueCS(), locationId, 0); 242 previousAtomic = true; 243 } else if (item instanceof DocumentInfo) { 244 SequenceIterator iter = ((DocumentInfo)item).iterateAxis(Axis.CHILD); 245 while (true) { 246 Item it = iter.next(); 247 if (it == null) break; 248 append(it, locationId, NodeInfo.ALL_NAMESPACES); 249 } 250 } else { 251 ((NodeInfo)item).copy(this, NodeInfo.ALL_NAMESPACES, true, locationId); 252 previousAtomic = false; 253 } 254 } 255 256 } 257 258 | Popular Tags |