PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/saxonB/net/sf/saxon/tinytree/TinyDocumentImpl.java

https://bitbucket.org/kalpert/nunda_pipeline
Java | 321 lines | 149 code | 61 blank | 111 comment | 23 complexity | edbcf5692a582f7172e9163056110396 MD5 | raw file
  1. package net.sf.saxon.tinytree;
  2. import net.sf.saxon.Configuration;
  3. import net.sf.saxon.sort.IntHashMap;
  4. import net.sf.saxon.event.Receiver;
  5. import net.sf.saxon.om.*;
  6. import net.sf.saxon.trans.XPathException;
  7. import net.sf.saxon.type.Type;
  8. import java.util.*;
  9. /**
  10. * A node in the XML parse tree representing the Document itself (or equivalently, the root
  11. * node of the Document).<P>
  12. */
  13. public final class TinyDocumentImpl extends TinyParentNodeImpl
  14. implements DocumentInfo {
  15. private HashMap idTable = null;
  16. private IntHashMap elementList = null;
  17. private HashMap entityTable = null;
  18. private String baseURI = null;
  19. public TinyDocumentImpl(TinyTree tree) {
  20. this.tree = tree;
  21. nodeNr = tree.numberOfNodes;
  22. }
  23. /**
  24. * Get the tree containing this node
  25. */
  26. public TinyTree getTree() {
  27. return tree;
  28. }
  29. /**
  30. * Set the Configuration that contains this document
  31. */
  32. public void setConfiguration(Configuration config) {
  33. if (config != tree.getConfiguration()) {
  34. throw new IllegalArgumentException(
  35. "Configuration of document differs from that of the supporting TinyTree");
  36. }
  37. }
  38. /**
  39. * Get the configuration previously set using setConfiguration
  40. */
  41. public Configuration getConfiguration() {
  42. return tree.getConfiguration();
  43. }
  44. /**
  45. * Set the system id of this node
  46. */
  47. public void setSystemId(String uri) {
  48. tree.setSystemId(nodeNr, uri);
  49. }
  50. /**
  51. * Get the system id of this root node
  52. */
  53. public String getSystemId() {
  54. return tree.getSystemId(nodeNr);
  55. }
  56. /**
  57. * Set the base URI of this document node
  58. */
  59. public void setBaseURI(String uri) {
  60. baseURI = uri;
  61. }
  62. /**
  63. * Get the base URI of this root node.
  64. */
  65. public String getBaseURI() {
  66. if (baseURI != null) {
  67. return baseURI;
  68. }
  69. return getSystemId();
  70. }
  71. /**
  72. * Get the line number of this root node.
  73. * @return 0 always
  74. */
  75. public int getLineNumber() {
  76. return 0;
  77. }
  78. /**
  79. * Return the type of node.
  80. * @return Type.DOCUMENT (always)
  81. */
  82. public final int getNodeKind() {
  83. return Type.DOCUMENT;
  84. }
  85. /**
  86. * Find the parent node of this node.
  87. * @return The Node object describing the containing element or root node.
  88. */
  89. public NodeInfo getParent() {
  90. return null;
  91. }
  92. /**
  93. * Get the root node
  94. * @return the NodeInfo that is the root of the tree - not necessarily a document node
  95. */
  96. public NodeInfo getRoot() {
  97. return this;
  98. }
  99. /**
  100. * Get the root (document) node
  101. * @return the DocumentInfo representing the document node, or null if the
  102. * root of the tree is not a document node
  103. */
  104. public DocumentInfo getDocumentRoot() {
  105. return this;
  106. }
  107. /**
  108. * Get a character string that uniquely identifies this node
  109. * @param buffer to contain an identifier based on the document number
  110. */
  111. public void generateId(FastStringBuffer buffer) {
  112. buffer.append('d');
  113. buffer.append(Integer.toString(getDocumentNumber()));
  114. }
  115. /**
  116. * Get a list of all elements with a given name. This is implemented
  117. * as a memo function: the first time it is called for a particular
  118. * element type, it remembers the result for next time.
  119. */
  120. AxisIterator getAllElements(int fingerprint) {
  121. if (elementList==null) {
  122. elementList = new IntHashMap(20);
  123. }
  124. List list = (List)elementList.get(fingerprint);
  125. if (list==null) {
  126. list = getElementList(fingerprint);
  127. elementList.put(fingerprint, list);
  128. }
  129. return new NodeListIterator(list);
  130. }
  131. /**
  132. * Get a list containing all the elements with a given element name
  133. * @param fingerprint the fingerprint of the element name
  134. * @return list a List containing the TinyElementImpl objects
  135. */
  136. List getElementList(int fingerprint) {
  137. int size = tree.getNumberOfNodes()/20;
  138. if (size > 100) {
  139. size = 100;
  140. }
  141. if (size < 20) {
  142. size = 20;
  143. }
  144. List list = new ArrayList(size);
  145. int i = nodeNr+1;
  146. try {
  147. while (tree.depth[i] != 0) {
  148. if (tree.nodeKind[i]==Type.ELEMENT &&
  149. (tree.nameCode[i] & 0xfffff) == fingerprint) {
  150. list.add(tree.getNode(i));
  151. }
  152. i++;
  153. }
  154. } catch (ArrayIndexOutOfBoundsException e) {
  155. // this shouldn't happen. If it does happen, it means the tree wasn't properly closed
  156. // during construction (there is no stopper node at the end). In this case, we'll recover
  157. return list;
  158. }
  159. return list;
  160. }
  161. /**
  162. * Register a unique element ID. Fails if there is already an element with that ID.
  163. * @param e The NodeInfo (always an element) having a particular unique ID value
  164. * @param id The unique ID value. The caller is responsible for checking that this
  165. * is a valid NCName.
  166. */
  167. void registerID(NodeInfo e, String id) {
  168. if (idTable==null) {
  169. idTable = new HashMap(256);
  170. }
  171. // the XPath spec (5.2.1) says ignore the second ID if it's not unique
  172. NodeInfo old = (NodeInfo)idTable.get(id);
  173. if (old==null) {
  174. idTable.put(id, e);
  175. }
  176. }
  177. /**
  178. * Get the element with a given ID.
  179. * @param id The unique ID of the required element, previously registered using registerID()
  180. * @return The NodeInfo (always an Element) for the given ID if one has been registered,
  181. * otherwise null.
  182. */
  183. public NodeInfo selectID(String id) {
  184. if (idTable==null) return null; // no ID values found
  185. return (NodeInfo)idTable.get(id);
  186. }
  187. /**
  188. * Set an unparsed entity URI associated with this document. For system use only, while
  189. * building the document.
  190. */
  191. void setUnparsedEntity(String name, String uri, String publicId) {
  192. if (entityTable==null) {
  193. entityTable = new HashMap(20);
  194. }
  195. String[] ids = new String[2];
  196. ids[0] = uri;
  197. ids[1] = publicId;
  198. entityTable.put(name, ids);
  199. }
  200. /**
  201. * Get the list of unparsed entities defined in this document
  202. * @return an Iterator, whose items are of type String, containing the names of all
  203. * unparsed entities defined in this document. If there are no unparsed entities or if the
  204. * information is not available then an empty iterator is returned
  205. */
  206. public Iterator getUnparsedEntityNames() {
  207. if (entityTable == null) {
  208. return Collections.EMPTY_LIST.iterator();
  209. } else {
  210. return entityTable.keySet().iterator();
  211. }
  212. }
  213. /**
  214. * Get the unparsed entity with a given nameID if there is one, or null if not. If the entity
  215. * does not exist, return null.
  216. * @param name the name of the entity
  217. * @return if the entity exists, return an array of two Strings, the first holding the system ID
  218. * of the entity, the second holding the public
  219. */
  220. public String[] getUnparsedEntity(String name) {
  221. if (entityTable==null) {
  222. return null;
  223. }
  224. return (String[])entityTable.get(name);
  225. }
  226. /**
  227. * Copy this node to a given outputter
  228. */
  229. public void copy(Receiver out, int whichNamespaces, boolean copyAnnotations, int locationId) throws XPathException {
  230. out.startDocument(0);
  231. // output the children
  232. AxisIterator children = iterateAxis(Axis.CHILD);
  233. while (true) {
  234. NodeInfo n = (NodeInfo)children.next();
  235. if (n == null) {
  236. break;
  237. }
  238. n.copy(out, whichNamespaces, copyAnnotations, locationId);
  239. }
  240. out.endDocument();
  241. }
  242. public void showSize() {
  243. tree.showSize();
  244. }
  245. }
  246. //
  247. // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
  248. // you may not use this file except in compliance with the License. You may obtain a copy of the
  249. // License at http://www.mozilla.org/MPL/
  250. //
  251. // Software distributed under the License is distributed on an "AS IS" basis,
  252. // WITHOUT WARRANTY OF ANY KIND, either express or implied.
  253. // See the License for the specific language governing rights and limitations under the License.
  254. //
  255. // The Original Code is: all this file
  256. //
  257. // The Initial Developer of the Original Code is Michael H. Kay.
  258. //
  259. // Contributor(s):
  260. //