1 4 5 9 10 package org.openlaszlo.compiler; 11 import java.io.File ; 12 import java.util.*; 13 import org.jdom.Element; 14 import org.jdom.JDOMException; 15 import org.xml.sax.SAXParseException ; 16 import org.openlaszlo.utils.ChainedException; 17 import org.openlaszlo.xml.internal.XMLUtils; 18 import org.openlaszlo.sc.parser.ParseException; 19 20 23 public class CompilationError extends RuntimeException { 24 25 private String mPathname = null; 26 27 private Integer mLineNumber = null; 28 29 private Integer mColumnNumber = null; 30 31 private Element mElement = null; 32 private String mAttribute = null; 33 34 private String mFileBase = ""; 35 private List errors = new Vector(); 36 37 38 private String solutionMessage = ""; 39 40 44 public static boolean ThrowCompilationErrors = false; 45 46 49 public CompilationError(String message) { 50 super(message); 51 } 52 53 57 public CompilationError(String message, Element element) { 58 super(message); 59 initElement(element, null); 60 } 61 62 66 public CompilationError(Element element, Throwable cause) { 67 super(getCauseMessage(cause)); 68 initElement(element, cause); 69 } 70 71 public CompilationError(Element element, String attribute, 72 Throwable cause) { 73 super(getCauseMessage(cause)); 74 initElement(element, attribute, cause); 75 } 76 77 78 public CompilationError(Throwable cause, String solution) { 79 this(cause); 80 this.solutionMessage = solution; 81 } 82 83 86 public CompilationError(Throwable cause) { 87 super(getCauseMessage(cause)); 88 SAXParseException se = null; if (cause instanceof JDOMException) { 90 JDOMException je = (JDOMException) cause; 91 if (je.getCause() instanceof SAXParseException ) { 92 se = (SAXParseException ) je.getCause(); 93 } 94 } else if (cause instanceof SAXParseException ) { 95 se = (SAXParseException ) cause; 96 } 97 if (se != null) { 98 initPathname(se.getPublicId()); 99 setLineNumber(se.getLineNumber()); 100 setColumnNumber(se.getColumnNumber()); 101 } 102 } 103 104 105 private static String getCauseMessage(Throwable cause) { 106 if (ThrowCompilationErrors) { 107 cause.printStackTrace(); 108 throw new ChainedException(cause); 109 } 110 if (cause instanceof JDOMException && 111 ((JDOMException) cause).getCause() != null) 112 cause = ((JDOMException) cause).getCause(); 113 String message = cause.getMessage(); 114 if (cause instanceof java.io.FileNotFoundException ) { 115 return "file not found: " + message; 116 } else if (cause instanceof java.lang.NumberFormatException ) { 117 return "invalid number: " + message; 118 } else if (cause instanceof org.openlaszlo.compiler.ViewSchema.ColorFormatException) { 119 return "invalid color: " + message; 120 } else if (cause instanceof ParseException ) { 121 return ((ParseException ) cause).getMessage(false); 122 } else { 123 128 return message; 129 } 130 } 131 132 public void attachErrors(List errors) { 133 this.errors.addAll(errors); 134 } 135 136 140 public void setSolution(String sol) { 141 this.solutionMessage = sol; 142 } 143 144 149 150 void initElement(Element element) { 151 initElement(element, null); 152 } 153 154 void initElement(Element element, Throwable cause) { 155 if (this.mElement != null && mElement != element) { 156 throw new IllegalStateException ("initElement called twice, on " + 157 mElement + " and " + element); 158 } 159 this.mElement = element; 160 this.initPathname(Parser.getSourceMessagePathname(element)); 161 162 if (cause instanceof ParseException ) { 165 this.setLineNumber(((ParseException ) cause).getBeginLine()); 166 this.setColumnNumber(((ParseException ) cause).getBeginColumn()); 167 } else { 168 this.setLineNumber(Parser.getSourceLocation(element, Parser.LINENO).intValue()); 169 this.setColumnNumber(Parser.getSourceLocation(element, Parser.COLNO).intValue()); 170 } 171 } 172 173 void initElement(Element element, String attribute, Throwable cause) { 174 initElement(element, cause); 175 this.mAttribute = attribute; 176 } 177 178 181 public Element getElement() 182 { 183 return this.mElement; 184 } 185 186 public String getSolutionMessage() { 187 return solutionMessage; 188 } 189 190 public void setFileBase(String fileBase) { 191 this.mFileBase = fileBase; 192 } 193 194 199 public void initPathname(String pathname) { 200 if (mPathname != null && mPathname.intern() != pathname.intern()) { 201 throw new IllegalStateException ("initPathname called twice, on " + 202 mPathname + " and " + pathname); 203 } 204 this.mPathname = pathname; 205 } 206 207 211 public String getPathname() { 212 return mPathname; 213 } 214 215 public void setPathname(String pathname) { 218 mPathname = pathname; 219 } 220 221 225 public Integer getColumnNumber() { 226 return mColumnNumber; 227 } 228 229 public void setColumnNumber(int columnNumber) { 230 mColumnNumber = new Integer (columnNumber); 231 } 232 233 public void initColumnNumber(int columnNumber) { 234 if (mColumnNumber != null && mColumnNumber.intValue() != columnNumber) 235 throw new IllegalStateException ("initColumnNumber called twice, on " + 236 mColumnNumber + " and " + columnNumber); 237 setColumnNumber(columnNumber); 238 } 239 240 244 public Integer getLineNumber() { 245 return mLineNumber; 246 } 247 248 public void setLineNumber(int lineNumber) { 249 mLineNumber = new Integer (lineNumber); 250 } 251 252 public void initLineNumber(int lineNumber) { 253 if (mLineNumber != null && mLineNumber.intValue() != lineNumber) 254 throw new IllegalStateException ("initLineNumber called twice, on " + 255 mLineNumber + " and " + lineNumber); 256 setLineNumber(lineNumber); 257 } 258 259 263 public String getMessage (File base) { 264 return getMessage(base.getAbsolutePath() + File.separator); 265 } 266 267 271 public String getMessage (String base) { 272 String errmsg = _getMessage(); 273 274 if (base != null && errmsg.startsWith(base)) { 275 return errmsg.substring(base.length()); 277 } else { 278 return errmsg; 279 } 280 } 281 282 283 286 private String _getMessage() { 287 String message = ""; 288 if (mPathname != null && !super.getMessage().startsWith(mPathname)) { 289 message += mPathname + ":"; 290 if (mLineNumber != null) { 291 message += mLineNumber + ":"; 292 if (mColumnNumber != null) { 293 message += mColumnNumber + ":"; 294 } 295 } 296 message += " "; 297 } 298 299 String errorText = super.getMessage(); 300 if (errorText == null) { 303 errorText = ""; 304 } 305 if (!solutionMessage.equals("") && 306 !errorText.equals("") && 307 !errorText.endsWith(".") && !errorText.endsWith(". ")) { 308 errorText += ". "; 309 } 310 if (!solutionMessage.equals("") && 311 !errorText.endsWith(" ")) { 312 errorText += " "; 313 } 314 return message + errorText + getSolutionMessage(); 315 } 316 317 public String getMessage() { 318 return getMessage(mFileBase + File.separator); 319 } 320 321 public String toHTML() { 322 String sourceFile = getPathname(); 324 String message = getMessage(); 325 String solution = "<font color=\"green\">" + getSolutionMessage() + "</font>"; 326 if (sourceFile != null && message.startsWith(sourceFile)) { 327 message = "<A HREF=\"" + sourceFile + "\">" + sourceFile + "</A>" + 328 message.substring(sourceFile.length()); 329 } 330 return "<HTML><HEAD>" + 331 "<TITLE>" + "Compilation Error" + "</TITLE>" + 332 "</HEAD><BODY>" + message + "<p>" + solution + 333 "</BODY></HTML>"; 334 } 335 336 public String toXML() { 337 StringBuffer buffer = new StringBuffer (); 338 buffer.append("<error>"); 339 buffer.append(XMLUtils.escapeXml(getMessage())); 340 for (Iterator iter = errors.iterator(); iter.hasNext(); ) 341 buffer.append(((CompilationError) iter.next()).toXML()); 342 buffer.append("</error>"); 343 return buffer.toString(); 344 } 345 346 350 public String getErrorMessage() { 351 return super.getMessage(); 352 } 353 } 354 | Popular Tags |