KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > compiler > SolutionMessages


1 /* *****************************************************************************
2  * SolutionMessages.java
3 * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.compiler;
11 import java.io.*;
12 import java.lang.*;
13 import java.util.*;
14
15 // TODO [hqm 3/24/2003] This 3rd-party regexp package can be replaced
16
// by the Java language built-in regexp stuff in Java 1.4, when we
17
// move up to Java 1.4.
18
import org.apache.oro.text.regex.*;
19
20 /**
21  * Looks up error messages in a database of known errors, and suggests solutions
22  *
23  * @author Henry Minsky
24  */

25
26 class SolutionMessages {
27
28     static final Perl5Matcher sMatcher = new Perl5Matcher();
29     static final Perl5Compiler compiler = new Perl5Compiler();
30     static final Perl5Substitution sSubst = new Perl5Substitution();
31
32     /** Perform string substitution using pattern matching.
33      * @param str the source string
34      * @param pattern pattern to look for
35      * @param subst the string to replace <i>pattern</i> with. Perl5 references to matches are allowed. See
36      * <a HREF="http://jakarta.apache.org/oro/api/org/apache/oro/text/regex/Perl5Substitution.html">http://jakarta.apache.org/oro/api/org/apache/oro/text/regex/Perl5Substitution.html</a>
37      * @return the string with the substitution made for ALL occurences of the pattern
38      */

39
40     public static String JavaDoc regsub (String JavaDoc str,
41                           String JavaDoc pattern,
42                           String JavaDoc subst) {
43         return regsub(str, pattern, subst, Util.SUBSTITUTE_ALL);
44     }
45
46     /** Perform string substitution using pattern matching.
47      * @param str the source string
48      * @param pattern pattern to look for
49      * @param subst the string to replace <i>pattern</i> with. Perl5 references to matches are allowed. See
50      * <a HREF="http://jakarta.apache.org/oro/api/org/apache/oro/text/regex/Perl5Substitution.html">http://jakarta.apache.org/oro/api/org/apache/oro/text/regex/Perl5Substitution.html</a>
51      * @param numSubs number of substitutions to perform, Util.SUBSTITUTE_ALL will cause all occurences to be replaced
52      * @return the string with the substitution made for numSubs occurences of the pattern
53      */

54
55     public static String JavaDoc regsub (String JavaDoc str, String JavaDoc p, String JavaDoc s, int numSubs) {
56         try {
57             Pattern pattern = compiler.compile(p);
58             Perl5Substitution subst = new Perl5Substitution(s);
59
60             String JavaDoc result = Util.substitute(sMatcher, pattern, subst, str, numSubs);
61             return result;
62         } catch (MalformedPatternException e) {
63             throw new CompilationError(e);
64         }
65     }
66
67     /**
68      * Return true if the regexp pattern is found to occur in the input string.
69      * @param input the input string
70      * @param p the pattern
71      */

72
73     public static boolean regexp (String JavaDoc input, String JavaDoc p) {
74         try {
75             Pattern pattern = compiler.compile(p);
76             return sMatcher.contains(input, pattern);
77         } catch (MalformedPatternException e) {
78             // We should probably print something to a debug log or something to mention that the pattern we tested
79
// threw an error and probably has some bogus regexp syntax.
80
return false;
81         }
82     }
83
84
85
86     // We classify the error messages into several areas, to make it
87
// easier to give a specific solution that might apply.
88
static final String JavaDoc PARSER = "PARSER";
89     static final String JavaDoc VALIDATOR = "VALIDATOR";
90     static final String JavaDoc VIEWCOMPILER = "VIEWCOMPILER";
91
92     static final ArrayList errs = new ArrayList();
93
94     static {
95         errs.add(new SolutionMessage(PARSER,
96                                      // This error message indicates a stray XML escape character
97
"The content of elements must consist of well-formed character data or markup",
98                                      "Look for stray or unmatched XML escape chars ( <, >, or & ) in your source code. When using '<' in a Script, XML requires wrapping the Script content with '<![CDATA[' and ']]>'. "));
99
100         errs.add(new SolutionMessage(PARSER,
101                                      "entity name must immediately follow the '&' in the entity reference",
102                                      "Look for unescaped '&' characters in your source code."));
103                                      
104         errs.add(new SolutionMessage(VIEWCOMPILER,
105                                      "Was expecting one of: instanceof",
106                                      "The element and attribute names in .lzx files are case-sensitive; Look for a mistake in the upper/lower case spelling of attribute names, i.e., \"onClick\" instead of \"onclick\""));
107
108
109         errs.add(new SolutionMessage(PARSER,
110                                      "\"\" is not a valid local name",
111                                      "Make sure that every <class> and <attribute> tag element contains a non-empty 'name' attribute, and each <method> element contains a non-empty 'name' or 'event' attribute."));
112
113         errs.add(new SolutionMessage(PARSER,
114                                      "The root element is required in a well-formed document",
115                                      "Check for invalid UTF8 characters in your source file. LZX XML files may contain only legal UTF-8 character codes. If you see a character with an accent over it, it might be the problem."));
116
117         errs.add(new SolutionMessage(PARSER,
118                                      "Content is not allowed in prolog",
119                                      "Some text editors may insert a Byte-Order Mark (the sequence of characters 0xEFBBBF) at the start of your source file without your knowledge. Please remove any non-whitespace characters before the start of the first '<' character."));
120
121         errs.add(new SolutionMessage(PARSER,
122                                      "The reference to entity.*must end with the",
123                                      "Look for a misplaced or unescaped ampersand ('&') character in your source code."));
124
125
126         errs.add(new SolutionMessage(VIEWCOMPILER,
127                                      "Lexical error. The source location is for the element that contains the erroneous script",
128                                      "Examine the compiler warnings for warnings about undefined class or attribute names."
129                                      ));
130
131         errs.add(new SolutionMessage(PARSER,
132                                      "Error in building",
133                                      "Check for invalid UTF8 characters in your source file. LZX XML files may contain only legal UTF-8 character codes. If you see a character with an accent over it, it might be the problem."));
134
135
136
137
138     }
139
140
141     /** Look through the list of known error message strings, looking for a match,
142      * and return the suggested solution.
143      *
144      * @param err an error message you want to look up the solution for
145      * @param type the class of error (parser, validator, viewcompiler), or null for any match
146      *
147      * @return a solution message if available, otherwise the empty string
148     */

149     static String JavaDoc findSolution (String JavaDoc err, String JavaDoc type) {
150         for (int i = 0; i < errs.size(); i++) {
151             SolutionMessage msg = (SolutionMessage) errs.get(i);
152             // Look for match of the err message within our error string
153
if ((type == null || msg.errType.equals(type)) && regexp(err, msg.errMessage)) {
154                 return msg.solution;
155             }
156         }
157         return "";
158     }
159
160     /** Find matching solution from the first matching solution pattern */
161     static String JavaDoc findSolution (String JavaDoc err) {
162         return findSolution(err, null);
163     }
164
165 }
166
167 class SolutionMessage {
168         String JavaDoc errType;
169         String JavaDoc errMessage;
170         String JavaDoc solution;
171
172         SolutionMessage(String JavaDoc type, String JavaDoc err, String JavaDoc sol) {
173             errType = type;
174             errMessage = err;
175             solution = sol;
176         }
177
178     }
179
180
Popular Tags