1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001, 2002 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 2001, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.impl.dv.xs;
  58. import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
  59. import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
  60. /**
  61. * Represent the schema type "decimal"
  62. *
  63. * @author Neeraj Bajaj, Sun Microsystems, inc.
  64. * @author Sandy Gao, IBM
  65. *
  66. * @version $Id: DecimalDV.java,v 1.9 2003/05/08 20:11:55 elena Exp $
  67. */
  68. public class DecimalDV extends TypeValidator {
  69. public final short getAllowedFacets(){
  70. return ( XSSimpleTypeDecl.FACET_PATTERN | XSSimpleTypeDecl.FACET_WHITESPACE | XSSimpleTypeDecl.FACET_ENUMERATION |XSSimpleTypeDecl.FACET_MAXINCLUSIVE |XSSimpleTypeDecl.FACET_MININCLUSIVE | XSSimpleTypeDecl.FACET_MAXEXCLUSIVE | XSSimpleTypeDecl.FACET_MINEXCLUSIVE | XSSimpleTypeDecl.FACET_TOTALDIGITS | XSSimpleTypeDecl.FACET_FRACTIONDIGITS);
  71. }
  72. public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException {
  73. try {
  74. return new XDecimal(content);
  75. } catch (NumberFormatException nfe) {
  76. throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "decimal"});
  77. }
  78. }
  79. public final int compare(Object value1, Object value2){
  80. return ((XDecimal)value1).compareTo((XDecimal)value2);
  81. }
  82. public final int getTotalDigits(Object value){
  83. return ((XDecimal)value).totalDigits;
  84. }
  85. public final int getFractionDigits(Object value){
  86. return ((XDecimal)value).fracDigits;
  87. }
  88. // Avoid using the heavy-weight java.math.BigDecimal
  89. static class XDecimal {
  90. // sign: 0 for vlaue 0; 1 for positive values; -1 for negative values
  91. int sign = 1;
  92. // total digits. >= 1
  93. int totalDigits = 0;
  94. // integer digits when sign != 0
  95. int intDigits = 0;
  96. // fraction digits when sign != 0
  97. int fracDigits = 0;
  98. // the string representing the integer part
  99. String ivalue = "";
  100. // the string representing the fraction part
  101. String fvalue = "";
  102. // whether the canonical form contains decimal point
  103. boolean integer = false;
  104. XDecimal(String content) throws NumberFormatException {
  105. initD(content);
  106. }
  107. XDecimal(String content, boolean integer) throws NumberFormatException {
  108. if (integer)
  109. initI(content);
  110. else
  111. initD(content);
  112. }
  113. void initD(String content) throws NumberFormatException {
  114. int len = content.length();
  115. if (len == 0)
  116. throw new NumberFormatException();
  117. // these 4 variables are used to indicate where the integre/fraction
  118. // parts start/end.
  119. int intStart = 0, intEnd = 0, fracStart = 0, fracEnd = 0;
  120. // Deal with leading sign symbol if present
  121. if (content.charAt(0) == '+') {
  122. // skip '+', so intStart should be 1
  123. intStart = 1;
  124. }
  125. else if (content.charAt(0) == '-') {
  126. // keep '-', so intStart is stil 0
  127. intStart = 1;
  128. sign = -1;
  129. }
  130. // skip leading zeroes in integer part
  131. int actualIntStart = intStart;
  132. while (actualIntStart < len && content.charAt(actualIntStart) == '0') {
  133. actualIntStart++;
  134. }
  135. // Find the ending position of the integer part
  136. for (intEnd = actualIntStart;
  137. intEnd < len && TypeValidator.isDigit(content.charAt(intEnd));
  138. intEnd++);
  139. // Not reached the end yet
  140. if (intEnd < len) {
  141. // the remaining part is not ".DDD", error
  142. if (content.charAt(intEnd) != '.')
  143. throw new NumberFormatException();
  144. // fraction part starts after '.', and ends at the end of the input
  145. fracStart = intEnd + 1;
  146. fracEnd = len;
  147. }
  148. // no integer part, no fraction part, error.
  149. if (intStart == intEnd && fracStart == fracEnd)
  150. throw new NumberFormatException();
  151. // ignore trailing zeroes in fraction part
  152. while (fracEnd > fracStart && content.charAt(fracEnd-1) == '0') {
  153. fracEnd--;
  154. }
  155. // check whether there is non-digit characters in the fraction part
  156. for (int fracPos = fracStart; fracPos < fracEnd; fracPos++) {
  157. if (!TypeValidator.isDigit(content.charAt(fracPos)))
  158. throw new NumberFormatException();
  159. }
  160. intDigits = intEnd - actualIntStart;
  161. fracDigits = fracEnd - fracStart;
  162. totalDigits = intDigits + fracDigits;
  163. if (intDigits > 0) {
  164. ivalue = content.substring(actualIntStart, intEnd);
  165. if (fracDigits > 0)
  166. fvalue = content.substring(fracStart, fracEnd);
  167. }
  168. else {
  169. if (fracDigits > 0) {
  170. fvalue = content.substring(fracStart, fracEnd);
  171. }
  172. else {
  173. // ".00", treat it as "0"
  174. sign = 0;
  175. }
  176. }
  177. }
  178. void initI(String content) throws NumberFormatException {
  179. int len = content.length();
  180. if (len == 0)
  181. throw new NumberFormatException();
  182. // these 2 variables are used to indicate where the integre start/end.
  183. int intStart = 0, intEnd = 0;
  184. // Deal with leading sign symbol if present
  185. if (content.charAt(0) == '+') {
  186. // skip '+', so intStart should be 1
  187. intStart = 1;
  188. }
  189. else if (content.charAt(0) == '-') {
  190. // keep '-', so intStart is stil 0
  191. intStart = 1;
  192. sign = -1;
  193. }
  194. // skip leading zeroes in integer part
  195. int actualIntStart = intStart;
  196. while (actualIntStart < len && content.charAt(actualIntStart) == '0') {
  197. actualIntStart++;
  198. }
  199. // Find the ending position of the integer part
  200. for (intEnd = actualIntStart;
  201. intEnd < len && TypeValidator.isDigit(content.charAt(intEnd));
  202. intEnd++);
  203. // Not reached the end yet, error
  204. if (intEnd < len)
  205. throw new NumberFormatException();
  206. // no integer part, error.
  207. if (intStart == intEnd)
  208. throw new NumberFormatException();
  209. intDigits = intEnd - actualIntStart;
  210. fracDigits = 0;
  211. totalDigits = intDigits;
  212. if (intDigits > 0) {
  213. ivalue = content.substring(actualIntStart, intEnd);
  214. }
  215. else {
  216. // "00", treat it as "0"
  217. sign = 0;
  218. }
  219. integer = true;
  220. }
  221. public boolean equals(Object val) {
  222. if (val == this)
  223. return true;
  224. if (!(val instanceof XDecimal))
  225. return false;
  226. XDecimal oval = (XDecimal)val;
  227. if (sign != oval.sign)
  228. return false;
  229. if (sign == 0)
  230. return true;
  231. return intDigits == oval.intDigits && fracDigits == oval.fracDigits &&
  232. ivalue.equals(oval.ivalue) && fvalue.equals(oval.fvalue);
  233. }
  234. public int compareTo(XDecimal val) {
  235. if (sign != val.sign)
  236. return sign > val.sign ? 1 : -1;
  237. if (sign == 0)
  238. return 0;
  239. return sign * intComp(val);
  240. }
  241. private int intComp(XDecimal val) {
  242. if (intDigits != val.intDigits)
  243. return intDigits > val.intDigits ? 1 : -1;
  244. int ret = ivalue.compareTo(val.ivalue);
  245. if (ret != 0)
  246. return ret > 0 ? 1 : -1;;
  247. ret = fvalue.compareTo(val.fvalue);
  248. return ret == 0 ? 0 : (ret > 0 ? 1 : -1);
  249. }
  250. private String canonical;
  251. public synchronized String toString() {
  252. if (canonical == null) {
  253. makeCanonical();
  254. }
  255. return canonical;
  256. }
  257. private void makeCanonical() {
  258. if (sign == 0) {
  259. if (integer)
  260. canonical = "0";
  261. else
  262. canonical = "0.0";
  263. return;
  264. }
  265. if (integer && sign > 0) {
  266. canonical = ivalue;
  267. return;
  268. }
  269. // for -0.1, total digits is 1, so we need 3 extra spots
  270. StringBuffer buffer = new StringBuffer(totalDigits+3);
  271. if (sign == -1)
  272. buffer.append('-');
  273. if (intDigits != 0)
  274. buffer.append(ivalue);
  275. else
  276. buffer.append('0');
  277. if (!integer) {
  278. buffer.append('.');
  279. if (fracDigits != 0) {
  280. buffer.append(fvalue);
  281. }
  282. else {
  283. buffer.append('0');
  284. }
  285. }
  286. canonical = buffer.toString();
  287. }
  288. }
  289. } // class DecimalDV