1 16 package org.apache.cocoon.components.language; 17 18 import org.apache.avalon.framework.CascadingException; 19 import org.apache.cocoon.components.language.programming.CompilerError; 20 21 import java.io.PrintStream ; 22 import java.io.PrintWriter ; 23 import java.io.File ; 24 import java.io.FileReader ; 25 26 33 public class LanguageException extends CascadingException { 34 35 private CompilerError[] errors = null; 36 private String filename = null; 37 38 public LanguageException(String message, String filename, CompilerError[] errors) { 39 super(message); 40 this.filename = filename; 41 this.errors = errors; 42 } 43 44 public LanguageException(String message) { 45 super(message); 46 } 47 48 public LanguageException(String message, Throwable t) { 49 super(message, t); 50 } 51 52 public String getMessage() { 53 if (errors != null) { 54 StringBuffer extendedMessage = new StringBuffer (); 55 extendedMessage.append(super.getMessage()); 56 57 if (errors != null && filename != null) { 58 extendedMessage.append(getSource(filename)); 59 } 60 61 for (int i = 0; i < errors.length; i++) { 62 CompilerError error = errors[i]; 63 if (i > 0) extendedMessage.append("\n"); 64 extendedMessage.append("Line "); 65 extendedMessage.append(error.getStartLine()); 66 extendedMessage.append(", column "); 67 extendedMessage.append(error.getStartColumn()); 68 extendedMessage.append(": "); 69 extendedMessage.append(error.getMessage()); 70 } 71 return (extendedMessage.toString()); 72 } 73 else { 74 return(super.getMessage()); 75 } 76 } 77 78 80 public String toString() { 81 StringBuffer s = new StringBuffer (); 82 s.append(super.toString()); 83 if(getCause()!=null) { 84 s.append(": "); 85 s.append(getCause().toString()); 86 } 87 return s.toString(); 88 } 89 90 public void printStackTrace() { 91 super.printStackTrace(); 92 if(getCause()!=null) 93 getCause().printStackTrace(); 94 } 95 96 public void printStackTrace( PrintStream s ) { 97 super.printStackTrace(s); 98 if(getCause()!=null) 99 getCause().printStackTrace(s); 100 } 101 102 public void printStackTrace( PrintWriter s ) { 103 super.printStackTrace(s); 104 if(getCause()!=null) 105 getCause().printStackTrace(s); 106 } 107 108 private final static int linesBefore = 3; 109 private final static int linesAfter = 3; 110 111 112 private final static String getString( char[] buffer, int start, int end) { 113 int currentLine = 1; 114 int currentPos = 0; 115 116 while(currentLine < start && currentPos < buffer.length) { 117 if (buffer[currentPos++] == '\n') { 118 currentLine++; 119 } 120 } 121 int startPos = currentPos; 122 123 while(currentLine < (end+1) && currentPos < buffer.length) { 124 if (buffer[currentPos++] == '\n') { 125 currentLine++; 126 } 127 } 128 int endPos = currentPos; 129 130 return( new String ( buffer, startPos, endPos-startPos )); 131 } 132 133 private String getSource( String filename ) { 134 File file = new File (filename); 135 long fileSize = file.length(); 136 if (file != null && file.exists() && file.isFile() && fileSize > 0) { 137 if (fileSize < Integer.MAX_VALUE || fileSize < 10 * 1024 * 1024) { 139 char[] buffer = new char[(int) fileSize]; 140 try { 141 FileReader fileReader = new FileReader (file); 142 fileReader.read(buffer, 0, (int) fileSize); 143 144 StringBuffer listing = new StringBuffer (); 145 146 for (int i = 0; i < errors.length; i++) { 147 CompilerError error = errors[i]; 148 149 int start = error.getStartLine(); 150 int end = error.getEndLine(); 151 152 if (start > 0 && end > 0) { 153 String before = getString(buffer, start - 1 - linesBefore, start - 1); 154 String itself = getString(buffer, start, end); 155 String after = getString(buffer, end + 1, end + 1 + linesAfter); 156 157 listing.append("ERROR ").append(i + 1).append(" (").append(error.getFile()).append("):\n"); 158 listing.append("...\n"); 159 listing.append(before); 160 listing.append("\n// start error (lines ").append(error.getStartLine()).append("-").append(error.getEndLine()).append(") \"").append(error.getMessage()).append("\"\n"); 161 listing.append(itself); 162 listing.append("\n// end error\n"); 163 listing.append(after); 164 listing.append("\n...\n"); 165 } 166 } 167 168 fileReader.close(); 169 170 return (listing.toString()); 171 } 172 catch (Exception e) { 173 } 174 } 175 } 176 return (null); 177 } 178 179 206 } 207 | Popular Tags |