1 /*
2  * Copyright (c) 2017, 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.SchemaDVFactory;
24 import com.sun.org.apache.xerces.internal.impl.dv.XSFacets;
25 import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType;
26 import com.sun.org.apache.xerces.internal.util.SymbolHash;
27 import com.sun.org.apache.xerces.internal.xs.XSConstants;
28 import com.sun.org.apache.xerces.internal.xs.XSObjectList;
29 
30 /**
31  * the factory to create/return built-in schema DVs and create user-defined DVs
32  *
33  * @xerces.internal
34  *
35  * @author Neeraj Bajaj, Sun Microsystems, inc.
36  * @author Sandy Gao, IBM
37  *
38  * @LastModified: Nov 2017
39  */
40 public class BaseDVFactory extends SchemaDVFactory {
41 
42     static final String URI_SCHEMAFORSCHEMA = "http://www.w3.org/2001/XMLSchema";
43 
44     // there are 27 types. 53 is the closest prime number to 27*2=54.
45     static SymbolHash fBaseTypes = new SymbolHash(53);
46     static {
47         createBuiltInTypes(fBaseTypes);
48     }
49 
50     /**
51      * Get a built-in simple type of the given name
52      * REVISIT: its still not decided within the Schema WG how to define the
53      *          ur-types and if all simple types should be derived from a
54      *          complex type, so as of now we ignore the fact that anySimpleType
55      *          is derived from anyType, and pass 'null' as the base of
56      *          anySimpleType. It needs to be changed as per the decision taken.
57      *
58      * @param name  the name of the datatype
59      * @return      the datatype validator of the given name
60      */
getBuiltInType(String name)61     public XSSimpleType getBuiltInType(String name) {
62         return (XSSimpleType)fBaseTypes.get(name);
63     }
64 
65     /**
66      * get all built-in simple types, which are stored in a hashtable keyed by
67      * the name
68      *
69      * @return      a hashtable which contains all built-in simple types
70      */
getBuiltInTypes()71     public SymbolHash getBuiltInTypes() {
72         return fBaseTypes.makeClone();
73     }
74 
75     /**
76      * Create a new simple type which is derived by restriction from another
77      * simple type.
78      *
79      * @param name              name of the new type, could be null
80      * @param targetNamespace   target namespace of the new type, could be null
81      * @param finalSet          value of "final"
82      * @param base              base type of the new type
83      * @param annotations       set of annotations
84      * @return                  the newly created simple type
85      */
createTypeRestriction(String name, String targetNamespace, short finalSet, XSSimpleType base, XSObjectList annotations)86     public XSSimpleType createTypeRestriction(String name, String targetNamespace,
87                                               short finalSet, XSSimpleType base, XSObjectList annotations) {
88         return new XSSimpleTypeDecl((XSSimpleTypeDecl)base, name, targetNamespace, finalSet, false, annotations);
89     }
90 
91     /**
92      * Create a new simple type which is derived by list from another simple
93      * type.
94      *
95      * @param name              name of the new type, could be null
96      * @param targetNamespace   target namespace of the new type, could be null
97      * @param finalSet          value of "final"
98      * @param itemType          item type of the list type
99      * @param annotations       set of annotations
100      * @return                  the newly created simple type
101      */
createTypeList(String name, String targetNamespace, short finalSet, XSSimpleType itemType, XSObjectList annotations)102     public XSSimpleType createTypeList(String name, String targetNamespace,
103                                        short finalSet, XSSimpleType itemType,
104                                        XSObjectList annotations) {
105         return new XSSimpleTypeDecl(name, targetNamespace, finalSet, (XSSimpleTypeDecl)itemType, false, annotations);
106     }
107 
108     /**
109      * Create a new simple type which is derived by union from a list of other
110      * simple types.
111      *
112      * @param name              name of the new type, could be null
113      * @param targetNamespace   target namespace of the new type, could be null
114      * @param finalSet          value of "final"
115      * @param memberTypes       member types of the union type
116      * @param annotations       set of annotations
117      * @return                  the newly created simple type
118      */
createTypeUnion(String name, String targetNamespace, short finalSet, XSSimpleType[] memberTypes, XSObjectList annotations)119     public XSSimpleType createTypeUnion(String name, String targetNamespace,
120                                         short finalSet, XSSimpleType[] memberTypes,
121                                         XSObjectList annotations) {
122         int typeNum = memberTypes.length;
123         XSSimpleTypeDecl[] mtypes = new XSSimpleTypeDecl[typeNum];
124         System.arraycopy(memberTypes, 0, mtypes, 0, typeNum);
125 
126         return new XSSimpleTypeDecl(name, targetNamespace, finalSet, mtypes, annotations);
127     }
128 
129     // create all built-in types
createBuiltInTypes(SymbolHash types)130     static void createBuiltInTypes(SymbolHash types) {
131         // base schema simple type names
132         final String ANYSIMPLETYPE     = "anySimpleType";
133         final String ANYURI            = "anyURI";
134         final String BASE64BINARY      = "base64Binary";
135         final String BOOLEAN           = "boolean";
136         final String BYTE              = "byte";
137         final String DATE              = "date";
138         final String DATETIME          = "dateTime";
139         final String DAY               = "gDay";
140         final String DECIMAL           = "decimal";
141         final String INT               = "int";
142         final String INTEGER           = "integer";
143         final String LONG              = "long";
144         final String NEGATIVEINTEGER   = "negativeInteger";
145         final String MONTH             = "gMonth";
146         final String MONTHDAY          = "gMonthDay";
147         final String NONNEGATIVEINTEGER= "nonNegativeInteger";
148         final String NONPOSITIVEINTEGER= "nonPositiveInteger";
149         final String POSITIVEINTEGER   = "positiveInteger";
150         final String SHORT             = "short";
151         final String STRING            = "string";
152         final String TIME              = "time";
153         final String UNSIGNEDBYTE      = "unsignedByte";
154         final String UNSIGNEDINT       = "unsignedInt";
155         final String UNSIGNEDLONG      = "unsignedLong";
156         final String UNSIGNEDSHORT     = "unsignedShort";
157         final String YEAR              = "gYear";
158         final String YEARMONTH         = "gYearMonth";
159 
160         final XSFacets facets = new XSFacets();
161 
162         XSSimpleTypeDecl anySimpleType = XSSimpleTypeDecl.fAnySimpleType;
163         types.put(ANYSIMPLETYPE, anySimpleType);
164         XSSimpleTypeDecl stringDV = new XSSimpleTypeDecl(anySimpleType, STRING, XSSimpleTypeDecl.DV_STRING, XSSimpleType.ORDERED_FALSE, false, false, false, true, XSConstants.STRING_DT);
165         types.put(STRING, stringDV);
166         types.put(BOOLEAN, new XSSimpleTypeDecl(anySimpleType, BOOLEAN, XSSimpleTypeDecl.DV_BOOLEAN, XSSimpleType.ORDERED_FALSE, false, true, false, true, XSConstants.BOOLEAN_DT));
167         XSSimpleTypeDecl decimalDV = new XSSimpleTypeDecl(anySimpleType, DECIMAL, XSSimpleTypeDecl.DV_DECIMAL, XSSimpleType.ORDERED_TOTAL, false, false, true, true, XSConstants.DECIMAL_DT);
168         types.put(DECIMAL, decimalDV);
169 
170         types.put(ANYURI, new XSSimpleTypeDecl(anySimpleType, ANYURI, XSSimpleTypeDecl.DV_ANYURI, XSSimpleType.ORDERED_FALSE, false, false, false, true, XSConstants.ANYURI_DT));
171         types.put(BASE64BINARY, new XSSimpleTypeDecl(anySimpleType, BASE64BINARY, XSSimpleTypeDecl.DV_BASE64BINARY, XSSimpleType.ORDERED_FALSE, false, false, false, true, XSConstants.BASE64BINARY_DT));
172         types.put(DATETIME, new XSSimpleTypeDecl(anySimpleType, DATETIME, XSSimpleTypeDecl.DV_DATETIME, XSSimpleType.ORDERED_PARTIAL, false, false, false, true, XSConstants.DATETIME_DT));
173         types.put(TIME, new XSSimpleTypeDecl(anySimpleType, TIME, XSSimpleTypeDecl.DV_TIME, XSSimpleType.ORDERED_PARTIAL, false, false, false, true, XSConstants.TIME_DT));
174         types.put(DATE, new XSSimpleTypeDecl(anySimpleType, DATE, XSSimpleTypeDecl.DV_DATE, XSSimpleType.ORDERED_PARTIAL, false, false, false, true, XSConstants.DATE_DT));
175         types.put(YEARMONTH, new XSSimpleTypeDecl(anySimpleType, YEARMONTH, XSSimpleTypeDecl.DV_GYEARMONTH, XSSimpleType.ORDERED_PARTIAL, false, false, false, true, XSConstants.GYEARMONTH_DT));
176         types.put(YEAR, new XSSimpleTypeDecl(anySimpleType, YEAR, XSSimpleTypeDecl.DV_GYEAR, XSSimpleType.ORDERED_PARTIAL, false, false, false, true, XSConstants.GYEAR_DT));
177         types.put(MONTHDAY, new XSSimpleTypeDecl(anySimpleType, MONTHDAY, XSSimpleTypeDecl.DV_GMONTHDAY, XSSimpleType.ORDERED_PARTIAL, false, false, false, true, XSConstants.GMONTHDAY_DT));
178         types.put(DAY, new XSSimpleTypeDecl(anySimpleType, DAY, XSSimpleTypeDecl.DV_GDAY, XSSimpleType.ORDERED_PARTIAL, false, false, false, true, XSConstants.GDAY_DT));
179         types.put(MONTH, new XSSimpleTypeDecl(anySimpleType, MONTH, XSSimpleTypeDecl.DV_GMONTH, XSSimpleType.ORDERED_PARTIAL, false, false, false, true, XSConstants.GMONTH_DT));
180 
181         XSSimpleTypeDecl integerDV = new XSSimpleTypeDecl(decimalDV, INTEGER, XSSimpleTypeDecl.DV_INTEGER, XSSimpleType.ORDERED_TOTAL, false, false, true, true, XSConstants.INTEGER_DT);
182         types.put(INTEGER, integerDV);
183 
184         facets.maxInclusive = "0";
185         XSSimpleTypeDecl nonPositiveDV = new XSSimpleTypeDecl(integerDV, NONPOSITIVEINTEGER, URI_SCHEMAFORSCHEMA, (short)0, false, null, XSConstants.NONPOSITIVEINTEGER_DT);
186         nonPositiveDV.applyFacets1(facets , XSSimpleType.FACET_MAXINCLUSIVE, (short)0);
187         types.put(NONPOSITIVEINTEGER, nonPositiveDV);
188 
189         facets.maxInclusive = "-1";
190         XSSimpleTypeDecl negativeDV = new XSSimpleTypeDecl(nonPositiveDV, NEGATIVEINTEGER, URI_SCHEMAFORSCHEMA, (short)0, false, null, XSConstants.NEGATIVEINTEGER_DT);
191         negativeDV.applyFacets1(facets , XSSimpleType.FACET_MAXINCLUSIVE, (short)0);
192         types.put(NEGATIVEINTEGER, negativeDV);
193 
194         facets.maxInclusive = "9223372036854775807";
195         facets.minInclusive = "-9223372036854775808";
196         XSSimpleTypeDecl longDV = new XSSimpleTypeDecl(integerDV, LONG, URI_SCHEMAFORSCHEMA, (short)0, false, null, XSConstants.LONG_DT);
197         longDV.applyFacets1(facets , (short)(XSSimpleType.FACET_MAXINCLUSIVE | XSSimpleType.FACET_MININCLUSIVE), (short)0 );
198         types.put(LONG, longDV);
199 
200 
201         facets.maxInclusive = "2147483647";
202         facets.minInclusive =  "-2147483648";
203         XSSimpleTypeDecl intDV = new XSSimpleTypeDecl(longDV, INT, URI_SCHEMAFORSCHEMA, (short)0, false, null, XSConstants.INT_DT);
204         intDV.applyFacets1(facets, (short)(XSSimpleType.FACET_MAXINCLUSIVE | XSSimpleType.FACET_MININCLUSIVE), (short)0 );
205         types.put(INT, intDV);
206 
207         facets.maxInclusive = "32767";
208         facets.minInclusive = "-32768";
209         XSSimpleTypeDecl shortDV = new XSSimpleTypeDecl(intDV, SHORT , URI_SCHEMAFORSCHEMA, (short)0, false, null, XSConstants.SHORT_DT);
210         shortDV.applyFacets1(facets, (short)(XSSimpleType.FACET_MAXINCLUSIVE | XSSimpleType.FACET_MININCLUSIVE), (short)0 );
211         types.put(SHORT, shortDV);
212 
213         facets.maxInclusive = "127";
214         facets.minInclusive = "-128";
215         XSSimpleTypeDecl byteDV = new XSSimpleTypeDecl(shortDV, BYTE , URI_SCHEMAFORSCHEMA, (short)0, false, null, XSConstants.BYTE_DT);
216         byteDV.applyFacets1(facets, (short)(XSSimpleType.FACET_MAXINCLUSIVE | XSSimpleType.FACET_MININCLUSIVE), (short)0 );
217         types.put(BYTE, byteDV);
218 
219         facets.minInclusive =  "0" ;
220         XSSimpleTypeDecl nonNegativeDV = new XSSimpleTypeDecl(integerDV, NONNEGATIVEINTEGER , URI_SCHEMAFORSCHEMA, (short)0, false, null, XSConstants.NONNEGATIVEINTEGER_DT);
221         nonNegativeDV.applyFacets1(facets, XSSimpleType.FACET_MININCLUSIVE, (short)0 );
222         types.put(NONNEGATIVEINTEGER, nonNegativeDV);
223 
224         facets.maxInclusive = "18446744073709551615" ;
225         XSSimpleTypeDecl unsignedLongDV = new XSSimpleTypeDecl(nonNegativeDV, UNSIGNEDLONG , URI_SCHEMAFORSCHEMA, (short)0, false, null, XSConstants.UNSIGNEDLONG_DT);
226         unsignedLongDV.applyFacets1(facets, XSSimpleType.FACET_MAXINCLUSIVE, (short)0 );
227         types.put(UNSIGNEDLONG, unsignedLongDV);
228 
229         facets.maxInclusive = "4294967295" ;
230         XSSimpleTypeDecl unsignedIntDV = new XSSimpleTypeDecl(unsignedLongDV, UNSIGNEDINT , URI_SCHEMAFORSCHEMA, (short)0, false, null, XSConstants.UNSIGNEDINT_DT);
231         unsignedIntDV.applyFacets1(facets, XSSimpleType.FACET_MAXINCLUSIVE, (short)0 );
232         types.put(UNSIGNEDINT, unsignedIntDV);
233 
234         facets.maxInclusive = "65535" ;
235         XSSimpleTypeDecl unsignedShortDV = new XSSimpleTypeDecl(unsignedIntDV, UNSIGNEDSHORT , URI_SCHEMAFORSCHEMA, (short)0, false, null, XSConstants.UNSIGNEDSHORT_DT);
236         unsignedShortDV.applyFacets1(facets, XSSimpleType.FACET_MAXINCLUSIVE, (short)0 );
237         types.put(UNSIGNEDSHORT, unsignedShortDV);
238 
239         facets.maxInclusive = "255" ;
240         XSSimpleTypeDecl unsignedByteDV = new XSSimpleTypeDecl(unsignedShortDV, UNSIGNEDBYTE , URI_SCHEMAFORSCHEMA, (short)0, false, null, XSConstants.UNSIGNEDBYTE_DT);
241         unsignedByteDV.applyFacets1(facets, XSSimpleType.FACET_MAXINCLUSIVE, (short)0 );
242         types.put(UNSIGNEDBYTE, unsignedByteDV);
243 
244         facets.minInclusive = "1" ;
245         XSSimpleTypeDecl positiveIntegerDV = new XSSimpleTypeDecl(nonNegativeDV, POSITIVEINTEGER , URI_SCHEMAFORSCHEMA, (short)0, false, null, XSConstants.POSITIVEINTEGER_DT);
246         positiveIntegerDV.applyFacets1(facets, XSSimpleType.FACET_MININCLUSIVE, (short)0 );
247         types.put(POSITIVEINTEGER, positiveIntegerDV);
248     }//createBuiltInTypes(SymbolHash)
249 
250 }//BaseDVFactory
251