001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.axi.impl;
043:
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.Collections;
047: import java.util.HashMap;
048: import java.util.List;
049: import org.netbeans.modules.xml.axi.AXIModel;
050: import org.netbeans.modules.xml.axi.datatype.*;
051: import org.netbeans.modules.xml.axi.datatype.Datatype.Facet;
052: import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
053: import org.netbeans.modules.xml.schema.model.SchemaComponent;
054: import org.netbeans.modules.xml.schema.model.SchemaModel;
055: import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
056: import org.netbeans.modules.xml.schema.model.SimpleType;
057:
058: /**
059: *
060: * @author Ayub Khan
061: */
062: public class DatatypeFactoryImpl extends DatatypeFactory {
063:
064: private static HashMap<Datatype.Kind, List<Class<? extends SchemaComponent>>> asfCache = new HashMap<Datatype.Kind, List<Class<? extends SchemaComponent>>>();
065:
066: /** Creates a new instance of DatatypeFactory */
067: public DatatypeFactoryImpl() {
068: }
069:
070: public synchronized List<Class<? extends SchemaComponent>> getApplicableSchemaFacets(
071: SimpleType st) {
072: List<Class<? extends SchemaComponent>> facetClasses = Collections
073: .emptyList();
074: Datatype d = new DatatypeBuilder().getDatatype(st);
075: if (d != null) {
076: facetClasses = asfCache.get(d.getKind());
077: if (facetClasses == null) {
078: facetClasses = new ArrayList<Class<? extends SchemaComponent>>();
079: List<Facet> afs = d.getApplicableFacets();
080: for (Facet f : afs)
081: facetClasses.add(f.getComponentType());
082: asfCache.put(d.getKind(), facetClasses);
083: }
084: }
085: return facetClasses;
086: }
087:
088: /**
089: * Creates an AXI Datatype, given a typeName (built-in types
090: * like "string" or custom types "SKU" etc).
091: */
092: public Datatype createPrimitive(String typeName) {
093: Datatype returnType = null;
094: SimpleType schemaType = getPrimitiveType(typeName);
095: if (schemaType != null) {
096: if (typeName.equals(Datatype.Kind.STRING.getName())) {
097: returnType = new StringType();
098: } else if (typeName.equals(Datatype.Kind.NORMALIZED_STRING
099: .getName())) {
100: returnType = new NormalizedStringType();
101: } else if (typeName.equals(Datatype.Kind.TOKEN.getName())) {
102: returnType = new TokenType();
103: } else if (typeName
104: .equals(Datatype.Kind.LANGUAGE.getName())) {
105: returnType = new LanguageType();
106: } else if (typeName.equals(Datatype.Kind.NAME.getName())) {
107: returnType = new NameType();
108: } else if (typeName.equals(Datatype.Kind.NMTOKEN.getName())) {
109: returnType = new NmTokenType();
110: } else if (typeName.equals(Datatype.Kind.NCNAME.getName())) {
111: returnType = new NcNameType();
112: } else if (typeName
113: .equals(Datatype.Kind.NMTOKENS.getName())) {
114: returnType = new NmTokensType();
115: } else if (typeName.equals(Datatype.Kind.ID.getName())) {
116: returnType = new IdType();
117: } else if (typeName.equals(Datatype.Kind.IDREF.getName())) {
118: returnType = new IdRefType();
119: } else if (typeName.equals(Datatype.Kind.ENTITY.getName())) {
120: returnType = new EntityType();
121: } else if (typeName.equals(Datatype.Kind.IDREFS.getName())) {
122: returnType = new IdRefsType();
123: } else if (typeName
124: .equals(Datatype.Kind.ENTITIES.getName())) {
125: returnType = new EntitiesType();
126: } else if (typeName.equals(Datatype.Kind.DECIMAL.getName())) {
127: returnType = new DecimalType();
128: } else if (typeName.equals(Datatype.Kind.INTEGER.getName())) {
129: returnType = new IntegerType();
130: } else if (typeName
131: .equals(Datatype.Kind.NON_POSITIVE_INTEGER
132: .getName())) {
133: returnType = new NonPositiveIntegerType();
134: } else if (typeName.equals(Datatype.Kind.LONG.getName())) {
135: returnType = new LongType();
136: } else if (typeName
137: .equals(Datatype.Kind.NON_NEGATIVE_INTEGER
138: .getName())) {
139: returnType = new NonNegativeIntegerType();
140: } else if (typeName.equals(Datatype.Kind.NEGATIVE_INTEGER
141: .getName())) {
142: returnType = new NegativeIntegerType();
143: } else if (typeName.equals(Datatype.Kind.INT.getName())) {
144: returnType = new IntType();
145: } else if (typeName.equals(Datatype.Kind.SHORT.getName())) {
146: returnType = new ShortType();
147: } else if (typeName.equals(Datatype.Kind.BYTE.getName())) {
148: returnType = new ByteType();
149: } else if (typeName.equals(Datatype.Kind.UNSIGNED_LONG
150: .getName())) {
151: returnType = new UnsignedLongType();
152: } else if (typeName.equals(Datatype.Kind.UNSIGNED_INT
153: .getName())) {
154: returnType = new UnsignedIntType();
155: } else if (typeName.equals(Datatype.Kind.UNSIGNED_SHORT
156: .getName())) {
157: returnType = new UnsignedShortType();
158: } else if (typeName.equals(Datatype.Kind.UNSIGNED_BYTE
159: .getName())) {
160: returnType = new UnsignedByteType();
161: } else if (typeName.equals(Datatype.Kind.POSITIVE_INTEGER
162: .getName())) {
163: returnType = new PositiveIntegerType();
164: } else if (typeName
165: .equals(Datatype.Kind.DURATION.getName())) {
166: returnType = new DurationType();
167: } else if (typeName.equals(Datatype.Kind.DATE_TIME
168: .getName())) {
169: returnType = new DateTimeType();
170: } else if (typeName.equals(Datatype.Kind.TIME.getName())) {
171: returnType = new TimeType();
172: } else if (typeName.equals(Datatype.Kind.DATE.getName())) {
173: returnType = new DateType();
174: } else if (typeName.equals(Datatype.Kind.G_YEAR_MONTH
175: .getName())) {
176: returnType = new GYearMonthType();
177: } else if (typeName.equals(Datatype.Kind.G_YEAR.getName())) {
178: returnType = new GYearType();
179: } else if (typeName.equals(Datatype.Kind.G_MONTH_DAY
180: .getName())) {
181: returnType = new GMonthDayType();
182: } else if (typeName.equals(Datatype.Kind.G_DAY.getName())) {
183: returnType = new GDayType();
184: } else if (typeName.equals(Datatype.Kind.G_MONTH.getName())) {
185: returnType = new GMonthType();
186: } else if (typeName.equals(Datatype.Kind.BOOLEAN.getName())) {
187: returnType = new BooleanType();
188: } else if (typeName.equals(Datatype.Kind.BASE64_BINARY
189: .getName())) {
190: returnType = new Base64BinaryType();
191: } else if (typeName.equals(Datatype.Kind.HEX_BINARY
192: .getName())) {
193: returnType = new HexBinaryType();
194: } else if (typeName.equals(Datatype.Kind.FLOAT.getName())) {
195: returnType = new FloatType();
196: } else if (typeName.equals(Datatype.Kind.DOUBLE.getName())) {
197: returnType = new DoubleType();
198: } else if (typeName.equals(Datatype.Kind.ANYURI.getName())) {
199: returnType = new AnyURIType();
200: } else if (typeName.equals(Datatype.Kind.ANYTYPE.getName())) {
201: returnType = new AnyType();
202: } else if (typeName.equals(Datatype.Kind.QNAME.getName())) {
203: returnType = new QNameType();
204: } else if (typeName
205: .equals(Datatype.Kind.NOTATION.getName())) {
206: returnType = new NotationType();
207: }
208: }
209: return returnType;
210: }
211:
212: /**
213: * Creates an AXI Datatype, given a schema component.
214: */
215: public Datatype getDatatype(AXIModel axiModel,
216: SchemaComponent component) {
217: DatatypeBuilder builder = new DatatypeBuilder(axiModel);
218: return builder.getDatatype(component);
219: }
220:
221: static GlobalSimpleType getPrimitiveType(String typeName) {
222: SchemaModel primitiveModel = SchemaModelFactory.getDefault()
223: .getPrimitiveTypesModel();
224: Collection<GlobalSimpleType> primitives = primitiveModel
225: .getSchema().getSimpleTypes();
226: for (GlobalSimpleType ptype : primitives) {
227: if (ptype.getName().equals(typeName)) {
228: return ptype;
229: }
230: }
231: return null;
232: }
233: }
|