use of org.apache.jena.datatypes.DatatypeFormatException in project jena by apache.
the class XSDDatatype method loadUserDefined.
/**
* Internal implementation of loadUserDefined
*
* @param uri the absolute uri of the schema file to be loaded
* @param reader the Reader stream onto the file (useful if you wish to load a cached copy of the schema file)
* @param encoding the encoding of the source file (can be null)
* @param tm the type mapper into which to load the definitions
* @return a List of strings giving the uri's of the newly defined datatypes
* @throws DatatypeFormatException if there is a problem during load (not that we use Xerces
* in default mode for load which may provide diagnostic output direct to stderr)
*/
private static List<String> loadUserDefined(XMLInputSource source, TypeMapper tm) throws DatatypeFormatException {
XMLGrammarPreparser parser = new XMLGrammarPreparser();
parser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
try {
XSGrammar xsg = (XSGrammar) parser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, source);
org.apache.xerces.xs.XSModel xsm = xsg.toXSModel();
XSNamedMap map = xsm.getComponents(XSTypeDefinition.SIMPLE_TYPE);
int numDefs = map.getLength();
ArrayList<String> names = new ArrayList<>(numDefs);
for (int i = 0; i < numDefs; i++) {
XSSimpleType xstype = (XSSimpleType) map.item(i);
// Filter built in types - only needed for 2.6.0
if (!XSD.equals(xstype.getNamespace())) {
//xstype.derivedFrom()
XSDDatatype definedType = new XSDGenericType(xstype, source.getSystemId());
tm.registerDatatype(definedType);
names.add(definedType.getURI());
}
}
return names;
} catch (Exception e) {
// Temp
e.printStackTrace();
throw new DatatypeFormatException(e.toString());
}
}
use of org.apache.jena.datatypes.DatatypeFormatException in project jena by apache.
the class XSDDateTimeStampType method parse.
@Override
public Object parse(String lex) {
Object obj = super.parse(lex);
// timezoneFrag ::= 'Z' | ('+' | '-') (('0' digit | '1' [0-3]) ':' minuteFrag | '14:00')
if (lex.indexOf('Z') != -1)
return obj;
// Check a legal xsd:dateTime ends with timezoneFrag
// Z or a +/- at length-6
// Avoid regex!
int n = lex.length();
char z = lex.charAt(n - 6);
if (z != '+' && z != '-')
throw new DatatypeFormatException("Not valid as xsd:dateTimeStamp: " + lex);
// || ! RiotChars.isDigit(lex.charAt(n-1)) )
return obj;
// Alternative:
//static Pattern p = Pattern.compile("\\d\\d:\\d\\d$") ;
//return p.matcher(x).find() ;
}
use of org.apache.jena.datatypes.DatatypeFormatException in project jena by apache.
the class XSDDayTimeDurationType method parse.
@Override
public Object parse(String lex) {
Object obj = super.parse(lex);
// Must not have Y
if (lex.indexOf('Y') != -1)
// has year.
throw new DatatypeFormatException("Not valid as xsd:dayTimeDuration: " + lex);
int idx_T = lex.indexOf('T');
int idx_M = lex.indexOf('M');
if (idx_T == -1) {
// There is no T ; must have D and no M.
if (lex.indexOf('D') == -1)
throw new DatatypeFormatException("Not valid as xsd:dayTimeDuration: " + lex);
if (idx_M != -1)
// M, no T -> month
throw new DatatypeFormatException("Not valid as xsd:dayTimeDuration: " + lex);
return obj;
}
// Must not have a M before T
if (idx_M != -1 && idx_M < idx_T)
// M before T => month.
throw new DatatypeFormatException("Not valid as xsd:dayTimeDuration: " + lex);
return obj;
}
use of org.apache.jena.datatypes.DatatypeFormatException in project jena by apache.
the class NodeValue method _setByValue.
// Returns null for unrecognized literal.
private static NodeValue _setByValue(Node node) {
// nodeToNodeValue should have dealt with it.
if (NodeUtils.hasLang(node))
return new NodeValueLang(node);
LiteralLabel lit = node.getLiteral();
String lex = lit.getLexicalForm();
RDFDatatype datatype = lit.getDatatype();
// Quick check.
// Only XSD supported.
// And (for testing) roman numerals.
String datatypeURI = datatype.getURI();
if (!datatypeURI.startsWith(xsdNamespace) && !SystemARQ.EnableRomanNumerals) {
// Not XSD.
return null;
}
try {
// DatatypeFormatException - should not happen
if (XSDstring.isValidLiteral(lit))
// String - plain or xsd:string, or derived datatype.
return new NodeValueString(lit.getLexicalForm(), node);
if (!datatype.equals(XSDdecimal)) {
// XSD integer and derived types
if (XSDinteger.isValidLiteral(lit)) {
// .trim() implements the facet of whitespace collapse.
// BigInteger does not accept such whitespace.
String s = node.getLiteralLexicalForm().trim();
if (s.startsWith("+"))
// BigInteger does not accept leading "+"
s = s.substring(1);
// Includes subtypes (int, byte, postiveInteger etc).
// NB Known to be valid for type by now
BigInteger integer = new BigInteger(s);
return new NodeValueInteger(integer, node);
}
}
if (datatype.equals(XSDdecimal) && XSDdecimal.isValidLiteral(lit)) {
BigDecimal decimal = new BigDecimal(lit.getLexicalForm());
return new NodeValueDecimal(decimal, node);
}
if (datatype.equals(XSDfloat) && XSDfloat.isValidLiteral(lit)) {
// NB If needed, call to floatValue, then assign to double.
// Gets 1.3f != 1.3d right
float f = ((Number) lit.getValue()).floatValue();
return new NodeValueFloat(f, node);
}
if (datatype.equals(XSDdouble) && XSDdouble.isValidLiteral(lit)) {
double d = ((Number) lit.getValue()).doubleValue();
return new NodeValueDouble(d, node);
}
if ((datatype.equals(XSDdateTime) || datatype.equals(XSDdateTimeStamp)) && XSDdateTime.isValid(lex)) {
XSDDateTime dateTime = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDdate) && XSDdate.isValidLiteral(lit)) {
// Jena datatype support works on masked dataTimes.
XSDDateTime dateTime = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDtime) && XSDtime.isValidLiteral(lit)) {
// Jena datatype support works on masked dataTimes.
XSDDateTime time = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDgYear) && XSDgYear.isValidLiteral(lit)) {
XSDDateTime time = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDgYearMonth) && XSDgYearMonth.isValidLiteral(lit)) {
XSDDateTime time = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDgMonth) && XSDgMonth.isValidLiteral(lit)) {
XSDDateTime time = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDgMonthDay) && XSDgMonthDay.isValidLiteral(lit)) {
XSDDateTime time = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDgDay) && XSDgDay.isValidLiteral(lit)) {
XSDDateTime time = (XSDDateTime) lit.getValue();
return new NodeValueDT(lex, node);
}
if (datatype.equals(XSDduration) && XSDduration.isValid(lex)) {
Duration duration = xmlDatatypeFactory.newDuration(lex);
return new NodeValueDuration(duration, node);
}
if (datatype.equals(XSDyearMonthDuration) && XSDyearMonthDuration.isValid(lex)) {
Duration duration = xmlDatatypeFactory.newDuration(lex);
return new NodeValueDuration(duration, node);
}
if (datatype.equals(XSDdayTimeDuration) && XSDdayTimeDuration.isValid(lex)) {
Duration duration = xmlDatatypeFactory.newDuration(lex);
return new NodeValueDuration(duration, node);
}
if (datatype.equals(XSDboolean) && XSDboolean.isValidLiteral(lit)) {
boolean b = (Boolean) lit.getValue();
return new NodeValueBoolean(b, node);
}
// Not wired in
if (SystemARQ.EnableRomanNumerals) {
if (lit.getDatatypeURI().equals(RomanNumeralDatatype.get().getURI())) {
Object obj = RomanNumeralDatatype.get().parse(lit.getLexicalForm());
if (obj instanceof Integer)
return new NodeValueInteger(((Integer) obj).longValue());
if (obj instanceof RomanNumeral)
return new NodeValueInteger(((RomanNumeral) obj).intValue());
throw new ARQInternalErrorException("DatatypeFormatException: Roman numeral is unknown class");
}
}
} catch (DatatypeFormatException ex) {
// Should have been caught earlier by special test in nodeToNodeValue
throw new ARQInternalErrorException("DatatypeFormatException: " + lit, ex);
}
return null;
}
use of org.apache.jena.datatypes.DatatypeFormatException in project jena by apache.
the class Rational method testDateTime.
/**
* Test data/time wrappers
*/
public void testDateTime() {
// Duration
Literal l1 = m.createTypedLiteral("P1Y2M3DT5H6M7.50S", XSDDatatype.XSDduration);
assertEquals("duration data type", XSDDatatype.XSDduration, l1.getDatatype());
assertEquals("duration java type", XSDDuration.class, l1.getValue().getClass());
assertEquals("duration value", 1, ((XSDDuration) l1.getValue()).getYears());
assertEquals("duration value", 2, ((XSDDuration) l1.getValue()).getMonths());
assertEquals("duration value", 3, ((XSDDuration) l1.getValue()).getDays());
assertEquals("duration value", 5, ((XSDDuration) l1.getValue()).getHours());
assertEquals("duration value", 6, ((XSDDuration) l1.getValue()).getMinutes());
assertEquals("duration value", 7, ((XSDDuration) l1.getValue()).getFullSeconds());
assertEquals("duration value", BigDecimal.valueOf(75, 1), ((XSDDuration) l1.getValue()).getBigSeconds());
assertFloatEquals("duration value", 18367.5, ((XSDDuration) l1.getValue()).getTimePart());
assertEquals("serialization", "P1Y2M3DT5H6M7.5S", l1.getValue().toString());
assertTrue("equality test", l1.sameValueAs(m.createTypedLiteral("P1Y2M3DT5H6M7.5S", XSDDatatype.XSDduration)));
assertTrue("inequality test", l1 != m.createTypedLiteral("P1Y2M2DT5H6M7.5S", XSDDatatype.XSDduration));
l1 = m.createTypedLiteral("P1Y2M3DT5H0M", XSDDatatype.XSDduration);
assertEquals("serialization", "P1Y2M3DT5H", l1.getValue().toString());
l1 = m.createTypedLiteral("P1Y", XSDDatatype.XSDduration);
assertEquals("duration data type", XSDDatatype.XSDduration, l1.getDatatype());
assertEquals("duration java type", XSDDuration.class, l1.getValue().getClass());
assertEquals("duration value", 1, ((XSDDuration) l1.getValue()).getYears());
assertEquals("serialization", "P1Y", l1.getValue().toString());
assertTrue("equality test", l1.sameValueAs(m.createTypedLiteral("P1Y", XSDDatatype.XSDduration)));
assertTrue("inequality test", l1 != m.createTypedLiteral("P1Y", XSDDatatype.XSDduration));
l1 = m.createTypedLiteral("-P120D", XSDDatatype.XSDduration);
Literal l2 = m.createTypedLiteral(l1.getValue());
assertEquals("-P120D", l2.getLexicalForm());
// Duration equality bug
Literal d1 = m.createTypedLiteral("PT1H1M1S", XSDDatatype.XSDduration);
Literal d2 = m.createTypedLiteral("PT1H1M1.1S", XSDDatatype.XSDduration);
assertTrue("duration compare", !d1.sameValueAs(d2));
XSDDuration dur1 = (XSDDuration) d1.getValue();
XSDDuration dur2 = (XSDDuration) d2.getValue();
assertEquals("duration compare order", 1, dur2.compare(dur1));
// dateTime
l1 = m.createTypedLiteral("1999-05-31T02:09:32Z", XSDDatatype.XSDdateTime);
XSDDateTime xdt = (XSDDateTime) l1.getValue();
assertEquals("dateTime data type", XSDDatatype.XSDdateTime, l1.getDatatype());
assertEquals("dateTime java type", XSDDateTime.class, l1.getValue().getClass());
assertEquals("dateTime value", 1999, xdt.getYears());
assertEquals("dateTime value", 5, xdt.getMonths());
assertEquals("dateTime value", 31, xdt.getDays());
assertEquals("dateTime value", 2, xdt.getHours());
assertEquals("dateTime value", 9, xdt.getMinutes());
assertEquals("dateTime value", 32, xdt.getFullSeconds());
assertEquals("serialization", "1999-05-31T02:09:32Z", l1.getValue().toString());
Calendar cal = xdt.asCalendar();
Calendar testCal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
testCal.set(1999, 4, 31, 2, 9, 32);
/*
assertEquals("calendar value", cal.get(Calendar.YEAR), testCal.get(Calendar.YEAR) );
assertEquals("calendar value", cal.get(Calendar.MONTH), testCal.get(Calendar.MONTH) );
assertEquals("calendar value", cal.get(Calendar.DATE), testCal.get(Calendar.DATE) );
assertEquals("calendar value", cal.get(Calendar.HOUR), testCal.get(Calendar.HOUR) );
assertEquals("calendar value", cal.get(Calendar.MINUTE), testCal.get(Calendar.MINUTE) );
assertEquals("calendar value", cal.get(Calendar.SECOND), testCal.get(Calendar.SECOND) );
*/
// ms field can be undefined on Linux
testCal.set(Calendar.MILLISECOND, 0);
assertEquals("calendar value", cal, testCal);
assertEquals("equality test", l1, m.createTypedLiteral("1999-05-31T02:09:32Z", XSDDatatype.XSDdateTime));
assertTrue("inequality test", l1 != m.createTypedLiteral("1999-04-31T02:09:32Z", XSDDatatype.XSDdateTime));
Calendar testCal2 = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
testCal2.set(1999, 4, 30, 15, 9, 32);
// ms field can be undefined on Linux
testCal2.set(Calendar.MILLISECOND, 0);
Literal lc = m.createTypedLiteral(testCal2);
assertEquals("calendar 24 hour test", m.createTypedLiteral("1999-05-30T15:09:32Z", XSDDatatype.XSDdateTime), lc);
assertEquals("calendar value", cal, testCal);
assertEquals("equality test", l1, m.createTypedLiteral("1999-05-31T02:09:32Z", XSDDatatype.XSDdateTime));
Calendar testCal3 = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
testCal3.clear();
testCal3.set(1999, Calendar.JANUARY, 30, 15, 9, 32);
lc = m.createTypedLiteral(testCal3);
assertEquals("1999-01-30T15:09:32Z", lc.getLexicalForm());
String urib = "rdf://test.com#";
String uri1 = urib + "1";
String urip = urib + "prop";
String testN3 = "<" + uri1 + "> <" + urip + "> \"" + lc.getLexicalForm() + "\"^^<" + lc.getDatatypeURI() + "> .";
java.io.StringReader sr = new java.io.StringReader(testN3);
m.read(sr, urib, "N3");
assertTrue(m.contains(m.getResource(uri1), m.getProperty(urip)));
Resource r1 = m.getResource(uri1);
Property p = m.getProperty(urip);
XSDDateTime returnedDateTime = (XSDDateTime) r1.getProperty(p).getLiteral().getValue();
assertEquals("deserialized calendar value", testCal3, returnedDateTime.asCalendar());
// dateTime to calendar with milliseconds
Calendar testCal4 = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
testCal4.set(1999, 4, 30, 15, 9, 32);
testCal4.set(Calendar.MILLISECOND, 25);
doDateTimeTest(testCal4, "1999-05-30T15:09:32.025Z", 32.025);
testCal4.set(Calendar.MILLISECOND, 250);
doDateTimeTest(testCal4, "1999-05-30T15:09:32.25Z", 32.25);
testCal4.set(Calendar.MILLISECOND, 2);
doDateTimeTest(testCal4, "1999-05-30T15:09:32.002Z", 32.002);
// Years before 1000 : xsd:dateTime requires at least a four digit year.
int[] years = { -7777, -777, -77, -7, 7, 77, 777, 7777 };
for (int y : years) {
Calendar calM1 = Calendar.getInstance();
calM1.set(Calendar.YEAR, y);
calM1.set(Calendar.MONTH, 10);
calM1.set(Calendar.DATE, 23);
XSDDateTime xdtM = new XSDDateTime(calM1);
LiteralLabel xdtM_ll = LiteralLabelFactory.createByValue(xdtM, "", XSDDatatype.XSDdateTime);
assertTrue("Pre-1000 calendar value", xdtM_ll.getLexicalForm().matches("-?[0-9]{4}-.*"));
assertTrue("Pre-1000 calendar value", xdtM_ll.isWellFormed());
}
// Illegal dateTimes
boolean ok = false;
boolean old = JenaParameters.enableEagerLiteralValidation;
try {
JenaParameters.enableEagerLiteralValidation = true;
l1 = m.createTypedLiteral(new Date(12345656l), XSDDatatype.XSDdateTime);
} catch (DatatypeFormatException e) {
ok = true;
} finally {
JenaParameters.enableEagerLiteralValidation = old;
}
assertTrue("Early detection of invalid literals", ok);
// date
l1 = m.createTypedLiteral("1999-05-31", XSDDatatype.XSDdate);
assertEquals("dateTime data type", XSDDatatype.XSDdate, l1.getDatatype());
assertEquals("dateTime java type", XSDDateTime.class, l1.getValue().getClass());
xdt = (XSDDateTime) l1.getValue();
assertEquals("dateTime value", 1999, xdt.getYears());
assertEquals("dateTime value", 5, xdt.getMonths());
assertEquals("dateTime value", 31, xdt.getDays());
try {
xdt.getHours();
assertTrue("Failed to prevent illegal access", false);
} catch (IllegalDateTimeFieldException e) {
}
// time
l1 = m.createTypedLiteral("12:56:32", XSDDatatype.XSDtime);
assertEquals("dateTime data type", XSDDatatype.XSDtime, l1.getDatatype());
assertEquals("dateTime java type", XSDDateTime.class, l1.getValue().getClass());
xdt = (XSDDateTime) l1.getValue();
assertEquals("dateTime value", 12, xdt.getHours());
assertEquals("dateTime value", 56, xdt.getMinutes());
assertEquals("dateTime value", 32, xdt.getFullSeconds());
try {
xdt.getDays();
assertTrue("Failed to prevent illegal access", false);
} catch (IllegalDateTimeFieldException e) {
}
// gYearMonth
l1 = m.createTypedLiteral("1999-05", XSDDatatype.XSDgYearMonth);
assertEquals("dateTime data type", XSDDatatype.XSDgYearMonth, l1.getDatatype());
assertEquals("dateTime java type", XSDDateTime.class, l1.getValue().getClass());
xdt = (XSDDateTime) l1.getValue();
assertEquals("dateTime value", 1999, xdt.getYears());
assertEquals("dateTime value", 5, xdt.getMonths());
try {
xdt.getDays();
assertTrue("Failed to prevent illegal access", false);
} catch (IllegalDateTimeFieldException e) {
}
// gYear
l1 = m.createTypedLiteral("1999", XSDDatatype.XSDgYear);
assertEquals("dateTime data type", XSDDatatype.XSDgYear, l1.getDatatype());
assertEquals("dateTime java type", XSDDateTime.class, l1.getValue().getClass());
xdt = (XSDDateTime) l1.getValue();
assertEquals("dateTime value", 1999, xdt.getYears());
try {
xdt.getMonths();
assertTrue("Failed to prevent illegal access", false);
} catch (IllegalDateTimeFieldException e) {
}
// gMonth
l1 = m.createTypedLiteral("--05--", XSDDatatype.XSDgMonth);
assertEquals("dateTime data type", XSDDatatype.XSDgMonth, l1.getDatatype());
assertEquals("dateTime java type", XSDDateTime.class, l1.getValue().getClass());
xdt = (XSDDateTime) l1.getValue();
assertEquals("dateTime value", 5, xdt.getMonths());
try {
xdt.getYears();
assertTrue("Failed to prevent illegal access", false);
} catch (IllegalDateTimeFieldException e) {
}
// gMonthDay
l1 = m.createTypedLiteral("--05-25", XSDDatatype.XSDgMonthDay);
assertEquals("dateTime data type", XSDDatatype.XSDgMonthDay, l1.getDatatype());
assertEquals("dateTime java type", XSDDateTime.class, l1.getValue().getClass());
xdt = (XSDDateTime) l1.getValue();
assertEquals("dateTime value", 5, xdt.getMonths());
assertEquals("dateTime value", 25, xdt.getDays());
try {
xdt.getYears();
assertTrue("Failed to prevent illegal access", false);
} catch (IllegalDateTimeFieldException e) {
}
// gDay
l1 = m.createTypedLiteral("---25", XSDDatatype.XSDgDay);
assertEquals("dateTime data type", XSDDatatype.XSDgDay, l1.getDatatype());
assertEquals("dateTime java type", XSDDateTime.class, l1.getValue().getClass());
xdt = (XSDDateTime) l1.getValue();
assertEquals("dateTime value", 25, xdt.getDays());
try {
xdt.getMonths();
assertTrue("Failed to prevent illegal access", false);
} catch (IllegalDateTimeFieldException e) {
}
// Creation of datetime from a date object
Calendar ncal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
ncal.set(2003, 11, 8, 10, 50, 42);
ncal.set(Calendar.MILLISECOND, 0);
l1 = m.createTypedLiteral(ncal);
assertEquals("DateTime from date", XSDDatatype.XSDdateTime, l1.getDatatype());
assertEquals("DateTime from date", XSDDateTime.class, l1.getValue().getClass());
assertEquals("DateTime from date", "2003-12-08T10:50:42Z", l1.getValue().toString());
// Thanks to Greg Shueler for DST patch and test case
//////some of below code from java.util.GregorianCalendar javadoc///////
// create a Pacific Standard Time time zone
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, "America/Los_Angeles");
// set up rules for daylight savings time
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
// create a GregorianCalendar with the Pacific Daylight time zone
ncal = new GregorianCalendar(pdt);
//before daylight savings time
ncal.set(2004, 02, 21, 12, 50, 42);
ncal.set(Calendar.MILLISECOND, 0);
//System.err.println("cal is: "+ncal);
l1 = m.createTypedLiteral(ncal);
assertEquals("DateTime from date", XSDDatatype.XSDdateTime, l1.getDatatype());
assertEquals("DateTime from date", XSDDateTime.class, l1.getValue().getClass());
assertEquals("DateTime from date", "2004-03-21T20:50:42Z", l1.getValue().toString());
//System.err.println("date is: "+ncal.getTime());
ncal = new GregorianCalendar(pdt);
//within daylight savings time
ncal.set(2004, 03, 21, 12, 50, 42);
ncal.set(Calendar.MILLISECOND, 0);
//System.err.println("cal is: "+ncal);
l1 = m.createTypedLiteral(ncal);
assertEquals("DateTime from date", XSDDatatype.XSDdateTime, l1.getDatatype());
assertEquals("DateTime from date", XSDDateTime.class, l1.getValue().getClass());
assertEquals("DateTime from date", "2004-04-21T19:50:42Z", l1.getValue().toString());
//System.err.println("date is: "+ncal.getTime());
}
Aggregations