1 package net.sf.saxon.tree; 2 import net.sf.saxon.Configuration; 3 import net.sf.saxon.event.Receiver; 4 import net.sf.saxon.om.*; 5 import net.sf.saxon.trans.XPathException; 6 import net.sf.saxon.type.Type; 7 8 import java.util.ArrayList ; 9 import java.util.HashMap ; 10 11 16 17 public final class DocumentImpl extends ParentNodeImpl 18 implements DocumentInfo { 19 20 22 private ElementImpl documentElement; 23 24 private HashMap idTable = null; 25 private int documentNumber; 26 private HashMap entityTable = null; 27 private HashMap elementList = null; 28 private Configuration config; 30 private LineNumberMap lineNumberMap; 31 private SystemIdMap systemIdMap = new SystemIdMap(); 32 33 public DocumentImpl() { 34 parent = null; 35 } 36 37 40 41 public void setConfiguration(Configuration config) { 42 this.config = config; 43 documentNumber = config.getDocumentNumberAllocator().allocateDocumentNumber(); 44 } 45 46 49 50 public Configuration getConfiguration() { 51 return config; 52 } 53 54 57 58 public NamePool getNamePool() { 59 return config.getNamePool(); 60 } 61 62 65 66 public int getDocumentNumber() { 67 return documentNumber; 68 } 69 70 76 77 void setDocumentElement(ElementImpl e) { 78 documentElement = e; 79 } 80 81 84 85 public void setSystemId(String uri) { 86 if (uri==null) { 87 uri = ""; 88 } 89 systemIdMap.setSystemId(sequence, uri); 90 } 91 92 95 96 public String getSystemId() { 97 return systemIdMap.getSystemId(sequence); 98 } 99 100 104 105 public String getBaseURI() { 106 return getSystemId(); 107 } 108 109 112 113 void setSystemId(int seq, String uri) { 114 if (uri==null) { 115 uri = ""; 116 } 117 systemIdMap.setSystemId(seq, uri); 118 } 119 120 121 124 125 String getSystemId(int seq) { 126 return systemIdMap.getSystemId(seq); 127 } 128 129 130 133 134 public void setLineNumbering() { 135 lineNumberMap = new LineNumberMap(); 136 lineNumberMap.setLineNumber(sequence, 0); 137 } 138 139 142 143 void setLineNumber(int sequence, int line) { 144 if (lineNumberMap != null) { 145 lineNumberMap.setLineNumber(sequence, line); 146 } 147 } 148 149 152 153 int getLineNumber(int sequence) { 154 if (lineNumberMap != null) { 155 return lineNumberMap.getLineNumber(sequence); 156 } 157 return -1; 158 } 159 160 164 165 public int getLineNumber() { 166 return 0; 167 } 168 169 173 174 public final int getNodeKind() { 175 return Type.DOCUMENT; 176 } 177 178 182 183 public final NodeInfo getNextSibling() { 184 return null; 185 } 186 187 191 192 public final NodeInfo getPreviousSibling() { 193 return null; 194 } 195 196 200 201 public ElementImpl getDocumentElement() { 202 return documentElement; 203 } 204 205 209 210 public NodeInfo getRoot() { 211 return this; 212 } 213 214 218 219 public DocumentInfo getDocumentRoot() { 220 return this; 221 } 222 223 227 228 public String generateId() { 229 return "d"+documentNumber; 230 } 231 232 235 236 AxisIterator getAllElements(int fingerprint) { 237 Integer elkey = new Integer (fingerprint); 238 if (elementList==null) { 239 elementList = new HashMap (500); 240 } 241 ArrayList list = (ArrayList )elementList.get(elkey); 242 if (list==null) { 243 list = new ArrayList (500); 244 NodeImpl next = getNextInDocument(this); 245 while (next!=null) { 246 if (next.getNodeKind()==Type.ELEMENT && 247 next.getFingerprint() == fingerprint) { 248 list.add(next); 249 } 250 next = next.getNextInDocument(this); 251 } 252 elementList.put(elkey, list); 253 } 254 return new ListIterator(list); 255 } 256 257 261 262 private void indexIDs() { 263 if (idTable!=null) return; idTable = new HashMap (256); 265 266 NodeImpl curr = this; 267 NodeImpl root = curr; 268 while(curr!=null) { 269 if (curr.getNodeKind()==Type.ELEMENT) { 270 ElementImpl e = (ElementImpl)curr; 271 AttributeCollection atts = e.getAttributeList(); 272 for (int i=0; i<atts.getLength(); i++) { 273 if (atts.isId(i) && XMLChar.isValidNCName(atts.getValue(i).trim())) { 274 registerID(e, atts.getValue(i).trim()); 276 } 277 } 278 } 279 curr = curr.getNextInDocument(root); 280 } 281 } 282 283 288 289 private void registerID(NodeInfo e, String id) { 290 Object old = idTable.get(id); 292 if (old==null) { 293 idTable.put(id, e); 294 } 295 296 } 297 298 303 304 public NodeInfo selectID(String id) { 305 if (idTable==null) indexIDs(); 306 return (NodeInfo)idTable.get(id); 307 } 308 309 313 314 void setUnparsedEntity(String name, String uri, String publicId) { 315 if (entityTable==null) { 317 entityTable = new HashMap (10); 318 } 319 String [] ids = new String [2]; 320 ids[0] = uri; 321 ids[1] = publicId; 322 entityTable.put(name, ids); 323 } 324 325 332 333 public String [] getUnparsedEntity(String name) { 334 if (entityTable==null) { 335 return null; 336 } 337 return (String [])entityTable.get(name); 338 } 339 340 343 344 public void copy(Receiver out, int whichNamespaces, boolean copyAnnotations, int locationId) throws XPathException { 345 out.startDocument(0); 346 NodeImpl next = (NodeImpl)getFirstChild(); 347 while (next!=null) { 348 next.copy(out, whichNamespaces, copyAnnotations, locationId); 349 next = (NodeImpl)next.getNextSibling(); 350 } 351 out.endDocument(); 352 } 353 354 } 355 356 | Popular Tags |