1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 /* 19 * $Id: FuncSubstring.java 468655 2006-10-28 07:12:06Z minchau $ 20 */ 21 package org.apache.xpath.functions; 22 23 import org.apache.xalan.res.XSLMessages; 24 import org.apache.xml.utils.XMLString; 25 import org.apache.xpath.XPathContext; 26 import org.apache.xpath.objects.XObject; 27 import org.apache.xpath.objects.XString; 28 import org.apache.xpath.res.XPATHErrorResources; 29 30 /** 31 * Execute the Substring() function. 32 * @xsl.usage advanced 33 */ 34 public class FuncSubstring extends Function3Args 35 { 36 static final long serialVersionUID = -5996676095024715502L; 37 38 /** 39 * Execute the function. The function must return 40 * a valid object. 41 * @param xctxt The current execution context. 42 * @return A valid XObject. 43 * 44 * @throws javax.xml.transform.TransformerException 45 */ execute(XPathContext xctxt)46 public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException 47 { 48 49 XMLString s1 = m_arg0.execute(xctxt).xstr(); 50 double start = m_arg1.execute(xctxt).num(); 51 int lenOfS1 = s1.length(); 52 XMLString substr; 53 54 if (lenOfS1 <= 0) 55 return XString.EMPTYSTRING; 56 else 57 { 58 int startIndex; 59 60 if (Double.isNaN(start)) 61 { 62 63 // Double.MIN_VALUE doesn't work with math below 64 // so just use a big number and hope I never get caught. 65 start = -1000000; 66 startIndex = 0; 67 } 68 else 69 { 70 start = Math.round(start); 71 startIndex = (start > 0) ? (int) start - 1 : 0; 72 } 73 74 if (null != m_arg2) 75 { 76 double len = m_arg2.num(xctxt); 77 int end = (int) (Math.round(len) + start) - 1; 78 79 // Normalize end index. 80 if (end < 0) 81 end = 0; 82 else if (end > lenOfS1) 83 end = lenOfS1; 84 85 if (startIndex > lenOfS1) 86 startIndex = lenOfS1; 87 88 substr = s1.substring(startIndex, end); 89 } 90 else 91 { 92 if (startIndex > lenOfS1) 93 startIndex = lenOfS1; 94 substr = s1.substring(startIndex); 95 } 96 } 97 98 return (XString)substr; // cast semi-safe 99 } 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 */ checkNumberArgs(int argNum)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 */ reportWrongNumberArgs()121 protected void reportWrongNumberArgs() throws WrongNumberArgsException { 122 throw new WrongNumberArgsException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_TWO_OR_THREE, null)); //"2 or 3"); 123 } 124 } 125