KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > LanguageException


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileReader JavaDoc;
25
26 /**
27  * The language exception.
28  *
29  * @author <a HREF="mailto:ricardo@apache.org">Ricardo Rocha</a>
30  * @author <a HREF="mailto:tcurdt@apache.org">Torsten Curdt</a>
31  * @version CVS $Id: LanguageException.java 30932 2004-07-29 17:35:38Z vgritsenko $
32  */

33 public class LanguageException extends CascadingException {
34
35     private CompilerError[] errors = null;
36     private String JavaDoc filename = null;
37
38     public LanguageException(String JavaDoc message, String JavaDoc filename, CompilerError[] errors) {
39         super(message);
40         this.filename = filename;
41         this.errors = errors;
42     }
43
44     public LanguageException(String JavaDoc message) {
45         super(message);
46     }
47
48     public LanguageException(String JavaDoc message, Throwable JavaDoc t) {
49         super(message, t);
50     }
51
52     public String JavaDoc getMessage() {
53         if (errors != null) {
54             StringBuffer JavaDoc extendedMessage = new StringBuffer JavaDoc();
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     // Stolen from ProcessingException...
79

80     public String JavaDoc toString() {
81         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
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 JavaDoc s ) {
97         super.printStackTrace(s);
98         if(getCause()!=null)
99             getCause().printStackTrace(s);
100     }
101
102     public void printStackTrace( PrintWriter JavaDoc 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 JavaDoc 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 JavaDoc( buffer, startPos, endPos-startPos ));
131     }
132
133     private String JavaDoc getSource( String JavaDoc filename ) {
134         File JavaDoc file = new File JavaDoc(filename);
135         long fileSize = file.length();
136         if (file != null && file.exists() && file.isFile() && fileSize > 0) {
137             // paranoid checking: nothing larger than ints can handle or 10MB
138
if (fileSize < Integer.MAX_VALUE || fileSize < 10 * 1024 * 1024) {
139                 char[] buffer = new char[(int) fileSize];
140                 try {
141                     FileReader JavaDoc fileReader = new FileReader JavaDoc(file);
142                     fileReader.read(buffer, 0, (int) fileSize);
143
144                     StringBuffer JavaDoc listing = new StringBuffer JavaDoc();
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 JavaDoc before = getString(buffer, start - 1 - linesBefore, start - 1);
154                             String JavaDoc itself = getString(buffer, start, end);
155                             String JavaDoc 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 JavaDoc e) {
173                 }
174             }
175         }
176         return (null);
177     }
178
179     /*
180     public static void main(String[] args) {
181         String s =
182         "1 \n"+
183         "2 System.out.println(\n"+
184         "3 \n"+
185         "4 this.contentHandler.startDocument();\n"+
186         "5 AttributesImpl xspAttr = new AttributesImpl();\n"+
187         "6 \n";
188
189         char[] buffer = s.toCharArray();
190
191         int start = 2;
192         int end = 2;
193
194         String before = getString(buffer, start - 1 - linesBefore, start - 1);
195         String itself = getString(buffer, start, end);
196         String after = getString(buffer, end + 1, end + 1 + linesAfter);
197
198
199         System.out.print(before);
200         System.out.println("--");
201         System.out.print(itself);
202         System.out.println("--");
203         System.out.print(after);
204     }
205     */

206 }
207
Popular Tags