| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.event; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.parser.ExplicitLocation; |
| 12 |
import net.sf.saxon.expr.parser.Location; |
| 13 |
import net.sf.saxon.ma.arrays.ArrayItem; |
| 14 |
import net.sf.saxon.ma.map.MapItem; |
| 15 |
import net.sf.saxon.om.*; |
| 16 |
import net.sf.saxon.trans.Err; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.tree.util.Orphan; |
| 19 |
import net.sf.saxon.type.SimpleType; |
| 20 |
import net.sf.saxon.type.Type; |
| 21 |
import net.sf.saxon.value.AtomicValue; |
| 22 |
import net.sf.saxon.value.ExternalObject; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public abstract class SequenceReceiver implements Receiver { |
| 31 |
|
| 32 |
protected boolean previousAtomic = false; |
| 33 |
|
| 34 |
protected PipelineConfiguration pipelineConfiguration; |
| 35 |
|
| 36 |
protected String systemId = null; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public SequenceReceiver( PipelineConfiguration pipe) { |
| 45 |
this.pipelineConfiguration = pipe; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
public final PipelineConfiguration getPipelineConfiguration() { |
| 50 |
return pipelineConfiguration; |
| 51 |
} |
| 52 |
|
| 53 |
public void setPipelineConfiguration( PipelineConfiguration pipelineConfiguration) { |
| 54 |
this.pipelineConfiguration = pipelineConfiguration; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public final Configuration getConfiguration() { |
| 64 |
return pipelineConfiguration.getConfiguration(); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public void setSystemId( String systemId) { |
| 74 |
this.systemId = systemId; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public String getSystemId() { |
| 85 |
return systemId; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public void setUnparsedEntity(String name, String systemID, String publicID) throws XPathException { |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public void open() throws XPathException { |
| 104 |
previousAtomic = false; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public abstract void append(Item item, Location locationId, int properties) throws XPathException; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public void append(Item item) throws XPathException { |
| 130 |
append(item, ExplicitLocation.UNKNOWN_LOCATION, ReceiverOptions.ALL_NAMESPACES); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public NamePool getNamePool() { |
| 140 |
return pipelineConfiguration.getConfiguration().getNamePool(); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
protected void flatten(ArrayItem array, Location locationId, int copyNamespaces) throws XPathException { |
| 148 |
for (Sequence<?> member : array.members()) { |
| 149 |
member.iterate().forEachOrFail(it -> append(it, locationId, copyNamespaces)); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
protected void decompose(Item item, Location locationId, int copyNamespaces) throws XPathException { |
| 160 |
if (item != null) { |
| 161 |
if (item instanceof AtomicValue || item instanceof ExternalObject) { |
| 162 |
if (previousAtomic) { |
| 163 |
characters(" ", locationId, 0); |
| 164 |
} |
| 165 |
characters(item.getStringValueCS(), locationId, 0); |
| 166 |
previousAtomic = true; |
| 167 |
} else if (item instanceof ArrayItem) { |
| 168 |
flatten((ArrayItem)item, locationId, copyNamespaces); |
| 169 |
} else if (item instanceof Function) { |
| 170 |
String thing = item instanceof MapItem ? "map" : "function item"; |
| 171 |
String errorCode = getErrorCodeForDecomposingFunctionItems(); |
| 172 |
if (errorCode.startsWith("SENR")) { |
| 173 |
throw new XPathException("Cannot serialize a " + thing + " using this output method", errorCode, locationId); |
| 174 |
} else { |
| 175 |
throw new XPathException("Cannot add a " + thing + " to an XDM node tree", errorCode, locationId); |
| 176 |
} |
| 177 |
} else { |
| 178 |
NodeInfo node = (NodeInfo)item; |
| 179 |
if (node instanceof Orphan && ((Orphan) node).isDisableOutputEscaping()) { |
| 180 |
|
| 181 |
characters(item.getStringValueCS(), locationId, ReceiverOptions.DISABLE_ESCAPING); |
| 182 |
previousAtomic = false; |
| 183 |
} else if (node.getNodeKind() == Type.DOCUMENT) { |
| 184 |
startDocument(0); |
| 185 |
node.iterateAxis(AxisInfo.CHILD).forEachOrFail( |
| 186 |
child -> append(child, locationId, copyNamespaces) |
| 187 |
); |
| 188 |
previousAtomic = false; |
| 189 |
endDocument(); |
| 190 |
} else { |
| 191 |
if (node.getNodeKind() == Type.ATTRIBUTE && ((SimpleType) node.getSchemaType()).isNamespaceSensitive()) { |
| 192 |
XPathException err = new XPathException("Cannot copy attributes whose type is namespace-sensitive (QName or NOTATION): " + |
| 193 |
Err.wrap(node.getDisplayName(), Err.ATTRIBUTE)); |
| 194 |
err.setErrorCode(getPipelineConfiguration().getHostLanguage() == Configuration.XSLT ? "XTTE0950" : "XQTY0086"); |
| 195 |
throw err; |
| 196 |
} |
| 197 |
int copyOptions = CopyOptions.TYPE_ANNOTATIONS; |
| 198 |
if (copyNamespaces == ReceiverOptions.LOCAL_NAMESPACES) { |
| 199 |
copyOptions |= CopyOptions.LOCAL_NAMESPACES; |
| 200 |
} else if (copyNamespaces == ReceiverOptions.ALL_NAMESPACES) { |
| 201 |
copyOptions |= CopyOptions.ALL_NAMESPACES; |
| 202 |
} |
| 203 |
((NodeInfo) item).copy(this, copyOptions, locationId); |
| 204 |
previousAtomic = false; |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
protected String getErrorCodeForDecomposingFunctionItems() { |
| 211 |
boolean isXSLT = getPipelineConfiguration().getHostLanguage() == Configuration.XSLT; |
| 212 |
return isXSLT ? "XTDE0450" : "XQTY0105"; |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public boolean handlesAppend() { |
| 229 |
return true; |
| 230 |
} |
| 231 |
} |
| 232 |
|