if(selectedTextIterator.hasNext()){
// test if the selected text is part of the TEXT_TO_TEST
selectedTextResource = selectedTextIterator.next().getObject();
assertTrue("fise:selected-text MUST BE of type PlainLiteral (uri: "+textAnnotation+")",
selectedTextResource instanceof PlainLiteral);
Literal selectedText = (Literal)selectedTextResource;
assertTrue("The parsed content MUST contain the fise:selected-text value '"
+selectedText.getLexicalForm()+"' (uri: "+textAnnotation+")!",content.contains(selectedText.getLexicalForm()));
Assert.assertFalse("fise:selected-text MUST be single valued (uri: "+textAnnotation+")",selectedTextIterator.hasNext());
} else {
selectedTextResource = null; //no selected text
}
//check against an expected value
Resource expectedSelectedText = expectedValues.get(ENHANCER_SELECTED_TEXT);
if(expectedSelectedText != null){
assertEquals("The fise:selected-text is not the expected value "+expectedSelectedText+" (uri: "+textAnnotation+")!",
expectedSelectedText, selectedTextResource);
}
//check for fise:selection-head and fise:selection-tail (STANBOL-987)
Iterator<Triple> selectionHeadIterator = enhancements.filter(textAnnotation, Properties.ENHANCER_SELECTION_HEAD, null);
if(selectedTextResource != null){
Assert.assertFalse("If fise:selected-text is present fise:selection-head MUST NOT be present",selectionHeadIterator.hasNext());
}
Resource selectionHeadResource;
if(selectionHeadIterator.hasNext()){
// test if the selected text is part of the TEXT_TO_TEST
selectionHeadResource = selectionHeadIterator.next().getObject();
assertTrue("fise:selection-head MUST BE of type PlainLiteral (uri: "+textAnnotation+")",
selectionHeadResource instanceof PlainLiteral);
Literal selectionHeadText = (Literal)selectionHeadResource;
assertTrue("The parsed content MUST contain the fise:selected-head value '"
+selectionHeadText.getLexicalForm()+"' (uri: "+textAnnotation+")!",content.contains(selectionHeadText.getLexicalForm()));
Assert.assertFalse("fise:selection-head MUST be single valued (uri: "+textAnnotation+")",selectionHeadIterator.hasNext());
} else {
selectionHeadResource = null;
}
Iterator<Triple> selectionTailIterator = enhancements.filter(textAnnotation, Properties.ENHANCER_SELECTION_TAIL, null);
if(selectedTextResource != null){
Assert.assertFalse("If fise:selected-text is present fise:selection-tail MUST NOT be present",selectionTailIterator.hasNext());
}
Resource selectionTailResource;
if(selectionTailIterator.hasNext()){
// test if the selected text is part of the TEXT_TO_TEST
selectionTailResource = selectionTailIterator.next().getObject();
assertTrue("fise:selection-head MUST BE of type PlainLiteral (uri: "+textAnnotation+")",
selectionTailResource instanceof PlainLiteral);
Literal selectionTailText = (Literal)selectionTailResource;
assertTrue("The parsed content MUST contain the fise:selected-tail value '"
+selectionTailText.getLexicalForm()+"' (uri: "+textAnnotation+")!",content.contains(selectionTailText.getLexicalForm()));
Assert.assertFalse("fise:selection-tail MUST be single valued (uri: "+textAnnotation+")",selectionTailIterator.hasNext());
} else {
selectionTailResource = null;
}
Assert.assertTrue("Both fise:selection-tail AND fise:selection-head MUST BE defined "
+"(if one of them is present) (uri: "+textAnnotation+")",
(selectionHeadResource != null && selectionTailResource != null) ||
(selectionHeadResource == null && selectionTailResource == null));
Resource selectionContextResource;
// test if context is added
Iterator<Triple> selectionContextIterator = enhancements.filter(textAnnotation,
ENHANCER_SELECTION_CONTEXT, null);
if(selectionContextIterator.hasNext()) { //context is optional
//selection context is not allowed without selected-text
assertTrue("If fise:selection-context is present also fise:selected-text or fise:selection-head and fise:selection-tail MUST BE present (uri: "+textAnnotation+")",
selectedTextResource != null || (selectionHeadResource != null && selectionTailResource != null));
// test if the selected text is part of the TEXT_TO_TEST
selectionContextResource = selectionContextIterator.next().getObject();
assertTrue("The fise:selection-context MUST BE of type PlainLiteral (uri: "+textAnnotation+")",
selectionContextResource instanceof PlainLiteral);
//check that the content contains the context
assertTrue("The fise:selection-context MUST BE contained in the Content | context= "+ selectionContextResource,
content.contains(((Literal)selectionContextResource).getLexicalForm()));
//check that the context contains the selected text
if(selectedTextResource != null){
assertTrue("The fise:selected-text value MUST BE containted within the fise:selection-context value",
((Literal)selectionContextResource).getLexicalForm().contains(
((Literal)selectedTextResource).getLexicalForm()));
}
if(selectionHeadResource != null){
assertTrue("The fise:selection-head value MUST BE containted within the fise:selection-context value",
((Literal)selectionContextResource).getLexicalForm().contains(
((Literal)selectionHeadResource).getLexicalForm()));
}
if(selectionTailResource != null){
assertTrue("The fise:selection-tail value MUST BE containted within the fise:selection-context value",
((Literal)selectionContextResource).getLexicalForm().contains(
((Literal)selectionTailResource).getLexicalForm()));
}
} else {
assertNull("If no fise:selection-context is present also fise:selected-text MUST BE NOT present!", selectedTextResource);
selectionContextResource = null;
}
Resource expectedSelectionContext = expectedValues.get(ENHANCER_SELECTION_CONTEXT);
if(expectedSelectionContext != null){
assertEquals("The value of fise:selection-context has not the expected value "+expectedSelectionContext,
expectedSelectionContext, selectionContextResource);
}
//test start/end if present
Iterator<Triple> startPosIterator = enhancements.filter(textAnnotation,
ENHANCER_START, null);
Iterator<Triple> endPosIterator = enhancements.filter(textAnnotation,
ENHANCER_END, null);
//start end is optional, but if start is present, that also end needs to be set
TypedLiteral startPosLiteral;
TypedLiteral endPosLiteral;
if(startPosIterator.hasNext()){
//NOTE: TextAnnotations might be use to select whole sections of a text
// (e.g. see STANBOL-617) in those cases adding the text of the
// whole section is not feasible.
//assertNotNull("If fise:start is present the fise:selection-context MUST also be present (uri: "+textAnnotation+")!",
// selectionContextResource);
Resource resource = startPosIterator.next().getObject();
//only a single start position is supported
assertFalse("fise:start MUST HAVE only a single value (uri: "+textAnnotation+")!",startPosIterator.hasNext());
assertTrue("fise:start MUST be a typed Literal (uri: "+textAnnotation+")!",resource instanceof TypedLiteral);
startPosLiteral = (TypedLiteral) resource;
assertEquals("fise:start MUST use xsd:int as data type (uri: "+textAnnotation+")",XSD.int_, startPosLiteral.getDataType());
resource = null;
Integer start = LiteralFactory.getInstance().createObject(Integer.class, startPosLiteral);
assertNotNull("Unable to parse Integer from TypedLiteral "+startPosLiteral,start);
//now get the end
//end must be defined if start is present
assertTrue("If fise:start is present also fise:end MUST BE defined (uri: "+textAnnotation+")!",endPosIterator.hasNext());
resource = endPosIterator.next().getObject();
//only a single end position is supported
assertFalse("fise:end MUST HAVE only a single value (uri: "+textAnnotation+")!",endPosIterator.hasNext());
assertTrue("fise:end values MUST BE TypedLiterals (uri: "+textAnnotation+")",resource instanceof TypedLiteral);
endPosLiteral = (TypedLiteral) resource;
assertEquals("fise:end MUST use xsd:int as data type (uri: "+textAnnotation+")",XSD.int_, endPosLiteral.getDataType());
resource = null;
Integer end = LiteralFactory.getInstance().createObject(Integer.class, endPosLiteral);
assertNotNull("Unable to parse Integer from TypedLiteral "+endPosLiteral,end);
//check for equality of the selected text and the text on the selected position in the content
//System.out.println("TA ["+start+"|"+end+"]"+selectedText.getLexicalForm()+"<->"+content.substring(start,end));
if(selectedTextResource != null){
assertEquals("the substring [fise:start,fise:end] does not correspond to "
+ "the fise:selected-text value '"+((Literal)selectedTextResource).getLexicalForm()
+ "' of this TextAnnotation!",content.substring(start, end), ((Literal)selectedTextResource).getLexicalForm());
} // else no selected-text present ... unable to test this
} else {
assertNull("if fise:selected-text is present also fise:start AND fise:end MUST BE present!",selectedTextResource);
assertNull("If fise:selection-context is present also fise:start AND fise:end MUST BE present!",selectionContextResource);
assertFalse("if fise:end is presnet also fise:start AND fise:selection-context MUST BE present!",endPosIterator.hasNext());
startPosLiteral = null;
endPosLiteral = null;
}
Resource expectedStartPos = expectedValues.get(ENHANCER_START);
if(expectedStartPos != null){
assertEquals("The fise:start value is not the expected "+expectedStartPos,
expectedStartPos, startPosLiteral);
}
Resource expectedEndPos = expectedValues.get(ENHANCER_END);
if(expectedEndPos != null){
assertEquals("The fise:end value is not the expected "+expectedEndPos,
expectedEndPos, endPosLiteral);
}
//fise:selection-prefix and fise:selection-suffix (STANBOL-987)
Literal prefixLiteral;
Iterator<Triple> selectionPrefixIterator = enhancements.filter(textAnnotation,
Properties.ENHANCER_SELECTION_PREFIX, null);
if(startPosLiteral != null){
// check if the selectionPrefix text is present
assertTrue("fise:selection-prefix property is missing for fise:TextAnnotation "
+ textAnnotation, selectionPrefixIterator.hasNext() ||
!validatePrefixSuffix); //to support old and new fise:TextAnnotation model
// test if the selected text is part of the TEXT_TO_TEST
if(selectionPrefixIterator.hasNext()){
Resource selectionPrefixResource = selectionPrefixIterator.next().getObject();
assertTrue("fise:selection-prefix MUST BE of type PlainLiteral (uri: "+textAnnotation+")",
selectionPrefixResource instanceof PlainLiteral);
prefixLiteral = (Literal)selectionPrefixResource;
assertTrue("The parsed content MUST contain the fise:selection-prefix value '"
+prefixLiteral.getLexicalForm()+"' (uri: "+textAnnotation+")!",content.contains(prefixLiteral.getLexicalForm()));
assertFalse("fise:selection-prefix MUST BE single valued (uri: "+textAnnotation+")!",
selectionPrefixIterator.hasNext());
} else {
prefixLiteral = null;
}
} else {
prefixLiteral = null;
}
Literal suffixLiteral;
Iterator<Triple> selectionSuffixIterator = enhancements.filter(textAnnotation,
Properties.ENHANCER_SELECTION_SUFFIX, null);
if(endPosLiteral != null){
// check if the selectionPrefix text is present
assertTrue("fise:selection-suffix property is missing for fise:TextAnnotation "
+ textAnnotation, selectionSuffixIterator.hasNext() ||
!validatePrefixSuffix); //to support old and new fise:TextAnnotation model
if(selectionSuffixIterator.hasNext()){
// test if the selected text is part of the TEXT_TO_TEST
Resource selectionSuffixResource = selectionSuffixIterator.next().getObject();
assertTrue("fise:selection-suffix MUST BE of type PlainLiteral (uri: "+textAnnotation+")",
selectionSuffixResource instanceof PlainLiteral);
suffixLiteral = (Literal)selectionSuffixResource;
assertTrue("The parsed content MUST contain the fise:selection-suffix value '"
+suffixLiteral.getLexicalForm()+"' (uri: "+textAnnotation+")!",content.contains(suffixLiteral.getLexicalForm()));
assertFalse("fise:selection-suffix MUST BE single valued (uri: "+textAnnotation+")!",
selectionSuffixIterator.hasNext());
} else {
suffixLiteral = null;
}
} else {
suffixLiteral = null;
}
Assert.assertTrue("Both fise:selection-prefix AND fise:selection-suffix need to be present "
+ "(if one of them is present) (uri: "+textAnnotation+")",
(suffixLiteral != null && prefixLiteral != null) ||
(suffixLiteral == null && prefixLiteral == null));
if(prefixLiteral != null && selectedTextResource != null){
String occurrence = prefixLiteral.getLexicalForm() +
((Literal)selectedTextResource).getLexicalForm() +
suffixLiteral.getLexicalForm();
assertTrue("The parsed content MUST contain the concated value of fise:selection-prefix,"
+ "fise:selected-text and fise:selection-suffix (value: '"+occurrence
+ "' (uri: "+textAnnotation+")!",content.contains(occurrence));
}
if(prefixLiteral != null && selectionHeadResource != null){
String occurrence = prefixLiteral.getLexicalForm() +
((Literal)selectionHeadResource).getLexicalForm();
assertTrue("The parsed content MUST contain the concated value of fise:selection-prefix,"
+ "fise:selection-head (value: '"+occurrence
+ "' (uri: "+textAnnotation+")!",content.contains(occurrence));
occurrence = ((Literal)selectionTailResource).getLexicalForm() +
suffixLiteral.getLexicalForm();
assertTrue("The parsed content MUST contain the concated value of fise:selection-tail "
+ "and fise:selection-suffix (value: '"+occurrence
+ "' (uri: "+textAnnotation+")!",content.contains(occurrence));
}