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.BigDecimal;
019: import java.util.Calendar;
020:
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:decimal.
028: *
029: * <p>
030: * <pre>
031: * <code>
032: * <xs:simpleType name="decimal" id="decimal">
033: * <xs:annotation>
034: * <xs:appinfo>
035: * <hfp:hasFacet name="totalDigits"/>
036: * <hfp:hasFacet name="fractionDigits"/>
037: * <hfp:hasFacet name="pattern"/>
038: * <hfp:hasFacet name="whiteSpace"/>
039: * <hfp:hasFacet name="enumeration"/>
040: * <hfp:hasFacet name="maxInclusive"/>
041: * <hfp:hasFacet name="maxExclusive"/>
042: * <hfp:hasFacet name="minInclusive"/>
043: * <hfp:hasFacet name="minExclusive"/>
044: * <hfp:hasProperty name="ordered" value="total"/>
045: * <hfp:hasProperty name="bounded" value="false"/>
046: * <hfp:hasProperty name="cardinality" value="countably infinite"/>
047: * <hfp:hasProperty name="numeric" value="true"/>
048: * </xs:appinfo>
049: * <xs:documentation source="http://www.w3.org/TR/xmlschema-2/#decimal"/>
050: * </xs:annotation>
051: * <xs:restriction base="xs:anySimpleType">
052: * <xs:whiteSpace value="collapse" fixed="true" id="decimal.whiteSpace"/>
053: * </xs:restriction>
054: * </xs:simpleType>
055: *
056: * </code>
057: * </pre>
058: * </p>
059: *
060: * @generated
061: */
062: public class XSDecimalBinding implements SimpleBinding {
063: /**
064: * @generated
065: */
066: public QName getTarget() {
067: return XS.DECIMAL;
068: }
069:
070: /**
071: * <!-- begin-user-doc -->
072: * <!-- end-user-doc -->
073: *
074: * @generated modifiable
075: */
076: public int getExecutionMode() {
077: return AFTER;
078: }
079:
080: /**
081: * <!-- begin-user-doc -->
082: * This binding returns objects of type {@link BigDecimal}.
083: * <!-- end-user-doc -->
084: *
085: * @generated modifiable
086: */
087: public Class getType() {
088: return BigDecimal.class;
089: }
090:
091: /**
092: * /**
093: * <!-- begin-user-doc -->
094: * This is AFTER so value contains element.text after processing by AnySimpleType.
095: * This binding returns objects of type {@link Calendar}.
096: * <!-- end-user-doc -->
097: *
098: * @generated modifiable
099: */
100: public Object parse(InstanceComponent instance, Object value)
101: throws Exception {
102: //DatatypeConverter.setDatatypeConverter(DatatypeConverterImpl.theInstance);
103: //BigDecimal decimal = DatatypeConverter.parseDecimal((String) value);
104: String text = (String) value;
105:
106: if (text.startsWith("+")) {
107: text = text.substring(1);
108: }
109:
110: BigDecimal decimal = new BigDecimal(text);
111:
112: // // TODO: facet checks to be done by framework
113: // XSDSimpleTypeDefinition simple = (XSDSimpleTypeDefinition) instance.getTypeDefinition();
114: //
115: // BigDecimal maxInc = (BigDecimal) simple.getMaxInclusiveFacet().getValue();
116: // if (decimal.compareTo(maxInc) > 0) {
117: // throw new ValidationException("Decimal value is outside the inclusive max bounds of " + maxInc);
118: // }
119: //
120: // BigDecimal maxExc = (BigDecimal) simple.getMaxExclusiveFacet().getValue();
121: // if (decimal.compareTo(maxExc) >= 0) {
122: // throw new ValidationException("Decimal value is outside the exclusive max bounds of " + maxExc);
123: // }
124: //
125: // BigDecimal minInc = (BigDecimal) simple.getMinInclusiveFacet().getValue();
126: // if (decimal.compareTo(minInc) < 0) {
127: // throw new ValidationException("Decimal value is outside the inclusive min bounds of " + minInc);
128: // }
129: //
130: // BigDecimal minExc = (BigDecimal) simple.getMinExclusiveFacet().getValue();
131: // if (decimal.compareTo(minExc) <= 0) {
132: // throw new ValidationException("Decimal value is outside the exclusive min bounds of " + minExc);
133: // }
134:
135: // int precision = decimal.precision();
136: // int totalDigits = simple.getTotalDigitsFacet().getValue();
137: // if (precision > totalDigits) {
138: // throw new ValidationException("Decimal value's precision ("+precision+
139: // ") is higher than allowed ("+totalDigits+")");
140: // }
141: return decimal;
142: }
143:
144: /**
145: * <!-- begin-user-doc -->
146: * <!-- end-user-doc -->
147: *
148: * @generated modifiable
149: */
150: public String encode(Object object, String value) {
151: BigDecimal decimal = (BigDecimal) object;
152:
153: return decimal.toString();
154: }
155: }
|