1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 package com.sun.org.apache.xalan.internal.lib;
23 
24 import javax.xml.transform.SourceLocator;
25 
26 import com.sun.org.apache.xalan.internal.extensions.ExpressionContext;
27 import com.sun.org.apache.xml.internal.dtm.ref.DTMNodeProxy;
28 
29 import org.w3c.dom.Node;
30 import org.w3c.dom.NodeList;
31 
32 /**
33  * <code>NodeInfo</code> defines a set of XSLT extension functions to be
34  * used from stylesheets.
35  *
36  * @author <a href="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
37  * @since May 24, 2001
38  */
39 public class NodeInfo
40 {
41   /**
42    * <code>systemId</code> returns the system id of the current
43    * context node.
44    *
45    * @param context an <code>ExpressionContext</code> value
46    * @return a <code>String</code> value
47    */
systemId(ExpressionContext context)48   public static String systemId(ExpressionContext context)
49   {
50     Node contextNode = context.getContextNode();
51     int nodeHandler = ((DTMNodeProxy)contextNode).getDTMNodeNumber();
52     SourceLocator locator = ((DTMNodeProxy)contextNode).getDTM()
53       .getSourceLocatorFor(nodeHandler);
54 
55     if (locator != null)
56       return locator.getSystemId();
57     else
58       return null;
59   }
60 
61   /**
62    * <code>systemId</code> returns the system id of the node passed as
63    * argument. If a node set is passed as argument, the system id of
64    * the first node in the set is returned.
65    *
66    * @param nodeList a <code>NodeList</code> value
67    * @return a <code>String</code> value
68    */
systemId(NodeList nodeList)69   public static String systemId(NodeList nodeList)
70   {
71     if (nodeList == null || nodeList.getLength() == 0)
72       return null;
73 
74     Node node = nodeList.item(0);
75     int nodeHandler = ((DTMNodeProxy)node).getDTMNodeNumber();
76     SourceLocator locator = ((DTMNodeProxy)node).getDTM()
77       .getSourceLocatorFor(nodeHandler);
78 
79     if (locator != null)
80       return locator.getSystemId();
81     else
82       return null;
83   }
84 
85   /**
86    * <code>publicId</code> returns the public identifier of the current
87    * context node.
88    *
89    * Xalan does not currently record this value, and will return null.
90    *
91    * @param context an <code>ExpressionContext</code> value
92    * @return a <code>String</code> value
93    */
publicId(ExpressionContext context)94   public static String publicId(ExpressionContext context)
95   {
96     Node contextNode = context.getContextNode();
97     int nodeHandler = ((DTMNodeProxy)contextNode).getDTMNodeNumber();
98     SourceLocator locator = ((DTMNodeProxy)contextNode).getDTM()
99       .getSourceLocatorFor(nodeHandler);
100 
101     if (locator != null)
102       return locator.getPublicId();
103     else
104       return null;
105   }
106 
107   /**
108    * <code>publicId</code> returns the public identifier of the node passed as
109    * argument. If a node set is passed as argument, the public identifier of
110    * the first node in the set is returned.
111    *
112    * Xalan does not currently record this value, and will return null.
113    *
114    * @param nodeList a <code>NodeList</code> value
115    * @return a <code>String</code> value
116    */
publicId(NodeList nodeList)117   public static String publicId(NodeList nodeList)
118   {
119     if (nodeList == null || nodeList.getLength() == 0)
120       return null;
121 
122     Node node = nodeList.item(0);
123     int nodeHandler = ((DTMNodeProxy)node).getDTMNodeNumber();
124     SourceLocator locator = ((DTMNodeProxy)node).getDTM()
125       .getSourceLocatorFor(nodeHandler);
126 
127     if (locator != null)
128       return locator.getPublicId();
129     else
130       return null;
131   }
132 
133   /**
134    * <code>lineNumber</code> returns the line number of the current
135    * context node.
136    *
137    * NOTE: Xalan does not normally record location information for each node.
138    * To obtain it, you must set the custom TrAX attribute
139    * "http://xml.apache.org/xalan/features/source_location"
140    * true in the TransformerFactory before generating the Transformer and executing
141    * the stylesheet. Storage cost per node will be noticably increased in this mode.
142    *
143    * @param context an <code>ExpressionContext</code> value
144    * @return an <code>int</code> value. This may be -1 to indicate that the
145    * line number is not known.
146    */
lineNumber(ExpressionContext context)147   public static int lineNumber(ExpressionContext context)
148   {
149     Node contextNode = context.getContextNode();
150     int nodeHandler = ((DTMNodeProxy)contextNode).getDTMNodeNumber();
151     SourceLocator locator = ((DTMNodeProxy)contextNode).getDTM()
152       .getSourceLocatorFor(nodeHandler);
153 
154     if (locator != null)
155       return locator.getLineNumber();
156     else
157       return -1;
158   }
159 
160   /**
161    * <code>lineNumber</code> returns the line number of the node
162    * passed as argument. If a node set is passed as argument, the line
163    * number of the first node in the set is returned.
164    *
165    * NOTE: Xalan does not normally record location information for each node.
166    * To obtain it, you must set the custom TrAX attribute
167    * "http://xml.apache.org/xalan/features/source_location"
168    * true in the TransformerFactory before generating the Transformer and executing
169    * the stylesheet. Storage cost per node will be noticably increased in this mode.
170    *
171    * @param nodeList a <code>NodeList</code> value
172    * @return an <code>int</code> value. This may be -1 to indicate that the
173    * line number is not known.
174    */
lineNumber(NodeList nodeList)175   public static int lineNumber(NodeList nodeList)
176   {
177     if (nodeList == null || nodeList.getLength() == 0)
178       return -1;
179 
180     Node node = nodeList.item(0);
181     int nodeHandler = ((DTMNodeProxy)node).getDTMNodeNumber();
182     SourceLocator locator = ((DTMNodeProxy)node).getDTM()
183       .getSourceLocatorFor(nodeHandler);
184 
185     if (locator != null)
186       return locator.getLineNumber();
187     else
188       return -1;
189   }
190 
191   /**
192    * <code>columnNumber</code> returns the column number of the
193    * current context node.
194    *
195    * NOTE: Xalan does not normally record location information for each node.
196    * To obtain it, you must set the custom TrAX attribute
197    * "http://xml.apache.org/xalan/features/source_location"
198    * true in the TransformerFactory before generating the Transformer and executing
199    * the stylesheet. Storage cost per node will be noticably increased in this mode.
200    *
201    * @param context an <code>ExpressionContext</code> value
202    * @return an <code>int</code> value. This may be -1 to indicate that the
203    * column number is not known.
204    */
columnNumber(ExpressionContext context)205   public static int columnNumber(ExpressionContext context)
206   {
207     Node contextNode = context.getContextNode();
208     int nodeHandler = ((DTMNodeProxy)contextNode).getDTMNodeNumber();
209     SourceLocator locator = ((DTMNodeProxy)contextNode).getDTM()
210       .getSourceLocatorFor(nodeHandler);
211 
212     if (locator != null)
213       return locator.getColumnNumber();
214     else
215       return -1;
216   }
217 
218   /**
219    * <code>columnNumber</code> returns the column number of the node
220    * passed as argument. If a node set is passed as argument, the line
221    * number of the first node in the set is returned.
222    *
223    * NOTE: Xalan does not normally record location information for each node.
224    * To obtain it, you must set the custom TrAX attribute
225    * "http://xml.apache.org/xalan/features/source_location"
226    * true in the TransformerFactory before generating the Transformer and executing
227    * the stylesheet. Storage cost per node will be noticably increased in this mode.
228    *
229    * @param nodeList a <code>NodeList</code> value
230    * @return an <code>int</code> value. This may be -1 to indicate that the
231    * column number is not known.
232    */
columnNumber(NodeList nodeList)233   public static int columnNumber(NodeList nodeList)
234   {
235     if (nodeList == null || nodeList.getLength() == 0)
236       return -1;
237 
238     Node node = nodeList.item(0);
239     int nodeHandler = ((DTMNodeProxy)node).getDTMNodeNumber();
240     SourceLocator locator = ((DTMNodeProxy)node).getDTM()
241       .getSourceLocatorFor(nodeHandler);
242 
243     if (locator != null)
244       return locator.getColumnNumber();
245     else
246       return -1;
247   }
248 }
249