1 package com.icl.saxon.tree; 2 import com.icl.saxon.om.NodeInfo; 3 import com.icl.saxon.om.DocumentInfo; 4 import com.icl.saxon.om.NamePool; 5 import com.icl.saxon.Controller; 6 import com.icl.saxon.Context; 7 import com.icl.saxon.KeyManager; 8 import com.icl.saxon.om.AxisEnumeration; 9 import com.icl.saxon.expr.NodeSetExtent; 10 import com.icl.saxon.output.Outputter; 11 import com.icl.saxon.pattern.Pattern; 12 import com.icl.saxon.sort.LocalOrderComparer; 13 14 import java.util.*; 15 import java.net.*; 16 import org.xml.sax.Attributes ; 17 import javax.xml.transform.TransformerException ; 18 19 import org.w3c.dom.*; 20 21 26 27 public final class DocumentImpl extends ParentNodeImpl 28 implements DocumentInfo, Document { 29 30 32 private ElementImpl documentElement; 33 34 private Hashtable idTable = null; 35 private Hashtable entityTable = null; 37 private Hashtable elementList = null; 38 private StringBuffer characterBuffer; 39 private NamePool namePool; 40 private NodeFactory nodeFactory; 41 private LineNumberMap lineNumberMap; 42 private SystemIdMap systemIdMap = new SystemIdMap(); 43 44 47 private Object [] index = new Object [30]; 48 private int indexEntriesUsed = 0; 49 50 public DocumentImpl() { 51 parent = null; 52 } 53 54 57 58 protected void setCharacterBuffer(StringBuffer buffer) { 59 characterBuffer = buffer; 60 } 61 62 65 66 public final StringBuffer getCharacterBuffer() { 67 return characterBuffer; 68 } 69 70 73 74 public void setNamePool(NamePool pool) { 75 namePool = pool; 76 } 77 78 81 82 public NamePool getNamePool() { 83 return namePool; 84 } 85 86 89 90 public void setNodeFactory(NodeFactory factory) { 91 nodeFactory = factory; 92 } 93 94 97 98 public NodeFactory getNodeFactory() { 99 return nodeFactory; 100 } 101 102 108 109 protected void setDocumentElement(ElementImpl e) { 110 documentElement = e; 111 } 112 113 116 117 public void setSystemId(String uri) { 118 if (uri==null) { 122 uri = ""; 123 } 124 systemIdMap.setSystemId(sequence, uri); 125 } 126 127 130 131 public String getSystemId() { 132 return systemIdMap.getSystemId(sequence); 133 } 134 135 139 140 public String getBaseURI() { 141 return getSystemId(); 142 } 143 144 147 148 protected void setSystemId(int seq, String uri) { 149 if (uri==null) { 150 uri = ""; 151 } 154 systemIdMap.setSystemId(seq, uri); 155 } 156 157 158 161 162 protected String getSystemId(int seq) { 163 return systemIdMap.getSystemId(seq); 164 } 165 166 167 170 171 public void setLineNumbering() { 172 lineNumberMap = new LineNumberMap(); 173 lineNumberMap.setLineNumber(sequence, 0); 174 } 175 176 179 180 protected void setLineNumber(int sequence, int line) { 181 if (lineNumberMap != null) { 182 lineNumberMap.setLineNumber(sequence, line); 183 } 184 } 185 186 189 190 protected int getLineNumber(int sequence) { 191 if (lineNumberMap != null) { 192 return lineNumberMap.getLineNumber(sequence); 193 } 194 return -1; 195 } 196 197 201 202 public int getLineNumber() { 203 return 0; 204 } 205 206 210 211 public final short getNodeType() { 212 return ROOT; 213 } 214 215 219 220 public final Node getNextSibling() { 221 return null; 222 } 223 224 228 229 public final Node getPreviousSibling() { 230 return null; 231 } 232 233 237 238 public Element getDocumentElement() { 239 return (ElementImpl)documentElement; 240 } 241 242 246 247 public DocumentInfo getDocumentRoot() { 248 return this; 249 } 250 251 255 256 public String generateId() { 257 return ""; 258 } 259 260 263 264 protected AxisEnumeration getAllElements(int fingerprint) { 265 Integer elkey = new Integer (fingerprint); 266 if (elementList==null) { 267 elementList = new Hashtable(); 268 } 269 NodeSetExtent list = (NodeSetExtent)elementList.get(elkey); 270 if (list==null) { 271 list = new NodeSetExtent(LocalOrderComparer.getInstance()); 272 list.setSorted(true); 273 NodeImpl next = getNextInDocument(this); 274 while (next!=null) { 275 if (next.getNodeType()==ELEMENT && 276 next.getFingerprint() == fingerprint) { 277 list.append(next); 278 } 279 next = next.getNextInDocument(this); 280 } 281 elementList.put(elkey, list); 282 } 283 return (AxisEnumeration)list.enumerate(); 284 } 285 286 290 291 private void indexIDs() { 292 if (idTable!=null) return; idTable = new Hashtable(); 294 295 NodeImpl curr = this; 296 NodeImpl root = curr; 297 while(curr!=null) { 298 if (curr.getNodeType()==ELEMENT) { 299 ElementImpl e = (ElementImpl)curr; 300 Attributes atts = e.getAttributeList(); 301 for (int i=0; i<atts.getLength(); i++) { 302 if ("ID".equals(atts.getType(i))) { 303 registerID(e, atts.getValue(i)); 304 } 305 } 306 } 307 curr = curr.getNextInDocument(root); 308 } 309 } 310 311 316 317 private void registerID(NodeInfo e, String id) { 318 Object old = idTable.get(id); 320 if (old==null) { 321 idTable.put(id, e); 322 } 323 324 } 325 326 331 332 public NodeInfo selectID(String id) { 333 if (idTable==null) indexIDs(); 334 return (NodeInfo)idTable.get(id); 335 } 336 337 345 346 public synchronized Hashtable getKeyIndex(KeyManager keymanager, int fingerprint) { 347 for (int k=0; k<indexEntriesUsed; k+=3) { 348 if (((KeyManager)index[k])==keymanager && 349 ((Integer )index[k+1]).intValue()==fingerprint) { 350 Object ix = index[k+2]; 351 return (Hashtable)index[k+2]; 352 353 } 355 } 356 return null; 357 } 358 359 368 369 public synchronized void setKeyIndex(KeyManager keymanager, int fingerprint, Hashtable keyindex) { 370 for (int k=0; k<indexEntriesUsed; k+=3) { 371 if (((KeyManager)index[k])==keymanager && 372 ((Integer )index[k+1]).intValue() == fingerprint) { 373 index[k+2] = keyindex; 374 return; 375 } 376 } 377 378 if (indexEntriesUsed+3 >= index.length) { 379 Object [] index2 = new Object [indexEntriesUsed*2]; 380 System.arraycopy(index, 0, index2, 0, indexEntriesUsed); 381 index = index2; 382 } 383 index[indexEntriesUsed++] = keymanager; 384 index[indexEntriesUsed++] = new Integer (fingerprint); 385 index[indexEntriesUsed++] = keyindex; 386 } 387 388 392 393 protected void setUnparsedEntity(String name, String uri) { 394 if (entityTable==null) { 395 entityTable = new Hashtable(); 396 } 397 entityTable.put(name, uri); 398 } 399 400 405 406 public String getUnparsedEntity(String name) { 407 if (entityTable==null) { 408 return ""; 409 } 410 String uri = (String )entityTable.get(name); 411 return (uri==null ? "" : uri); 412 } 413 414 417 418 public void copy(Outputter out) throws TransformerException { 419 NodeImpl next = (NodeImpl)getFirstChild(); 420 while (next!=null) { 421 next.copy(out); 422 next = (NodeImpl)next.getNextSibling(); 423 } 424 } 425 426 } 427 428 | Popular Tags |