| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.type; |
| 9 | |
| 10 | import net.sf.saxon.om.StandardNames; |
| 11 | import net.sf.saxon.z.IntHashMap; |
| 12 | |
| 13 | import java.util.HashMap; |
| 14 | import java.util.Map; |
| 15 | |
| 16 | /** |
| 17 | * This non-instantiable class acts as a register of Schema objects containing all the built-in types: |
| 18 | * that is, the types defined in the "xs" namespace. |
| 19 | * <p>Previously called BuiltInSchemaFactory; but its original function has largely been moved to the two |
| 20 | * classes {@link BuiltInAtomicType} and {@link BuiltInListType}</p> |
| 21 | */ |
| 22 | |
| 23 | public abstract class BuiltInType { |
| 24 | |
| 25 | /** |
| 26 | * Table of all built in types, indexed by fingerprint |
| 27 | */ |
| 28 | |
| 29 | private static IntHashMap<SchemaType> lookup = new IntHashMap<>(100); |
| 30 | |
| 31 | /** |
| 32 | * Table of all built in types, indexed by local name |
| 33 | */ |
| 34 | |
| 35 | private static Map<String, SchemaType> lookupByLocalName = new HashMap<>(100); |
| 36 | |
| 37 | /** |
| 38 | * Class is never instantiated |
| 39 | */ |
| 40 | |
| 41 | private BuiltInType() { |
| 42 | } |
| 43 | |
| 44 | static { |
| 45 | register(StandardNames.XS_ANY_SIMPLE_TYPE, AnySimpleType.getInstance()); |
| 46 | register(StandardNames.XS_ANY_TYPE, AnyType.getInstance()); |
| 47 | register(StandardNames.XS_UNTYPED, Untyped.getInstance()); |
| 48 | register(StandardNames.XS_ERROR, ErrorType.getInstance()); |
| 49 | register(StandardNames.XS_NUMERIC, NumericType.getInstance()); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get the schema type with a given fingerprint |
| 54 | * |
| 55 | * @param fingerprint the fingerprint representing the name of the required type |
| 56 | * @return the SchemaType object representing the given type, if known, otherwise null |
| 57 | */ |
| 58 | |
| 59 | public static SchemaType getSchemaType(int fingerprint) { |
| 60 | SchemaType st = lookup.get(fingerprint); |
| 61 | if (st == null) { |
| 62 | // this means the method has been called before doing the static initialization of BuiltInAtomicType |
| 63 | // or BuiltInListType. So force it now |
| 64 | if (BuiltInAtomicType.DOUBLE == null || BuiltInListType.NMTOKENS == null) { |
| 65 | // no action, except to force the initialization to run |
| 66 | } |
| 67 | st = lookup.get(fingerprint); |
| 68 | } |
| 69 | return st; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get the schema type with a given local name |
| 74 | * |
| 75 | * @param name the local name of the required type |
| 76 | * @return the SchemaType object representing the given type, if known, otherwise null |
| 77 | */ |
| 78 | |
| 79 | public static SchemaType getSchemaTypeByLocalName(String name) { |
| 80 | SchemaType st = lookupByLocalName.get(name); |
| 81 | if (st == null) { |
| 82 | // this means the method has been called before doing the static initialization of BuiltInAtomicType |
| 83 | // or BuiltInListType. So force it now |
| 84 | if (BuiltInAtomicType.DOUBLE == null || BuiltInListType.NMTOKENS == null) { |
| 85 | // no action, except to force the initialization to run |
| 86 | } |
| 87 | st = lookupByLocalName.get(name); |
| 88 | } |
| 89 | return st; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Method for internal use to register a built in type with this class |
| 94 | * |
| 95 | * @param fingerprint the fingerprint of the type name |
| 96 | * @param type the SchemaType representing the built in type |
| 97 | */ |
| 98 | |
| 99 | static void register(int fingerprint, SchemaType type) { |
| 100 | lookup.put(fingerprint, type); |
| 101 | lookupByLocalName.put(type.getName(), type); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | } |
| 106 |