001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-impl/api-impl/src/java/org/sakaiproject/metaobj/utils/xml/impl/DecimalElementType.java $
003: * $Id: DecimalElementType.java 18332 2006-11-21 05:54:57Z john.ellis@rsmart.com $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.metaobj.utils.xml.impl;
021:
022: import java.text.DecimalFormat;
023: import java.text.Format;
024: import java.text.ParseException;
025:
026: import org.jdom.Element;
027: import org.jdom.Namespace;
028: import org.sakaiproject.metaobj.utils.xml.NormalizationException;
029: import org.sakaiproject.metaobj.utils.xml.SchemaNode;
030:
031: /**
032: * Created by IntelliJ IDEA.
033: * User: John Ellis
034: * Date: Apr 19, 2004
035: * Time: 10:09:49 AM
036: * To change this template use File | Settings | File Templates.
037: */
038: public class DecimalElementType extends NumberElementType {
039:
040: private Format format = null;
041: private int fractionDigits = -1;
042:
043: public DecimalElementType(String typeName, Element schemaElement,
044: SchemaNode parentNode, Namespace xsdNamespace) {
045: super (typeName, schemaElement, parentNode, xsdNamespace);
046:
047: Element simpleType = schemaElement.getChild("simpleType",
048: xsdNamespace);
049: if (simpleType != null) {
050: Element restrictions = simpleType.getChild("restriction",
051: xsdNamespace);
052: if (restrictions != null) {
053: // process restrictions
054: fractionDigits = processIntRestriction(restrictions,
055: "fractionDigits", xsdNamespace, fractionDigits);
056:
057: if (fractionDigits != -1) {
058: ((DecimalFormat) getFormatter())
059: .setMaximumFractionDigits(fractionDigits);
060: ((DecimalFormat) getFormatter())
061: .setMinimumFractionDigits(fractionDigits);
062: }
063: }
064: }
065:
066: }
067:
068: protected Format getFormatter() {
069: if (format == null) {
070: format = new DecimalFormat();
071: }
072: return format;
073: }
074:
075: protected Object checkConstraints(Object o) {
076:
077: double i = ((Number) o).doubleValue();
078:
079: if (maxIncl != null && i > maxIncl.doubleValue()) {
080: throw new NormalizationException(
081: "Invalid number",
082: NormalizationException.TOO_LARGE_INCLUSIVE_ERROR_CODE,
083: new Object[] { o, maxIncl });
084: }
085:
086: if (minIncl != null && i < minIncl.doubleValue()) {
087: throw new NormalizationException(
088: "Invalid number",
089: NormalizationException.TOO_SMALL_INCLUSIVE_ERROR_CODE,
090: new Object[] { o, minIncl });
091: }
092:
093: if (maxExcl != null && i >= maxExcl.doubleValue()) {
094: throw new NormalizationException("Invalid number",
095: NormalizationException.TOO_LARGE_ERROR_CODE,
096: new Object[] { o, maxExcl });
097: }
098:
099: if (minExcl != null && i <= minExcl.doubleValue()) {
100: throw new NormalizationException("Invalid number",
101: NormalizationException.TOO_SMALL_ERROR_CODE,
102: new Object[] { o, minExcl });
103: }
104:
105: if (totalDigits != -1 && o.toString().length() > totalDigits) {
106: throw new NormalizationException("Invalid number",
107: NormalizationException.TOO_MANY_DIGITS_ERROR_CODE,
108: new Object[] { o, new Integer(totalDigits) });
109: }
110:
111: return o;
112: }
113:
114: protected String parserException(String value, ParseException e) {
115: throw new NormalizationException(
116: "Invalid number",
117: NormalizationException.INVALID_DECIMAL_NUMBER_ERROR_CODE,
118: new Object[] { value });
119: }
120:
121: }
|