[Mulgara-svn] r889 - trunk/src/jar/query/java/org/mulgara/query/filter/value
pag at mulgara.org
pag at mulgara.org
Thu May 1 18:06:29 UTC 2008
Author: pag
Date: 2008-05-01 11:06:28 -0700 (Thu, 01 May 2008)
New Revision: 889
Modified:
trunk/src/jar/query/java/org/mulgara/query/filter/value/TypedLiteral.java
Log:
Creating a literal can now throw a QueryException due to casting incompatible types. This is reflected in the numeric types which now throw QueryException instead of returning 0 for unparsable values. Added casting operations for the numeric types. Finally, added in support for the xsd: prefix when the XSD namespace has been ommited, via inclusion of XSDAbbrev
Modified: trunk/src/jar/query/java/org/mulgara/query/filter/value/TypedLiteral.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/filter/value/TypedLiteral.java 2008-05-01 18:01:33 UTC (rev 888)
+++ trunk/src/jar/query/java/org/mulgara/query/filter/value/TypedLiteral.java 2008-05-01 18:06:28 UTC (rev 889)
@@ -12,14 +12,15 @@
package org.mulgara.query.filter.value;
import java.net.URI;
-import java.util.ArrayList;
+import java.util.Date;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
import org.mulgara.query.QueryException;
import org.mulgara.query.filter.Context;
import org.mulgara.query.filter.ContextOwner;
+import org.mulgara.query.rdf.XSD;
+import org.mulgara.query.rdf.XSDAbbrev;
/**
@@ -33,7 +34,7 @@
public class TypedLiteral extends AbstractComparableLiteral {
/** Generated Serialization ID for RMI */
- private static final long serialVersionUID = -4765911650373775807L;
+ private static final long serialVersionUID = -6070455650703063913L;
/** The type URI for this literal */
private URI type;
@@ -41,9 +42,6 @@
/** The lexical representation for this literal */
private String lexical;
- /** The namespace for all XSD types */
- public static final String XSD_NS = "http://www.w3.org/2001/XMLSchema#";
-
/**
* Creates the value to wrap the string
* @param value The data to wrap
@@ -61,17 +59,11 @@
* @param type The type of the literal. May be null.
* @param lang The language code of the literal. May be null.
*/
- public static ValueLiteral newLiteral(String value, URI type, String lang) {
+ public static ValueLiteral newLiteral(String value, URI type, String lang) throws QueryException {
if (type != null) {
// get the info registered for this type URI
TypeInfo info = infoMap.get(type);
- if (info != null && info.delegatedConstruction()) {
- try {
- return newLiteral(info.toData(value));
- } catch (QueryException e) { // should not happen
- throw new AssertionError("Internal type maps are inconsistent for: " + info.getClass().getSimpleName());
- }
- }
+ if (info != null) return info.newLiteral(info.toData(value));
// no type info for the given URI, just pass through as a general typed literal
return new TypedLiteral(value, type);
}
@@ -146,31 +138,46 @@
return test.ebv(value.toString());
}
- /** A map of XSD datatypes onto the tests for their types */
- private static Map<URI,TypeInfo> infoMap = new HashMap<URI,TypeInfo>();
+ ///////////////////////////////////////////////////////////////////////////////////
+ // Maps to data specific functionality, and supporting functions
+ ///////////////////////////////////////////////////////////////////////////////////
+
+ /** A map of XSD datatypes onto the data conversion operations for their types */
+ protected static Map<URI,TypeInfo> infoMap = new HashMap<URI,TypeInfo>();
- private static final URI xsd(String s) { return URI.create(XSD_NS + s); }
-
/** This interface tests if datatype matches the data to give an EBV of <code>true</code> */
public interface TypeInfo {
/** Returns an EBV of <code>true</code> iff the data matches the type sufficiently */
public boolean ebv(String data) throws QueryException;
/** Returns data parsed out of the string literal */
- public Object toData(String representation);
+ public Object toData(String representation) throws QueryException;
/** Returns the URI for this type */
public URI getTypeURI();
- /** Indicates if construction should be delegated */
- public boolean delegatedConstruction();
- /** All the registered types */
- static final List<TypeInfo> types = new ArrayList<TypeInfo>();
+ /** Returns the abbreviated URI for this type, in the absence of a namespace. */
+ public URI getTypeURI2();
+ /**
+ * Creates a new ValueLiteral compatible for this data
+ * @param data The data in the correct native {@link java.lang.Class} to be converted.
+ * @return A new literal containing the data.
+ */
+ public ValueLiteral newLiteral(Object data);
+ /**
+ * Get the value of a given number, according to the type of the implementing class.
+ * @param n The number to convert.
+ * @return The new Number of the required type.
+ */
+ public Number valueOf(Number n);
}
/** Simple extension to TypeInfo to store the type URI for all implementing classes */
private static abstract class AbstractXSD implements TypeInfo {
private final URI typeURI;
- AbstractXSD(String fragment) { typeURI = xsd(fragment); }
+ private final URI typeURI2;
+ protected AbstractXSD(URI type, URI type2) { typeURI = type; typeURI2 = type2; }
public URI getTypeURI() { return typeURI; }
- public boolean delegatedConstruction() { return true; }
+ public URI getTypeURI2() { return typeURI2; }
+ public Number valueOf(Number n) { throw new UnsupportedOperationException("Numeric casts only applicable to numbers"); }
+ public ValueLiteral newLiteral(Object data) { throw new UnsupportedOperationException(); }
}
/**
@@ -179,29 +186,40 @@
*/
private static void addDefaultTypeInfo(TypeInfo info) {
infoMap.put(info.getTypeURI(), info);
+ infoMap.put(info.getTypeURI2(), info);
}
// initialize the types
static {
- addDefaultTypeInfo(new XSDString());
- addDefaultTypeInfo(new XSDBoolean());
- addDefaultTypeInfo(new XSDDouble());
- addDefaultTypeInfo(new XSDFloat());
- addDefaultTypeInfo(new XSDLong());
- addDefaultTypeInfo(new XSDInteger());
- addDefaultTypeInfo(new XSDShort());
- addDefaultTypeInfo(new XSDByte());
- addDefaultTypeInfo(new XSDDate());
- infoMap.put(xsd("decimal"), new XSDLong());
- infoMap.put(xsd("integer"), new XSDLong());
- infoMap.put(xsd("nonPositiveInteger"), new XSDLong());
- infoMap.put(xsd("negativeInteger"), new XSDLong());
- infoMap.put(xsd("nonNegativeInteger"), new XSDLong());
- infoMap.put(xsd("positiveInteger"), new XSDInteger());
- infoMap.put(xsd("unsignedLong"), new XSDLong());
- infoMap.put(xsd("unsignedInt"), new XSDLong());
- infoMap.put(xsd("unsignedShort"), new XSDInteger());
- infoMap.put(xsd("unsignedByte"), new XSDShort());
+ addDefaultTypeInfo(XSDString.INSTANCE);
+ addDefaultTypeInfo(XSDBoolean.INSTANCE);
+ addDefaultTypeInfo(XSDDouble.INSTANCE);
+ addDefaultTypeInfo(XSDFloat.INSTANCE);
+ addDefaultTypeInfo(XSDLong.INSTANCE);
+ addDefaultTypeInfo(XSDInteger.INSTANCE);
+ addDefaultTypeInfo(XSDShort.INSTANCE);
+ addDefaultTypeInfo(XSDByte.INSTANCE);
+ addDefaultTypeInfo(XSDDate.INSTANCE);
+ infoMap.put(XSD.DECIMAL_URI, XSDLong.INSTANCE);
+ infoMap.put(XSD.INTEGER_URI, XSDLong.INSTANCE);
+ infoMap.put(XSD.NON_POSITIVE_INTEGER_URI, XSDLong.INSTANCE);
+ infoMap.put(XSD.NEGATIVE_INTEGER_URI, XSDLong.INSTANCE);
+ infoMap.put(XSD.NON_NEGATIVE_INTEGER_URI, XSDLong.INSTANCE);
+ infoMap.put(XSD.POSITIVE_INTEGER_URI, XSDInteger.INSTANCE);
+ infoMap.put(XSD.UNSIGNED_LONG_URI, XSDLong.INSTANCE);
+ infoMap.put(XSD.UNSIGNED_INT_URI, XSDLong.INSTANCE);
+ infoMap.put(XSD.UNSIGNED_SHORT_URI, XSDInteger.INSTANCE);
+ infoMap.put(XSD.UNSIGNED_BYTE_URI, XSDShort.INSTANCE);
+ infoMap.put(XSDAbbrev.DECIMAL_URI, XSDLong.INSTANCE);
+ infoMap.put(XSDAbbrev.INTEGER_URI, XSDLong.INSTANCE);
+ infoMap.put(XSDAbbrev.NON_POSITIVE_INTEGER_URI, XSDLong.INSTANCE);
+ infoMap.put(XSDAbbrev.NEGATIVE_INTEGER_URI, XSDLong.INSTANCE);
+ infoMap.put(XSDAbbrev.NON_NEGATIVE_INTEGER_URI, XSDLong.INSTANCE);
+ infoMap.put(XSDAbbrev.POSITIVE_INTEGER_URI, XSDInteger.INSTANCE);
+ infoMap.put(XSDAbbrev.UNSIGNED_LONG_URI, XSDLong.INSTANCE);
+ infoMap.put(XSDAbbrev.UNSIGNED_INT_URI, XSDLong.INSTANCE);
+ infoMap.put(XSDAbbrev.UNSIGNED_SHORT_URI, XSDInteger.INSTANCE);
+ infoMap.put(XSDAbbrev.UNSIGNED_BYTE_URI, XSDShort.INSTANCE);
}
//////////////////////////////////////////////////////////////////
@@ -209,20 +227,24 @@
//////////////////////////////////////////////////////////////////
static class XSDString extends AbstractXSD {
- XSDString() { super("string"); }
+ public static final XSDString INSTANCE = new XSDString();
+ private XSDString() { super(XSD.STRING_URI, XSDAbbrev.STRING_URI); }
public boolean ebv(String data) { return data != null && data.length() != 0; }
public Object toData(String r) { return r; }
- public boolean delegatedConstruction() { return false; }
+ public ValueLiteral newLiteral(Object data) { return new TypedLiteral((String)data, getTypeURI()); }
}
private static class XSDBoolean extends AbstractXSD {
- XSDBoolean() { super("boolean"); }
+ public static final XSDBoolean INSTANCE = new XSDBoolean();
+ private XSDBoolean() { super(XSD.BOOLEAN_URI, XSDAbbrev.BOOLEAN_URI); }
public boolean ebv(String data) { return Boolean.parseBoolean(data); }
public Object toData(String r) { return Boolean.parseBoolean(r); }
+ public ValueLiteral newLiteral(Object data) { return new Bool((Boolean)data); }
}
private static class XSDDouble extends AbstractXSD {
- XSDDouble() { super("double"); }
+ public static final XSDDouble INSTANCE = new XSDDouble();
+ private XSDDouble() { super(XSD.DOUBLE_URI, XSDAbbrev.DOUBLE_URI); }
public boolean ebv(String data) {
try {
if (data == null) return false;
@@ -232,17 +254,22 @@
return false;
}
}
- public Object toData(String r) {
+ public Object toData(String r) throws QueryException {
try {
return Double.parseDouble(r);
} catch (NumberFormatException nfe) {
- return Double.valueOf(0.0);
+ throw new QueryException("Type Error: Cannot convert to a Double: " + r);
}
}
+ public ValueLiteral newLiteral(Object data) {
+ return new NumericLiteral((Double)data, getTypeURI());
+ }
+ public Number valueOf(Number n) { return n.doubleValue(); }
}
private static class XSDFloat extends AbstractXSD {
- XSDFloat() { super("float"); }
+ public static final XSDFloat INSTANCE = new XSDFloat();
+ private XSDFloat() { super(XSD.FLOAT_URI, XSDAbbrev.FLOAT_URI); }
public boolean ebv(String data) {
try {
if (data == null) return false;
@@ -252,17 +279,22 @@
return false;
}
}
- public Object toData(String r) {
+ public Object toData(String r) throws QueryException {
try {
return Float.parseFloat(r);
} catch (NumberFormatException nfe) {
- return Float.valueOf(0);
+ throw new QueryException("Type Error: Cannot convert to a Float: " + r);
}
}
+ public ValueLiteral newLiteral(Object data) {
+ return new NumericLiteral((Float)data, getTypeURI());
+ }
+ public Number valueOf(Number n) { return n.floatValue(); }
}
private static class XSDLong extends AbstractXSD {
- XSDLong() { super("long"); }
+ public static final XSDLong INSTANCE = new XSDLong();
+ private XSDLong() { super(XSD.LONG_URI, XSDAbbrev.LONG_URI); }
public boolean ebv(String data) {
try {
return data != null && 0 != Long.parseLong(data);
@@ -270,17 +302,22 @@
return false;
}
}
- public Object toData(String r) {
+ public Object toData(String r) throws QueryException {
try {
return Long.parseLong(r);
} catch (NumberFormatException nfe) {
- return Long.valueOf(0);
+ throw new QueryException("Type Error: Cannot convert to a Long: " + r);
}
}
+ public ValueLiteral newLiteral(Object data) {
+ return new NumericLiteral((Long)data, getTypeURI());
+ }
+ public Number valueOf(Number n) { return n.longValue(); }
}
private static class XSDInteger extends AbstractXSD {
- XSDInteger() { super("int"); }
+ public static final XSDInteger INSTANCE = new XSDInteger();
+ private XSDInteger() { super(XSD.INT_URI, XSDAbbrev.INT_URI); }
public boolean ebv(String data) {
try {
return data != null && 0 != Integer.parseInt(data);
@@ -288,17 +325,22 @@
return false;
}
}
- public Object toData(String r) {
+ public Object toData(String r) throws QueryException {
try {
return Integer.parseInt(r);
} catch (NumberFormatException nfe) {
- return Integer.valueOf(0);
+ throw new QueryException("Type Error: Cannot convert to an Integer: " + r);
}
}
+ public ValueLiteral newLiteral(Object data) {
+ return new NumericLiteral((Integer)data, getTypeURI());
+ }
+ public Number valueOf(Number n) { return n.intValue(); }
}
private static class XSDShort extends AbstractXSD {
- XSDShort() { super("short"); }
+ public static final XSDShort INSTANCE = new XSDShort();
+ private XSDShort() { super(XSD.SHORT_URI, XSDAbbrev.SHORT_URI); }
public boolean ebv(String data) {
try {
return data != null && 0 != Short.parseShort(data);
@@ -306,17 +348,22 @@
return false;
}
}
- public Object toData(String r) {
+ public Object toData(String r) throws QueryException {
try {
return Short.parseShort(r);
} catch (NumberFormatException nfe) {
- return Short.valueOf((short)0);
+ throw new QueryException("Type Error: Cannot convert to a Short: " + r);
}
}
+ public ValueLiteral newLiteral(Object data) {
+ return new NumericLiteral((Short)data, getTypeURI());
+ }
+ public Number valueOf(Number n) { return n.shortValue(); }
}
private static class XSDByte extends AbstractXSD {
- XSDByte() { super("byte"); }
+ public static final XSDByte INSTANCE = new XSDByte();
+ private XSDByte() { super(XSD.BYTE_URI, XSDAbbrev.BYTE_URI); }
public boolean ebv(String data) {
try {
return data != null && 0 != Byte.parseByte(data);
@@ -324,19 +371,25 @@
return false;
}
}
- public Object toData(String r) {
+ public Object toData(String r) throws QueryException {
try {
return Long.parseLong(r);
} catch (NumberFormatException nfe) {
- return Byte.valueOf((byte)0);
+ throw new QueryException("Type Error: Cannot convert to a Byte: " + r);
}
}
+ public ValueLiteral newLiteral(Object data) {
+ return new NumericLiteral((Byte)data, getTypeURI());
+ }
+ public Number valueOf(Number n) { return n.byteValue(); }
}
private static class XSDDate extends AbstractXSD {
- XSDDate() { super("dateTime"); }
+ public static final XSDDate INSTANCE = new XSDDate();
+ private XSDDate() { super(XSD.DATE_TIME_URI, XSDAbbrev.DATE_TIME_URI); }
public boolean ebv(String data) throws QueryException { throw new QueryException("Unable to convert a date to a boolean"); }
public Object toData(String r) { return DateTime.parseDate(r); }
+ public ValueLiteral newLiteral(Object data) { return new DateTime((Date)data); }
}
}
More information about the Mulgara-svn
mailing list