001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.bpmscript.web;
018:
019: import java.io.BufferedReader;
020: import java.io.IOException;
021: import java.io.StringReader;
022: import java.text.MessageFormat;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import org.apache.commons.lang.StringEscapeUtils;
027:
028: public class SourceHighLighter {
029:
030: private enum HighlightColour {
031: RED, GREEN, BLUE
032: };
033:
034: private HighlightColour highlightColor = HighlightColour.GREEN;
035:
036: private String format = "<a name=\"{0}\"><span style=\"background-color: {1}\">{2}</span></a>";
037:
038: private String lineSeparator = System.getProperty("line.separator");
039:
040: private int colourStrength = 220;
041:
042: /**
043: * Goes through the stack trace getting elements relevant to this
044: * source given the name. A range of colors is used to show which
045: * level it's at, darker colors means it's higher up the stack.
046: *
047: * @param name
048: * @param source
049: * @param stackTrace
050: * @return
051: */
052: public String highlight(String name, String source,
053: StackTraceElement[] stackTrace) {
054:
055: Map<Integer, Integer> map = new HashMap<Integer, Integer>();
056: StringBuffer result = new StringBuffer();
057:
058: int counter = 0;
059: for (StackTraceElement element : stackTrace) {
060: if (name.equals(element.getFileName())) {
061: map.put(element.getLineNumber(), counter++);
062: }
063: }
064:
065: BufferedReader reader = new BufferedReader(new StringReader(
066: source));
067:
068: String line = null;
069: int period = 245 / counter;
070:
071: try {
072: int lineNumber = 0;
073: while ((line = reader.readLine()) != null) {
074: Integer depth = map.get(lineNumber);
075: result.append(lineNumber + " ");
076: if (depth != null) {
077: int level = period * depth + 10;
078: String webColor = toWebColor(
079: (highlightColor == HighlightColour.RED ? colourStrength
080: : level),
081: (highlightColor == HighlightColour.GREEN ? colourStrength
082: : level),
083: (highlightColor == HighlightColour.BLUE ? colourStrength
084: : level));
085: result.append(MessageFormat.format(format, name
086: + '-' + lineNumber, webColor,
087: StringEscapeUtils.escapeHtml(line)));
088: result.append(lineSeparator);
089: } else {
090: result.append(StringEscapeUtils.escapeHtml(line));
091: result.append(lineSeparator);
092: }
093: lineNumber++;
094: }
095: } catch (IOException e) {
096: // should never happen with a string reader!
097: throw new RuntimeException(e);
098: }
099: return result.toString();
100: }
101:
102: public String addLineNumbers(String source) {
103: StringBuffer result = new StringBuffer();
104: BufferedReader reader = new BufferedReader(new StringReader(
105: source));
106: int lineNumber = 0;
107: String line = null;
108: try {
109: while ((line = reader.readLine()) != null) {
110: result.append(lineNumber++);
111: result.append(" ");
112: result.append(line);
113: result.append(lineSeparator);
114: }
115: } catch (IOException e) {
116: // should never happen with a string reader!
117: throw new RuntimeException(e);
118: }
119: return result.toString();
120: }
121:
122: public String toWebColor(int red, int green, int blue) {
123: return '#' + toHex(red) + toHex(green) + toHex(blue);
124: }
125:
126: public String toHex(int value) {
127: String hex = Integer.toHexString(value);
128: if (hex.length() == 1) {
129: hex = '0' + hex;
130: }
131: return hex;
132: }
133: }
|