use of org.apache.jena.graph.impl.LiteralLabel 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());
}
use of org.apache.jena.graph.impl.LiteralLabel in project jena by apache.
the class Rational method testUserDefined.
/**
* Test user defined data types.
* This is based on a corrected, modified version of an early DAML+OIL example
* but is not specific to DAML+OIL.
*/
public void testUserDefined() throws IOException {
String uri = "http://www.daml.org/2001/03/daml+oil-ex-dt";
String filename = "testing/xsd/daml+oil-ex-dt.xsd";
TypeMapper tm = TypeMapper.getInstance();
List<String> typenames = XSDDatatype.loadUserDefined(uri, new FileReader(filename), null, tm);
assertIteratorValues(typenames.iterator(), new Object[] { uri + "#XSDEnumerationHeight", uri + "#over12", uri + "#over17", uri + "#over59", uri + "#clothingsize" });
// Check the string restriction
RDFDatatype heightType = tm.getSafeTypeByName(uri + "#XSDEnumerationHeight");
checkLegalLiteral("short", heightType, String.class, "short");
checkLegalLiteral("tall", heightType, String.class, "tall");
checkIllegalLiteral("shortish", heightType);
// Check the numeric restriction
RDFDatatype over12Type = tm.getSafeTypeByName(uri + "#over12");
checkLegalLiteral("15", over12Type, Integer.class, 15);
checkIllegalLiteral("12", over12Type);
// Check the union type
RDFDatatype clothingsize = tm.getSafeTypeByName(uri + "#clothingsize");
checkLegalLiteral("42", clothingsize, Integer.class, 42);
checkLegalLiteral("short", clothingsize, String.class, "short");
// Check use of isValidLiteral for base versus derived combinations
LiteralLabel iOver12 = m.createTypedLiteral("13", over12Type).asNode().getLiteral();
LiteralLabel iDecimal14 = m.createTypedLiteral("14", XSDDatatype.XSDdecimal).asNode().getLiteral();
LiteralLabel iDecimal10 = m.createTypedLiteral("10", XSDDatatype.XSDdecimal).asNode().getLiteral();
LiteralLabel iString = m.createTypedLiteral("15", XSDDatatype.XSDstring).asNode().getLiteral();
LiteralLabel iPlain = m.createLiteral("foo").asNode().getLiteral();
assertTrue(over12Type.isValidLiteral(iOver12));
assertTrue(over12Type.isValidLiteral(iDecimal14));
assertTrue(!over12Type.isValidLiteral(iDecimal10));
assertTrue(!over12Type.isValidLiteral(iString));
assertTrue(!over12Type.isValidLiteral(iPlain));
assertTrue(XSDDatatype.XSDdecimal.isValidLiteral(iOver12));
assertTrue(XSDDatatype.XSDdecimal.isValidLiteral(iDecimal14));
assertTrue(XSDDatatype.XSDdecimal.isValidLiteral(iDecimal10));
assertTrue(!XSDDatatype.XSDdecimal.isValidLiteral(iString));
assertTrue(!XSDDatatype.XSDdecimal.isValidLiteral(iPlain));
assertTrue(XSDDatatype.XSDstring.isValidLiteral(iString));
assertTrue(XSDDatatype.XSDstring.isValidLiteral(iPlain));
assertTrue(!XSDDatatype.XSDstring.isValidLiteral(iOver12));
assertTrue(!XSDDatatype.XSDstring.isValidLiteral(iDecimal10));
assertTrue(!XSDDatatype.XSDstring.isValidLiteral(iDecimal14));
}
use of org.apache.jena.graph.impl.LiteralLabel in project jena by apache.
the class DirectionWithPointPFBase method objectToStruct.
/** Deconstruct the node or list object argument and make a SpatialMatch */
@Override
protected SpatialMatch objectToStruct(PropFuncArg argObject) {
if (argObject.isNode()) {
log.warn("Object not a List: " + argObject);
return null;
}
List<Node> list = argObject.getArgList();
if (list.size() < 2 || list.size() > 3)
throw new SpatialIndexException("Change in object list size");
int idx = 0;
Node x = list.get(idx);
if (!x.isLiteral()) {
log.warn("Latitude is not a literal " + list);
return null;
}
if (!SpatialValueUtil.isDecimal(x)) {
log.warn("Latitude is not a decimal " + list);
return null;
}
Double latitude = Double.parseDouble(x.getLiteralLexicalForm());
idx++;
x = list.get(idx);
if (!x.isLiteral()) {
log.warn("Longitude is not a literal " + list);
return null;
}
if (!SpatialValueUtil.isDecimal(x)) {
log.warn("Longitude is not a decimal " + list);
return null;
}
Double longitude = Double.parseDouble(x.getLiteralLexicalForm());
idx++;
int limit = -1;
if (idx < list.size()) {
x = list.get(idx);
if (!x.isLiteral()) {
log.warn("Limit is not a literal " + list);
return null;
}
LiteralLabel lit = x.getLiteral();
if (!XSDDatatype.XSDinteger.isValidLiteral(lit)) {
log.warn("Limit is not an integer " + list);
return null;
}
int v = NodeFactoryExtra.nodeToInt(x);
limit = (v < 0) ? -1 : v;
idx++;
if (idx < list.size()) {
log.warn("Limit is not the last parameter " + list);
return null;
}
}
SpatialMatch match = this.getSpatialMatch(latitude, longitude, limit);
if (log.isDebugEnabled())
log.debug("Trying SpatialMatch: " + match.toString());
return match;
}
use of org.apache.jena.graph.impl.LiteralLabel in project timbuctoo by HuygensING.
the class TripleParserTest method getObjectAsLiteralReturnsTheValueAndTypeOfTheObject.
@Test
public void getObjectAsLiteralReturnsTheValueAndTypeOfTheObject() {
Triple triple = createSingleTripleWithLiteralObject(SUBJECT_URI_NODE, PREDICATE_URI_NODE, OBJECT_LITERAL_NODE);
TripleParser instance = TripleParser.fromTriple(triple);
LiteralLabel value = instance.getObjectAsLiteral();
assertThat(value, allOf(hasProperty("value", is(OBJECT_VALUE)), hasProperty("datatype", hasProperty("URI", is(OBJECT_TYPE_URI)))));
}
use of org.apache.jena.graph.impl.LiteralLabel in project jena by apache.
the class NodeId method inline$.
private static NodeId inline$(Node node) {
LiteralLabel lit = node.getLiteral();
if (node.getLiteralDatatype().equals(XSDDatatype.XSDdecimal)) {
// Check lexical form.
if (!XSDDatatype.XSDdecimal.isValidLiteral(lit))
return null;
// Not lit.getValue() because that may be a narrower type e.g. Integer.
// .trim is how Jena does it but it rather savage. spc, \n \r \t.
// But at this point we know it's a valid literal so the excessive
// chopping by .trim is safe.
BigDecimal decimal = new BigDecimal(lit.getLexicalForm().trim());
// Does range checking.
DecimalNode dn = DecimalNode.valueOf(decimal);
// null is "does not fit"
if (dn != null)
// setType
return new NodeId(dn.pack());
else
return null;
} else {
// Not decimal.
if (XSDDatatype.XSDinteger.isValidLiteral(lit)) {
if (lit.getLexicalForm().length() > 19)
return null;
try {
long v = ((Number) lit.getValue()).longValue();
v = IntegerNode.pack(v);
// Value -1 is "does not fit"
if (v != -1)
return new NodeId(v);
else
return null;
}// Out of range for the type, not a long etc etc.
catch (Throwable ex) {
return null;
}
}
}
if (XSDDatatype.XSDdateTime.isValidLiteral(lit)) {
long v = DateTimeNode.packDateTime(lit.getLexicalForm());
if (v == -1)
return null;
v = setType(v, DATETIME);
return new NodeId(v);
}
if (XSDDatatype.XSDdate.isValidLiteral(lit)) {
long v = DateTimeNode.packDate(lit.getLexicalForm());
if (v == -1)
return null;
v = setType(v, DATE);
return new NodeId(v);
}
if (XSDDatatype.XSDboolean.isValidLiteral(lit)) {
long v = 0;
boolean b = (Boolean) lit.getValue();
// return new NodeValueBoolean(b, node) ;
v = setType(v, BOOLEAN);
if (b)
v = v | 0x01;
return new NodeId(v);
}
return null;
}
Aggregations