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. import com.sun.org.apache.xalan.internal.res.XSLMessages;
  21. import com.sun.org.apache.xml.internal.utils.XMLString;
  22. import com.sun.org.apache.xpath.internal.XPathContext;
  23. import com.sun.org.apache.xpath.internal.objects.XObject;
  24. import com.sun.org.apache.xpath.internal.objects.XString;
  25. import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
  26. /**
  27. * Execute the Substring() function.
  28. * @xsl.usage advanced
  29. */
  30. public class FuncSubstring extends Function3Args
  31. {
  32. /**
  33. * Execute the function. The function must return
  34. * a valid object.
  35. * @param xctxt The current execution context.
  36. * @return A valid XObject.
  37. *
  38. * @throws javax.xml.transform.TransformerException
  39. */
  40. public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
  41. {
  42. XMLString s1 = m_arg0.execute(xctxt).xstr();
  43. double start = m_arg1.execute(xctxt).num();
  44. int lenOfS1 = s1.length();
  45. XMLString substr;
  46. if (lenOfS1 <= 0)
  47. return XString.EMPTYSTRING;
  48. else
  49. {
  50. int startIndex;
  51. if (Double.isNaN(start))
  52. {
  53. // Double.MIN_VALUE doesn't work with math below
  54. // so just use a big number and hope I never get caught.
  55. start = -1000000;
  56. startIndex = 0;
  57. }
  58. else
  59. {
  60. start = Math.round(start);
  61. startIndex = (start > 0) ? (int) start - 1 : 0;
  62. }
  63. if (null != m_arg2)
  64. {
  65. double len = m_arg2.num(xctxt);
  66. int end = (int) (Math.round(len) + start) - 1;
  67. // Normalize end index.
  68. if (end < 0)
  69. end = 0;
  70. else if (end > lenOfS1)
  71. end = lenOfS1;
  72. if (startIndex > lenOfS1)
  73. startIndex = lenOfS1;
  74. substr = s1.substring(startIndex, end);
  75. }
  76. else
  77. {
  78. if (startIndex > lenOfS1)
  79. startIndex = lenOfS1;
  80. substr = s1.substring(startIndex);
  81. }
  82. }
  83. return (XString)substr; // cast semi-safe
  84. }
  85. /**
  86. * Check that the number of arguments passed to this function is correct.
  87. *
  88. *
  89. * @param argNum The number of arguments that is being passed to the function.
  90. *
  91. * @throws WrongNumberArgsException
  92. */
  93. public void checkNumberArgs(int argNum) throws WrongNumberArgsException
  94. {
  95. if (argNum < 2)
  96. reportWrongNumberArgs();
  97. }
  98. /**
  99. * Constructs and throws a WrongNumberArgException with the appropriate
  100. * message for this function object.
  101. *
  102. * @throws WrongNumberArgsException
  103. */
  104. protected void reportWrongNumberArgs() throws WrongNumberArgsException {
  105. throw new WrongNumberArgsException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_TWO_OR_THREE, null)); //"2 or 3");
  106. }
  107. }