001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: FuncSubstring.java,v 1.13 2004/08/17 19:25:37 jycli Exp $
018: */
019: package org.apache.xpath.functions;
020:
021: import org.apache.xalan.res.XSLMessages;
022: import org.apache.xml.utils.XMLString;
023: import org.apache.xpath.XPathContext;
024: import org.apache.xpath.objects.XObject;
025: import org.apache.xpath.objects.XString;
026: import org.apache.xpath.res.XPATHErrorResources;
027:
028: /**
029: * Execute the Substring() function.
030: * @xsl.usage advanced
031: */
032: public class FuncSubstring extends Function3Args {
033: static final long serialVersionUID = -5996676095024715502L;
034:
035: /**
036: * Execute the function. The function must return
037: * a valid object.
038: * @param xctxt The current execution context.
039: * @return A valid XObject.
040: *
041: * @throws javax.xml.transform.TransformerException
042: */
043: public XObject execute(XPathContext xctxt)
044: throws javax.xml.transform.TransformerException {
045:
046: XMLString s1 = m_arg0.execute(xctxt).xstr();
047: double start = m_arg1.execute(xctxt).num();
048: int lenOfS1 = s1.length();
049: XMLString substr;
050:
051: if (lenOfS1 <= 0)
052: return XString.EMPTYSTRING;
053: else {
054: int startIndex;
055:
056: if (Double.isNaN(start)) {
057:
058: // Double.MIN_VALUE doesn't work with math below
059: // so just use a big number and hope I never get caught.
060: start = -1000000;
061: startIndex = 0;
062: } else {
063: start = Math.round(start);
064: startIndex = (start > 0) ? (int) start - 1 : 0;
065: }
066:
067: if (null != m_arg2) {
068: double len = m_arg2.num(xctxt);
069: int end = (int) (Math.round(len) + start) - 1;
070:
071: // Normalize end index.
072: if (end < 0)
073: end = 0;
074: else if (end > lenOfS1)
075: end = lenOfS1;
076:
077: if (startIndex > lenOfS1)
078: startIndex = lenOfS1;
079:
080: substr = s1.substring(startIndex, end);
081: } else {
082: if (startIndex > lenOfS1)
083: startIndex = lenOfS1;
084: substr = s1.substring(startIndex);
085: }
086: }
087:
088: return (XString) substr; // cast semi-safe
089: }
090:
091: /**
092: * Check that the number of arguments passed to this function is correct.
093: *
094: *
095: * @param argNum The number of arguments that is being passed to the function.
096: *
097: * @throws WrongNumberArgsException
098: */
099: public void checkNumberArgs(int argNum)
100: throws WrongNumberArgsException {
101: if (argNum < 2)
102: reportWrongNumberArgs();
103: }
104:
105: /**
106: * Constructs and throws a WrongNumberArgException with the appropriate
107: * message for this function object.
108: *
109: * @throws WrongNumberArgsException
110: */
111: protected void reportWrongNumberArgs()
112: throws WrongNumberArgsException {
113: throw new WrongNumberArgsException(XSLMessages
114: .createXPATHMessage(
115: XPATHErrorResources.ER_TWO_OR_THREE, null)); //"2 or 3");
116: }
117: }
|