/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bpmscript.web;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.StringEscapeUtils;
public class SourceHighLighter {
private enum HighlightColour { RED, GREEN, BLUE };
private HighlightColour highlightColor = HighlightColour.GREEN;
private String format = "<a name=\"{0}\"><span style=\"background-color: {1}\">{2}</span></a>";
private String lineSeparator = System.getProperty("line.separator");
private int colourStrength = 220;
/**
* Goes through the stack trace getting elements relevant to this
* source given the name. A range of colors is used to show which
* level it's at, darker colors means it's higher up the stack.
*
* @param name
* @param source
* @param stackTrace
* @return
*/
public String highlight(String name, String source, StackTraceElement[] stackTrace) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
StringBuffer result = new StringBuffer();
int counter = 0;
for (StackTraceElement element : stackTrace) {
if(name.equals(element.getFileName())) {
map.put(element.getLineNumber(), counter++);
}
}
BufferedReader reader = new BufferedReader(new StringReader(source));
String line = null;
int period = 245 / counter;
try {
int lineNumber = 0;
while((line = reader.readLine()) != null) {
Integer depth = map.get(lineNumber);
result.append(lineNumber + " ");
if(depth != null) {
int level = period * depth + 10;
String webColor = toWebColor(
(highlightColor == HighlightColour.RED ? colourStrength : level),
(highlightColor == HighlightColour.GREEN ? colourStrength : level),
(highlightColor == HighlightColour.BLUE ? colourStrength : level)
);
result.append(MessageFormat.format(format, name + '-' + lineNumber, webColor, StringEscapeUtils.escapeHtml(line)));
result.append(lineSeparator);
} else {
result.append(StringEscapeUtils.escapeHtml(line));
result.append(lineSeparator);
}
lineNumber++;
}
} catch (IOException e) {
// should never happen with a string reader!
throw new RuntimeException(e);
}
return result.toString();
}
public String addLineNumbers(String source) {
StringBuffer result = new StringBuffer();
BufferedReader reader = new BufferedReader(new StringReader(source));
int lineNumber = 0;
String line = null;
try {
while((line = reader.readLine()) != null) {
result.append(lineNumber++);
result.append(" ");
result.append(line);
result.append(lineSeparator);
}
} catch (IOException e) {
// should never happen with a string reader!
throw new RuntimeException(e);
}
return result.toString();
}
public String toWebColor(int red, int green, int blue) {
return '#' + toHex(red) + toHex(green) + toHex(blue);
}
public String toHex(int value) {
String hex = Integer.toHexString(value);
if(hex.length() == 1) {
hex = '0' + hex;
}
return hex;
}
}
|