KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > datatypes > TypeMapper


1 /******************************************************************
2  * File: TypeMapper.java
3  * Created by: Dave Reynolds
4  * Created on: 07-Dec-02
5  *
6  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: TypeMapper.java,v 1.7 2005/02/21 12:01:51 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.datatypes;
11
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15 import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
16 import com.hp.hpl.jena.datatypes.xsd.impl.XMLLiteralType;
17 import com.hp.hpl.jena.shared.impl.JenaParameters;
18
19 /**
20  * The TypeMapper provides a global registry of known datatypes.
21  * The datatypes can be retrieved by their URI or from the java class
22  * that is used to represent them.
23  *
24  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
25  * @version $Revision: 1.7 $ on $Date: 2005/02/21 12:01:51 $
26  */

27 public class TypeMapper {
28
29 //=======================================================================
30
// Statics
31

32     /**
33      * Return the single global instance of the TypeMapper.
34      * Done this way rather than simply making the static
35      * field directly accessible to allow us to dynamically
36      * replace the entire mapper table if needed.
37      */

38     public static TypeMapper getInstance() {
39         return theTypeMap;
40     }
41     
42     /**
43      * The single global instance of the TypeMapper
44      */

45     private static TypeMapper theTypeMap;
46     
47     /**
48      * Static initializer. Adds builtin datatypes to the mapper.
49      */

50     static {
51         theTypeMap = new TypeMapper();
52         theTypeMap.registerDatatype(XMLLiteralType.theXMLLiteralType);
53         XSDDatatype.loadXSDSimpleTypes(theTypeMap);
54     }
55     
56 //=======================================================================
57
// Variables
58

59     /** Map from uri to datatype */
60     private HashMap JavaDoc uriToDT = new HashMap JavaDoc();
61     
62     /** Map from java class to datatype */
63     private HashMap JavaDoc classToDT = new HashMap JavaDoc();
64     
65 //=======================================================================
66
// Methods
67

68
69     /**
70      * Version of getTypeByName which will treat unknown URIs as typed
71      * literals but with just the default implementation
72      *
73      * @param uri the URI of the desired datatype
74      * @return Datatype the datatype definition
75      * registered at uri, if there is no such registered type it
76      * returns a new instance of the default datatype implementation, if the
77      * uri is null it returns null (indicating a plain RDF literal).
78      */

79     public RDFDatatype getSafeTypeByName(String JavaDoc uri) {
80         RDFDatatype dtype = (RDFDatatype) uriToDT.get(uri);
81         if (dtype == null) {
82             if (uri == null) {
83                 // Plain literal
84
return null;
85             } else {
86                 // Uknown datatype
87
if (JenaParameters.enableSilentAcceptanceOfUnknownDatatypes) {
88                     dtype = new BaseDatatype(uri);
89                     registerDatatype(dtype);
90                 } else {
91                     throw new DatatypeFormatException(
92                         "Attempted to created typed literal using an unknown datatype - " + uri);
93                 }
94             }
95         }
96         return dtype;
97     }
98     
99     /**
100      * Lookup a known datatype. An unkown datatype or a datatype with uri null
101      * will return null will mean that the value will be treated as a old-style plain literal.
102      *
103      * @param uri the URI of the desired datatype
104      * @return Datatype the datatype definition of null if not known.
105      */

106     public RDFDatatype getTypeByName(String JavaDoc uri) {
107         return (RDFDatatype) uriToDT.get(uri);
108     }
109     
110     /**
111      * Method getTypeByValue. Look up a datatype suitable for representing
112      * the given java value object.
113      *
114      * @param value a value instance to be represented
115      * @return Datatype a datatype whose value space matches the java class
116      * of <code>value</code>
117      */

118     public RDFDatatype getTypeByValue(Object JavaDoc value) {
119         return (RDFDatatype) classToDT.get(value.getClass());
120     }
121     
122     /**
123      * List all the known datatypes
124      */

125     public Iterator JavaDoc listTypes() {
126         return uriToDT.values().iterator();
127     }
128     
129     /**
130      * Register a new datatype
131      */

132     public void registerDatatype(RDFDatatype type) {
133         uriToDT.put(type.getURI(), type);
134         Class JavaDoc jc = type.getJavaClass();
135         if (jc != null) {
136             classToDT.put(jc, type);
137         }
138     }
139
140     // Temporary development code
141
public static void main(String JavaDoc[] args) {
142         for (Iterator JavaDoc iter = theTypeMap.listTypes(); iter.hasNext();) {
143             RDFDatatype dt = (RDFDatatype) iter.next();
144             System.out.println(" - " + dt);
145         }
146     }
147 }
148
149 /*
150     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
151     All rights reserved.
152
153     Redistribution and use in source and binary forms, with or without
154     modification, are permitted provided that the following conditions
155     are met:
156
157     1. Redistributions of source code must retain the above copyright
158        notice, this list of conditions and the following disclaimer.
159
160     2. Redistributions in binary form must reproduce the above copyright
161        notice, this list of conditions and the following disclaimer in the
162        documentation and/or other materials provided with the distribution.
163
164     3. The name of the author may not be used to endorse or promote products
165        derived from this software without specific prior written permission.
166
167     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
168     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
169     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
170     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
171     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
172     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
173     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
174     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
175     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
176     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
177 */

178
Popular Tags