KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > impl > DatatypeFactoryImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.axi.impl;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27 import org.netbeans.modules.xml.axi.AXIModel;
28 import org.netbeans.modules.xml.axi.datatype.*;
29 import org.netbeans.modules.xml.axi.datatype.Datatype.Facet;
30 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
31 import org.netbeans.modules.xml.schema.model.SchemaComponent;
32 import org.netbeans.modules.xml.schema.model.SchemaModel;
33 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
34 import org.netbeans.modules.xml.schema.model.SimpleType;
35
36 /**
37  *
38  * @author Ayub Khan
39  */

40 public class DatatypeFactoryImpl extends DatatypeFactory {
41     
42     private static HashMap JavaDoc<Datatype.Kind, List JavaDoc<Class JavaDoc<? extends SchemaComponent>>> asfCache =
43             new HashMap JavaDoc<Datatype.Kind, List JavaDoc<Class JavaDoc<? extends SchemaComponent>>>();
44     
45     /** Creates a new instance of DatatypeFactory */
46     public DatatypeFactoryImpl() {
47     }
48     
49     public synchronized List JavaDoc<Class JavaDoc<? extends SchemaComponent>> getApplicableSchemaFacets(SimpleType st) {
50         List JavaDoc<Class JavaDoc<? extends SchemaComponent>> facetClasses = Collections.emptyList();
51         Datatype d = new DatatypeBuilder().getDatatype(st);
52         if(d != null) {
53             facetClasses = asfCache.get(d.getKind());
54             if(facetClasses == null) {
55                 facetClasses = new ArrayList JavaDoc<Class JavaDoc<? extends SchemaComponent>>();
56                 List JavaDoc<Facet> afs = d.getApplicableFacets();
57                 for(Facet f:afs)
58                     facetClasses.add(f.getComponentType());
59                 asfCache.put(d.getKind(), facetClasses);
60             }
61         }
62         return facetClasses;
63     }
64     
65     /**
66      * Creates an AXI Datatype, given a typeName (built-in types
67      * like "string" or custom types "SKU" etc).
68      */

69     public Datatype createPrimitive(String JavaDoc typeName) {
70         Datatype returnType = null;
71         SimpleType schemaType = getPrimitiveType(typeName);
72         if(schemaType != null) {
73             if(typeName.equals(Datatype.Kind.STRING.getName())) {
74                 returnType = new StringType();
75             } else if(typeName.equals(Datatype.Kind.NORMALIZED_STRING.getName())) {
76                 returnType = new NormalizedStringType();
77             } else if(typeName.equals(Datatype.Kind.TOKEN.getName())) {
78                 returnType = new TokenType();
79             } else if(typeName.equals(Datatype.Kind.LANGUAGE.getName())) {
80                 returnType = new LanguageType();
81             } else if(typeName.equals(Datatype.Kind.NAME.getName())) {
82                 returnType = new NameType();
83             } else if(typeName.equals(Datatype.Kind.NMTOKEN.getName())) {
84                 returnType = new NmTokenType();
85             } else if(typeName.equals(Datatype.Kind.NCNAME.getName())) {
86                 returnType = new NcNameType();
87             } else if(typeName.equals(Datatype.Kind.NMTOKENS.getName())) {
88                 returnType = new NmTokensType();
89             } else if(typeName.equals(Datatype.Kind.ID.getName())) {
90                 returnType = new IdType();
91             } else if(typeName.equals(Datatype.Kind.IDREF.getName())) {
92                 returnType = new IdRefType();
93             } else if(typeName.equals(Datatype.Kind.ENTITY.getName())) {
94                 returnType = new EntityType();
95             } else if(typeName.equals(Datatype.Kind.IDREFS.getName())) {
96                 returnType = new IdRefsType();
97             } else if(typeName.equals(Datatype.Kind.ENTITIES.getName())) {
98                 returnType = new EntitiesType();
99             } else if(typeName.equals(Datatype.Kind.DECIMAL.getName())) {
100                 returnType = new DecimalType();
101             } else if(typeName.equals(Datatype.Kind.INTEGER.getName())) {
102                 returnType = new IntegerType();
103             } else if(typeName.equals(Datatype.Kind.NON_POSITIVE_INTEGER.getName())) {
104                 returnType = new NonPositiveIntegerType();
105             } else if(typeName.equals(Datatype.Kind.LONG.getName())) {
106                 returnType = new LongType();
107             } else if(typeName.equals(Datatype.Kind.NON_NEGATIVE_INTEGER.getName())) {
108                 returnType = new NonNegativeIntegerType();
109             } else if(typeName.equals(Datatype.Kind.NEGATIVE_INTEGER.getName())) {
110                 returnType = new NegativeIntegerType();
111             } else if(typeName.equals(Datatype.Kind.INT.getName())) {
112                 returnType = new IntType();
113             } else if(typeName.equals(Datatype.Kind.SHORT.getName())) {
114                 returnType = new ShortType();
115             } else if(typeName.equals(Datatype.Kind.BYTE.getName())) {
116                 returnType = new ByteType();
117             } else if(typeName.equals(Datatype.Kind.UNSIGNED_LONG.getName())) {
118                 returnType = new UnsignedLongType();
119             } else if(typeName.equals(Datatype.Kind.UNSIGNED_INT.getName())) {
120                 returnType = new UnsignedIntType();
121             } else if(typeName.equals(Datatype.Kind.UNSIGNED_SHORT.getName())) {
122                 returnType = new UnsignedShortType();
123             } else if(typeName.equals(Datatype.Kind.UNSIGNED_BYTE.getName())) {
124                 returnType = new UnsignedByteType();
125             } else if(typeName.equals(Datatype.Kind.POSITIVE_INTEGER.getName())) {
126                 returnType = new PositiveIntegerType();
127             } else if(typeName.equals(Datatype.Kind.DURATION.getName())) {
128                 returnType = new DurationType();
129             } else if(typeName.equals(Datatype.Kind.DATE_TIME.getName())) {
130                 returnType = new DateTimeType();
131             } else if(typeName.equals(Datatype.Kind.TIME.getName())) {
132                 returnType = new TimeType();
133             } else if(typeName.equals(Datatype.Kind.DATE.getName())) {
134                 returnType = new DateType();
135             } else if(typeName.equals(Datatype.Kind.G_YEAR_MONTH.getName())) {
136                 returnType = new GYearMonthType();
137             } else if(typeName.equals(Datatype.Kind.G_YEAR.getName())) {
138                 returnType = new GYearType();
139             } else if(typeName.equals(Datatype.Kind.G_MONTH_DAY.getName())) {
140                 returnType = new GMonthDayType();
141             } else if(typeName.equals(Datatype.Kind.G_DAY.getName())) {
142                 returnType = new GDayType();
143             } else if(typeName.equals(Datatype.Kind.G_MONTH.getName())) {
144                 returnType = new GMonthType();
145             } else if(typeName.equals(Datatype.Kind.BOOLEAN.getName())) {
146                 returnType = new BooleanType();
147             } else if(typeName.equals(Datatype.Kind.BASE64_BINARY.getName())) {
148                 returnType = new Base64BinaryType();
149             } else if(typeName.equals(Datatype.Kind.HEX_BINARY.getName())) {
150                 returnType = new HexBinaryType();
151             } else if(typeName.equals(Datatype.Kind.FLOAT.getName())) {
152                 returnType = new FloatType();
153             } else if(typeName.equals(Datatype.Kind.DOUBLE.getName())) {
154                 returnType = new DoubleType();
155             } else if(typeName.equals(Datatype.Kind.ANYURI.getName())) {
156                 returnType = new AnyURIType();
157             } else if(typeName.equals(Datatype.Kind.ANYTYPE.getName())) {
158                 returnType = new AnyType();
159             } else if(typeName.equals(Datatype.Kind.QNAME.getName())) {
160                 returnType = new QNameType();
161             } else if(typeName.equals(Datatype.Kind.NOTATION.getName())) {
162                 returnType = new NotationType();
163             }
164         }
165         return returnType;
166     }
167     
168     /**
169      * Creates an AXI Datatype, given a schema component.
170      */

171     public Datatype getDatatype(AXIModel axiModel, SchemaComponent component) {
172         DatatypeBuilder builder = new DatatypeBuilder(axiModel);
173         return builder.getDatatype(component);
174     }
175     
176     static GlobalSimpleType getPrimitiveType(String JavaDoc typeName){
177         SchemaModel primitiveModel = SchemaModelFactory.getDefault().getPrimitiveTypesModel();
178         Collection JavaDoc<GlobalSimpleType> primitives = primitiveModel.getSchema().getSimpleTypes();
179         for(GlobalSimpleType ptype: primitives){
180             if(ptype.getName().equals(typeName)){
181                 return ptype;
182             }
183         }
184         return null;
185     }
186 }
187
Popular Tags