1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by Ovidiu
  51. * Predescu <ovidiu@cup.hp.com> on behalf of the Apache Software
  52. * Foundation and was originally developed at Hewlett Packard Company.
  53. * For more information on the Apache Software Foundation, please see
  54. * <http://www.apache.org/>.
  55. */
  56. package org.apache.xalan.lib;
  57. import org.apache.xml.dtm.ref.DTMNodeProxy;
  58. import org.apache.xalan.extensions.ExpressionContext;
  59. import javax.xml.transform.SourceLocator;
  60. import org.w3c.dom.Node;
  61. import org.w3c.dom.NodeList;
  62. /**
  63. * <code>NodeInfo</code> defines a set of XSLT extension functions to be
  64. * used from stylesheets.
  65. *
  66. * @author <a href="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
  67. * @since May 24, 2001
  68. */
  69. public class NodeInfo
  70. {
  71. /**
  72. * <code>systemId</code> returns the system id of the current
  73. * context node.
  74. *
  75. * @param context an <code>ExpressionContext</code> value
  76. * @return a <code>String</code> value
  77. */
  78. public static String systemId(ExpressionContext context)
  79. {
  80. Node contextNode = context.getContextNode();
  81. int nodeHandler = ((DTMNodeProxy)contextNode).getDTMNodeNumber();
  82. SourceLocator locator = ((DTMNodeProxy)contextNode).getDTM()
  83. .getSourceLocatorFor(nodeHandler);
  84. if (locator != null)
  85. return locator.getSystemId();
  86. else
  87. return null;
  88. }
  89. /**
  90. * <code>systemId</code> returns the system id of the node passed as
  91. * argument. If a node set is passed as argument, the system id of
  92. * the first node in the set is returned.
  93. *
  94. * @param context an <code>ExpressionContext</code> value
  95. * @param nodeList a <code>NodeList</code> value
  96. * @return a <code>String</code> value
  97. */
  98. public static String systemId(NodeList nodeList)
  99. {
  100. if (nodeList == null || nodeList.getLength() == 0)
  101. return null;
  102. Node node = nodeList.item(0);
  103. int nodeHandler = ((DTMNodeProxy)node).getDTMNodeNumber();
  104. SourceLocator locator = ((DTMNodeProxy)node).getDTM()
  105. .getSourceLocatorFor(nodeHandler);
  106. if (locator != null)
  107. return locator.getSystemId();
  108. else
  109. return null;
  110. }
  111. /**
  112. * <code>publicId</code> returns the public identifier of the current
  113. * context node.
  114. *
  115. * Xalan does not currently record this value, and will return null.
  116. *
  117. * @param context an <code>ExpressionContext</code> value
  118. * @return a <code>String</code> value
  119. */
  120. public static String publicId(ExpressionContext context)
  121. {
  122. Node contextNode = context.getContextNode();
  123. int nodeHandler = ((DTMNodeProxy)contextNode).getDTMNodeNumber();
  124. SourceLocator locator = ((DTMNodeProxy)contextNode).getDTM()
  125. .getSourceLocatorFor(nodeHandler);
  126. if (locator != null)
  127. return locator.getPublicId();
  128. else
  129. return null;
  130. }
  131. /**
  132. * <code>publicId</code> returns the public identifier of the node passed as
  133. * argument. If a node set is passed as argument, the public identifier of
  134. * the first node in the set is returned.
  135. *
  136. * Xalan does not currently record this value, and will return null.
  137. *
  138. * @param nodeList a <code>NodeList</code> value
  139. * @return a <code>String</code> value
  140. */
  141. public static String publicId(NodeList nodeList)
  142. {
  143. if (nodeList == null || nodeList.getLength() == 0)
  144. return null;
  145. Node node = nodeList.item(0);
  146. int nodeHandler = ((DTMNodeProxy)node).getDTMNodeNumber();
  147. SourceLocator locator = ((DTMNodeProxy)node).getDTM()
  148. .getSourceLocatorFor(nodeHandler);
  149. if (locator != null)
  150. return locator.getPublicId();
  151. else
  152. return null;
  153. }
  154. /**
  155. * <code>lineNumber</code> returns the line number of the current
  156. * context node.
  157. *
  158. * NOTE: Xalan does not normally record location information for each node.
  159. * To obtain it, you must set the custom TrAX attribute
  160. * "http://xml.apache.org/xalan/features/source_location"
  161. * true in the TransformerFactory before generating the Transformer and executing
  162. * the stylesheet. Storage cost per node will be noticably increased in this mode.
  163. *
  164. * @param context an <code>ExpressionContext</code> value
  165. * @return an <code>int</code> value. This may be -1 to indicate that the
  166. * line number is not known.
  167. */
  168. public static int lineNumber(ExpressionContext context)
  169. {
  170. Node contextNode = context.getContextNode();
  171. int nodeHandler = ((DTMNodeProxy)contextNode).getDTMNodeNumber();
  172. SourceLocator locator = ((DTMNodeProxy)contextNode).getDTM()
  173. .getSourceLocatorFor(nodeHandler);
  174. if (locator != null)
  175. return locator.getLineNumber();
  176. else
  177. return -1;
  178. }
  179. /**
  180. * <code>lineNumber</code> returns the line number of the node
  181. * passed as argument. If a node set is passed as argument, the line
  182. * number of the first node in the set is returned.
  183. *
  184. * NOTE: Xalan does not normally record location information for each node.
  185. * To obtain it, you must set the custom TrAX attribute
  186. * "http://xml.apache.org/xalan/features/source_location"
  187. * true in the TransformerFactory before generating the Transformer and executing
  188. * the stylesheet. Storage cost per node will be noticably increased in this mode.
  189. *
  190. * @param nodeList a <code>NodeList</code> value
  191. * @return an <code>int</code> value. This may be -1 to indicate that the
  192. * line number is not known.
  193. */
  194. public static int lineNumber(NodeList nodeList)
  195. {
  196. if (nodeList == null || nodeList.getLength() == 0)
  197. return -1;
  198. Node node = nodeList.item(0);
  199. int nodeHandler = ((DTMNodeProxy)node).getDTMNodeNumber();
  200. SourceLocator locator = ((DTMNodeProxy)node).getDTM()
  201. .getSourceLocatorFor(nodeHandler);
  202. if (locator != null)
  203. return locator.getLineNumber();
  204. else
  205. return -1;
  206. }
  207. /**
  208. * <code>columnNumber</code> returns the column number of the
  209. * current context node.
  210. *
  211. * NOTE: Xalan does not normally record location information for each node.
  212. * To obtain it, you must set the custom TrAX attribute
  213. * "http://xml.apache.org/xalan/features/source_location"
  214. * true in the TransformerFactory before generating the Transformer and executing
  215. * the stylesheet. Storage cost per node will be noticably increased in this mode.
  216. *
  217. * @param context an <code>ExpressionContext</code> value
  218. * @return an <code>int</code> value. This may be -1 to indicate that the
  219. * column number is not known.
  220. */
  221. public static int columnNumber(ExpressionContext context)
  222. {
  223. Node contextNode = context.getContextNode();
  224. int nodeHandler = ((DTMNodeProxy)contextNode).getDTMNodeNumber();
  225. SourceLocator locator = ((DTMNodeProxy)contextNode).getDTM()
  226. .getSourceLocatorFor(nodeHandler);
  227. if (locator != null)
  228. return locator.getColumnNumber();
  229. else
  230. return -1;
  231. }
  232. /**
  233. * <code>columnNumber</code> returns the column number of the node
  234. * passed as argument. If a node set is passed as argument, the line
  235. * number of the first node in the set is returned.
  236. *
  237. * NOTE: Xalan does not normally record location information for each node.
  238. * To obtain it, you must set the custom TrAX attribute
  239. * "http://xml.apache.org/xalan/features/source_location"
  240. * true in the TransformerFactory before generating the Transformer and executing
  241. * the stylesheet. Storage cost per node will be noticably increased in this mode.
  242. *
  243. * @param nodeList a <code>NodeList</code> value
  244. * @return an <code>int</code> value. This may be -1 to indicate that the
  245. * column number is not known.
  246. */
  247. public static int columnNumber(NodeList nodeList)
  248. {
  249. if (nodeList == null || nodeList.getLength() == 0)
  250. return -1;
  251. Node node = nodeList.item(0);
  252. int nodeHandler = ((DTMNodeProxy)node).getDTMNodeNumber();
  253. SourceLocator locator = ((DTMNodeProxy)node).getDTM()
  254. .getSourceLocatorFor(nodeHandler);
  255. if (locator != null)
  256. return locator.getColumnNumber();
  257. else
  258. return -1;
  259. }
  260. }