1 16 19 package org.apache.xpath.functions; 20 21 import org.apache.xalan.res.XSLMessages; 22 import org.apache.xml.utils.XMLString; 23 import org.apache.xpath.XPathContext; 24 import org.apache.xpath.objects.XObject; 25 import org.apache.xpath.objects.XString; 26 import org.apache.xpath.res.XPATHErrorResources; 27 28 32 public class FuncSubstring extends Function3Args 33 { 34 35 43 public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException 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 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 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; } 97 98 106 public void checkNumberArgs(int argNum) throws WrongNumberArgsException 107 { 108 if (argNum < 2) 109 reportWrongNumberArgs(); 110 } 111 112 118 protected void reportWrongNumberArgs() throws WrongNumberArgsException { 119 throw new WrongNumberArgsException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_TWO_OR_THREE, null)); } 121 } 122 | Popular Tags |