KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > vfs > metadata > range > RangeFactory


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.vfs.metadata.range;
20
21 import org.openharmonise.commons.xml.*;
22 import org.openharmonise.vfs.metadata.*;
23 import org.w3c.dom.Element JavaDoc;
24 import org.w3c.dom.Node JavaDoc;
25 import org.w3c.dom.NodeList JavaDoc;
26
27
28 /**
29  * The RangeFactory creates the correct range type from range XML.
30  *
31  * @author Matthew Large
32  * @version $Revision: 1.1 $
33  *
34  */

35 public class RangeFactory {
36
37     /**
38      *
39      */

40     private RangeFactory() {
41         super();
42     }
43     
44     /**
45      * Returns a range for a give range XML element
46      *
47      * @param elRange Root range XML element
48      * @return Range
49      */

50     public static Range getRange(Element JavaDoc elRange) {
51         
52         Range range = null;
53         
54         Element JavaDoc elChild = null;
55         NodeList JavaDoc nl = elRange.getChildNodes();
56         for(int i=0; i<nl.getLength(); i++) {
57             if(nl.item(i).getNodeType()==Node.ELEMENT_NODE) {
58                 elChild = (Element JavaDoc)nl.item(i);
59                 break;
60             }
61         }
62         
63         
64         if( elChild.getLocalName().equalsIgnoreCase("restriction")) {
65             Element JavaDoc elRestriction = elChild;
66             String JavaDoc sType = elRestriction.getAttributeNS("http://www.w3.org/2001/XMLSchema", "base");
67             if( sType!=null && sType.equalsIgnoreCase( elRestriction.getPrefix() + ":string")) {
68                 range = new StringRange();
69             } else if( sType!=null && sType.equalsIgnoreCase( elRestriction.getPrefix() + ":date")) {
70                 range = new DateRange();
71             } else if( sType!=null && sType.equalsIgnoreCase( elRestriction.getPrefix() + ":dateTime")) {
72                 range = new DateTimeRange();
73             } else if( sType!=null && sType.equalsIgnoreCase( elRestriction.getPrefix() + ":integer")) {
74                 range = new IntegerRange();
75             } else if( sType!=null && sType.equalsIgnoreCase( elRestriction.getPrefix() + ":float")) {
76                 range = new FloatRange();
77             } else if( sType!=null && sType.equalsIgnoreCase( elRestriction.getPrefix() + ":boolean")) {
78                 range = new BooleanRange();
79             } else if( sType!=null && sType.equalsIgnoreCase( elRestriction.getPrefix() + ":anyURI")) {
80                 range = new URIRange();
81             }
82         } else {
83             Element JavaDoc elResourceType = XMLUtils.getFirstElementChild( XMLUtils.getFirstNamedChild(elRange, "resourcetype") );
84             if( elResourceType.getLocalName().equalsIgnoreCase("resource")) {
85                 range = new ResourceRange();
86             } else if( elResourceType.getLocalName().equalsIgnoreCase("value")) {
87                 range = new ValueRange();
88             } else if( elResourceType.getLocalName().equalsIgnoreCase("property-resource")) {
89                 range = new PropertyRange();
90             } else if( elResourceType.getLocalName().equalsIgnoreCase("collection")) {
91                 range = new CollectionRange();
92             }
93         }
94         
95         if(range!=null) {
96             range.instantiate(elRange);
97         }
98         
99         return range;
100     }
101
102 }
103
Popular Tags