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.apache.cocoon.components.language;
018:
019: import org.apache.avalon.framework.CascadingException;
020: import org.apache.cocoon.components.language.programming.CompilerError;
021:
022: import java.io.PrintStream;
023: import java.io.PrintWriter;
024: import java.io.File;
025: import java.io.FileReader;
026:
027: /**
028: * The language exception.
029: *
030: * @author <a href="mailto:ricardo@apache.org">Ricardo Rocha</a>
031: * @author <a href="mailto:tcurdt@apache.org">Torsten Curdt</a>
032: * @version CVS $Id: LanguageException.java 433543 2006-08-22 06:22:54Z crossley $
033: */
034: public class LanguageException extends CascadingException {
035:
036: private CompilerError[] errors = null;
037: private String filename = null;
038:
039: public LanguageException(String message, String filename,
040: CompilerError[] errors) {
041: super (message);
042: this .filename = filename;
043: this .errors = errors;
044: }
045:
046: public LanguageException(String message) {
047: super (message);
048: }
049:
050: public LanguageException(String message, Throwable t) {
051: super (message, t);
052: }
053:
054: public String getMessage() {
055: if (errors != null) {
056: StringBuffer extendedMessage = new StringBuffer();
057: extendedMessage.append(super .getMessage());
058:
059: if (errors != null && filename != null) {
060: extendedMessage.append(getSource(filename));
061: }
062:
063: for (int i = 0; i < errors.length; i++) {
064: CompilerError error = errors[i];
065: if (i > 0)
066: extendedMessage.append("\n");
067: extendedMessage.append("Line ");
068: extendedMessage.append(error.getStartLine());
069: extendedMessage.append(", column ");
070: extendedMessage.append(error.getStartColumn());
071: extendedMessage.append(": ");
072: extendedMessage.append(error.getMessage());
073: }
074: return (extendedMessage.toString());
075: } else {
076: return (super .getMessage());
077: }
078: }
079:
080: // Stolen from ProcessingException...
081:
082: public String toString() {
083: StringBuffer s = new StringBuffer();
084: s.append(super .toString());
085: if (getCause() != null) {
086: s.append(": ");
087: s.append(getCause().toString());
088: }
089: return s.toString();
090: }
091:
092: public void printStackTrace() {
093: super .printStackTrace();
094: if (getCause() != null)
095: getCause().printStackTrace();
096: }
097:
098: public void printStackTrace(PrintStream s) {
099: super .printStackTrace(s);
100: if (getCause() != null)
101: getCause().printStackTrace(s);
102: }
103:
104: public void printStackTrace(PrintWriter s) {
105: super .printStackTrace(s);
106: if (getCause() != null)
107: getCause().printStackTrace(s);
108: }
109:
110: private final static int linesBefore = 3;
111: private final static int linesAfter = 3;
112:
113: private final static String getString(char[] buffer, int start,
114: int end) {
115: int currentLine = 1;
116: int currentPos = 0;
117:
118: while (currentLine < start && currentPos < buffer.length) {
119: if (buffer[currentPos++] == '\n') {
120: currentLine++;
121: }
122: }
123: int startPos = currentPos;
124:
125: while (currentLine < (end + 1) && currentPos < buffer.length) {
126: if (buffer[currentPos++] == '\n') {
127: currentLine++;
128: }
129: }
130: int endPos = currentPos;
131:
132: return (new String(buffer, startPos, endPos - startPos));
133: }
134:
135: private String getSource(String filename) {
136: File file = new File(filename);
137: long fileSize = file.length();
138: if (file != null && file.exists() && file.isFile()
139: && fileSize > 0) {
140: // paranoid checking: nothing larger than ints can handle or 10MB
141: if (fileSize < Integer.MAX_VALUE
142: || fileSize < 10 * 1024 * 1024) {
143: char[] buffer = new char[(int) fileSize];
144: try {
145: FileReader fileReader = new FileReader(file);
146: fileReader.read(buffer, 0, (int) fileSize);
147:
148: StringBuffer listing = new StringBuffer();
149:
150: for (int i = 0; i < errors.length; i++) {
151: CompilerError error = errors[i];
152:
153: int start = error.getStartLine();
154: int end = error.getEndLine();
155:
156: if (start > 0 && end > 0) {
157: String before = getString(buffer, start - 1
158: - linesBefore, start - 1);
159: String itself = getString(buffer, start,
160: end);
161: String after = getString(buffer, end + 1,
162: end + 1 + linesAfter);
163:
164: listing.append("ERROR ").append(i + 1)
165: .append(" (").append(
166: error.getFile()).append(
167: "):\n");
168: listing.append("...\n");
169: listing.append(before);
170: listing.append("\n// start error (lines ")
171: .append(error.getStartLine())
172: .append("-").append(
173: error.getEndLine()).append(
174: ") \"").append(
175: error.getMessage()).append(
176: "\"\n");
177: listing.append(itself);
178: listing.append("\n// end error\n");
179: listing.append(after);
180: listing.append("\n...\n");
181: }
182: }
183:
184: fileReader.close();
185:
186: return (listing.toString());
187: } catch (Exception e) {
188: }
189: }
190: }
191: return (null);
192: }
193:
194: /*
195: public static void main(String[] args) {
196: String s =
197: "1 \n"+
198: "2 System.out.println(\n"+
199: "3 \n"+
200: "4 this.contentHandler.startDocument();\n"+
201: "5 AttributesImpl xspAttr = new AttributesImpl();\n"+
202: "6 \n";
203:
204: char[] buffer = s.toCharArray();
205:
206: int start = 2;
207: int end = 2;
208:
209: String before = getString(buffer, start - 1 - linesBefore, start - 1);
210: String itself = getString(buffer, start, end);
211: String after = getString(buffer, end + 1, end + 1 + linesAfter);
212:
213:
214: System.out.print(before);
215: System.out.println("--");
216: System.out.print(itself);
217: System.out.println("--");
218: System.out.print(after);
219: }
220: */
221: }
|