use of org.apache.jena.datatypes.DatatypeFormatException in project jena by apache.
the class Rational method parse.
/**
* Parse a lexical form of this datatype to a value
* @throws DatatypeFormatException if the lexical form is not legal
*/
@Override
public Object parse(String lexicalForm) throws DatatypeFormatException {
int index = lexicalForm.indexOf("/");
if (index == -1) {
throw new DatatypeFormatException(lexicalForm, theRationalType, "");
}
try {
int numerator = Integer.parseInt(lexicalForm.substring(0, index));
int denominator = Integer.parseInt(lexicalForm.substring(index + 1));
return new Rational(numerator, denominator);
} catch (NumberFormatException e) {
throw new DatatypeFormatException(lexicalForm, theRationalType, "");
}
}
use of org.apache.jena.datatypes.DatatypeFormatException in project jena by apache.
the class Rational method testUnknown.
/**
* Test the base functioning of unknown datatypes
*/
public void testUnknown() {
String typeURI = "urn:x-hp-dt:unknown";
String typeURI2 = "urn:x-hp-dt:unknown2";
boolean originalFlag = JenaParameters.enableSilentAcceptanceOfUnknownDatatypes;
JenaParameters.enableSilentAcceptanceOfUnknownDatatypes = true;
Literal l1 = m.createTypedLiteral("foo", typeURI);
Literal l3 = m.createTypedLiteral("15", typeURI);
Literal l5 = m.createTypedLiteral("foo", typeURI2);
Literal l6 = m.createLiteral("foo", "lang1");
JenaParameters.enableSilentAcceptanceOfUnknownDatatypes = originalFlag;
// Check for successful creation
assertNotNull(l1);
assertNotNull(l3);
assertNotNull(l5);
// check equality function
assertDiffer("datatype sensitive", l1, l5);
assertDiffer("value sensitive", l1, l3);
assertDiffer("typed and plain differ", l1, l6);
// Check typed accessors
try {
l3.getInt();
assertTrue("Allowed int conversion", false);
} catch (DatatypeFormatException e) {
}
assertEquals("Extract value", l1.getValue(), new BaseDatatype.TypedValue("foo", typeURI));
assertEquals("Extract xml tag", l1.isWellFormedXML(), false);
JenaParameters.enableSilentAcceptanceOfUnknownDatatypes = false;
boolean foundException = false;
try {
m.createTypedLiteral("food", typeURI + "3");
} catch (DatatypeFormatException e2) {
foundException = true;
}
JenaParameters.enableSilentAcceptanceOfUnknownDatatypes = originalFlag;
assertTrue("Detected unknown datatype", foundException);
// Check we can create a literal of an unregistered java type without anything blowing up
Object foo = new java.sql.Date(123456l);
LiteralLabel ll = LiteralLabelFactory.createTypedLiteral(foo);
assertEquals(ll.getLexicalForm(), foo.toString());
}
use of org.apache.jena.datatypes.DatatypeFormatException in project jena by apache.
the class XSDDatatype method parse.
/**
* Parse a lexical form of this datatype to a value
* @throws DatatypeFormatException if the lexical form is not legal
*/
@Override
public Object parse(String lexicalForm) throws DatatypeFormatException {
try {
ValidationContext context = new ValidationState();
ValidatedInfo resultInfo = new ValidatedInfo();
typeDeclaration.validate(lexicalForm, context, resultInfo);
return convertValidatedDataValue(resultInfo);
} catch (InvalidDatatypeValueException e) {
throw new DatatypeFormatException(lexicalForm, this, "during parse -" + e);
}
}
use of org.apache.jena.datatypes.DatatypeFormatException in project jena by apache.
the class Rational method testUserDef.
/**
* Tests the base functioning of a user defined datatype
*/
public void testUserDef() {
// Register the user defined type for rationals
RDFDatatype rtype = RationalType.theRationalType;
TypeMapper.getInstance().registerDatatype(rtype);
Literal l1 = m.createTypedLiteral("3/5", rtype);
Literal l3 = m.createTypedLiteral("7/5", rtype);
// Check for successful creation
assertNotNull(l1);
assertNotNull(l3);
// check equality function
assertDiffer("values should be tested!", l1, l3);
// Check typed accessors
assertSame("Datatype incorrect", l1.getDatatype(), rtype);
assertEquals("Datatype uri incorrect", l1.getDatatypeURI(), RationalType.theTypeURI);
Object val = l1.getValue();
assertTrue("Value space check", val instanceof Rational);
assertTrue("Value check", ((Rational) val).getNumerator() == 3);
assertTrue("Value check", ((Rational) val).getDenominator() == 5);
try {
l1.getInt();
assertTrue("Allowed int conversion", false);
} catch (DatatypeFormatException e) {
}
assertEquals("Extract xml tag", l1.isWellFormedXML(), false);
}
Aggregations