001: /******************************************************************
002: * File: TypeMapper.java
003: * Created by: Dave Reynolds
004: * Created on: 07-Dec-02
005: *
006: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: TypeMapper.java,v 1.10 2008/01/02 12:10:24 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.datatypes;
010:
011: import java.util.HashMap;
012: import java.util.Iterator;
013:
014: import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
015: import com.hp.hpl.jena.datatypes.xsd.impl.XMLLiteralType;
016: import com.hp.hpl.jena.shared.impl.JenaParameters;
017:
018: /**
019: * The TypeMapper provides a global registry of known datatypes.
020: * The datatypes can be retrieved by their URI or from the java class
021: * that is used to represent them.
022: *
023: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
024: * @version $Revision: 1.10 $ on $Date: 2008/01/02 12:10:24 $
025: */
026: public class TypeMapper {
027:
028: //=======================================================================
029: // Statics
030:
031: /**
032: * Return the single global instance of the TypeMapper.
033: * Done this way rather than simply making the static
034: * field directly accessible to allow us to dynamically
035: * replace the entire mapper table if needed.
036: */
037: public static TypeMapper getInstance() {
038: return theTypeMap;
039: }
040:
041: /**
042: * The single global instance of the TypeMapper
043: */
044: private static TypeMapper theTypeMap;
045:
046: /**
047: * Static initializer. Adds builtin datatypes to the mapper.
048: */
049: static {
050: theTypeMap = new TypeMapper();
051: theTypeMap.registerDatatype(XMLLiteralType.theXMLLiteralType);
052: XSDDatatype.loadXSDSimpleTypes(theTypeMap);
053: }
054:
055: //=======================================================================
056: // Variables
057:
058: /** Map from uri to datatype */
059: private HashMap uriToDT = new HashMap();
060:
061: /** Map from java class to datatype */
062: private HashMap classToDT = new HashMap();
063:
064: //=======================================================================
065: // Methods
066:
067: /**
068: * Version of getTypeByName which will treat unknown URIs as typed
069: * literals but with just the default implementation
070: *
071: * @param uri the URI of the desired datatype
072: * @return Datatype the datatype definition
073: * registered at uri, if there is no such registered type it
074: * returns a new instance of the default datatype implementation, if the
075: * uri is null it returns null (indicating a plain RDF literal).
076: */
077: public RDFDatatype getSafeTypeByName(String uri) {
078: RDFDatatype dtype = (RDFDatatype) uriToDT.get(uri);
079: if (dtype == null) {
080: if (uri == null) {
081: // Plain literal
082: return null;
083: } else {
084: // Uknown datatype
085: if (JenaParameters.enableSilentAcceptanceOfUnknownDatatypes) {
086: dtype = new BaseDatatype(uri);
087: registerDatatype(dtype);
088: } else {
089: throw new DatatypeFormatException(
090: "Attempted to created typed literal using an unknown datatype - "
091: + uri);
092: }
093: }
094: }
095: return dtype;
096: }
097:
098: /**
099: * Lookup a known datatype. An unkown datatype or a datatype with uri null
100: * will return null will mean that the value will be treated as a old-style plain literal.
101: *
102: * @param uri the URI of the desired datatype
103: * @return Datatype the datatype definition of null if not known.
104: */
105: public RDFDatatype getTypeByName(String uri) {
106: return (RDFDatatype) uriToDT.get(uri);
107: }
108:
109: /**
110: * Method getTypeByValue. Look up a datatype suitable for representing
111: * the given java value object.
112: *
113: * @param value a value instance to be represented
114: * @return Datatype a datatype whose value space matches the java class
115: * of <code>value</code>
116: */
117: public RDFDatatype getTypeByValue(Object value) {
118: return (RDFDatatype) classToDT.get(value.getClass());
119: }
120:
121: /**
122: * List all the known datatypes
123: */
124: public Iterator listTypes() {
125: return uriToDT.values().iterator();
126: }
127:
128: /**
129: * Register a new datatype
130: */
131: public void registerDatatype(RDFDatatype type) {
132: uriToDT.put(type.getURI(), type);
133: Class jc = type.getJavaClass();
134: if (jc != null) {
135: classToDT.put(jc, type);
136: }
137: }
138:
139: // Temporary development code
140: public static void main(String[] args) {
141: for (Iterator iter = theTypeMap.listTypes(); iter.hasNext();) {
142: RDFDatatype dt = (RDFDatatype) iter.next();
143: System.out.println(" - " + dt);
144: }
145: }
146: }
147:
148: /*
149: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
150: All rights reserved.
151:
152: Redistribution and use in source and binary forms, with or without
153: modification, are permitted provided that the following conditions
154: are met:
155:
156: 1. Redistributions of source code must retain the above copyright
157: notice, this list of conditions and the following disclaimer.
158:
159: 2. Redistributions in binary form must reproduce the above copyright
160: notice, this list of conditions and the following disclaimer in the
161: documentation and/or other materials provided with the distribution.
162:
163: 3. The name of the author may not be used to endorse or promote products
164: derived from this software without specific prior written permission.
165:
166: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
168: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
169: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
170: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
171: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
172: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
173: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
174: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
175: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
176: */
|