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/NumberElementType.java $
003: * $Id: NumberElementType.java 14225 2006-09-05 17:39:44Z chmaurer@iupui.edu $
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.Format;
023: import java.text.NumberFormat;
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.SchemaInvalidException;
030: import org.sakaiproject.metaobj.utils.xml.SchemaNode;
031: import org.sakaiproject.metaobj.utils.xml.ValueRange;
032:
033: /**
034: * Created by IntelliJ IDEA.
035: * User: John Ellis
036: * Date: Apr 16, 2004
037: * Time: 4:05:47 PM
038: * To change this template use File | Settings | File Templates.
039: */
040: public class NumberElementType extends FormatterElementType {
041:
042: protected Number maxIncl = null;
043: protected Number minIncl = null;
044: protected Number maxExcl = null;
045: protected Number minExcl = null;
046: protected int totalDigits = -1;
047:
048: private ValueRange range = null;
049:
050: private Format format = null;
051:
052: public NumberElementType(String typeName, Element schemaElement,
053: SchemaNode parentNode, Namespace xsdNamespace) {
054: super (typeName, schemaElement, parentNode, xsdNamespace);
055:
056: format = NumberFormat.getIntegerInstance();
057:
058: totalDigits = (new String(Long.MAX_VALUE + "")).length();
059:
060: Element simpleType = schemaElement.getChild("simpleType",
061: xsdNamespace);
062: if (simpleType != null) {
063: Element restrictions = simpleType.getChild("restriction",
064: xsdNamespace);
065: if (restrictions != null) {
066: // process restrictions
067: try {
068: maxIncl = (Number) getFormattedRestriction(
069: restrictions, "maxInclusive", xsdNamespace);
070: minIncl = (Number) getFormattedRestriction(
071: restrictions, "minInclusive", xsdNamespace);
072: maxExcl = (Number) getFormattedRestriction(
073: restrictions, "maxExclusive", xsdNamespace);
074: minExcl = (Number) getFormattedRestriction(
075: restrictions, "minExclusive", xsdNamespace);
076: } catch (ParseException e) {
077: throw new SchemaInvalidException(e);
078: }
079:
080: totalDigits = processIntRestriction(restrictions,
081: "totalDigits", xsdNamespace, totalDigits);
082: }
083: }
084:
085: if (maxIncl != null || minIncl != null || maxExcl != null
086: || minExcl != null) {
087: // one must not be null, create a range
088: Comparable min = (Comparable) minIncl;
089: if (min == null) {
090: min = (Comparable) minExcl;
091: }
092:
093: Comparable max = (Comparable) maxIncl;
094: if (max == null) {
095: max = (Comparable) maxExcl;
096: }
097:
098: range = new NumberValueRange(max, min, maxIncl != null,
099: minIncl != null);
100: }
101: }
102:
103: protected Format getFormatter() {
104: return format;
105: }
106:
107: protected Object checkConstraints(Object o) {
108:
109: int i = ((Number) o).intValue();
110:
111: if (maxIncl != null && i > maxIncl.intValue()) {
112: throw new NormalizationException(
113: "Invalid number",
114: NormalizationException.TOO_LARGE_INCLUSIVE_ERROR_CODE,
115: new Object[] { o, maxIncl });
116: }
117:
118: if (minIncl != null && i < minIncl.intValue()) {
119: throw new NormalizationException(
120: "Invalid number",
121: NormalizationException.TOO_SMALL_INCLUSIVE_ERROR_CODE,
122: new Object[] { o, minIncl });
123: }
124:
125: if (maxExcl != null && i >= maxExcl.intValue()) {
126: throw new NormalizationException("Invalid number",
127: NormalizationException.TOO_LARGE_ERROR_CODE,
128: new Object[] { o, maxExcl });
129: }
130:
131: if (minExcl != null && i <= minExcl.intValue()) {
132: throw new NormalizationException("Invalid number",
133: NormalizationException.TOO_SMALL_ERROR_CODE,
134: new Object[] { o, minExcl });
135: }
136:
137: if (totalDigits != -1 && o.toString().length() > totalDigits) {
138: throw new NormalizationException("Invalid number",
139: NormalizationException.TOO_MANY_DIGITS_ERROR_CODE,
140: new Object[] { o, new Integer(totalDigits) });
141: }
142:
143: return o;
144: }
145:
146: protected String parserException(String value, ParseException e) {
147: throw new NormalizationException("Invalid number",
148: NormalizationException.INVALID_NUMBER_ERROR_CODE,
149: new Object[] { value });
150: }
151:
152: public Class getObjectType() {
153: return Number.class;
154: }
155:
156: public int getMaxLength() {
157: return totalDigits;
158: }
159:
160: public ValueRange getRange() {
161: return range;
162: }
163:
164: }
|