use of org.apache.jena.datatypes.TypeMapper in project jena by apache.
the class TextIndexLucene method query$.
private List<TextHit> query$(IndexReader indexReader, Node property, String qs, String graphURI, String lang, int limit) throws ParseException, IOException {
String textField = docDef.getField(property);
String textClause;
String langClause = null;
String graphClause = null;
//for language-based search extension
if (getDocDef().getLangField() != null) {
String langField = getDocDef().getLangField();
if (lang != null) {
if (this.isMultilingual && !lang.equals("none")) {
textField = textField + "_" + lang;
}
langClause = !"none".equals(lang) ? langField + ":" + lang : "-" + langField + ":*";
}
}
if (textField != null)
textClause = textField + ":" + qs;
else
textClause = qs;
if (graphURI != null) {
String escaped = QueryParserBase.escape(graphURI);
graphClause = getDocDef().getGraphField() + ":" + escaped;
}
String queryString = textClause;
if (langClause != null)
queryString = "(" + queryString + ") AND " + langClause;
if (graphClause != null)
queryString = "(" + queryString + ") AND " + graphClause;
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
Query query = parseQuery(queryString, queryAnalyzer);
if (limit <= 0)
limit = MAX_N;
ScoreDoc[] sDocs = indexSearcher.search(query, limit).scoreDocs;
List<TextHit> results = new ArrayList<>();
for (ScoreDoc sd : sDocs) {
Document doc = indexSearcher.doc(sd.doc);
String[] values = doc.getValues(docDef.getEntityField());
Node literal = null;
String field = (property != null) ? docDef.getField(property) : docDef.getPrimaryField();
String[] lexicals = doc.getValues(field);
if (lexicals.length > 0) {
String lexical = lexicals[0];
String[] langs = doc.getValues(docDef.getLangField());
if (langs.length > 0) {
String doclang = langs[0];
if (doclang.startsWith(DATATYPE_PREFIX)) {
String datatype = doclang.substring(DATATYPE_PREFIX.length());
TypeMapper tmap = TypeMapper.getInstance();
literal = NodeFactory.createLiteral(lexical, tmap.getSafeTypeByName(datatype));
} else {
literal = NodeFactory.createLiteral(lexical, doclang);
}
} else {
literal = NodeFactory.createLiteral(lexical);
}
}
for (String v : values) {
Node n = TextQueryFuncs.stringToNode(v);
TextHit hit = new TextHit(n, sd.score, literal);
results.add(hit);
}
}
return results;
}
use of org.apache.jena.datatypes.TypeMapper in project jena by apache.
the class Rational method testDateTimeBug.
/**
* Test a user error report concerning date/time literals
*/
public void testDateTimeBug() {
// Bug in serialization
String XSDDateURI = XSD.date.getURI();
TypeMapper typeMapper = TypeMapper.getInstance();
RDFDatatype dt = typeMapper.getSafeTypeByName(XSDDateURI);
Object obj = dt.parse("2003-05-21");
Literal literal = m.createTypedLiteral(obj, dt);
literal.toString();
Object value2 = dt.parse(obj.toString());
assertEquals(obj, value2);
// Check alternative form doesn't provoke exceptions
RDFDatatype dateType = XSDDatatype.XSDdate;
m.createTypedLiteral("2003-05-21", dateType);
// Check alt time times
checkSerialization("2003-05-21", XSDDatatype.XSDdate);
checkSerialization("2003-05-21T12:56:10Z", XSDDatatype.XSDdateTime);
checkSerialization("2003-05", XSDDatatype.XSDgYearMonth);
checkSerialization("2003", XSDDatatype.XSDgYear);
checkSerialization("--05", XSDDatatype.XSDgMonth);
checkSerialization("--05-12", XSDDatatype.XSDgMonthDay);
checkSerialization("---12", XSDDatatype.XSDgDay);
}
use of org.apache.jena.datatypes.TypeMapper 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.datatypes.TypeMapper in project jena by apache.
the class TestNode method testLiteralToString.
public void testLiteralToString() {
TypeMapper tm = TypeMapper.getInstance();
RDFDatatype dtInt = tm.getTypeByValue(new Integer(10));
Node plain = NodeFactory.createLiteral("rhubarb", "");
Node english = NodeFactory.createLiteral("eccentric", "en_UK");
Node typed = NodeFactory.createLiteral("10", dtInt);
assertEquals("\"rhubarb\"", plain.toString());
assertEquals("rhubarb", plain.toString(false));
assertEquals("\"eccentric\"@en_UK", english.toString());
assertEquals("10^^http://www.w3.org/2001/XMLSchema#int", typed.toString(false));
}
use of org.apache.jena.datatypes.TypeMapper in project jena by apache.
the class TestNode method testDataMatches.
public void testDataMatches() {
TypeMapper tm = TypeMapper.getInstance();
RDFDatatype dt1 = tm.getTypeByValue(new Integer(10));
RDFDatatype dt2 = tm.getTypeByValue(new Short((short) 10));
Node a = NodeFactory.createLiteral("10", dt1);
Node b = NodeFactory.createLiteral("10", dt2);
assertDiffer("types must make a difference", a, b);
assertTrue("A and B must express the same value", a.sameValueAs(b));
assertTrue("matching literals must respect sameValueAs", a.matches(b));
}
Aggregations