in SimpleServer/src/main/java/org/apache/uima/simpleserver/output/InlineXMLGenerator.java [38:197]
public static String getInlineXML(Result result) {
// a list of all positions where some tags begin
TreeSet<Integer> beginPositions = new TreeSet<Integer>();
// a list of all positions where some tags end
TreeSet<Integer> endPositions = new TreeSet<Integer>();
// a list of all positions where empty tags begin and end
TreeSet<Integer> emptyPositions = new TreeSet<Integer>();
// this will be used to merge the three lists above
TreeSet<Integer> allPositions = new TreeSet<Integer>();
// maps a position number to the collection of tags which
// begin at this position.
HashMap<Integer, List<ResultEntry>> beginTags = new HashMap<Integer, List<ResultEntry>>(result
.getResultEntries().size());
// maps a position number to the collection of tags which
// end at this position.
HashMap<Integer, List<ResultEntry>> endTags = new HashMap<Integer, List<ResultEntry>>(result
.getResultEntries().size());
// maps a position number to the collection of empty tags which
// begin and end at this position.
HashMap<Integer, List<ResultEntry>> emptyTags = new HashMap<Integer, List<ResultEntry>>();
// first of all, we decide which annotions to take
// this loop iterates over all available annotations
loop: for (ResultEntry entry : result.getResultEntries()) {
Integer begin;
Integer end;
try {
begin = new Integer(entry.getBegin());
end = new Integer(entry.getEnd());
} catch (NumberFormatException e) {
// we don't take annotations without features "begin" and "end"
continue loop;
}
// a zero-length-annotation can not cause any conflicts and should
// always be taken
if (begin.equals(end)) {
emptyPositions.add(begin);
List<ResultEntry> emptyTagList = emptyTags.get(end);
if (emptyTagList == null) {
emptyTagList = new LinkedList<ResultEntry>();
emptyTags.put(end, emptyTagList);
}
// and add the entry to both tag lists
emptyTagList.add(entry);
continue loop;
}
// if an anotation is not zero-length, it can cause conflicts,
// so we should decide, whether we take it or not
// now let's analyse the candidates for conflicts
Set<Integer> conflictPoints;
// 1. which begin in the body of current annotation
conflictPoints = beginPositions.subSet(begin + 1, end);
for (int i : conflictPoints) {
for (ResultEntry e : beginTags.get(i)) {
if (isConflict(e, begin, end)) {
continue loop;
}
}
}
// 2. which end in the body of current annotation
conflictPoints = endPositions.subSet(begin + 1, end);
for (int i : conflictPoints) {
for (ResultEntry e : endTags.get(i)) {
if (isConflict(e, begin, end)) {
continue loop;
}
}
}
// now, if no conflicts found, we can
// add the entry to our results
beginPositions.add(begin);
endPositions.add(end);
// now create the tag lists, if necessary
// and add the tag to the lists
List<ResultEntry> beginTagList = beginTags.get(begin);
if (beginTagList == null) {
beginTagList = new LinkedList<ResultEntry>();
beginTags.put(begin, beginTagList);
}
List<ResultEntry> endTagList = endTags.get(end);
if (endTagList == null) {
endTagList = new LinkedList<ResultEntry>();
endTags.put(end, endTagList);
}
// and add the entry to both tag lists
beginTagList.add(entry);
endTagList.add(entry);
}
// end of loop over all entries (annotations)
// now we have all the entries in our collections and we can
// begin with the output
// first, we make a mixed set of all used positions, both of end tags
// and
// begin tags
allPositions.addAll(beginPositions);
allPositions.addAll(endPositions);
allPositions.addAll(emptyPositions);
allPositions.add(0);
StringBuffer sourceText = new StringBuffer(result.getText());
allPositions.add(sourceText.length());
// now, construct the output
StringBuffer resultSB = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<result>");
int begin = 0;
for (int end : allPositions) {
// print closing tags
List<ResultEntry> endingTags = endTags.get(begin);
// BEGIN!!! it's not a typing error!
if (endingTags != null) {
printEndTags(endingTags, resultSB);
}
// print empty tags
List<ResultEntry> empTags = emptyTags.get(begin);
if (empTags != null) {
printEmptyTags(empTags, resultSB);
}
// print opening tags
List<ResultEntry> beginningTags = beginTags.get(begin);
if (beginningTags != null) {
printBeginTags(beginningTags, resultSB);
}
// print content
resultSB.append(xmlEscape(sourceText.substring(begin, end)));
// proceed to next step
begin = end;
}
// printout tags that may be at the end
List<ResultEntry> endingTags = endTags.get(begin); // BEGIN!!! it's
if (endingTags != null) {
printEndTags(endingTags, resultSB);
}
// print empty tags
List<ResultEntry> empTags = emptyTags.get(begin);
if (empTags != null) {
printEmptyTags(empTags, resultSB);
}
List<ResultEntry> beginningTags = beginTags.get(begin);
if (beginningTags != null) {
printBeginTags(beginningTags, resultSB);
}
resultSB.append("\n</result>");
return resultSB.toString();
}