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