| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.event; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.parser.ExplicitLocation; |
| 11 |
import net.sf.saxon.expr.parser.Location; |
| 12 |
import net.sf.saxon.om.Item; |
| 13 |
import net.sf.saxon.om.NamespaceBinding; |
| 14 |
import net.sf.saxon.om.NamespaceBindingSet; |
| 15 |
import net.sf.saxon.om.NodeName; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.type.SchemaType; |
| 18 |
import net.sf.saxon.type.SimpleType; |
| 19 |
import net.sf.saxon.type.Type; |
| 20 |
|
| 21 |
import java.util.Arrays; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class TreeReceiver extends SequenceReceiver { |
| 40 |
private Receiver nextReceiver; |
| 41 |
private int level = 0; |
| 42 |
private boolean[] isDocumentLevel = new boolean[20]; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
private boolean inStartTag = false; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public TreeReceiver( Receiver nextInChain) { |
| 57 |
super(nextInChain.getPipelineConfiguration()); |
| 58 |
nextReceiver = nextInChain; |
| 59 |
previousAtomic = false; |
| 60 |
setPipelineConfiguration(nextInChain.getPipelineConfiguration()); |
| 61 |
} |
| 62 |
|
| 63 |
public void setSystemId(String systemId) { |
| 64 |
if (systemId != null && !systemId.equals(this.systemId)) { |
| 65 |
this.systemId = systemId; |
| 66 |
if (nextReceiver != null) { |
| 67 |
nextReceiver.setSystemId(systemId); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public void setPipelineConfiguration( PipelineConfiguration pipe) { |
| 73 |
if (pipelineConfiguration != pipe) { |
| 74 |
pipelineConfiguration = pipe; |
| 75 |
if (nextReceiver != null) { |
| 76 |
nextReceiver.setPipelineConfiguration(pipe); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public Receiver getNextReceiver() { |
| 88 |
return nextReceiver; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public void open() throws XPathException { |
| 96 |
if (nextReceiver == null) { |
| 97 |
throw new IllegalStateException("TreeReceiver.open(): no underlying receiver provided"); |
| 98 |
} |
| 99 |
nextReceiver.open(); |
| 100 |
previousAtomic = false; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public void close() throws XPathException { |
| 108 |
if (nextReceiver != null) { |
| 109 |
nextReceiver.close(); |
| 110 |
} |
| 111 |
previousAtomic = false; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public void startDocument(int properties) throws XPathException { |
| 119 |
if (level == 0) { |
| 120 |
nextReceiver.startDocument(properties); |
| 121 |
} |
| 122 |
if (isDocumentLevel.length - 1 < level) { |
| 123 |
isDocumentLevel = Arrays.copyOf(isDocumentLevel, level * 2); |
| 124 |
} |
| 125 |
isDocumentLevel[level++] = true; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public void endDocument() throws XPathException { |
| 133 |
level--; |
| 134 |
if (level == 0) { |
| 135 |
nextReceiver.endDocument(); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public void startElement(NodeName nameCode, SchemaType typeCode, Location location, int properties) throws XPathException { |
| 148 |
if (inStartTag) { |
| 149 |
startContent(); |
| 150 |
} |
| 151 |
inStartTag = true; |
| 152 |
nextReceiver.startElement(nameCode, typeCode, location, properties); |
| 153 |
previousAtomic = false; |
| 154 |
if (isDocumentLevel.length - 1 < level) { |
| 155 |
isDocumentLevel = Arrays.copyOf(isDocumentLevel, level * 2); |
| 156 |
} |
| 157 |
isDocumentLevel[level++] = false; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public void namespace(NamespaceBindingSet namespaceBindings, int properties) throws XPathException { |
| 172 |
boolean documentLevel = level == 0 || isDocumentLevel[level - 1]; |
| 173 |
if (documentLevel || !inStartTag) { |
| 174 |
|
| 175 |
for (NamespaceBinding ns : namespaceBindings) { |
| 176 |
|
| 177 |
throw NoOpenStartTagException.makeNoOpenStartTagException( |
| 178 |
Type.NAMESPACE, ns.getPrefix(), |
| 179 |
getPipelineConfiguration().getHostLanguage(), |
| 180 |
documentLevel, ExplicitLocation.UNKNOWN_LOCATION); |
| 181 |
} |
| 182 |
} |
| 183 |
nextReceiver.namespace(namespaceBindings, properties); |
| 184 |
previousAtomic = false; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public void attribute(NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties) |
| 204 |
throws XPathException { |
| 205 |
boolean documentLevel = level == 0 || isDocumentLevel[level - 1]; |
| 206 |
if (documentLevel || !inStartTag) { |
| 207 |
throw NoOpenStartTagException.makeNoOpenStartTagException( |
| 208 |
Type.ATTRIBUTE, nameCode.getDisplayName(), |
| 209 |
getPipelineConfiguration().getHostLanguage(), |
| 210 |
documentLevel, ExplicitLocation.UNKNOWN_LOCATION); |
| 211 |
} |
| 212 |
nextReceiver.attribute(nameCode, typeCode, value, locationId, properties); |
| 213 |
previousAtomic = false; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public void startContent() throws XPathException { |
| 225 |
inStartTag = false; |
| 226 |
nextReceiver.startContent(); |
| 227 |
previousAtomic = false; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public void endElement() throws XPathException { |
| 235 |
if (inStartTag) { |
| 236 |
startContent(); |
| 237 |
} |
| 238 |
nextReceiver.endElement(); |
| 239 |
previousAtomic = false; |
| 240 |
level--; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public void characters(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 248 |
if (chars.length() > 0) { |
| 249 |
if (inStartTag) { |
| 250 |
startContent(); |
| 251 |
} |
| 252 |
nextReceiver.characters(chars, locationId, properties); |
| 253 |
} |
| 254 |
previousAtomic = false; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException { |
| 263 |
if (inStartTag) { |
| 264 |
startContent(); |
| 265 |
} |
| 266 |
nextReceiver.processingInstruction(target, data, locationId, properties); |
| 267 |
previousAtomic = false; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
public void comment(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 275 |
if (inStartTag) { |
| 276 |
startContent(); |
| 277 |
} |
| 278 |
nextReceiver.comment(chars, locationId, properties); |
| 279 |
previousAtomic = false; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
public void setUnparsedEntity(String name, String uri, String publicId) throws XPathException { |
| 288 |
nextReceiver.setUnparsedEntity(name, uri, publicId); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public void append( Item item, Location locationId, int copyNamespaces) throws XPathException { |
| 296 |
decompose(item, locationId, copyNamespaces); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public boolean usesTypeAnnotations() { |
| 308 |
return nextReceiver.usesTypeAnnotations(); |
| 309 |
} |
| 310 |
} |
| 311 |
|