< prev index next >

src/jdk.xml.bind/share/classes/com/sun/tools/internal/xjc/reader/xmlschema/SimpleTypeBuilder.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 123      * of this class for details.
 124      *
 125      * UGLY: Implemented as a Stack of XSComponent to fix a bug
 126      */
 127     public final Stack<XSComponent> refererStack = new Stack<XSComponent>();
 128 
 129     /**
 130      * Records what xmime:expectedContentTypes annotations we honored and processed,
 131      * so that we can later check if the user had these annotations in the places
 132      * where we didn't anticipate them.
 133      */
 134     private final Set<XSComponent> acknowledgedXmimeContentTypes = new HashSet<XSComponent>();
 135 
 136     /**
 137      * The type that was originally passed to this {@link SimpleTypeBuilder#build(XSSimpleType)}.
 138      * Never null.
 139      */
 140     private XSSimpleType initiatingType;
 141 
 142     /** {@link TypeUse}s for the built-in types. Read-only. */
 143     public static final Map<String,TypeUse> builtinConversions = new HashMap<String,TypeUse>();
 144 
 145 
 146     /**
 147      * Entry point from outside. Builds a BGM type expression
 148      * from a simple type schema component.
 149      *
 150      * @param type
 151      *      the simple type to be bound.
 152      */
 153     public TypeUse build( XSSimpleType type ) {
 154         XSSimpleType oldi = initiatingType;
 155         this.initiatingType = type;
 156 
 157         TypeUse e = checkRefererCustomization(type);
 158         if(e==null)
 159             e = compose(type);
 160 
 161         initiatingType = oldi;
 162 
 163         return e;


 401 
 402         // see if this type should be mapped to a type-safe enumeration by default.
 403         // if so, built a EnumXDucer from it and return it.
 404         if(type.isRestriction() && !noAutoEnum) {
 405             XSRestrictionSimpleType rst = type.asRestriction();
 406             if(shouldBeMappedToTypeSafeEnumByDefault(rst)) {
 407                 r = bindToTypeSafeEnum(rst,null,null, Collections.<String, BIEnumMember>emptyMap(),
 408                             getEnumMemberMode(),null);
 409                 if(r!=null)
 410                     return r;
 411             }
 412         }
 413 
 414         return (CNonElement)getClassSelector()._bindToClass(type,null,false);
 415     }
 416 
 417     private static Set<XSRestrictionSimpleType> reportedEnumMemberSizeWarnings;
 418 
 419     /**
 420      * Returns true if a type-safe enum should be created from
 421      * the given simple type by default without an explicit &lt;jaxb:enum> customization.
 422      */
 423     private boolean shouldBeMappedToTypeSafeEnumByDefault( XSRestrictionSimpleType type ) {
 424 
 425         // if not, there will be a problem wrt the class name of this type safe enum type.
 426         if( type.isLocal() )    return false;
 427 
 428         // if redefined, we should map the new definition, not the old one.
 429         if( type.getRedefinedBy()!=null )   return false;
 430 
 431         List<XSFacet> facets = type.getDeclaredFacets(XSFacet.FACET_ENUMERATION);
 432         if( facets.isEmpty() )
 433             // if the type itself doesn't have the enumeration facet,
 434             // it won't be mapped to a type-safe enum.
 435             return false;
 436 
 437         if(facets.size() > builder.getGlobalBinding().getDefaultEnumMemberSizeCap()) {
 438             // if there are too many facets, it's not very useful
 439             // produce warning when simple type is not mapped to enum
 440             // see issue https://jaxb.dev.java.net/issues/show_bug.cgi?id=711
 441 


 859         if(b==null) return a;
 860         return a.min(b);
 861     }
 862 
 863     private BigInteger max(BigInteger a, BigInteger b) {
 864         if(a==null) return b;
 865         if(b==null) return a;
 866         return a.max(b);
 867     }
 868 
 869     private static final BigInteger LONG_MIN = BigInteger.valueOf(Long.MIN_VALUE);
 870     private static final BigInteger LONG_MAX = BigInteger.valueOf(Long.MAX_VALUE);
 871     private static final BigInteger INT_MIN = BigInteger.valueOf(Integer.MIN_VALUE);
 872     private static final BigInteger INT_MAX = BigInteger.valueOf(Integer.MAX_VALUE);
 873 
 874     static {
 875         // list of datatypes which have built-in conversions.
 876         // note that although xs:token and xs:normalizedString are not
 877         // specified in the spec, they need to be here because they
 878         // have different whitespace normalization semantics.
 879         Map<String,TypeUse> m = builtinConversions;
 880 
 881         // TODO: this is so dumb
 882         m.put("string",         CBuiltinLeafInfo.STRING);
 883         m.put("anyURI",         CBuiltinLeafInfo.STRING);
 884         m.put("boolean",        CBuiltinLeafInfo.BOOLEAN);
 885         // we'll also look at the expected media type, so don't just add this to the map
 886         // m.put("base64Binary",   CBuiltinLeafInfo.BASE64_BYTE_ARRAY);
 887         m.put("hexBinary",      CBuiltinLeafInfo.HEXBIN_BYTE_ARRAY);
 888         m.put("float",          CBuiltinLeafInfo.FLOAT);
 889         m.put("decimal",        CBuiltinLeafInfo.BIG_DECIMAL);
 890         m.put("integer",        CBuiltinLeafInfo.BIG_INTEGER);
 891         m.put("long",           CBuiltinLeafInfo.LONG);
 892         m.put("unsignedInt",    CBuiltinLeafInfo.LONG);
 893         m.put("int",            CBuiltinLeafInfo.INT);
 894         m.put("unsignedShort",  CBuiltinLeafInfo.INT);
 895         m.put("short",          CBuiltinLeafInfo.SHORT);
 896         m.put("unsignedByte",   CBuiltinLeafInfo.SHORT);
 897         m.put("byte",           CBuiltinLeafInfo.BYTE);
 898         m.put("double",         CBuiltinLeafInfo.DOUBLE);
 899         m.put("QName",          CBuiltinLeafInfo.QNAME);
 900         m.put("NOTATION",       CBuiltinLeafInfo.QNAME);
 901         m.put("dateTime",       CBuiltinLeafInfo.CALENDAR);
 902         m.put("date",           CBuiltinLeafInfo.CALENDAR);
 903         m.put("time",           CBuiltinLeafInfo.CALENDAR);
 904         m.put("gYearMonth",     CBuiltinLeafInfo.CALENDAR);
 905         m.put("gYear",          CBuiltinLeafInfo.CALENDAR);
 906         m.put("gMonthDay",      CBuiltinLeafInfo.CALENDAR);
 907         m.put("gDay",           CBuiltinLeafInfo.CALENDAR);
 908         m.put("gMonth",         CBuiltinLeafInfo.CALENDAR);
 909         m.put("duration",       CBuiltinLeafInfo.DURATION);
 910         m.put("token",          CBuiltinLeafInfo.TOKEN);
 911         m.put("normalizedString",CBuiltinLeafInfo.NORMALIZED_STRING);
 912         m.put("ID",             CBuiltinLeafInfo.ID);
 913         m.put("IDREF",          CBuiltinLeafInfo.IDREF);


 914         // TODO: handling dateTime, time, and date type
 915 //        String[] names = {
 916 //            "date", "dateTime", "time", "hexBinary" };
 917     }
 918 }
   1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 123      * of this class for details.
 124      *
 125      * UGLY: Implemented as a Stack of XSComponent to fix a bug
 126      */
 127     public final Stack<XSComponent> refererStack = new Stack<XSComponent>();
 128 
 129     /**
 130      * Records what xmime:expectedContentTypes annotations we honored and processed,
 131      * so that we can later check if the user had these annotations in the places
 132      * where we didn't anticipate them.
 133      */
 134     private final Set<XSComponent> acknowledgedXmimeContentTypes = new HashSet<XSComponent>();
 135 
 136     /**
 137      * The type that was originally passed to this {@link SimpleTypeBuilder#build(XSSimpleType)}.
 138      * Never null.
 139      */
 140     private XSSimpleType initiatingType;
 141 
 142     /** {@link TypeUse}s for the built-in types. Read-only. */
 143     public static final Map<String,TypeUse> builtinConversions;
 144 
 145 
 146     /**
 147      * Entry point from outside. Builds a BGM type expression
 148      * from a simple type schema component.
 149      *
 150      * @param type
 151      *      the simple type to be bound.
 152      */
 153     public TypeUse build( XSSimpleType type ) {
 154         XSSimpleType oldi = initiatingType;
 155         this.initiatingType = type;
 156 
 157         TypeUse e = checkRefererCustomization(type);
 158         if(e==null)
 159             e = compose(type);
 160 
 161         initiatingType = oldi;
 162 
 163         return e;


 401 
 402         // see if this type should be mapped to a type-safe enumeration by default.
 403         // if so, built a EnumXDucer from it and return it.
 404         if(type.isRestriction() && !noAutoEnum) {
 405             XSRestrictionSimpleType rst = type.asRestriction();
 406             if(shouldBeMappedToTypeSafeEnumByDefault(rst)) {
 407                 r = bindToTypeSafeEnum(rst,null,null, Collections.<String, BIEnumMember>emptyMap(),
 408                             getEnumMemberMode(),null);
 409                 if(r!=null)
 410                     return r;
 411             }
 412         }
 413 
 414         return (CNonElement)getClassSelector()._bindToClass(type,null,false);
 415     }
 416 
 417     private static Set<XSRestrictionSimpleType> reportedEnumMemberSizeWarnings;
 418 
 419     /**
 420      * Returns true if a type-safe enum should be created from
 421      * the given simple type by default without an explicit {@code <jaxb:enum>} customization.
 422      */
 423     private boolean shouldBeMappedToTypeSafeEnumByDefault( XSRestrictionSimpleType type ) {
 424 
 425         // if not, there will be a problem wrt the class name of this type safe enum type.
 426         if( type.isLocal() )    return false;
 427 
 428         // if redefined, we should map the new definition, not the old one.
 429         if( type.getRedefinedBy()!=null )   return false;
 430 
 431         List<XSFacet> facets = type.getDeclaredFacets(XSFacet.FACET_ENUMERATION);
 432         if( facets.isEmpty() )
 433             // if the type itself doesn't have the enumeration facet,
 434             // it won't be mapped to a type-safe enum.
 435             return false;
 436 
 437         if(facets.size() > builder.getGlobalBinding().getDefaultEnumMemberSizeCap()) {
 438             // if there are too many facets, it's not very useful
 439             // produce warning when simple type is not mapped to enum
 440             // see issue https://jaxb.dev.java.net/issues/show_bug.cgi?id=711
 441 


 859         if(b==null) return a;
 860         return a.min(b);
 861     }
 862 
 863     private BigInteger max(BigInteger a, BigInteger b) {
 864         if(a==null) return b;
 865         if(b==null) return a;
 866         return a.max(b);
 867     }
 868 
 869     private static final BigInteger LONG_MIN = BigInteger.valueOf(Long.MIN_VALUE);
 870     private static final BigInteger LONG_MAX = BigInteger.valueOf(Long.MAX_VALUE);
 871     private static final BigInteger INT_MIN = BigInteger.valueOf(Integer.MIN_VALUE);
 872     private static final BigInteger INT_MAX = BigInteger.valueOf(Integer.MAX_VALUE);
 873 
 874     static {
 875         // list of datatypes which have built-in conversions.
 876         // note that although xs:token and xs:normalizedString are not
 877         // specified in the spec, they need to be here because they
 878         // have different whitespace normalization semantics.
 879         Map<String,TypeUse> m = new HashMap<String,TypeUse>();
 880 
 881         // TODO: this is so dumb
 882         m.put("string",         CBuiltinLeafInfo.STRING);
 883         m.put("anyURI",         CBuiltinLeafInfo.STRING);
 884         m.put("boolean",        CBuiltinLeafInfo.BOOLEAN);
 885         // we'll also look at the expected media type, so don't just add this to the map
 886         // m.put("base64Binary",   CBuiltinLeafInfo.BASE64_BYTE_ARRAY);
 887         m.put("hexBinary",      CBuiltinLeafInfo.HEXBIN_BYTE_ARRAY);
 888         m.put("float",          CBuiltinLeafInfo.FLOAT);
 889         m.put("decimal",        CBuiltinLeafInfo.BIG_DECIMAL);
 890         m.put("integer",        CBuiltinLeafInfo.BIG_INTEGER);
 891         m.put("long",           CBuiltinLeafInfo.LONG);
 892         m.put("unsignedInt",    CBuiltinLeafInfo.LONG);
 893         m.put("int",            CBuiltinLeafInfo.INT);
 894         m.put("unsignedShort",  CBuiltinLeafInfo.INT);
 895         m.put("short",          CBuiltinLeafInfo.SHORT);
 896         m.put("unsignedByte",   CBuiltinLeafInfo.SHORT);
 897         m.put("byte",           CBuiltinLeafInfo.BYTE);
 898         m.put("double",         CBuiltinLeafInfo.DOUBLE);
 899         m.put("QName",          CBuiltinLeafInfo.QNAME);
 900         m.put("NOTATION",       CBuiltinLeafInfo.QNAME);
 901         m.put("dateTime",       CBuiltinLeafInfo.CALENDAR);
 902         m.put("date",           CBuiltinLeafInfo.CALENDAR);
 903         m.put("time",           CBuiltinLeafInfo.CALENDAR);
 904         m.put("gYearMonth",     CBuiltinLeafInfo.CALENDAR);
 905         m.put("gYear",          CBuiltinLeafInfo.CALENDAR);
 906         m.put("gMonthDay",      CBuiltinLeafInfo.CALENDAR);
 907         m.put("gDay",           CBuiltinLeafInfo.CALENDAR);
 908         m.put("gMonth",         CBuiltinLeafInfo.CALENDAR);
 909         m.put("duration",       CBuiltinLeafInfo.DURATION);
 910         m.put("token",          CBuiltinLeafInfo.TOKEN);
 911         m.put("normalizedString",CBuiltinLeafInfo.NORMALIZED_STRING);
 912         m.put("ID",             CBuiltinLeafInfo.ID);
 913         m.put("IDREF",          CBuiltinLeafInfo.IDREF);
 914 
 915         builtinConversions = Collections.unmodifiableMap(m);
 916         // TODO: handling dateTime, time, and date type
 917 //        String[] names = {
 918 //            "date", "dateTime", "time", "hexBinary" };
 919     }
 920 }
< prev index next >