1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: DOMAdapter.java,v 1.23 2004/02/16 22:54:59 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.dom;
  20. import com.sun.org.apache.xalan.internal.xsltc.DOM;
  21. import com.sun.org.apache.xalan.internal.xsltc.DOMEnhancedForDTM;
  22. import com.sun.org.apache.xalan.internal.xsltc.StripFilter;
  23. import com.sun.org.apache.xalan.internal.xsltc.TransletException;
  24. import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;
  25. import com.sun.org.apache.xml.internal.dtm.DTM;
  26. import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
  27. import com.sun.org.apache.xml.internal.serializer.SerializationHandler;
  28. import org.w3c.dom.Node;
  29. import org.w3c.dom.NodeList;
  30. /**
  31. * @author Jacek Ambroziak
  32. * @author Morten Jorgensen
  33. */
  34. public final class DOMAdapter implements DOM {
  35. // Mutually exclusive casting of DOM interface to known implementations
  36. private DOMEnhancedForDTM _enhancedDOM;
  37. private DOM _dom;
  38. private String[] _namesArray;
  39. private String[] _urisArray;
  40. private int[] _typesArray;
  41. private String[] _namespaceArray;
  42. // Cached mappings
  43. private short[] _mapping = null;
  44. private int[] _reverse = null;
  45. private short[] _NSmapping = null;
  46. private short[] _NSreverse = null;
  47. private StripFilter _filter = null;
  48. private int _multiDOMMask;
  49. public DOMAdapter(DOM dom,
  50. String[] namesArray,
  51. String[] urisArray,
  52. int[] typesArray,
  53. String[] namespaceArray) {
  54. if (dom instanceof DOMEnhancedForDTM){
  55. _enhancedDOM = (DOMEnhancedForDTM) dom;
  56. }
  57. _dom = dom;
  58. _namesArray = namesArray;
  59. _urisArray = urisArray;
  60. _typesArray = typesArray;
  61. _namespaceArray = namespaceArray;
  62. }
  63. public void setupMapping(String[] names, String[] urisArray,
  64. int[] typesArray, String[] namespaces) {
  65. _namesArray = names;
  66. _urisArray = urisArray;
  67. _typesArray = typesArray;
  68. _namespaceArray = namespaces;
  69. }
  70. public String[] getNamesArray() {
  71. return _namesArray;
  72. }
  73. public String[] getUrisArray() {
  74. return _urisArray;
  75. }
  76. public int[] getTypesArray() {
  77. return _typesArray;
  78. }
  79. public String[] getNamespaceArray() {
  80. return _namespaceArray;
  81. }
  82. public DOM getDOMImpl() {
  83. return _dom;
  84. }
  85. private short[] getMapping() {
  86. if (_mapping == null) {
  87. if (_enhancedDOM != null) {
  88. _mapping = _enhancedDOM.getMapping(_namesArray, _urisArray,
  89. _typesArray);
  90. }
  91. }
  92. return _mapping;
  93. }
  94. private int[] getReverse() {
  95. if (_reverse == null) {
  96. if (_enhancedDOM != null) {
  97. _reverse = _enhancedDOM.getReverseMapping(_namesArray,
  98. _urisArray,
  99. _typesArray);
  100. }
  101. }
  102. return _reverse;
  103. }
  104. private short[] getNSMapping() {
  105. if (_NSmapping == null) {
  106. if (_enhancedDOM != null) {
  107. _NSmapping = _enhancedDOM.getNamespaceMapping(_namespaceArray);
  108. }
  109. }
  110. return _NSmapping;
  111. }
  112. private short[] getNSReverse() {
  113. if (_NSreverse == null) {
  114. if (_enhancedDOM != null) {
  115. _NSreverse = _enhancedDOM
  116. .getReverseNamespaceMapping(_namespaceArray);
  117. }
  118. }
  119. return _NSreverse;
  120. }
  121. /**
  122. * Returns singleton iterator containg the document root
  123. */
  124. public DTMAxisIterator getIterator() {
  125. return _dom.getIterator();
  126. }
  127. public String getStringValue() {
  128. return _dom.getStringValue();
  129. }
  130. public DTMAxisIterator getChildren(final int node) {
  131. if (_enhancedDOM != null) {
  132. return _enhancedDOM.getChildren(node);
  133. }
  134. else {
  135. DTMAxisIterator iterator = _dom.getChildren(node);
  136. return iterator.setStartNode(node);
  137. }
  138. }
  139. public void setFilter(StripFilter filter) {
  140. _filter = filter;
  141. }
  142. public DTMAxisIterator getTypedChildren(final int type) {
  143. final int[] reverse = getReverse();
  144. if (_enhancedDOM != null) {
  145. return _enhancedDOM.getTypedChildren(reverse[type]);
  146. }
  147. else {
  148. return _dom.getTypedChildren(type);
  149. }
  150. }
  151. public DTMAxisIterator getNamespaceAxisIterator(final int axis,
  152. final int ns) {
  153. return _dom.getNamespaceAxisIterator(axis, getNSReverse()[ns]);
  154. }
  155. public DTMAxisIterator getAxisIterator(final int axis) {
  156. if (_enhancedDOM != null) {
  157. return _enhancedDOM.getAxisIterator(axis);
  158. }
  159. else {
  160. return _dom.getAxisIterator(axis);
  161. }
  162. }
  163. public DTMAxisIterator getTypedAxisIterator(final int axis,
  164. final int type) {
  165. final int[] reverse = getReverse();
  166. if (_enhancedDOM != null) {
  167. return _enhancedDOM.getTypedAxisIterator(axis, reverse[type]);
  168. } else {
  169. return _dom.getTypedAxisIterator(axis, type);
  170. }
  171. }
  172. public int getMultiDOMMask() {
  173. return _multiDOMMask;
  174. }
  175. public void setMultiDOMMask(int mask) {
  176. _multiDOMMask = mask;
  177. }
  178. public DTMAxisIterator getNthDescendant(int type, int n,
  179. boolean includeself) {
  180. return _dom.getNthDescendant(getReverse()[type], n, includeself);
  181. }
  182. public DTMAxisIterator getNodeValueIterator(DTMAxisIterator iterator,
  183. int type, String value,
  184. boolean op) {
  185. return _dom.getNodeValueIterator(iterator, type, value, op);
  186. }
  187. public DTMAxisIterator orderNodes(DTMAxisIterator source, int node) {
  188. return _dom.orderNodes(source, node);
  189. }
  190. public int getExpandedTypeID(final int node) {
  191. if (_enhancedDOM != null) {
  192. return getMapping()[_enhancedDOM.getExpandedTypeID2(node)];
  193. }
  194. else {
  195. return getMapping()[_dom.getExpandedTypeID(node)];
  196. }
  197. }
  198. public int getNamespaceType(final int node) {
  199. return getNSMapping()[_dom.getNSType(node)];
  200. }
  201. public int getNSType(int node) {
  202. return _dom.getNSType(node);
  203. }
  204. public int getParent(final int node) {
  205. return _dom.getParent(node);
  206. }
  207. public int getAttributeNode(final int type, final int element) {
  208. return _dom.getAttributeNode(getReverse()[type], element);
  209. }
  210. public String getNodeName(final int node) {
  211. if (node == DTM.NULL) {
  212. return "";
  213. }
  214. return _dom.getNodeName(node);
  215. }
  216. public String getNodeNameX(final int node)
  217. {
  218. if (node == DTM.NULL) {
  219. return "";
  220. }
  221. return _dom.getNodeNameX(node);
  222. }
  223. public String getNamespaceName(final int node)
  224. {
  225. if (node == DTM.NULL) {
  226. return "";
  227. }
  228. return _dom.getNamespaceName(node);
  229. }
  230. public String getStringValueX(final int node)
  231. {
  232. if (_enhancedDOM != null) {
  233. return _enhancedDOM.getStringValueX(node);
  234. }
  235. else {
  236. if (node == DTM.NULL) {
  237. return "";
  238. }
  239. return _dom.getStringValueX(node);
  240. }
  241. }
  242. public void copy(final int node, SerializationHandler handler)
  243. throws TransletException
  244. {
  245. _dom.copy(node, handler);
  246. }
  247. public void copy(DTMAxisIterator nodes,SerializationHandler handler)
  248. throws TransletException
  249. {
  250. _dom.copy(nodes, handler);
  251. }
  252. public String shallowCopy(final int node, SerializationHandler handler)
  253. throws TransletException
  254. {
  255. if (_enhancedDOM != null) {
  256. return _enhancedDOM.shallowCopy(node, handler);
  257. }
  258. else {
  259. return _dom.shallowCopy(node, handler);
  260. }
  261. }
  262. public boolean lessThan(final int node1, final int node2)
  263. {
  264. return _dom.lessThan(node1, node2);
  265. }
  266. public void characters(final int textNode, SerializationHandler handler)
  267. throws TransletException
  268. {
  269. if (_enhancedDOM != null) {
  270. _enhancedDOM.characters(textNode, handler);
  271. }
  272. else {
  273. _dom.characters(textNode, handler);
  274. }
  275. }
  276. public Node makeNode(int index)
  277. {
  278. return _dom.makeNode(index);
  279. }
  280. public Node makeNode(DTMAxisIterator iter)
  281. {
  282. return _dom.makeNode(iter);
  283. }
  284. public NodeList makeNodeList(int index)
  285. {
  286. return _dom.makeNodeList(index);
  287. }
  288. public NodeList makeNodeList(DTMAxisIterator iter)
  289. {
  290. return _dom.makeNodeList(iter);
  291. }
  292. public String getLanguage(int node)
  293. {
  294. return _dom.getLanguage(node);
  295. }
  296. public int getSize()
  297. {
  298. return _dom.getSize();
  299. }
  300. public void setDocumentURI(String uri)
  301. {
  302. if (_enhancedDOM != null) {
  303. _enhancedDOM.setDocumentURI(uri);
  304. }
  305. }
  306. public String getDocumentURI()
  307. {
  308. if (_enhancedDOM != null) {
  309. return _enhancedDOM.getDocumentURI();
  310. }
  311. else {
  312. return "";
  313. }
  314. }
  315. public String getDocumentURI(int node)
  316. {
  317. return _dom.getDocumentURI(node);
  318. }
  319. public int getDocument()
  320. {
  321. return _dom.getDocument();
  322. }
  323. public boolean isElement(final int node)
  324. {
  325. return(_dom.isElement(node));
  326. }
  327. public boolean isAttribute(final int node)
  328. {
  329. return(_dom.isAttribute(node));
  330. }
  331. public int getNodeIdent(int nodeHandle)
  332. {
  333. return _dom.getNodeIdent(nodeHandle);
  334. }
  335. public int getNodeHandle(int nodeId)
  336. {
  337. return _dom.getNodeHandle(nodeId);
  338. }
  339. /**
  340. * Return a instance of a DOM class to be used as an RTF
  341. */
  342. public DOM getResultTreeFrag(int initSize, int rtfType)
  343. {
  344. if (_enhancedDOM != null) {
  345. return _enhancedDOM.getResultTreeFrag(initSize, rtfType);
  346. }
  347. else {
  348. return _dom.getResultTreeFrag(initSize, rtfType);
  349. }
  350. }
  351. /**
  352. * Return a instance of a DOM class to be used as an RTF
  353. */
  354. public DOM getResultTreeFrag(int initSize, int rtfType,
  355. boolean addToManager)
  356. {
  357. if (_enhancedDOM != null) {
  358. return _enhancedDOM.getResultTreeFrag(initSize, rtfType,
  359. addToManager);
  360. }
  361. else {
  362. return _dom.getResultTreeFrag(initSize, rtfType, addToManager);
  363. }
  364. }
  365. /**
  366. * Returns a SerializationHandler class wrapped in a SAX adapter.
  367. */
  368. public SerializationHandler getOutputDomBuilder()
  369. {
  370. return _dom.getOutputDomBuilder();
  371. }
  372. public String lookupNamespace(int node, String prefix)
  373. throws TransletException
  374. {
  375. return _dom.lookupNamespace(node, prefix);
  376. }
  377. public String getUnparsedEntityURI(String entity) {
  378. return _dom.getUnparsedEntityURI(entity);
  379. }
  380. public Hashtable getElementsWithIDs() {
  381. return _dom.getElementsWithIDs();
  382. }
  383. }