001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-impl/api-impl/src/java/org/sakaiproject/metaobj/utils/xml/impl/ElementTypeFactory.java $
003: * $Id: ElementTypeFactory.java 14225 2006-09-05 17:39:44Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.metaobj.utils.xml.impl;
021:
022: import org.jdom.Element;
023: import org.jdom.Namespace;
024: import org.sakaiproject.metaobj.utils.xml.SchemaNode;
025:
026: /**
027: * Created by IntelliJ IDEA.
028: * User: John Ellis
029: * Date: Apr 15, 2004
030: * Time: 6:02:54 PM
031: * To change this template use File | Settings | File Templates.
032: */
033: public class ElementTypeFactory {
034:
035: private static ElementTypeFactory instance = new ElementTypeFactory();
036:
037: private static final String INT_TYPES = "" + " xs:byte" + //A signed 8-bit integer \n" +
038: " xs:int" + //A signed 32-bit integer \n" +
039: " xs:integer" + //An integer value \n" +
040: " xs:long" + //A signed 64-bit integer \n" +
041: " xs:negativeInteger" + //An integer containing only negative values ( .., -2, -1.) \n" +
042: " xs:nonNegativeInteger" + //An integer containing only non-negative values (0, 1, 2, ..) \n" +
043: " xs:nonPositiveInteger" + //An integer containing only non-positive values (.., -2, -1, 0) \n" +
044: " xs:positiveInteger" + //An integer containing only positive values (1, 2, ..) \n" +
045: " xs:short" + //A signed 16-bit integer \n" +
046: " xs:unsignedLong" + //An unsigned 64-bit integer \n" +
047: " xs:unsignedInt" + //An unsigned 32-bit integer \n" +
048: " xs:unsignedShort" + //An unsigned 16-bit integer \n" +
049: " xs:unsignedByte"; //An unsigned 8-bit integer";
050:
051: private static final String DECIMAL_TYPES = ""
052: + " xs:decimal";
053:
054: private static final String BOOLEAN_TYPES = ""
055: + " xs:boolean";
056:
057: private static final String DATE_TYPES = "" + " xs:date"
058: + " xs:time" + " xs:dateTime";
059:
060: private static final String URI_TYPES = "" + " xs:anyURI";
061:
062: protected ElementTypeFactory() {
063:
064: }
065:
066: public static ElementTypeFactory getInstance() {
067: return instance;
068: }
069:
070: public BaseElementType createElementType(Element schemaElement,
071: SchemaNode parentNode, Namespace xsdNamespace) {
072: String typeName = "unknown";
073:
074: if (schemaElement.getAttributeValue("type") != null) {
075: typeName = schemaElement.getAttributeValue("type");
076: } else {
077: Element simpleType = schemaElement.getChild("simpleType",
078: xsdNamespace);
079: if (simpleType == null) {
080: simpleType = schemaElement.getChild("simpleContent",
081: xsdNamespace);
082: }
083:
084: if (simpleType != null) {
085: Element restrictions = simpleType.getChild(
086: "restriction", xsdNamespace);
087: if (restrictions != null) {
088: typeName = restrictions.getAttributeValue("base");
089: } else {
090: typeName = simpleType.getChild("extension",
091: xsdNamespace).getAttributeValue("base");
092: }
093: }
094: }
095:
096: if (INT_TYPES.indexOf(typeName) != -1) {
097: return new NumberElementType(typeName, schemaElement,
098: parentNode, xsdNamespace).postInit(xsdNamespace);
099: } else if (DECIMAL_TYPES.indexOf(typeName) != -1) {
100: return new DecimalElementType(typeName, schemaElement,
101: parentNode, xsdNamespace).postInit(xsdNamespace);
102: } else if (BOOLEAN_TYPES.indexOf(typeName) != -1) {
103: return new BooleanElementType(typeName, schemaElement,
104: parentNode, xsdNamespace).postInit(xsdNamespace);
105: } else if (DATE_TYPES.indexOf(typeName) != -1) {
106: return new DateFormatterElementType(typeName,
107: schemaElement, parentNode, xsdNamespace)
108: .postInit(xsdNamespace);
109: } else if (URI_TYPES.indexOf(typeName) != -1) {
110: return new UriElementType(typeName, schemaElement,
111: parentNode, xsdNamespace).postInit(xsdNamespace);
112: }
113:
114: // default to BaseElementType
115: return new BaseElementType(typeName, schemaElement, parentNode,
116: xsdNamespace).postInit(xsdNamespace);
117: }
118:
119: }
|