use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.
the class JsonModelWriter method convertFieldValueToJSON.
/**
* The value to write. Special support for {@link Reference} and {@link Text}.
* The {@link #toString()} Method is used to write the "value" key.
*
* @param value the value
* @return the {@link JSONObject} representing the value
* @throws JSONException
*/
private JSONObject convertFieldValueToJSON(Object value) throws JSONException {
JSONObject jValue = new JSONObject();
if (value instanceof Reference) {
jValue.put("type", "reference");
jValue.put("xsd:datatype", DataTypeEnum.AnyUri.getShortName());
jValue.put("value", ((Reference) value).getReference());
} else if (value instanceof Text) {
jValue.put("type", "text");
jValue.put("xml:lang", ((Text) value).getLanguage());
jValue.put("value", ((Text) value).getText());
} else if (value instanceof Date) {
jValue.put("type", "value");
jValue.put("value", TimeUtils.toString(DataTypeEnum.DateTime, (Date) value));
jValue.put("xsd:datatype", DataTypeEnum.DateTime.getShortName());
} else {
jValue.put("type", "value");
Set<DataTypeEnum> dataTypes = DataTypeEnum.getPrimaryDataTypes(value.getClass());
if (!dataTypes.isEmpty()) {
jValue.put("xsd:datatype", dataTypes.iterator().next().getShortName());
} else {
jValue.put("xsd:datatype", DataTypeEnum.String.getShortName());
}
jValue.put("value", value);
}
return jValue;
}
use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.
the class RdfRepresentation method add.
// protected IRI getRepresentationType(){
// Iterator<IRI> it = this.graphNode.getIRIObjects(REPRESENTATION_TYPE_PROPERTY);
// return it.hasNext()?it.next():null;
// }
@Override
public void add(String field, Object value) {
if (field == null) {
throw new IllegalArgumentException("The parsed field MUST NOT be NULL");
} else if (field.isEmpty()) {
throw new IllegalArgumentException("The parsed field MUST NOT be Empty");
}
if (value == null) {
throw new IllegalArgumentException("NULL values are not supported by Representations");
}
IRI fieldIRI = new IRI(field);
Collection<Object> values = new ArrayList<Object>();
//process the parsed value with the Utility Method ->
// this converts Objects as defined in the specification
ModelUtils.checkValues(valueFactory, value, values);
//We still need to implement support for specific types supported by this implementation
for (Object current : values) {
if (current instanceof RDFTerm) {
//native support for Clerezza types!
graphNode.addProperty(fieldIRI, (RDFTerm) current);
} else if (current instanceof RdfReference) {
//treat RDF Implementations special to avoid creating new instances
graphNode.addProperty(fieldIRI, ((RdfReference) current).getIRI());
} else if (current instanceof Reference) {
graphNode.addProperty(fieldIRI, new IRI(((Reference) current).getReference()));
} else if (current instanceof RdfText) {
//treat RDF Implementations special to avoid creating new instances
graphNode.addProperty(fieldIRI, ((RdfText) current).getLiteral());
} else if (current instanceof Text) {
addNaturalText(fieldIRI, ((Text) current).getText(), ((Text) current).getLanguage());
} else {
//else add an typed Literal!
addTypedLiteral(fieldIRI, current);
}
}
}
use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.
the class RdfRepresentationTest method testPlainLiteralToTextConversion.
/*--------------------------------------------------------------------------
* Additional Tests for special Features of the Clerezza based implementation
*
* This includes mainly support for additional types like PlainLiteral,
* TypedLiteral, IRIs. The conversion to such types as well as getter for
* such types.
*--------------------------------------------------------------------------
*/
/**
* {@link PlainLiteral} is used for natural language text in the Clerezza
* RDF API. This tests if adding {@link PlainLiteral}s to the
* {@link Representation#add(String, Object)} method makes them available
* as {@link Text} instances via the {@link Representation} API (e.g.
* {@link Representation#get(String, String...)}).
*/
@Test
public void testPlainLiteralToTextConversion() {
String field = "urn:test.RdfRepresentation:test.field";
Literal noLangLiteral = new PlainLiteralImpl("A plain literal without Language");
Literal enLiteral = new PlainLiteralImpl("An english literal", new Language("en"));
Literal deLiteral = new PlainLiteralImpl("Ein Deutsches Literal", new Language("de"));
Literal deATLiteral = new PlainLiteralImpl("Ein Topfen Verband hilft bei Zerrungen", new Language("de-AT"));
Collection<Literal> plainLiterals = Arrays.asList(noLangLiteral, enLiteral, deLiteral, deATLiteral);
Representation rep = createRepresentation(null);
rep.add(field, plainLiterals);
//now test, that the Plain Literals are available as natural language
//tests via the Representation Interface!
//1) one without a language
Iterator<Text> noLangaugeTexts = rep.get(field, (String) null);
assertTrue(noLangaugeTexts.hasNext());
Text noLanguageText = noLangaugeTexts.next();
assertEquals(noLangLiteral.getLexicalForm(), noLanguageText.getText());
assertNull(noLanguageText.getLanguage());
//only a single result
assertFalse(noLangaugeTexts.hasNext());
//2) one with a language
Iterator<Text> enLangaugeTexts = rep.get(field, "en");
assertTrue(enLangaugeTexts.hasNext());
Text enLangageText = enLangaugeTexts.next();
assertEquals(enLiteral.getLexicalForm(), enLangageText.getText());
assertEquals(enLiteral.getLanguage().toString(), enLangageText.getLanguage());
//only a single result
assertFalse(enLangaugeTexts.hasNext());
//3) test to get all natural language values
Set<String> stringValues = new HashSet<String>();
for (Literal plainLiteral : plainLiterals) {
stringValues.add(plainLiteral.getLexicalForm());
}
Iterator<Text> texts = rep.getText(field);
while (texts.hasNext()) {
assertTrue(stringValues.remove(texts.next().getText()));
}
assertTrue(stringValues.isEmpty());
}
use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.
the class RdfRepresentationTest method testPlainLiteralToTextConversion.
/*--------------------------------------------------------------------------
* Additional Tests for special Features of the Clerezza based implementation
*
* This includes mainly support for additional types like PlainLiteral,
* TypedLiteral, UriRefs. The conversion to such types as well as getter for
* such types.
*--------------------------------------------------------------------------
*/
/**
* {@link PlainLiteral} is used for natural language text in the Clerezza
* RDF API. This tests if adding {@link PlainLiteral}s to the
* {@link Representation#add(String, Object)} method makes them available
* as {@link Text} instances via the {@link Representation} API (e.g.
* {@link Representation#get(String, String...)}).
*/
@Test
public void testPlainLiteralToTextConversion() {
String field = "urn:test.RdfRepresentation:test.field";
Literal noLangLiteral = valueFactory.getSesameFactory().createLiteral("A plain literal without Language");
Literal enLiteral = valueFactory.getSesameFactory().createLiteral("An english literal", "en");
Literal deLiteral = valueFactory.getSesameFactory().createLiteral("Ein Deutsches Literal", "de");
Literal deATLiteral = valueFactory.getSesameFactory().createLiteral("Ein Topfen Verband hilft bei Zerrungen", "de-AT");
Collection<Literal> plainLiterals = Arrays.asList(noLangLiteral, enLiteral, deLiteral, deATLiteral);
Representation rep = createRepresentation(null);
rep.add(field, plainLiterals);
//now test, that the Plain Literals are available as natural language
//tests via the Representation Interface!
//1) one without a language
Iterator<Text> noLangaugeTexts = rep.get(field, (String) null);
assertTrue(noLangaugeTexts.hasNext());
Text noLanguageText = noLangaugeTexts.next();
assertEquals(noLangLiteral.getLabel(), noLanguageText.getText());
assertNull(noLanguageText.getLanguage());
//only a single result
assertFalse(noLangaugeTexts.hasNext());
//2) one with a language
Iterator<Text> enLangaugeTexts = rep.get(field, "en");
assertTrue(enLangaugeTexts.hasNext());
Text enLangageText = enLangaugeTexts.next();
assertEquals(enLiteral.getLabel(), enLangageText.getText());
assertEquals(enLiteral.getLanguage(), enLangageText.getLanguage());
//only a single result
assertFalse(enLangaugeTexts.hasNext());
//3) test to get all natural language values
Set<String> stringValues = new HashSet<String>();
for (Literal plainLiteral : plainLiterals) {
stringValues.add(plainLiteral.getLabel());
}
Iterator<Text> texts = rep.getText(field);
while (texts.hasNext()) {
assertTrue(stringValues.remove(texts.next().getText()));
}
assertTrue(stringValues.isEmpty());
}
use of org.apache.stanbol.entityhub.servicesapi.model.Text in project stanbol by apache.
the class RdfRepresentation method add.
@Override
public void add(String field, Object value) throws IllegalArgumentException {
if (field == null) {
throw new IllegalArgumentException("The parsed field MUST NOT be NULL");
} else if (field.isEmpty()) {
throw new IllegalArgumentException("The parsed field MUST NOT be Empty");
}
if (value == null) {
throw new IllegalArgumentException("NULL values are not supported by Representations");
}
URI property = sesameFactory.createURI(field);
Collection<Object> values = new ArrayList<Object>();
//process the parsed value with the Utility Method ->
// this converts Objects as defined in the specification
ModelUtils.checkValues(factory, value, values);
//We still need to implement support for specific types supported by this implementation
for (Object current : values) {
if (current instanceof Value) {
//native support for Sesame types!
addValue(property, (Value) current);
} else if (current instanceof RdfWrapper) {
//for Sesame RDF wrapper we can directly use the Value
addValue(property, ((RdfWrapper) current).getValue());
} else if (current instanceof Reference) {
addValue(property, sesameFactory.createURI(((Reference) current).getReference()));
} else if (current instanceof Text) {
addValue(property, sesameFactory.createLiteral(((Text) current).getText(), ((Text) current).getLanguage()));
} else {
//else add an typed Literal!
addValue(property, createTypedLiteral(current));
}
}
}
Aggregations