KickJava   Java API By Example, From Geeks To Geeks.

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


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: FuncSubstring.java,v 1.12 2004/02/17 04:34:01 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.utils.XMLString;
23 import com.sun.org.apache.xpath.internal.XPathContext;
24 import com.sun.org.apache.xpath.internal.objects.XObject;
25 import com.sun.org.apache.xpath.internal.objects.XString;
26 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
27
28 /**
29  * Execute the Substring() function.
30  * @xsl.usage advanced
31  */

32 public class FuncSubstring extends Function3Args
33 {
34
35   /**
36    * Execute the function. The function must return
37    * a valid object.
38    * @param xctxt The current execution context.
39    * @return A valid XObject.
40    *
41    * @throws javax.xml.transform.TransformerException
42    */

43   public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException JavaDoc
44   {
45
46     XMLString s1 = m_arg0.execute(xctxt).xstr();
47     double start = m_arg1.execute(xctxt).num();
48     int lenOfS1 = s1.length();
49     XMLString substr;
50
51     if (lenOfS1 <= 0)
52       return XString.EMPTYSTRING;
53     else
54     {
55       int startIndex;
56
57       if (Double.isNaN(start))
58       {
59
60         // Double.MIN_VALUE doesn't work with math below
61
// so just use a big number and hope I never get caught.
62
start = -1000000;
63         startIndex = 0;
64       }
65       else
66       {
67         start = Math.round(start);
68         startIndex = (start > 0) ? (int) start - 1 : 0;
69       }
70
71       if (null != m_arg2)
72       {
73         double len = m_arg2.num(xctxt);
74         int end = (int) (Math.round(len) + start) - 1;
75
76         // Normalize end index.
77
if (end < 0)
78           end = 0;
79         else if (end > lenOfS1)
80           end = lenOfS1;
81
82         if (startIndex > lenOfS1)
83           startIndex = lenOfS1;
84
85         substr = s1.substring(startIndex, end);
86       }
87       else
88       {
89         if (startIndex > lenOfS1)
90           startIndex = lenOfS1;
91         substr = s1.substring(startIndex);
92       }
93     }
94
95     return (XString)substr; // cast semi-safe
96
}
97
98   /**
99    * Check that the number of arguments passed to this function is correct.
100    *
101    *
102    * @param argNum The number of arguments that is being passed to the function.
103    *
104    * @throws WrongNumberArgsException
105    */

106   public void checkNumberArgs(int argNum) throws WrongNumberArgsException
107   {
108     if (argNum < 2)
109       reportWrongNumberArgs();
110   }
111
112   /**
113    * Constructs and throws a WrongNumberArgException with the appropriate
114    * message for this function object.
115    *
116    * @throws WrongNumberArgsException
117    */

118   protected void reportWrongNumberArgs() throws WrongNumberArgsException {
119       throw new WrongNumberArgsException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_TWO_OR_THREE, null)); //"2 or 3");
120
}
121 }
122
Popular Tags