~ubuntu-branches/ubuntu/intrepid/tomcat5.5/intrepid

« back to all changes in this revision

Viewing changes to jasper/src/share/org/apache/jasper/compiler/ErrorDispatcher.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-09-27 11:19:17 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060927111917-wov6fmkz3x6rsl68
Tags: 5.5.17-1ubuntu1
(Build-) depend on libmx4j-java (>= 3.0).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.jasper.compiler;
 
17
 
 
18
import java.io.BufferedReader;
 
19
import java.io.IOException;
 
20
import java.io.StringReader;
 
21
import java.util.Vector;
 
22
import java.net.MalformedURLException;
 
23
 
 
24
import org.apache.jasper.JasperException;
 
25
import org.xml.sax.SAXException;
 
26
 
 
27
/**
 
28
 * Class responsible for dispatching JSP parse and javac compilation errors
 
29
 * to the configured error handler.
 
30
 *
 
31
 * This class is also responsible for localizing any error codes before they
 
32
 * are passed on to the configured error handler.
 
33
 * 
 
34
 * In the case of a Java compilation error, the compiler error message is
 
35
 * parsed into an array of JavacErrorDetail instances, which is passed on to 
 
36
 * the configured error handler.
 
37
 *
 
38
 * @author Jan Luehe
 
39
 * @author Kin-man Chung
 
40
 */
 
41
public class ErrorDispatcher {
 
42
 
 
43
    // Custom error handler
 
44
    private ErrorHandler errHandler;
 
45
 
 
46
    // Indicates whether the compilation was initiated by JspServlet or JspC
 
47
    private boolean jspcMode = false;
 
48
 
 
49
 
 
50
    /*
 
51
     * Constructor.
 
52
     *
 
53
     * @param jspcMode true if compilation has been initiated by JspC, false
 
54
     * otherwise
 
55
     */
 
56
    public ErrorDispatcher(boolean jspcMode) {
 
57
        // XXX check web.xml for custom error handler
 
58
        errHandler = new DefaultErrorHandler();
 
59
        this.jspcMode = jspcMode;
 
60
    }
 
61
 
 
62
    /*
 
63
     * Dispatches the given JSP parse error to the configured error handler.
 
64
     *
 
65
     * The given error code is localized. If it is not found in the
 
66
     * resource bundle for localized error messages, it is used as the error
 
67
     * message.
 
68
     *
 
69
     * @param errCode Error code
 
70
     */
 
71
    public void jspError(String errCode) throws JasperException {
 
72
        dispatch(null, errCode, null, null);
 
73
    }
 
74
 
 
75
    /*
 
76
     * Dispatches the given JSP parse error to the configured error handler.
 
77
     *
 
78
     * The given error code is localized. If it is not found in the
 
79
     * resource bundle for localized error messages, it is used as the error
 
80
     * message.
 
81
     *
 
82
     * @param where Error location
 
83
     * @param errCode Error code
 
84
     */
 
85
    public void jspError(Mark where, String errCode) throws JasperException {
 
86
        dispatch(where, errCode, null, null);
 
87
    }
 
88
 
 
89
    /*
 
90
     * Dispatches the given JSP parse error to the configured error handler.
 
91
     *
 
92
     * The given error code is localized. If it is not found in the
 
93
     * resource bundle for localized error messages, it is used as the error
 
94
     * message.
 
95
     *
 
96
     * @param n Node that caused the error
 
97
     * @param errCode Error code
 
98
     */
 
99
    public void jspError(Node n, String errCode) throws JasperException {
 
100
        dispatch(n.getStart(), errCode, null, null);
 
101
    }
 
102
 
 
103
    /*
 
104
     * Dispatches the given JSP parse error to the configured error handler.
 
105
     *
 
106
     * The given error code is localized. If it is not found in the
 
107
     * resource bundle for localized error messages, it is used as the error
 
108
     * message.
 
109
     *
 
110
     * @param errCode Error code
 
111
     * @param arg Argument for parametric replacement
 
112
     */
 
113
    public void jspError(String errCode, String arg) throws JasperException {
 
114
        dispatch(null, errCode, new Object[] {arg}, null);
 
115
    }
 
116
 
 
117
    /*
 
118
     * Dispatches the given JSP parse error to the configured error handler.
 
119
     *
 
120
     * The given error code is localized. If it is not found in the
 
121
     * resource bundle for localized error messages, it is used as the error
 
122
     * message.
 
123
     *
 
124
     * @param where Error location
 
125
     * @param errCode Error code
 
126
     * @param arg Argument for parametric replacement
 
127
     */
 
128
    public void jspError(Mark where, String errCode, String arg)
 
129
                throws JasperException {
 
130
        dispatch(where, errCode, new Object[] {arg}, null);
 
131
    }
 
132
 
 
133
    /*
 
134
     * Dispatches the given JSP parse error to the configured error handler.
 
135
     *
 
136
     * The given error code is localized. If it is not found in the
 
137
     * resource bundle for localized error messages, it is used as the error
 
138
     * message.
 
139
     *
 
140
     * @param n Node that caused the error
 
141
     * @param errCode Error code
 
142
     * @param arg Argument for parametric replacement
 
143
     */
 
144
    public void jspError(Node n, String errCode, String arg)
 
145
                throws JasperException {
 
146
        dispatch(n.getStart(), errCode, new Object[] {arg}, null);
 
147
    }
 
148
 
 
149
    /*
 
150
     * Dispatches the given JSP parse error to the configured error handler.
 
151
     *
 
152
     * The given error code is localized. If it is not found in the
 
153
     * resource bundle for localized error messages, it is used as the error
 
154
     * message.
 
155
     *
 
156
     * @param errCode Error code
 
157
     * @param arg1 First argument for parametric replacement
 
158
     * @param arg2 Second argument for parametric replacement
 
159
     */
 
160
    public void jspError(String errCode, String arg1, String arg2)
 
161
                throws JasperException {
 
162
        dispatch(null, errCode, new Object[] {arg1, arg2}, null);
 
163
    }
 
164
 
 
165
    /*
 
166
     * Dispatches the given JSP parse error to the configured error handler.
 
167
     *
 
168
     * The given error code is localized. If it is not found in the
 
169
     * resource bundle for localized error messages, it is used as the error
 
170
     * message.
 
171
     *
 
172
     * @param errCode Error code
 
173
     * @param arg1 First argument for parametric replacement
 
174
     * @param arg2 Second argument for parametric replacement
 
175
     * @param arg3 Third argument for parametric replacement
 
176
     */
 
177
    public void jspError(String errCode, String arg1, String arg2, String arg3)
 
178
                throws JasperException {
 
179
        dispatch(null, errCode, new Object[] {arg1, arg2, arg3}, null);
 
180
    }
 
181
 
 
182
    /*
 
183
     * Dispatches the given JSP parse error to the configured error handler.
 
184
     *
 
185
     * The given error code is localized. If it is not found in the
 
186
     * resource bundle for localized error messages, it is used as the error
 
187
     * message.
 
188
     *
 
189
     * @param where Error location
 
190
     * @param errCode Error code
 
191
     * @param arg1 First argument for parametric replacement
 
192
     * @param arg2 Second argument for parametric replacement
 
193
     */
 
194
    public void jspError(Mark where, String errCode, String arg1, String arg2)
 
195
                throws JasperException {
 
196
        dispatch(where, errCode, new Object[] {arg1, arg2}, null);
 
197
    }
 
198
 
 
199
    /*
 
200
     * Dispatches the given JSP parse error to the configured error handler.
 
201
     *
 
202
     * The given error code is localized. If it is not found in the
 
203
     * resource bundle for localized error messages, it is used as the error
 
204
     * message.
 
205
     *
 
206
     * @param where Error location
 
207
     * @param errCode Error code
 
208
     * @param arg1 First argument for parametric replacement
 
209
     * @param arg2 Second argument for parametric replacement
 
210
     * @param arg3 Third argument for parametric replacement
 
211
     */
 
212
 
 
213
    public void jspError(Mark where, String errCode, String arg1, String arg2,
 
214
                         String arg3)
 
215
                throws JasperException {
 
216
        dispatch(where, errCode, new Object[] {arg1, arg2, arg3}, null);
 
217
    }
 
218
 
 
219
    /*
 
220
     * Dispatches the given JSP parse error to the configured error handler.
 
221
     *
 
222
     * The given error code is localized. If it is not found in the
 
223
     * resource bundle for localized error messages, it is used as the error
 
224
     * message.
 
225
     *
 
226
     * @param n Node that caused the error
 
227
     * @param errCode Error code
 
228
     * @param arg1 First argument for parametric replacement
 
229
     * @param arg2 Second argument for parametric replacement
 
230
     */
 
231
 
 
232
    public void jspError(Node n, String errCode, String arg1, String arg2)
 
233
                throws JasperException {
 
234
        dispatch(n.getStart(), errCode, new Object[] {arg1, arg2}, null);
 
235
    }
 
236
 
 
237
    /*
 
238
     * Dispatches the given JSP parse error to the configured error handler.
 
239
     *
 
240
     * The given error code is localized. If it is not found in the
 
241
     * resource bundle for localized error messages, it is used as the error
 
242
     * message.
 
243
     *
 
244
     * @param n Node that caused the error
 
245
     * @param errCode Error code
 
246
     * @param arg1 First argument for parametric replacement
 
247
     * @param arg2 Second argument for parametric replacement
 
248
     * @param arg3 Third argument for parametric replacement
 
249
     */
 
250
 
 
251
    public void jspError(Node n, String errCode, String arg1, String arg2,
 
252
                         String arg3)
 
253
                throws JasperException {
 
254
        dispatch(n.getStart(), errCode, new Object[] {arg1, arg2, arg3}, null);
 
255
    }
 
256
 
 
257
    /*
 
258
     * Dispatches the given parsing exception to the configured error handler.
 
259
     *
 
260
     * @param e Parsing exception
 
261
     */
 
262
    public void jspError(Exception e) throws JasperException {
 
263
        dispatch(null, null, null, e);
 
264
    }
 
265
 
 
266
    /*
 
267
     * Dispatches the given JSP parse error to the configured error handler.
 
268
     *
 
269
     * The given error code is localized. If it is not found in the
 
270
     * resource bundle for localized error messages, it is used as the error
 
271
     * message.
 
272
     *
 
273
     * @param errCode Error code
 
274
     * @param arg Argument for parametric replacement
 
275
     * @param e Parsing exception
 
276
     */
 
277
    public void jspError(String errCode, String arg, Exception e)
 
278
                throws JasperException {
 
279
        dispatch(null, errCode, new Object[] {arg}, e);
 
280
    }
 
281
 
 
282
    /*
 
283
     * Dispatches the given JSP parse error to the configured error handler.
 
284
     *
 
285
     * The given error code is localized. If it is not found in the
 
286
     * resource bundle for localized error messages, it is used as the error
 
287
     * message.
 
288
     *
 
289
     * @param n Node that caused the error
 
290
     * @param errCode Error code
 
291
     * @param arg Argument for parametric replacement
 
292
     * @param e Parsing exception
 
293
     */
 
294
    public void jspError(Node n, String errCode, String arg, Exception e)
 
295
                throws JasperException {
 
296
        dispatch(n.getStart(), errCode, new Object[] {arg}, e);
 
297
    }
 
298
 
 
299
    /**
 
300
     * Parses the given error message into an array of javac compilation error
 
301
     * messages (one per javac compilation error line number).
 
302
     *
 
303
     * @param errMsg Error message
 
304
     * @param fname Name of Java source file whose compilation failed
 
305
     * @param page Node representation of JSP page from which the Java source
 
306
     * file was generated
 
307
     *
 
308
     * @return Array of javac compilation errors, or null if the given error
 
309
     * message does not contain any compilation error line numbers
 
310
     */
 
311
    public static JavacErrorDetail[] parseJavacErrors(String errMsg,
 
312
                                                      String fname,
 
313
                                                      Node.Nodes page)
 
314
            throws JasperException, IOException {
 
315
 
 
316
        return parseJavacMessage(errMsg, fname, page);
 
317
    }
 
318
 
 
319
    /*
 
320
     * Dispatches the given javac compilation errors to the configured error
 
321
     * handler.
 
322
     *
 
323
     * @param javacErrors Array of javac compilation errors
 
324
     */
 
325
    public void javacError(JavacErrorDetail[] javacErrors)
 
326
            throws JasperException {
 
327
 
 
328
        errHandler.javacError(javacErrors);
 
329
    }
 
330
 
 
331
 
 
332
    /*
 
333
     * Dispatches the given compilation error report and exception to the
 
334
     * configured error handler.
 
335
     *
 
336
     * @param errorReport Compilation error report
 
337
     * @param e Compilation exception
 
338
     */
 
339
    public void javacError(String errorReport, Exception e)
 
340
                throws JasperException {
 
341
 
 
342
        errHandler.javacError(errorReport, e);
 
343
    }
 
344
 
 
345
 
 
346
    //*********************************************************************
 
347
    // Private utility methods
 
348
 
 
349
    /*
 
350
     * Dispatches the given JSP parse error to the configured error handler.
 
351
     *
 
352
     * The given error code is localized. If it is not found in the
 
353
     * resource bundle for localized error messages, it is used as the error
 
354
     * message.
 
355
     *
 
356
     * @param where Error location
 
357
     * @param errCode Error code
 
358
     * @param args Arguments for parametric replacement
 
359
     * @param e Parsing exception
 
360
     */
 
361
    private void dispatch(Mark where, String errCode, Object[] args,
 
362
                          Exception e) throws JasperException {
 
363
        String file = null;
 
364
        String errMsg = null;
 
365
        int line = -1;
 
366
        int column = -1;
 
367
        boolean hasLocation = false;
 
368
 
 
369
        // Localize
 
370
        if (errCode != null) {
 
371
            errMsg = Localizer.getMessage(errCode, args);
 
372
        } else if (e != null) {
 
373
            // give a hint about what's wrong
 
374
            errMsg = e.getMessage();
 
375
        }
 
376
 
 
377
        // Get error location
 
378
        if (where != null) {
 
379
            if (jspcMode) {
 
380
                // Get the full URL of the resource that caused the error
 
381
                try {
 
382
                    file = where.getURL().toString();
 
383
                } catch (MalformedURLException me) {
 
384
                    // Fallback to using context-relative path
 
385
                    file = where.getFile();
 
386
                }
 
387
            } else {
 
388
                // Get the context-relative resource path, so as to not
 
389
                // disclose any local filesystem details
 
390
                file = where.getFile();
 
391
            }
 
392
            line = where.getLineNumber();
 
393
            column = where.getColumnNumber();
 
394
            hasLocation = true;
 
395
        }
 
396
 
 
397
        // Get nested exception
 
398
        Exception nestedEx = e;
 
399
        if ((e instanceof SAXException)
 
400
                && (((SAXException) e).getException() != null)) {
 
401
            nestedEx = ((SAXException) e).getException();
 
402
        }
 
403
 
 
404
        if (hasLocation) {
 
405
            errHandler.jspError(file, line, column, errMsg, nestedEx);
 
406
        } else {
 
407
            errHandler.jspError(errMsg, nestedEx);
 
408
        }
 
409
    }
 
410
 
 
411
    /*
 
412
     * Parses the given Java compilation error message, which may contain one
 
413
     * or more compilation errors, into an array of JavacErrorDetail instances.
 
414
     *
 
415
     * Each JavacErrorDetail instance contains the information about a single
 
416
     * compilation error.
 
417
     *
 
418
     * @param errMsg Compilation error message that was generated by the
 
419
     * javac compiler
 
420
     * @param fname Name of Java source file whose compilation failed
 
421
     * @param page Node representation of JSP page from which the Java source
 
422
     * file was generated
 
423
     *
 
424
     * @return Array of JavacErrorDetail instances corresponding to the
 
425
     * compilation errors
 
426
     */
 
427
    private static JavacErrorDetail[] parseJavacMessage(
 
428
                                String errMsg, String fname, Node.Nodes page)
 
429
                throws IOException, JasperException {
 
430
 
 
431
        Vector errVec = new Vector();
 
432
        StringBuffer errMsgBuf = null;
 
433
        int lineNum = -1;
 
434
        JavacErrorDetail javacError = null;
 
435
        
 
436
        BufferedReader reader = new BufferedReader(new StringReader(errMsg));
 
437
        
 
438
        /*
 
439
         * Parse compilation errors. Each compilation error consists of a file
 
440
         * path and error line number, followed by a number of lines describing
 
441
         * the error.
 
442
         */
 
443
        String line = null;
 
444
        while ((line = reader.readLine()) != null) {
 
445
            
 
446
            /*
 
447
             * Error line number is delimited by set of colons.
 
448
             * Ignore colon following drive letter on Windows (fromIndex = 2).
 
449
             * XXX Handle deprecation warnings that don't have line info
 
450
             */
 
451
            int beginColon = line.indexOf(':', 2); 
 
452
            int endColon = line.indexOf(':', beginColon + 1);
 
453
            if ((beginColon >= 0) && (endColon >= 0)) {
 
454
                if (javacError != null) {
 
455
                    // add previous error to error vector
 
456
                    errVec.add(javacError);
 
457
                }
 
458
                
 
459
                String lineNumStr = line.substring(beginColon + 1, endColon);
 
460
                try {
 
461
                    lineNum = Integer.parseInt(lineNumStr);
 
462
                } catch (NumberFormatException e) {
 
463
                    // XXX
 
464
                }
 
465
                
 
466
                errMsgBuf = new StringBuffer();
 
467
                
 
468
                javacError = createJavacError(fname, page, errMsgBuf, lineNum);
 
469
            }
 
470
            
 
471
            // Ignore messages preceding first error
 
472
            if (errMsgBuf != null) {
 
473
                errMsgBuf.append(line);
 
474
                errMsgBuf.append("\n");
 
475
            }
 
476
        }
 
477
        
 
478
        // Add last error to error vector
 
479
        if (javacError != null) {
 
480
            errVec.add(javacError);
 
481
        } 
 
482
        
 
483
        reader.close();
 
484
        
 
485
        JavacErrorDetail[] errDetails = null;
 
486
        if (errVec.size() > 0) {
 
487
            errDetails = new JavacErrorDetail[errVec.size()];
 
488
            errVec.copyInto(errDetails);
 
489
        }
 
490
        
 
491
        return errDetails;
 
492
    }
 
493
 
 
494
 
 
495
    /**
 
496
     * @param fname
 
497
     * @param page
 
498
     * @param errMsgBuf
 
499
     * @param lineNum
 
500
     * @return JavacErrorDetail The error details
 
501
     * @throws JasperException
 
502
     */
 
503
    public static JavacErrorDetail createJavacError(String fname, Node.Nodes page, 
 
504
            StringBuffer errMsgBuf, int lineNum) throws JasperException {
 
505
        JavacErrorDetail javacError;
 
506
        // Attempt to map javac error line number to line in JSP page
 
507
        ErrorVisitor errVisitor = new ErrorVisitor(lineNum);
 
508
        page.visit(errVisitor);
 
509
        Node errNode = errVisitor.getJspSourceNode();
 
510
        if ((errNode != null) && (errNode.getStart() != null)) {
 
511
            javacError = new JavacErrorDetail(
 
512
                    fname,
 
513
                    lineNum,
 
514
                    errNode.getStart().getFile(),
 
515
                    errNode.getStart().getLineNumber(),
 
516
                    errMsgBuf);
 
517
        } else {
 
518
            /*
 
519
             * javac error line number cannot be mapped to JSP page
 
520
             * line number. For example, this is the case if a 
 
521
             * scriptlet is missing a closing brace, which causes
 
522
             * havoc with the try-catch-finally block that the code
 
523
             * generator places around all generated code: As a result
 
524
             * of this, the javac error line numbers will be outside
 
525
             * the range of begin and end java line numbers that were
 
526
             * generated for the scriptlet, and therefore cannot be
 
527
             * mapped to the start line number of the scriptlet in the
 
528
             * JSP page.
 
529
             * Include just the javac error info in the error detail.
 
530
             */
 
531
            javacError = new JavacErrorDetail(
 
532
                    fname,
 
533
                    lineNum,
 
534
                    errMsgBuf);
 
535
        }
 
536
        return javacError;
 
537
    }
 
538
 
 
539
 
 
540
    /*
 
541
     * Visitor responsible for mapping a line number in the generated servlet
 
542
     * source code to the corresponding JSP node.
 
543
     */
 
544
    static class ErrorVisitor extends Node.Visitor {
 
545
 
 
546
        // Java source line number to be mapped
 
547
        private int lineNum;
 
548
 
 
549
        /*
 
550
         * JSP node whose Java source code range in the generated servlet
 
551
         * contains the Java source line number to be mapped
 
552
         */
 
553
        Node found;
 
554
 
 
555
        /*
 
556
         * Constructor.
 
557
         *
 
558
         * @param lineNum Source line number in the generated servlet code
 
559
         */
 
560
        public ErrorVisitor(int lineNum) {
 
561
            this.lineNum = lineNum;
 
562
        }
 
563
 
 
564
        public void doVisit(Node n) throws JasperException {
 
565
            if ((lineNum >= n.getBeginJavaLine())
 
566
                    && (lineNum < n.getEndJavaLine())) {
 
567
                found = n;
 
568
            }
 
569
        }
 
570
 
 
571
        /*
 
572
         * Gets the JSP node to which the source line number in the generated
 
573
         * servlet code was mapped.
 
574
         *
 
575
         * @return JSP node to which the source line number in the generated
 
576
         * servlet code was mapped
 
577
         */
 
578
        public Node getJspSourceNode() {
 
579
            return found;
 
580
        }
 
581
    }
 
582
}