001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.xs.bindings;
017:
018: import java.math.BigInteger;
019:
020: import javax.xml.bind.ValidationException;
021: import javax.xml.namespace.QName;
022:
023: import org.geotools.xml.InstanceComponent;
024: import org.geotools.xml.SimpleBinding;
025:
026: /**
027: * Binding object for the type http://www.w3.org/2001/XMLSchema:nonNegativeInteger.
028: *
029: * <p>
030: * <pre>
031: * <code>
032: * <xs:simpleType name="nonNegativeInteger" id="nonNegativeInteger">
033: * <xs:annotation>
034: * <xs:documentation source="http://www.w3.org/TR/xmlschema-2/#nonNegativeInteger"/>
035: * </xs:annotation>
036: * <xs:restriction base="xs:integer">
037: * <xs:minInclusive value="0" id="nonNegativeInteger.minInclusive"/>
038: * </xs:restriction>
039: * </xs:simpleType>
040: *
041: * </code>
042: * </pre>
043: * </p>
044: *
045: * @generated
046: */
047: public class XSNonNegativeIntegerBinding implements SimpleBinding {
048: final BigInteger MAX_LONG = BigInteger.valueOf(Long.MAX_VALUE);
049: final BigInteger MAX_INTEGER = BigInteger
050: .valueOf(Integer.MAX_VALUE);
051:
052: /**
053: * @generated
054: */
055: public QName getTarget() {
056: return XS.NONNEGATIVEINTEGER;
057: }
058:
059: /**
060: * <!-- begin-user-doc -->
061: * <!-- end-user-doc -->
062: *
063: * @generated modifiable
064: */
065: public int getExecutionMode() {
066: return AFTER;
067: }
068:
069: /**
070: * <!-- begin-user-doc -->
071: * This binding returns objects of type {@link Number.class}.
072: * <!-- end-user-doc -->
073: *
074: * @generated modifiable
075: */
076: public Class getType() {
077: return BigInteger.class;
078: }
079:
080: /**
081: * <!-- begin-user-doc -->
082: * Restriction of integer to non negative values.
083: * <p>
084: * Please just treat this as a Number, actual value returned
085: * may be BigInteger or Long or Integer.
086: * </p>
087: * @param instance with text to be parsed
088: * @param value BigInteger from parent XSIntegerStratagy
089: * @return Number positive in range 0 to Integer.MAX_INT
090: * <!-- end-user-doc -->
091: *
092: * @generated modifiable
093: */
094: public Object parse(InstanceComponent instance, Object value)
095: throws Exception {
096: BigInteger number = (BigInteger) value;
097:
098: if (BigInteger.ZERO.compareTo(number) > 0) {
099: throw new ValidationException("Value '" + number
100: + "' must be non-negative (0 or above).");
101: }
102:
103: if (MAX_INTEGER.compareTo(number) >= 0) {
104: return new Integer(number.intValue());
105: }
106:
107: if (MAX_LONG.compareTo(number) >= 0) {
108: return new Long(number.longValue());
109: }
110:
111: return number;
112: }
113:
114: /**
115: * <!-- begin-user-doc -->
116: * <!-- end-user-doc -->
117: *
118: * @generated modifiable
119: */
120: public String encode(Object object, String value) throws Exception {
121: Number number = (Number) object;
122:
123: if (number.intValue() < 0) {
124: throw new ValidationException("Value '" + number
125: + "' must be non-negative (0 or above).");
126: }
127:
128: return value;
129: }
130: }
|