| 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.*; |
| 13 |
import net.sf.saxon.trans.XPathException; |
| 14 |
import net.sf.saxon.tree.linked.LinkedTreeBuilder; |
| 15 |
import net.sf.saxon.tree.tiny.TinyElementImpl; |
| 16 |
import net.sf.saxon.tree.util.Orphan; |
| 17 |
import net.sf.saxon.type.SchemaType; |
| 18 |
import net.sf.saxon.type.SimpleType; |
| 19 |
import net.sf.saxon.type.Type; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public abstract class SequenceWriter extends SequenceReceiver { |
| 42 |
private Receiver outputter = null; |
| 43 |
private TreeModel treeModel = null; |
| 44 |
private Builder builder = null; |
| 45 |
private int level = 0; |
| 46 |
private boolean inStartTag = false; |
| 47 |
|
| 48 |
public SequenceWriter( PipelineConfiguration pipe) { |
| 49 |
super(pipe); |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public abstract void write(Item item) throws XPathException; |
| 62 |
|
| 63 |
@Override |
| 64 |
public void startDocument(int properties) throws XPathException { |
| 65 |
if (outputter == null) { |
| 66 |
createTree((properties & ReceiverOptions.MUTABLE_TREE) != 0); |
| 67 |
} |
| 68 |
if (level++ == 0) { |
| 69 |
outputter.startDocument(properties); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public void setUnparsedEntity(String name, String systemID, String publicID) throws XPathException { |
| 75 |
if (builder != null) { |
| 76 |
builder.setUnparsedEntity(name, systemID, publicID); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
private void createTree(boolean mutable) throws XPathException { |
| 89 |
PipelineConfiguration pipe = getPipelineConfiguration(); |
| 90 |
|
| 91 |
if (treeModel != null) { |
| 92 |
builder = treeModel.makeBuilder(pipe); |
| 93 |
} else if (pipe.getController() != null) { |
| 94 |
if (mutable) { |
| 95 |
TreeModel model = pipe.getController().getModel(); |
| 96 |
if (model.isMutable()) { |
| 97 |
builder = pipe.getController().makeBuilder(); |
| 98 |
} else { |
| 99 |
builder = new LinkedTreeBuilder(pipe); |
| 100 |
} |
| 101 |
} else { |
| 102 |
builder = pipe.getController().makeBuilder(); |
| 103 |
} |
| 104 |
} else { |
| 105 |
TreeModel model = getConfiguration().getParseOptions().getModel(); |
| 106 |
builder = model.makeBuilder(pipe); |
| 107 |
} |
| 108 |
|
| 109 |
builder.setPipelineConfiguration(pipe); |
| 110 |
builder.setSystemId(systemId); |
| 111 |
builder.setBaseURI(systemId); |
| 112 |
builder.setTiming(false); |
| 113 |
builder.setUseEventLocation(false); |
| 114 |
|
| 115 |
NamespaceReducer reducer = new NamespaceReducer(builder); |
| 116 |
|
| 117 |
outputter = new ComplexContentOutputter(reducer); |
| 118 |
|
| 119 |
outputter.setSystemId(systemId); |
| 120 |
outputter.setPipelineConfiguration(pipe); |
| 121 |
outputter.open(); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public TreeModel getTreeModel() { |
| 130 |
return treeModel; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public void setTreeModel(TreeModel treeModel) { |
| 141 |
this.treeModel = treeModel; |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public void endDocument() throws XPathException { |
| 146 |
if (--level == 0) { |
| 147 |
outputter.endDocument(); |
| 148 |
outputter = null; |
| 149 |
NodeInfo doc = builder.getCurrentRoot(); |
| 150 |
|
| 151 |
append(doc, ExplicitLocation.UNKNOWN_LOCATION, ReceiverOptions.ALL_NAMESPACES); |
| 152 |
systemId = null; |
| 153 |
} |
| 154 |
previousAtomic = false; |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) throws XPathException { |
| 159 |
|
| 160 |
if (inStartTag) { |
| 161 |
startContent(); |
| 162 |
} |
| 163 |
|
| 164 |
if (outputter == null) { |
| 165 |
createTree((properties & ReceiverOptions.MUTABLE_TREE) != 0); |
| 166 |
} |
| 167 |
|
| 168 |
outputter.startElement(elemName, typeCode, location, properties); |
| 169 |
level++; |
| 170 |
inStartTag = true; |
| 171 |
previousAtomic = false; |
| 172 |
} |
| 173 |
|
| 174 |
@Override |
| 175 |
public void endElement() throws XPathException { |
| 176 |
if (inStartTag) { |
| 177 |
startContent(); |
| 178 |
} |
| 179 |
|
| 180 |
outputter.endElement(); |
| 181 |
if (--level == 0) { |
| 182 |
outputter.close(); |
| 183 |
outputter = null; |
| 184 |
NodeInfo element = builder.getCurrentRoot(); |
| 185 |
append(element, ExplicitLocation.UNKNOWN_LOCATION, ReceiverOptions.ALL_NAMESPACES); |
| 186 |
systemId = null; |
| 187 |
} |
| 188 |
previousAtomic = false; |
| 189 |
} |
| 190 |
|
| 191 |
@Override |
| 192 |
public void namespace(NamespaceBindingSet namespaceBindings, int properties) |
| 193 |
throws XPathException { |
| 194 |
if (level == 0) { |
| 195 |
for (NamespaceBinding ns : namespaceBindings) { |
| 196 |
Orphan o = new Orphan(getConfiguration()); |
| 197 |
o.setNodeKind(Type.NAMESPACE); |
| 198 |
o.setNodeName(new NoNamespaceName(ns.getPrefix())); |
| 199 |
o.setStringValue(ns.getURI()); |
| 200 |
append(o, ExplicitLocation.UNKNOWN_LOCATION, ReceiverOptions.ALL_NAMESPACES); |
| 201 |
} |
| 202 |
} else { |
| 203 |
outputter.namespace(namespaceBindings, properties); |
| 204 |
} |
| 205 |
previousAtomic = false; |
| 206 |
} |
| 207 |
|
| 208 |
@Override |
| 209 |
public void attribute(NodeName attName, SimpleType typeCode, CharSequence value, Location locationId, int properties) |
| 210 |
throws XPathException { |
| 211 |
if (level == 0) { |
| 212 |
Orphan o = new Orphan(getConfiguration()); |
| 213 |
o.setNodeKind(Type.ATTRIBUTE); |
| 214 |
o.setNodeName(attName); |
| 215 |
o.setStringValue(value); |
| 216 |
o.setTypeAnnotation(typeCode); |
| 217 |
append(o, locationId, ReceiverOptions.ALL_NAMESPACES); |
| 218 |
} else { |
| 219 |
outputter.attribute(attName, typeCode, value, locationId, properties); |
| 220 |
} |
| 221 |
previousAtomic = false; |
| 222 |
} |
| 223 |
|
| 224 |
@Override |
| 225 |
public void startContent() throws XPathException { |
| 226 |
inStartTag = false; |
| 227 |
outputter.startContent(); |
| 228 |
previousAtomic = false; |
| 229 |
} |
| 230 |
|
| 231 |
@Override |
| 232 |
public void characters(CharSequence s, Location locationId, int properties) throws XPathException { |
| 233 |
if (level == 0) { |
| 234 |
Orphan o = new Orphan(getConfiguration()); |
| 235 |
o.setNodeKind(Type.TEXT); |
| 236 |
o.setStringValue(s.toString()); |
| 237 |
write(o); |
| 238 |
} else { |
| 239 |
if (s.length() > 0) { |
| 240 |
if (inStartTag) { |
| 241 |
startContent(); |
| 242 |
} |
| 243 |
outputter.characters(s, locationId, properties); |
| 244 |
} |
| 245 |
} |
| 246 |
previousAtomic = false; |
| 247 |
} |
| 248 |
|
| 249 |
@Override |
| 250 |
public void comment(CharSequence comment, Location locationId, int properties) throws XPathException { |
| 251 |
if (inStartTag) { |
| 252 |
startContent(); |
| 253 |
} |
| 254 |
if (level == 0) { |
| 255 |
Orphan o = new Orphan(getConfiguration()); |
| 256 |
o.setNodeKind(Type.COMMENT); |
| 257 |
o.setStringValue(comment); |
| 258 |
write(o); |
| 259 |
} else { |
| 260 |
outputter.comment(comment, locationId, properties); |
| 261 |
} |
| 262 |
previousAtomic = false; |
| 263 |
} |
| 264 |
|
| 265 |
@Override |
| 266 |
public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException { |
| 267 |
if (inStartTag) { |
| 268 |
startContent(); |
| 269 |
} |
| 270 |
if (level == 0) { |
| 271 |
Orphan o = new Orphan(getConfiguration()); |
| 272 |
o.setNodeName(new NoNamespaceName(target)); |
| 273 |
o.setNodeKind(Type.PROCESSING_INSTRUCTION); |
| 274 |
o.setStringValue(data); |
| 275 |
write(o); |
| 276 |
} else { |
| 277 |
outputter.processingInstruction(target, data, locationId, properties); |
| 278 |
} |
| 279 |
previousAtomic = false; |
| 280 |
} |
| 281 |
|
| 282 |
@Override |
| 283 |
public void close() throws XPathException { |
| 284 |
previousAtomic = false; |
| 285 |
if (outputter != null) { |
| 286 |
outputter.close(); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
@Override |
| 291 |
public void append( Item item, Location locationId, int copyNamespaces) throws XPathException { |
| 292 |
if (item != null) { |
| 293 |
if (level == 0) { |
| 294 |
write(item); |
| 295 |
previousAtomic = false; |
| 296 |
} else { |
| 297 |
decompose(item, locationId, copyNamespaces); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
@Override |
| 303 |
public boolean usesTypeAnnotations() { |
| 304 |
return outputter == null || outputter.usesTypeAnnotations(); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
public boolean isReadyForBulkCopy() { |
| 309 |
return level > 0 && outputter instanceof ComplexContentOutputter && |
| 310 |
((ComplexContentOutputter)outputter).isReadyForBulkCopy(); |
| 311 |
} |
| 312 |
|
| 313 |
public void bulkCopyElementNode(TinyElementImpl node, int copyOptions) throws XPathException { |
| 314 |
if (inStartTag) { |
| 315 |
startContent(); |
| 316 |
} |
| 317 |
((ComplexContentOutputter) outputter).bulkCopyElementNode(node, copyOptions); |
| 318 |
} |
| 319 |
|
| 320 |
public boolean isReadyForGrafting() { |
| 321 |
return level > 0 && outputter instanceof ComplexContentOutputter && |
| 322 |
((ComplexContentOutputter) outputter).isReadyForGrafting(); |
| 323 |
} |
| 324 |
|
| 325 |
public void graftElementNode(TinyElementImpl node, int copyOptions) throws XPathException { |
| 326 |
if (inStartTag) { |
| 327 |
startContent(); |
| 328 |
} |
| 329 |
((ComplexContentOutputter) outputter).graftElementNode(node, copyOptions); |
| 330 |
} |
| 331 |
} |
| 332 |
|