1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.xerces.internal.impl.dv.xs;
23
24import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
25import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
26
27/**
28 * All primitive types plus ID/IDREF/ENTITY/INTEGER are derived from this abstract
29 * class. It provides extra information XSSimpleTypeDecl requires from each
30 * type: allowed facets, converting String to actual value, check equality,
31 * comparison, etc.
32 *
33 * @xerces.internal
34 *
35 * @author Neeraj Bajaj, Sun Microsystems, inc.
36 * @author Sandy Gao, IBM
37 *
38 */
39public abstract class TypeValidator {
40
41    // which facets are allowed for this type
42    public abstract short getAllowedFacets();
43
44    // convert a string to an actual value. for example,
45    // for number types (decimal, double, float, and types derived from them),
46    // get the BigDecimal, Double, Flout object.
47    // for some types (string and derived), they just return the string itself
48    public abstract Object getActualValue(String content, ValidationContext context)
49        throws InvalidDatatypeValueException;
50
51    // for ID/IDREF/ENTITY types, do some extra checking after the value is
52    // checked to be valid with respect to both lexical representation and
53    // facets
54    public void checkExtraRules(Object value, ValidationContext context) throws InvalidDatatypeValueException {
55    }
56
57    // the following methods might not be supported by every DV.
58    // but XSSimpleTypeDecl should know which type supports which methods,
59    // and it's an *internal* error if a method is called on a DV that
60    // doesn't support it.
61
62    //order constants
63    public static final short LESS_THAN     = -1;
64    public static final short EQUAL         = 0;
65    public static final short GREATER_THAN  = 1;
66    public static final short INDETERMINATE = 2;
67
68    // where there is distinction between identity and equality, this method
69    // will be overwritten
70    // checks whether the two values are identical; for ex, this distinguishes
71    // -0.0 from 0.0
72    public boolean isIdentical (Object value1, Object value2) {
73        return value1.equals(value2);
74    }
75
76    // check the order relation between the two values
77    // the parameters are in compiled form (from getActualValue)
78    public int compare(Object value1, Object value2) {
79        return -1;
80    }
81
82    // get the length of the value
83    // the parameters are in compiled form (from getActualValue)
84    public int getDataLength(Object value) {
85        return (value instanceof String) ? ((String)value).length() : -1;
86    }
87
88    // get the number of digits of the value
89    // the parameters are in compiled form (from getActualValue)
90    public int getTotalDigits(Object value) {
91        return -1;
92    }
93
94    // get the number of fraction digits of the value
95    // the parameters are in compiled form (from getActualValue)
96    public int getFractionDigits(Object value) {
97        return -1;
98    }
99
100    // check whether the character is in the range 0x30 ~ 0x39
101    public static final boolean isDigit(char ch) {
102        return ch >= '0' && ch <= '9';
103    }
104
105    // if the character is in the range 0x30 ~ 0x39, return its int value (0~9),
106    // otherwise, return -1
107    public static final int getDigit(char ch) {
108        return isDigit(ch) ? ch - '0' : -1;
109    }
110
111} // interface TypeValidator
112