View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.config.generator.xsom;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import com.sun.xml.xsom.XSAttributeDecl;
24  import com.sun.xml.xsom.XSFacet;
25  import com.sun.xml.xsom.XSRestrictionSimpleType;
26  import com.sun.xml.xsom.XSSimpleType;
27  
28  public abstract class XSDAttributeValueTypeFactory {
29  
30      public static XSDAttributeValueType createType(XSAttributeDecl attributeDecl) throws Exception {
31          XSSimpleType type = attributeDecl.getType();
32          if (type != null) {
33              XSDAttributeValueType attributeValueType = doCreateType(type);
34              attributeValueType.fillUpRestrictions(attributeDecl);
35              return attributeValueType;
36          }
37          throw new Exception("Type is null for the attributeDecl: " + attributeDecl);
38      }
39  
40      private static XSDAttributeValueType doCreateType(XSSimpleType type) throws Exception {
41          XSRestrictionSimpleType restriction = type.asRestriction();
42          List<String> enumeration = new ArrayList<String>();
43          if (restriction != null) {
44              Iterator<? extends XSFacet> i = restriction.getDeclaredFacets().iterator();
45              while (i.hasNext()) {
46                  XSFacet facet = i.next();
47                  if (facet.getName().equals(XSFacet.FACET_ENUMERATION)) {
48                      enumeration.add(facet.getValue().value);
49                  }
50              }
51          }
52          if (enumeration != null && enumeration.size() > 0) {
53              return new XSDAttributeValueType.XSDAttributeValueEnumerationType(enumeration.toArray(new String[0]));
54          }
55          if ("boolean".equalsIgnoreCase(type.getName())) {
56              return new XSDAttributeValueType.XSDAttributeValueBooleanType();
57          }
58          if ("integer".equalsIgnoreCase(type.getName())) {
59              return new XSDAttributeValueType.XSDAttributeValueIntegerType();
60          }
61          if ("anySimpleType".equalsIgnoreCase(type.getName())) {
62              return new XSDAttributeValueType.XSDAttributeValueAnySimpleType();
63          }
64          if ("string".equalsIgnoreCase(type.getName())) {
65              return new XSDAttributeValueType.XSDAttributeValueStringType();
66          }
67          if ("positiveInteger".equalsIgnoreCase(type.getName())) {
68              return new XSDAttributeValueType.XSDAttributeValuePositiveIntegerType();
69          }
70          if ("nonNegativeInteger".equalsIgnoreCase(type.getName())) {
71              return new XSDAttributeValueType.XSDAttributeValueNonNegativeIntegerType();
72          }
73          if ("memoryUnit".equalsIgnoreCase(type.getName())) {
74              return new XSDAttributeValueType.XSDAttributeValueMemoryUnitType();
75          }
76          if ("memoryUnitOrPercentage".equalsIgnoreCase(type.getName())) {
77              return new XSDAttributeValueType.XSDAttributeValueMemoryUnitOrPercentageType();
78          }
79          // if this exception is thrown, extend the above if clauses handling the type name with a new type in XSDAttributeValueType
80          throw new Exception("Unknown type : " + (type == null ? "NULL" : type.getName())
81                  + ". Please handle this type following instructions in the source code");
82      }
83  
84  }