use of org.apache.jena.datatypes.RDFDatatype in project jena by apache.
the class JSONInput method parseOneTerm.
private Node parseOneTerm(JsonObject term) {
checkContains(term, false, false, kType, kValue, kXmlLang, kDatatype);
String type = stringOrNull(term, kType);
String v = stringOrNull(term, kValue);
if (kUri.equals(type)) {
checkContains(term, false, true, kType, kValue);
String uri = v;
Node n = NodeFactory.createURI(v);
return n;
}
if (kLiteral.equals(type) || kTypedLiteral.equals(type)) {
String lang = stringOrNull(term, kXmlLang);
String dtStr = stringOrNull(term, kDatatype);
if (lang != null && dtStr != null)
throw new ResultSetException("Both language and datatype defined: " + term);
RDFDatatype dt = TypeMapper.getInstance().getSafeTypeByName(dtStr);
return NodeFactory.createLiteral(v, lang, dt);
}
if (kBnode.equals(type))
return labelMap.get(null, v);
throw new ResultSetException("Object key not recognized as valid for an RDF term: " + term);
}
use of org.apache.jena.datatypes.RDFDatatype in project jena by apache.
the class JsonLDReader method createNode.
private Node createNode(Map<String, Object> map) {
String type = (String) map.get("type");
String lex = (String) map.get("value");
if (type.equals(IRI))
return createURI(lex);
else if (type.equals(BLANK_NODE))
return createBlankNode(lex);
else if (type.equals(LITERAL)) {
String lang = (String) map.get("language");
String datatype = (String) map.get("datatype");
if (Objects.equals(xsdString, datatype))
// In RDF 1.1, simple literals and xsd:string are the same.
// During migration, we prefer simple literals to xsd:strings.
datatype = null;
if (lang == null && datatype == null)
return profile.createStringLiteral(lex, -1, -1);
if (lang != null)
return profile.createLangLiteral(lex, lang, -1, -1);
RDFDatatype dt = NodeFactory.getType(datatype);
return profile.createTypedLiteral(lex, dt, -1, -1);
} else
throw new InternalErrorException("Node is not a IRI, bNode or a literal: " + type);
}
use of org.apache.jena.datatypes.RDFDatatype in project jena by apache.
the class NodeFormatterBase method formatLiteral.
@Override
public void formatLiteral(AWriter w, Node n) {
RDFDatatype dt = n.getLiteralDatatype();
String lang = n.getLiteralLanguage();
String lex = n.getLiteralLexicalForm();
if (lang != null && !lang.equals("")) {
formatLitLang(w, lex, lang);
} else if (dt == null) {
// RDF 1.0, simple literal.
formatLitString(w, lex);
} else if (JenaRuntime.isRDF11 && dt.equals(XSDDatatype.XSDstring)) {
// RDF 1.1, xsd:string - output as short string.
formatLitString(w, lex);
} else {
// Datatype, no language tag, not short string.
formatLitDT(w, lex, dt.getURI());
}
}
use of org.apache.jena.datatypes.RDFDatatype in project jena by apache.
the class ThriftConvert method convert.
public static Node convert(RDF_Term term, PrefixMap pmap) {
if (term.isSetPrefixName()) {
String x = expand(term.getPrefixName(), pmap);
if (x != null)
return NodeFactory.createURI(x);
throw new RiotThriftException("Failed to expand " + term);
// Log.warn(BinRDF.class, "Failed to expand "+term) ;
// return NodeFactory.createURI(prefix+":"+localname) ;
}
if (term.isSetIri())
return NodeFactory.createURI(term.getIri().getIri());
if (term.isSetBnode())
return NodeFactory.createBlankNode(term.getBnode().getLabel());
if (term.isSetLiteral()) {
RDF_Literal lit = term.getLiteral();
String lex = lit.getLex();
String dtString = null;
if (lit.isSetDatatype())
dtString = lit.getDatatype();
else if (lit.isSetDtPrefix()) {
String x = expand(lit.getDtPrefix(), pmap);
if (x == null)
throw new RiotThriftException("Failed to expand datatype prefix name:" + term);
dtString = x;
}
RDFDatatype dt = NodeFactory.getType(dtString);
String lang = lit.getLangtag();
return NodeFactory.createLiteral(lex, lang, dt);
}
if (term.isSetValInteger()) {
long x = term.getValInteger();
String lex = Long.toString(x, 10);
RDFDatatype dt = XSDDatatype.XSDinteger;
return NodeFactory.createLiteral(lex, dt);
}
if (term.isSetValDouble()) {
double x = term.getValDouble();
String lex = Double.toString(x);
RDFDatatype dt = XSDDatatype.XSDdouble;
return NodeFactory.createLiteral(lex, dt);
}
if (term.isSetValDecimal()) {
long value = term.getValDecimal().getValue();
int scale = term.getValDecimal().getScale();
BigDecimal d = BigDecimal.valueOf(value, scale);
String lex = d.toPlainString();
RDFDatatype dt = XSDDatatype.XSDdecimal;
return NodeFactory.createLiteral(lex, dt);
}
if (term.isSetVariable())
return Var.alloc(term.getVariable().getName());
if (term.isSetAny())
return Node.ANY;
if (term.isSetUndefined())
return null;
throw new RiotThriftException("No conversion to a Node: " + term.toString());
}
use of org.apache.jena.datatypes.RDFDatatype in project jena by apache.
the class ThriftConvert method toThriftValue.
/** Attempt to encode a node by value (integer, decimal, double) into an RDF_term.
* @param node
* @param term
* @return true if the term was set, else false.
*/
public static boolean toThriftValue(Node node, RDF_Term term) {
if (!node.isLiteral())
return false;
// Value cases : Integer, Double, Decimal
String lex = node.getLiteralLexicalForm();
RDFDatatype rdt = node.getLiteralDatatype();
if (rdt == null)
return false;
if (rdt.equals(XSDDatatype.XSDdecimal)) {
if (rdt.isValid(lex)) {
BigDecimal decimal = new BigDecimal(lex.trim());
int scale = decimal.scale();
BigInteger bigInt = decimal.unscaledValue();
if (bigInt.compareTo(MAX_I) <= 0 && bigInt.compareTo(MIN_I) >= 0) {
// This check makes sure that bigInt.longValue() is safe
RDF_Decimal d = new RDF_Decimal(bigInt.longValue(), scale);
RDF_Term literal = new RDF_Term();
term.setValDecimal(d);
return true;
}
}
} else if (rdt.equals(XSDDatatype.XSDinteger) || rdt.equals(XSDDatatype.XSDlong) || rdt.equals(XSDDatatype.XSDint) || rdt.equals(XSDDatatype.XSDshort) || rdt.equals(XSDDatatype.XSDbyte)) {
// Conservative - no derived types.
if (rdt.isValid(lex)) {
try {
long v = ((Number) node.getLiteralValue()).longValue();
term.setValInteger(v);
return true;
}// Out of range for the type, not a long etc etc.
catch (Throwable ex) {
}
}
} else if (rdt.equals(XSDDatatype.XSDdouble)) {
// XSDfloat??
if (rdt.isValid(lex)) {
try {
double v = ((Double) node.getLiteralValue()).doubleValue();
term.setValDouble(v);
return true;
}// Out of range for the type, ...
catch (Throwable ex) {
}
}
}
return false;
}
Aggregations