private Item highlightArtifact(String contribution, String artifact, String query) throws NotFoundException {
Item item = this.contributionCollection.get(contribution);
if (item == null) {
throw new NotFoundException("contribution not found: " + contribution);
}
String location = item.getAlternate();
if (location.endsWith(".jar") || location.endsWith(".zip")) {
location =
"jar:" + (location.startsWith("file:") ? "" : "file:")
+ location
+ '!'
+ (artifact.startsWith("/") ? "" : "/")
+ artifact;
} else {
location += (location.endsWith("/") ? "" : "/") + artifact;
}
try {
Reader reader = new InputStreamReader(new URL(location).openStream());
StringBuilder sb = new StringBuilder();
int c;
// TODO: load the chars into an array buffer instead of one
// at a
// time
while ((c = reader.read()) != -1) {
char character = (char)c;
if (!Character.isIdentifierIgnorable(character)) {
sb.append(character);
}
}
String highlightedText = this.domainSearch.highlight(SearchFields.FILE_CONTENT_FIELD, sb.toString(), query);
highlightedText = highlightedText.replaceAll("\n", "<br/>");
highlightedText = highlightedText.replaceAll(" ", " ");
highlightedText =
HighlightingUtil.replaceHighlightMarkupBy(highlightedText, HIGHLIGHT_START, HIGHLIGHT_END);
item = new Item();
item.setTitle(contribution + ";" + artifact);
item.setContents(highlightedText);
return item;
} catch (Exception e) {
throw new NotFoundException("Could not highlight artifact: " + e.getMessage(), e);
}
}