001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.vfs.metadata.range;
020:
021: import org.openharmonise.commons.xml.*;
022: import org.openharmonise.vfs.metadata.*;
023: import org.w3c.dom.Element;
024: import org.w3c.dom.Node;
025: import org.w3c.dom.NodeList;
026:
027: /**
028: * The RangeFactory creates the correct range type from range XML.
029: *
030: * @author Matthew Large
031: * @version $Revision: 1.1 $
032: *
033: */
034: public class RangeFactory {
035:
036: /**
037: *
038: */
039: private RangeFactory() {
040: super ();
041: }
042:
043: /**
044: * Returns a range for a give range XML element
045: *
046: * @param elRange Root range XML element
047: * @return Range
048: */
049: public static Range getRange(Element elRange) {
050:
051: Range range = null;
052:
053: Element elChild = null;
054: NodeList nl = elRange.getChildNodes();
055: for (int i = 0; i < nl.getLength(); i++) {
056: if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
057: elChild = (Element) nl.item(i);
058: break;
059: }
060: }
061:
062: if (elChild.getLocalName().equalsIgnoreCase("restriction")) {
063: Element elRestriction = elChild;
064: String sType = elRestriction.getAttributeNS(
065: "http://www.w3.org/2001/XMLSchema", "base");
066: if (sType != null
067: && sType.equalsIgnoreCase(elRestriction.getPrefix()
068: + ":string")) {
069: range = new StringRange();
070: } else if (sType != null
071: && sType.equalsIgnoreCase(elRestriction.getPrefix()
072: + ":date")) {
073: range = new DateRange();
074: } else if (sType != null
075: && sType.equalsIgnoreCase(elRestriction.getPrefix()
076: + ":dateTime")) {
077: range = new DateTimeRange();
078: } else if (sType != null
079: && sType.equalsIgnoreCase(elRestriction.getPrefix()
080: + ":integer")) {
081: range = new IntegerRange();
082: } else if (sType != null
083: && sType.equalsIgnoreCase(elRestriction.getPrefix()
084: + ":float")) {
085: range = new FloatRange();
086: } else if (sType != null
087: && sType.equalsIgnoreCase(elRestriction.getPrefix()
088: + ":boolean")) {
089: range = new BooleanRange();
090: } else if (sType != null
091: && sType.equalsIgnoreCase(elRestriction.getPrefix()
092: + ":anyURI")) {
093: range = new URIRange();
094: }
095: } else {
096: Element elResourceType = XMLUtils
097: .getFirstElementChild(XMLUtils.getFirstNamedChild(
098: elRange, "resourcetype"));
099: if (elResourceType.getLocalName().equalsIgnoreCase(
100: "resource")) {
101: range = new ResourceRange();
102: } else if (elResourceType.getLocalName().equalsIgnoreCase(
103: "value")) {
104: range = new ValueRange();
105: } else if (elResourceType.getLocalName().equalsIgnoreCase(
106: "property-resource")) {
107: range = new PropertyRange();
108: } else if (elResourceType.getLocalName().equalsIgnoreCase(
109: "collection")) {
110: range = new CollectionRange();
111: }
112: }
113:
114: if (range != null) {
115: range.instantiate(elRange);
116: }
117:
118: return range;
119: }
120:
121: }
|