| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.tree.tiny; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.event.Receiver; |
| 12 |
import net.sf.saxon.expr.parser.Location; |
| 13 |
import net.sf.saxon.om.AtomicSequence; |
| 14 |
import net.sf.saxon.om.AxisInfo; |
| 15 |
import net.sf.saxon.om.CopyOptions; |
| 16 |
import net.sf.saxon.om.NodeInfo; |
| 17 |
import net.sf.saxon.pattern.NameTest; |
| 18 |
import net.sf.saxon.pattern.NodeKindTest; |
| 19 |
import net.sf.saxon.trans.XPathException; |
| 20 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 21 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 22 |
import net.sf.saxon.type.AnyType; |
| 23 |
import net.sf.saxon.type.SchemaType; |
| 24 |
import net.sf.saxon.type.Type; |
| 25 |
import net.sf.saxon.type.Untyped; |
| 26 |
import net.sf.saxon.value.UntypedAtomicValue; |
| 27 |
import net.sf.saxon.z.IntHashMap; |
| 28 |
|
| 29 |
import java.util.ArrayList; |
| 30 |
import java.util.List; |
| 31 |
import java.util.Map; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public final class TinyDocumentImpl extends TinyParentNodeImpl { |
| 40 |
|
| 41 |
|
| 42 |
private IntHashMap<List<NodeInfo>> elementList; |
| 43 |
private String baseURI; |
| 44 |
|
| 45 |
|
| 46 |
public TinyDocumentImpl( TinyTree tree) { |
| 47 |
this.tree = tree; |
| 48 |
nodeNr = 0; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public TinyTree getTree() { |
| 56 |
return tree; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public NodeInfo getRootNode() { |
| 66 |
return this; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public Configuration getConfiguration() { |
| 74 |
return tree.getConfiguration(); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public void setSystemId(String uri) { |
| 82 |
tree.setSystemId(nodeNr, uri); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public String getSystemId() { |
| 90 |
return tree.getSystemId(nodeNr); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public void setBaseURI(String uri) { |
| 100 |
baseURI = uri; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public String getBaseURI() { |
| 108 |
if (baseURI != null) { |
| 109 |
return baseURI; |
| 110 |
} |
| 111 |
return getSystemId(); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public int getLineNumber() { |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public boolean isTyped() { |
| 131 |
return tree.getTypeArray() != null; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public final int getNodeKind() { |
| 141 |
return Type.DOCUMENT; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public TinyNodeImpl getParent() { |
| 152 |
return null; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public NodeInfo getRoot() { |
| 163 |
return this; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public void generateId( FastStringBuffer buffer) { |
| 173 |
buffer.append('d'); |
| 174 |
buffer.append(Long.toString(getTreeInfo().getDocumentNumber())); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public AtomicSequence atomize() throws XPathException { |
| 186 |
return new UntypedAtomicValue(getStringValueCS()); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
AxisIterator getAllElements(int fingerprint) { |
| 199 |
if (elementList == null) { |
| 200 |
elementList = new IntHashMap<List<NodeInfo>>(20); |
| 201 |
} |
| 202 |
List<NodeInfo> list = elementList.get(fingerprint); |
| 203 |
if (list == null) { |
| 204 |
list = makeElementList(fingerprint); |
| 205 |
elementList.put(fingerprint, list); |
| 206 |
} |
| 207 |
return new net.sf.saxon.tree.iter.ListIterator.OfNodes(list); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
List<NodeInfo> makeElementList(int fingerprint) { |
| 218 |
int size = tree.getNumberOfNodes() / 20; |
| 219 |
if (size > 100) { |
| 220 |
size = 100; |
| 221 |
} |
| 222 |
if (size < 20) { |
| 223 |
size = 20; |
| 224 |
} |
| 225 |
ArrayList<NodeInfo> list = new ArrayList<>(size); |
| 226 |
int i = nodeNr + 1; |
| 227 |
try { |
| 228 |
while (tree.depth[i] != 0) { |
| 229 |
byte kind = tree.nodeKind[i]; |
| 230 |
if ((kind & 0x0f) == Type.ELEMENT && |
| 231 |
(tree.nameCode[i] & 0xfffff) == fingerprint) { |
| 232 |
list.add(tree.getNode(i)); |
| 233 |
} else if (kind == Type.EXTERNAL_NODE_REFERENCE) { |
| 234 |
AxisIterator externals = tree.externalNodes.get(tree.alpha[i]) |
| 235 |
.iterateAxis(AxisInfo.DESCENDANT, new NameTest(Type.ELEMENT, fingerprint, getNamePool())); |
| 236 |
NodeInfo ext; |
| 237 |
while ((ext = externals.next()) != null) { |
| 238 |
list.add(ext); |
| 239 |
} |
| 240 |
} |
| 241 |
i++; |
| 242 |
} |
| 243 |
} catch (ArrayIndexOutOfBoundsException e) { |
| 244 |
|
| 245 |
|
| 246 |
return list; |
| 247 |
} |
| 248 |
list.trimToSize(); |
| 249 |
return list; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public SchemaType getSchemaType() { |
| 266 |
AxisIterator children = iterateAxis(AxisInfo.CHILD, NodeKindTest.ELEMENT); |
| 267 |
NodeInfo node = children.next(); |
| 268 |
if (node == null || node.getSchemaType() == Untyped.getInstance()) { |
| 269 |
return Untyped.getInstance(); |
| 270 |
} else { |
| 271 |
return AnyType.getInstance(); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public void copy( Receiver out, int copyOptions, Location locationId) throws XPathException { |
| 280 |
|
| 281 |
out.startDocument(CopyOptions.getStartDocumentProperties(copyOptions)); |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
if (tree.entityTable != null) { |
| 286 |
for (Map.Entry<String, String[]> entry : tree.entityTable.entrySet()) { |
| 287 |
String name = entry.getKey(); |
| 288 |
String[] details = entry.getValue(); |
| 289 |
String systemId = details[0]; |
| 290 |
String publicId = details[1]; |
| 291 |
out.setUnparsedEntity(name, systemId, publicId); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
iterateAxis(AxisInfo.CHILD).forEachOrFail(n -> n.copy(out, copyOptions, locationId)); |
| 298 |
|
| 299 |
out.endDocument(); |
| 300 |
} |
| 301 |
|
| 302 |
public void showSize() { |
| 303 |
tree.showSize(); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public int hashCode() { |
| 316 |
|
| 317 |
|
| 318 |
return (int) tree.getDocumentNumber(); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
public boolean containsGraftedNodes() { |
| 326 |
return tree.externalNodes != null && !tree.externalNodes.isEmpty(); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
} |
| 331 |
|