KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xpath > internal > functions > FunctionDef1Arg


1 /*
2  * Copyright 1999-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: FunctionDef1Arg.java,v 1.12 2004/02/17 04:34:00 minchau Exp $
18  */

19 package com.sun.org.apache.xpath.internal.functions;
20
21 import com.sun.org.apache.xalan.internal.res.XSLMessages;
22 import com.sun.org.apache.xml.internal.dtm.DTM;
23 import com.sun.org.apache.xml.internal.utils.XMLString;
24 import com.sun.org.apache.xpath.internal.XPathContext;
25 import com.sun.org.apache.xpath.internal.objects.XString;
26 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
27
28 /**
29  * Base class for functions that accept one argument that can be defaulted if
30  * not specified.
31  * @xsl.usage advanced
32  */

33 public class FunctionDef1Arg extends FunctionOneArg
34 {
35
36   /**
37    * Execute the first argument expression that is expected to return a
38    * nodeset. If the argument is null, then return the current context node.
39    *
40    * @param xctxt Runtime XPath context.
41    *
42    * @return The first node of the executed nodeset, or the current context
43    * node if the first argument is null.
44    *
45    * @throws javax.xml.transform.TransformerException if an error occurs while
46    * executing the argument expression.
47    */

48   protected int getArg0AsNode(XPathContext xctxt)
49           throws javax.xml.transform.TransformerException JavaDoc
50   {
51
52     return (null == m_arg0)
53            ? xctxt.getCurrentNode() : m_arg0.asNode(xctxt);
54   }
55   
56   /**
57    * Tell if the expression is a nodeset expression.
58    * @return true if the expression can be represented as a nodeset.
59    */

60   public boolean Arg0IsNodesetExpr()
61   {
62     return (null == m_arg0) ? true : m_arg0.isNodesetExpr();
63   }
64
65   /**
66    * Execute the first argument expression that is expected to return a
67    * string. If the argument is null, then get the string value from the
68    * current context node.
69    *
70    * @param xctxt Runtime XPath context.
71    *
72    * @return The string value of the first argument, or the string value of the
73    * current context node if the first argument is null.
74    *
75    * @throws javax.xml.transform.TransformerException if an error occurs while
76    * executing the argument expression.
77    */

78   protected XMLString getArg0AsString(XPathContext xctxt)
79           throws javax.xml.transform.TransformerException JavaDoc
80   {
81     if(null == m_arg0)
82     {
83       int currentNode = xctxt.getCurrentNode();
84       if(DTM.NULL == currentNode)
85         return XString.EMPTYSTRING;
86       else
87       {
88         DTM dtm = xctxt.getDTM(currentNode);
89         return dtm.getStringValue(currentNode);
90       }
91       
92     }
93     else
94       return m_arg0.execute(xctxt).xstr();
95   }
96
97   /**
98    * Execute the first argument expression that is expected to return a
99    * number. If the argument is null, then get the number value from the
100    * current context node.
101    *
102    * @param xctxt Runtime XPath context.
103    *
104    * @return The number value of the first argument, or the number value of the
105    * current context node if the first argument is null.
106    *
107    * @throws javax.xml.transform.TransformerException if an error occurs while
108    * executing the argument expression.
109    */

110   protected double getArg0AsNumber(XPathContext xctxt)
111           throws javax.xml.transform.TransformerException JavaDoc
112   {
113
114     if(null == m_arg0)
115     {
116       int currentNode = xctxt.getCurrentNode();
117       if(DTM.NULL == currentNode)
118         return 0;
119       else
120       {
121         DTM dtm = xctxt.getDTM(currentNode);
122         XMLString str = dtm.getStringValue(currentNode);
123         return str.toDouble();
124       }
125       
126     }
127     else
128       return m_arg0.execute(xctxt).num();
129   }
130
131   /**
132    * Check that the number of arguments passed to this function is correct.
133    *
134    * @param argNum The number of arguments that is being passed to the function.
135    *
136    * @throws WrongNumberArgsException if the number of arguments is not 0 or 1.
137    */

138   public void checkNumberArgs(int argNum) throws WrongNumberArgsException
139   {
140     if (argNum > 1)
141       reportWrongNumberArgs();
142   }
143
144   /**
145    * Constructs and throws a WrongNumberArgException with the appropriate
146    * message for this function object.
147    *
148    * @throws WrongNumberArgsException
149    */

150   protected void reportWrongNumberArgs() throws WrongNumberArgsException {
151       throw new WrongNumberArgsException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ZERO_OR_ONE, null)); //"0 or 1");
152
}
153
154   /**
155    * Tell if this expression or it's subexpressions can traverse outside
156    * the current subtree.
157    *
158    * @return true if traversal outside the context node's subtree can occur.
159    */

160   public boolean canTraverseOutsideSubtree()
161   {
162     return (null == m_arg0) ? false : super.canTraverseOutsideSubtree();
163   }
164 }
165
Popular Tags