001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 1999 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: Facet.java 6605 2006-12-28 06:52:29Z ekuns $
044: */package org.exolab.castor.xml.schema;
045:
046: import org.exolab.castor.xml.ValidationException;
047:
048: /**
049: * Represents the base type for XML Schema Facets
050: * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
051: * @version $Revision: 6605 $ $Date: 2005-12-13 14:58:48 -0700 (Tue, 13 Dec 2005) $
052: **/
053: public class Facet extends Annotated {
054: /** SerialVersionUID */
055: private static final long serialVersionUID = 7821829275720939922L;
056:
057: public static final String ENUMERATION = "enumeration";
058: public static final String LENGTH = "length";
059: public static final String PATTERN = "pattern";
060: public static final String PRECISION = "precision";
061: public static final String MAX_EXCLUSIVE = "maxExclusive";
062: public static final String MAX_INCLUSIVE = "maxInclusive";
063: public static final String MIN_EXCLUSIVE = "minExclusive";
064: public static final String MIN_INCLUSIVE = "minInclusive";
065: public static final String MAX_LENGTH = "maxLength";
066: public static final String MIN_LENGTH = "minLength";
067: public static final String WHITESPACE = "whiteSpace";
068: public static final String TOTALDIGITS = "totalDigits";
069: public static final String FRACTIONDIGITS = "fractionDigits";
070:
071: public static final String WHITESPACE_PRESERVE = "preserve";
072: public static final String WHITESPACE_REPLACE = "replace";
073: public static final String WHITESPACE_COLLAPSE = "collapse";
074:
075: private static final String CLASSNAME = Facet.class.getName();
076:
077: private static final String NULL_ARGUMENT = "A null argument was passed to "
078: + CLASSNAME + "#";
079:
080: private static final String ZERO_LENGTH_STRING = "A zero-length String was passed to "
081: + CLASSNAME + "#";
082:
083: /** The name of this Facet. */
084: private final String _name;
085: /** The character value of this Facet. */
086: private final String _value;
087:
088: /**
089: * Creates a new Facet with the given name
090: * @param name the name of the Facet
091: * @param value the value of the Facet
092: **/
093: public Facet(String name, String value) {
094: if (name == null) {
095: String err = NULL_ARGUMENT;
096: err += "Facet: 'name' and 'value' must not be null.";
097: throw new IllegalArgumentException(err);
098: }
099: if (name.length() == 0) {
100: String err = ZERO_LENGTH_STRING;
101: err += "Facet: 'name' and 'value' must not be zero-length.";
102: throw new IllegalArgumentException(err);
103: }
104: this ._name = name;
105: this ._value = value;
106: } //-- Facet
107:
108: /**
109: * Returns the name of this Facet
110: * @return the name of this Facet
111: **/
112: public String getName() {
113: return _name;
114: } //-- getName
115:
116: /**
117: * Returns the character (String) representation of this facet
118: * @return the value of this facet
119: **/
120: public String getValue() {
121: return this ._value;
122: } //-- getValue
123:
124: /**
125: * Returns true if this Facet can occur more than once, such
126: * as the "enumeration" facet.
127: * @return true if this Facet can occur more than once.
128: **/
129: public boolean isMultivalued() {
130: return _name.equals(Facet.ENUMERATION)
131: || _name.equals(Facet.PATTERN);
132: }
133:
134: /**
135: * Returns an int representation of the value of this facet
136: * @return an int representation of the value of this facet
137: * @throws NumberFormatException if the value fails to parse as a int.
138: **/
139: public int toInt() throws NumberFormatException {
140: return Integer.parseInt(_value);
141: } //-- toInt
142:
143: /**
144: * Returns a long representation of the value of this facet
145: * @return a long representation of the value of this facet
146: * @throws NumberFormatException if the value fails to parse as a long.
147: **/
148: public long toLong() throws NumberFormatException {
149: return Long.parseLong(_value);
150: } //-- toInt
151:
152: /**
153: * Returns an short representation of the value of this facet
154: * @return an short representation of the value of this facet
155: * @throws NumberFormatException if the value fails to parse as a short.
156: **/
157: public short toShort() throws NumberFormatException {
158: return Short.parseShort(_value);
159: } //-- toShort
160:
161: /**
162: * Returns a double representation of the value of this facet
163: * @return a double representation of the value of this facet
164: * @throws NumberFormatException if the value fails to parse as a float.
165: */
166: public float toFloat() throws NumberFormatException {
167: if (_value.equals("INF"))
168: return Float.POSITIVE_INFINITY;
169: if (_value.equals("-INF"))
170: return Float.NEGATIVE_INFINITY;
171: return Float.valueOf(_value).floatValue();
172: } //-- toInt
173:
174: /**
175: * Returns a double representation of the value of this facet
176: * @return a double representation of the value of this facet
177: * @throws NumberFormatException if the value fails to parse as a double.
178: **/
179: public double toDouble() throws NumberFormatException {
180: return Double.valueOf(_value).doubleValue();
181: } //-- toInt
182:
183: /**
184: * Returns a byte representation of the value of this facet
185: * @return a byte representation of the value of this facet
186: * @throws NumberFormatException if the value fails to parse as a byte.
187: **/
188: public byte toByte() throws NumberFormatException {
189: return Byte.parseByte(_value);
190: } //-- toInt
191:
192: /**
193: * Returns the type of this Schema Structure
194: * @return the type of this Schema Structure
195: **/
196: public short getStructureType() {
197: return Structure.FACET;
198: } //-- getStructureType
199:
200: /**
201: * Checks the validity of this Schema defintion.
202: * @exception ValidationException when this Schema definition
203: * is invalid.
204: **/
205: public void validate() throws ValidationException {
206: //-- do nothing for now
207: } //-- validate
208:
209: } //-- Facet
|