1 16 17 package org.apache.commons.jelly; 18 19 import java.io.PrintStream ; 20 import java.io.PrintWriter ; 21 22 28 29 public class JellyException extends Exception implements LocationAware { 30 31 32 private Throwable cause; 33 34 35 private String fileName; 36 37 38 private String elementName; 39 40 41 private int lineNumber = -1; 42 43 44 private int columnNumber = -1; 45 46 public JellyException() { 47 } 48 49 public JellyException(String message) { 50 super(message); 51 } 52 53 public JellyException(String message, Throwable cause) { 54 super(message); 55 this.cause = cause; 56 } 57 58 public JellyException(Throwable cause) { 59 super(cause.getLocalizedMessage()); 60 this.cause = cause; 61 } 62 63 public JellyException(Throwable cause, String fileName, String elementName, int columnNumber, int lineNumber) { 64 this(cause.getLocalizedMessage(), cause, fileName, elementName, columnNumber, lineNumber); 65 } 66 67 public JellyException(String reason, Throwable cause, String fileName, String elementName, int columnNumber, int lineNumber) { 68 super( (reason==null?cause.getClass().getName():reason) ); 69 this.cause = cause; 70 this.fileName = fileName; 71 this.elementName = elementName; 72 this.columnNumber = columnNumber; 73 this.lineNumber = lineNumber; 74 } 75 76 public JellyException(String reason, String fileName, String elementName, int columnNumber, int lineNumber) { 77 super(reason); 78 this.fileName = fileName; 79 this.elementName = elementName; 80 this.columnNumber = columnNumber; 81 this.lineNumber = lineNumber; 82 } 83 84 public Throwable getCause() { 85 return cause; 86 } 87 88 89 92 public int getLineNumber() { 93 return lineNumber; 94 } 95 96 99 public void setLineNumber(int lineNumber) { 100 this.lineNumber = lineNumber; 101 } 102 103 106 public int getColumnNumber() { 107 return columnNumber; 108 } 109 110 113 public void setColumnNumber(int columnNumber) { 114 this.columnNumber = columnNumber; 115 } 116 117 120 public String getFileName() { 121 return fileName; 122 } 123 124 127 public void setFileName(String fileName) { 128 this.fileName = fileName; 129 } 130 131 132 135 public String getElementName() { 136 return elementName; 137 } 138 139 142 public void setElementName(String elementName) { 143 this.elementName = elementName; 144 } 145 146 147 public String getMessage() { 148 if (fileName == null && lineNumber == -1 && columnNumber == -1 && elementName == null) { 149 return getReason(); 150 } else { 151 return fileName + ":" + lineNumber + ":" + columnNumber + ": <" + elementName + "> " + getReason(); 152 } 153 } 154 155 public String getReason() { 156 return super.getMessage(); 157 } 158 159 public void printStackTrace(PrintWriter s) { 161 synchronized (s) { 162 super.printStackTrace(s); 163 if (cause != null && !isChainingSupported()) { 164 s.println("Root cause"); 165 cause.printStackTrace(s); 166 } 167 } 168 } 169 170 public void printStackTrace(PrintStream s) { 171 synchronized (s) { 172 super.printStackTrace(s); 173 if (cause != null && !isChainingSupported()) { 174 s.println("Root cause"); 175 cause.printStackTrace(s); 176 } 177 } 178 } 179 180 public void printStackTrace() { 181 super.printStackTrace(); 182 if (cause != null && !isChainingSupported()) { 183 System.out.println("Root cause"); 184 cause.printStackTrace(); 185 } 186 } 187 188 private boolean isChainingSupported() { 189 try { 190 Throwable .class.getMethod("getCause", new Class [0]); 191 return true; 192 } catch (NoSuchMethodException e) { 193 return false; 194 } catch (SecurityException e) { 195 return false; 196 } 197 } 198 } 199 | Popular Tags |