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