KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > js > JsParserException


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.js;
17
18 /**
19  * Indicates inability to parse JavaScript source.
20  */

21 public class JsParserException extends Exception JavaDoc {
22
23   /**
24    * Represents the location of a parser exception.
25    */

26   public static class SourceDetail {
27     private final int line;
28
29     private final int lineOffset;
30
31     private final String JavaDoc lineSource;
32
33     public SourceDetail(int line, String JavaDoc lineSource, int lineOffset) {
34       this.line = line;
35       this.lineSource = lineSource;
36       this.lineOffset = lineOffset;
37     }
38
39     public int getLine() {
40       return line;
41     }
42
43     public int getLineOffset() {
44       return lineOffset;
45     }
46
47     public String JavaDoc getLineSource() {
48       return lineSource;
49     }
50   }
51
52   private final SourceDetail sourceDetail;
53
54   public JsParserException(String JavaDoc msg) {
55     super(msg);
56     sourceDetail = null;
57   }
58
59   public JsParserException(String JavaDoc msg, int line, String JavaDoc lineSource,
60       int lineOffset) {
61     this(msg, line, lineSource, lineOffset, null);
62   }
63
64   public JsParserException(String JavaDoc msg, int line, String JavaDoc lineSource,
65       int lineOffset, Throwable JavaDoc cause) {
66     super(msg, cause);
67     sourceDetail = new SourceDetail(line, lineSource, lineOffset);
68   }
69
70   public JsParserException(String JavaDoc msg, Throwable JavaDoc cause) {
71     super(msg, cause);
72     sourceDetail = null;
73   }
74
75   public String JavaDoc getDescription() {
76     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
77
78     if (sourceDetail != null) {
79       sb.append("Line ");
80       sb.append(sourceDetail.getLine());
81       sb.append(": ");
82       sb.append(getMessage());
83       sb.append("\n");
84       sb.append("> ");
85       sb.append(sourceDetail.getLineSource());
86       sb.append("\n> ");
87       for (int i = 0, n = sourceDetail.getLineOffset(); i < n; ++i) {
88         sb.append('-');
89       }
90       sb.append('^');
91     } else {
92       sb.append(getMessage());
93     }
94
95     return sb.toString();
96   }
97
98   /**
99    * Provides additional source detail in some cases.
100    *
101    * @return additional detail regarding the error, or <code>null</code> if no
102    * additional detail is available
103    */

104   public SourceDetail getSourceDetail() {
105     return sourceDetail;
106   }
107 }
108
Popular Tags