use of de.ids_mannheim.korap.response.match.HighlightCombinatorElement in project Krill by KorAP.
the class Match method getSnippetHTML.
@JsonProperty("snippet")
public String getSnippetHTML() {
if (!this._processHighlight())
return null;
if (this.processed && this.snippetHTML != null)
return this.snippetHTML;
if (DEBUG)
log.trace("Create HTML Snippet");
StringBuilder sb = new StringBuilder();
StringBuilder rightContext = new StringBuilder();
// Remember ids already defined to
// have joined elements
HashSet<String> joins = new HashSet<>(100);
// Snippet stack sizes
short start = (short) 0;
short end = this.snippetArray.size();
// Create context
sb.append("<span class=\"context-left\">");
if (this.startMore)
sb.append("<span class=\"more\"></span>");
// Set levels for highlights
FixedBitSet level = new FixedBitSet(255);
level.set(0, 255);
byte[] levelCache = new byte[255];
HighlightCombinatorElement elem;
end--;
if (end > 0) {
// First element of sorted array
elem = this.snippetArray.getFirst();
// First element is textual
if (elem.type == 0) {
sb.append(elem.toHTML(this, level, levelCache, joins));
// Move start position
start++;
}
;
sb.append("</span>");
// Last element of sorted array
elem = this.snippetArray.getLast();
// Create right context, if there is any
rightContext.append("<span class=\"context-right\">");
// Last element is textual
if (elem != null && elem.type == 0) {
rightContext.append(elem.toHTML(this, level, levelCache, joins));
// decrement end
end--;
}
;
}
;
if (this.endMore)
rightContext.append("<span class=\"more\"></span>");
rightContext.append("</span>");
// Iterate through all remaining elements
sb.append("<span class=\"match\">");
for (short i = start; i <= end; i++) {
elem = this.snippetArray.get(i);
// UNTESTED
if (elem != null) {
String elemString = elem.toHTML(this, level, levelCache, joins);
if (DEBUG) {
log.trace("Add node {}", elemString);
}
;
sb.append(elemString);
}
}
;
sb.append("</span>");
sb.append(rightContext);
return (this.snippetHTML = sb.toString());
}
use of de.ids_mannheim.korap.response.match.HighlightCombinatorElement in project Krill by KorAP.
the class HighlightCombinator method addClose.
// Add closing highlight combinator to the stack
public void addClose(int number) {
HighlightCombinatorElement lastComb;
// Clean up temporary stack
this.tempStack.clear();
// Check if there is an opening tag at least
if (this.balanceStack.empty()) {
if (DEBUG)
log.trace("The balance stack is empty");
return;
}
;
// Just some debug information
if (DEBUG) {
StringBuilder sb = new StringBuilder("Stack for checking with number ");
sb.append(number).append(" is ");
for (int s : this.balanceStack) {
sb.append('[').append(s).append(']');
}
;
log.trace(sb.toString());
}
;
// class number of the last element
// It's already ensured the stack is not empty
int eold = this.balanceStack.pop();
// the closing element is not balanced, i.e. the last element differs
while (eold != number) {
// Retrieve last combinator on stack
lastComb = this.combine.peekLast();
if (DEBUG)
log.trace("Closing element is unbalanced - {} " + "!= {} with lastComb {}|{}|{}", eold, number, lastComb.type, lastComb.number, lastComb.characters);
// element on the balanceStack
if (lastComb.type == 1 && lastComb.number == eold) {
// Remove the last element - it's empty and uninteresting!
this.combine.removeLast();
} else // Last element is empty
if (lastComb.type == 3) {
log.error("The last element was empty -- and this is not correctly handled yet");
} else // combinator is either closing (??) or another opener
{
if (DEBUG)
log.trace("close element a) {}", eold);
// Add a closer for the old element (this has following elements)
this.combine.add(new HighlightCombinatorElement((byte) 2, eold, false));
}
;
// add this element number temporarily on the stack
tempStack.push(eold);
// There are no more elements on the balance stack
if (this.balanceStack.empty())
break;
// Check next element
eold = this.balanceStack.pop();
}
;
// Get last combinator on the stack
lastComb = this.combine.peekLast();
if (DEBUG) {
log.trace("Stack for checking 2: {}|{}|{}|{}", lastComb.type, lastComb.number, lastComb.characters, number);
}
;
if (lastComb.type == 1 && lastComb.number == number) {
while (lastComb.type == 1 && lastComb.number == number) {
// Remove the damn thing - It's empty and uninteresting!
this.combine.removeLast();
lastComb = this.combine.peekLast();
}
;
} else if (lastComb.type == 3) {
System.err.println("öööööööööööööööööööööööö");
} else {
if (DEBUG)
log.trace("close element b) {}", number);
// Add a closer
this.combine.add(new HighlightCombinatorElement((byte) 2, number));
}
;
// Fetch everything from the tempstack and reopen it
for (int e : tempStack) {
if (DEBUG)
log.trace("Reopen element {}", e);
combine.add(new HighlightCombinatorElement((byte) 1, e));
balanceStack.push(e);
}
;
}
use of de.ids_mannheim.korap.response.match.HighlightCombinatorElement in project Krill by KorAP.
the class HighlightCombinator method addOpen.
// Add opening highlight combinator to the stack
public void addOpen(int number) {
this.combine.add(new HighlightCombinatorElement((byte) 1, number));
this.balanceStack.push(number);
if (DEBUG)
log.trace("Add opening {} to stack", number);
}
use of de.ids_mannheim.korap.response.match.HighlightCombinatorElement in project Krill by KorAP.
the class Match method getSnippetBrackets.
@JsonIgnore
public String getSnippetBrackets() {
if (!this._processHighlight())
return null;
if (this.processed && this.snippetBrackets != null)
return this.snippetBrackets;
// Snippet stack sizes
short start = (short) 0;
short end = this.snippetArray.size();
end--;
StringBuilder sb = new StringBuilder();
if (this.startMore)
sb.append("... ");
// First element of sorted array
HighlightCombinatorElement elem = this.snippetArray.getFirst();
if (elem.type == 0) {
sb.append(elem.toBrackets(this));
start++;
}
;
sb.append("[");
// Last element of sorted array
elem = this.snippetArray.getLast();
StringBuilder rightContext = new StringBuilder();
// Last element is textual
if (elem != null && elem.type == 0) {
rightContext.append(elem.toBrackets(this));
// decrement end
end--;
}
;
for (short i = start; i <= end; i++) {
sb.append(this.snippetArray.get(i).toBrackets(this));
}
;
sb.append("]");
sb.append(rightContext);
if (this.endMore)
sb.append(" ...");
return (this.snippetBrackets = sb.toString());
}
Aggregations