001: /* *****************************************************************************
002: * SolutionMessages.java
003: * ****************************************************************************/
004:
005: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
006: * Copyright 2001-2006 Laszlo Systems, Inc. All Rights Reserved. *
007: * Use is subject to license terms. *
008: * J_LZ_COPYRIGHT_END *********************************************************/
009:
010: package org.openlaszlo.compiler;
011:
012: import java.io.*;
013: import java.lang.*;
014: import java.util.*;
015:
016: // TODO [hqm 3/24/2003] This 3rd-party regexp package can be replaced
017: // by the Java language built-in regexp stuff in Java 1.4, when we
018: // move up to Java 1.4.
019: import org.apache.oro.text.regex.*;
020:
021: /**
022: * Looks up error messages in a database of known errors, and suggests solutions
023: *
024: * @author Henry Minsky
025: */
026:
027: class SolutionMessages {
028:
029: static final Perl5Matcher sMatcher = new Perl5Matcher();
030: static final Perl5Compiler compiler = new Perl5Compiler();
031: static final Perl5Substitution sSubst = new Perl5Substitution();
032:
033: /** Perform string substitution using pattern matching.
034: * @param str the source string
035: * @param pattern pattern to look for
036: * @param subst the string to replace <i>pattern</i> with. Perl5 references to matches are allowed. See
037: * <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>
038: * @return the string with the substitution made for ALL occurences of the pattern
039: */
040:
041: public static String regsub(String str, String pattern, String subst) {
042: return regsub(str, pattern, subst, Util.SUBSTITUTE_ALL);
043: }
044:
045: /** Perform string substitution using pattern matching.
046: * @param str the source string
047: * @param p pattern to look for
048: * @param s the string to replace <i>pattern</i> with. Perl5 references to matches are allowed. See
049: * <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>
050: * @param numSubs number of substitutions to perform, Util.SUBSTITUTE_ALL will cause all occurences to be replaced
051: * @return the string with the substitution made for numSubs occurences of the pattern
052: */
053:
054: public static String regsub(String str, String p, String s,
055: int numSubs) {
056: try {
057: Pattern pattern = compiler.compile(p);
058: Perl5Substitution subst = new Perl5Substitution(s);
059:
060: String result = Util.substitute(sMatcher, pattern, subst,
061: str, numSubs);
062: return result;
063: } catch (MalformedPatternException e) {
064: throw new CompilationError(e);
065: }
066: }
067:
068: /**
069: * Return true if the regexp pattern is found to occur in the input string.
070: * @param input the input string
071: * @param p the pattern
072: */
073:
074: public static boolean regexp(String input, String p) {
075: try {
076: Pattern pattern = compiler.compile(p);
077: return sMatcher.contains(input, pattern);
078: } catch (MalformedPatternException e) {
079: // We should probably print something to a debug log or something to mention that the pattern we tested
080: // threw an error and probably has some bogus regexp syntax.
081: return false;
082: }
083: }
084:
085: // We classify the error messages into several areas, to make it
086: // easier to give a specific solution that might apply.
087: static final String PARSER = "PARSER";
088: static final String VALIDATOR = "VALIDATOR";
089: static final String VIEWCOMPILER = "VIEWCOMPILER";
090:
091: static final ArrayList errs = new ArrayList();
092:
093: static {
094: errs
095: .add(new SolutionMessage(PARSER,
096: // This error message indicates a stray XML escape character
097: /* (non-Javadoc)
098: * @i18n.test
099: * @org-mes="The content of elements must consist of well-formed character data or markup"
100: */
101: org.openlaszlo.i18n.LaszloMessages.getMessage(
102: SolutionMessages.class.getName(),
103: "051018-101"),
104: /* (non-Javadoc)
105: * @i18n.test
106: * @org-mes="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 ']]>'. "
107: */
108: org.openlaszlo.i18n.LaszloMessages.getMessage(
109: SolutionMessages.class.getName(),
110: "051018-108")));
111:
112: errs.add(new SolutionMessage(PARSER,
113: /* (non-Javadoc)
114: * @i18n.test
115: * @org-mes="entity name must immediately follow the '&' in the entity reference"
116: */
117: org.openlaszlo.i18n.LaszloMessages.getMessage(
118: SolutionMessages.class.getName(), "051018-117"),
119: /* (non-Javadoc)
120: * @i18n.test
121: * @org-mes="Look for unescaped '&' characters in your source code."
122: */
123: org.openlaszlo.i18n.LaszloMessages.getMessage(
124: SolutionMessages.class.getName(), "051018-124")));
125:
126: errs.add(new SolutionMessage(VIEWCOMPILER,
127: /* (non-Javadoc)
128: * @i18n.test
129: * @org-mes="Was expecting one of: instanceof"
130: */
131: org.openlaszlo.i18n.LaszloMessages.getMessage(
132: SolutionMessages.class.getName(), "051018-133"),
133: /* (non-Javadoc)
134: * @i18n.test
135: * @org-mes="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\""
136: */
137: org.openlaszlo.i18n.LaszloMessages.getMessage(
138: SolutionMessages.class.getName(), "051018-140")));
139:
140: errs.add(new SolutionMessage(PARSER,
141: /* (non-Javadoc)
142: * @i18n.test
143: * @org-mes="\"\" is not a valid local name"
144: */
145: org.openlaszlo.i18n.LaszloMessages.getMessage(
146: SolutionMessages.class.getName(), "051018-150"),
147: /* (non-Javadoc)
148: * @i18n.test
149: * @org-mes="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."
150: */
151: org.openlaszlo.i18n.LaszloMessages.getMessage(
152: SolutionMessages.class.getName(), "051018-157")));
153:
154: errs.add(new SolutionMessage(PARSER,
155: /* (non-Javadoc)
156: * @i18n.test
157: * @org-mes="The root element is required in a well-formed document"
158: */
159: org.openlaszlo.i18n.LaszloMessages.getMessage(
160: SolutionMessages.class.getName(), "051018-166"),
161: /* (non-Javadoc)
162: * @i18n.test
163: * @org-mes="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."
164: */
165: org.openlaszlo.i18n.LaszloMessages.getMessage(
166: SolutionMessages.class.getName(), "051018-173")));
167:
168: errs.add(new SolutionMessage(PARSER,
169: /* (non-Javadoc)
170: * @i18n.test
171: * @org-mes="Content is not allowed in prolog"
172: */
173: org.openlaszlo.i18n.LaszloMessages.getMessage(
174: SolutionMessages.class.getName(), "051018-182"),
175: /* (non-Javadoc)
176: * @i18n.test
177: * @org-mes="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."
178: */
179: org.openlaszlo.i18n.LaszloMessages.getMessage(
180: SolutionMessages.class.getName(), "051018-189")));
181:
182: errs.add(new SolutionMessage(PARSER,
183: /* (non-Javadoc)
184: * @i18n.test
185: * @org-mes="The reference to entity.*must end with the"
186: */
187: org.openlaszlo.i18n.LaszloMessages.getMessage(
188: SolutionMessages.class.getName(), "051018-198"),
189: /* (non-Javadoc)
190: * @i18n.test
191: * @org-mes="Look for a misplaced or unescaped ampersand ('&') character in your source code."
192: */
193: org.openlaszlo.i18n.LaszloMessages.getMessage(
194: SolutionMessages.class.getName(), "051018-205")));
195:
196: errs.add(new SolutionMessage(VIEWCOMPILER,
197: /* (non-Javadoc)
198: * @i18n.test
199: * @org-mes="Lexical error. The source location is for the element that contains the erroneous script"
200: */
201: org.openlaszlo.i18n.LaszloMessages.getMessage(
202: SolutionMessages.class.getName(), "051018-215"),
203: /* (non-Javadoc)
204: * @i18n.test
205: * @org-mes="Examine the compiler warnings for warnings about undefined class or attribute names."
206: */
207: org.openlaszlo.i18n.LaszloMessages.getMessage(
208: SolutionMessages.class.getName(), "051018-222")));
209:
210: errs.add(new SolutionMessage(PARSER,
211: /* (non-Javadoc)
212: * @i18n.test
213: * @org-mes="Error in building"
214: */
215: org.openlaszlo.i18n.LaszloMessages.getMessage(
216: SolutionMessages.class.getName(), "051018-231"),
217: /* (non-Javadoc)
218: * @i18n.test
219: * @org-mes="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."
220: */
221: org.openlaszlo.i18n.LaszloMessages.getMessage(
222: SolutionMessages.class.getName(), "051018-238")));
223:
224: }
225:
226: /** Look through the list of known error message strings, looking for a match,
227: * and return the suggested solution.
228: *
229: * @param err an error message you want to look up the solution for
230: * @param type the class of error (parser, validator, viewcompiler), or null for any match
231: *
232: * @return a solution message if available, otherwise the empty string
233: */
234: static String findSolution(String err, String type) {
235: for (int i = 0; i < errs.size(); i++) {
236: SolutionMessage msg = (SolutionMessage) errs.get(i);
237: // Look for match of the err message within our error string
238: if ((type == null || msg.errType.equals(type))
239: && regexp(err, msg.errMessage)) {
240: return msg.solution;
241: }
242: }
243: return "";
244: }
245:
246: /** Find matching solution from the first matching solution pattern */
247: static String findSolution(String err) {
248: return findSolution(err, null);
249: }
250:
251: }
252:
253: class SolutionMessage {
254: String errType;
255: String errMessage;
256: String solution;
257:
258: SolutionMessage(String type, String err, String sol) {
259: errType = type;
260: errMessage = err;
261: solution = sol;
262: }
263:
264: }
|