1 10 package com.hp.hpl.jena.datatypes; 11 12 import java.util.HashMap ; 13 import java.util.Iterator ; 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 27 public class TypeMapper { 28 29 32 38 public static TypeMapper getInstance() { 39 return theTypeMap; 40 } 41 42 45 private static TypeMapper theTypeMap; 46 47 50 static { 51 theTypeMap = new TypeMapper(); 52 theTypeMap.registerDatatype(XMLLiteralType.theXMLLiteralType); 53 XSDDatatype.loadXSDSimpleTypes(theTypeMap); 54 } 55 56 59 60 private HashMap uriToDT = new HashMap (); 61 62 63 private HashMap classToDT = new HashMap (); 64 65 68 69 79 public RDFDatatype getSafeTypeByName(String uri) { 80 RDFDatatype dtype = (RDFDatatype) uriToDT.get(uri); 81 if (dtype == null) { 82 if (uri == null) { 83 return null; 85 } else { 86 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 106 public RDFDatatype getTypeByName(String uri) { 107 return (RDFDatatype) uriToDT.get(uri); 108 } 109 110 118 public RDFDatatype getTypeByValue(Object value) { 119 return (RDFDatatype) classToDT.get(value.getClass()); 120 } 121 122 125 public Iterator listTypes() { 126 return uriToDT.values().iterator(); 127 } 128 129 132 public void registerDatatype(RDFDatatype type) { 133 uriToDT.put(type.getURI(), type); 134 Class jc = type.getJavaClass(); 135 if (jc != null) { 136 classToDT.put(jc, type); 137 } 138 } 139 140 public static void main(String [] args) { 142 for (Iterator iter = theTypeMap.listTypes(); iter.hasNext();) { 143 RDFDatatype dt = (RDFDatatype) iter.next(); 144 System.out.println(" - " + dt); 145 } 146 } 147 } 148 149 178 | Popular Tags |