1 package org.ejen; 22 23 import java.lang.reflect.InvocationTargetException ; 24 import java.util.Properties ; 25 import java.util.Vector ; 26 import java.util.Enumeration ; 27 import java.io.PrintWriter ; 28 import java.io.StringWriter ; 29 import javax.xml.transform.TransformerException ; 30 import javax.xml.transform.SourceLocator ; 31 import org.apache.xml.utils.WrappedRuntimeException; 32 import org.xml.sax.SAXException ; 33 import org.xml.sax.SAXParseException ; 34 35 40 public class EjenErrors { 41 public static final String EJEN_INFORMATION = "Ejen information"; 42 public static final String STACK_TRACE = "stack-trace"; 43 public static final String ID_FILE = "file"; 44 public static final String ID_NOTE = "note"; 45 public static final String ID_MESSAGE = "message"; 46 public static final String ID_PUBLIC_ID = "public-id"; 47 public static final String ID_SYSTEM_ID = "system-id"; 48 public static final String ID_LINE = "line"; 49 public static final String ID_COLUMN = "column"; 50 public static final String LINE_SEPARATOR = System.getProperty("line.separator", 51 "\n"); 52 private EjenErrors() {} 53 54 63 public static EjenError[] get(String file, String note, Throwable t) { 64 return get(file, note, t, false); 65 } 66 67 93 public static EjenError[] get(String file, String note, Throwable t, boolean printStackTrace) { 94 95 Vector errors = new Vector (); 96 Throwable lastNonNullThrowable = null; 97 98 if (file != null || note != null) { 99 EjenError ee = new EjenError(EJEN_INFORMATION); 100 101 if (file != null) { 102 ee.putMessage(ID_FILE, file); 103 } 104 if (note != null) { 105 ee.putMessage(ID_NOTE, note); 106 } 107 errors.add(ee); 108 } 109 int line, column; 110 String publicId, systemId, eMsg; 111 112 while (t != null) { 113 lastNonNullThrowable = t; 114 EjenError ee = new EjenError(t.getClass().getName()); 115 116 if (t.getMessage() != null) { 117 ee.putMessage(ID_MESSAGE, t.getMessage()); 118 } 119 line = column = -1; 120 publicId = systemId = null; 121 if (t instanceof EjenException) { 122 t = ((EjenException) t).getEmbeddedThrowable(); 123 } else if (t instanceof WrappedRuntimeException) { 124 t = ((WrappedRuntimeException) t).getException(); 125 } else if (t instanceof SAXException ) { 126 SAXException se = (SAXException ) t; 127 128 if (se instanceof SAXParseException ) { 129 SAXParseException spe = (SAXParseException ) se; 130 131 column = spe.getColumnNumber(); 132 line = spe.getLineNumber(); 133 publicId = spe.getPublicId(); 134 systemId = spe.getSystemId(); 135 } 136 t = se.getException(); 137 } else if (t instanceof TransformerException ) { 138 TransformerException te = (TransformerException ) t; 139 SourceLocator sl = te.getLocator(); 140 141 if (sl != null) { 142 column = sl.getColumnNumber(); 143 line = sl.getLineNumber(); 144 publicId = sl.getPublicId(); 145 systemId = sl.getSystemId(); 146 } 147 t = ((TransformerException ) t).getCause(); 148 } else if (t instanceof InvocationTargetException ) { 149 t = ((InvocationTargetException ) t).getTargetException(); 150 } else { 151 t = null; 152 } 153 if (publicId != null) { 154 ee.putMessage(ID_PUBLIC_ID, publicId); 155 } 156 if (systemId != null) { 157 ee.putMessage(ID_SYSTEM_ID, systemId); 158 } 159 if (line != -1) { 160 ee.putMessage(ID_LINE, Integer.toString(line)); 161 } 162 if (column != -1) { 163 ee.putMessage(ID_COLUMN, Integer.toString(column)); 164 } 165 errors.add(ee); 166 } 167 if (printStackTrace && lastNonNullThrowable != null) { 168 PrintWriter pw = null; 169 EjenError ee = new EjenError(lastNonNullThrowable.getClass().getName()); 170 171 try { 172 StringWriter sw = new StringWriter (); 173 174 pw = new PrintWriter (sw); 175 lastNonNullThrowable.printStackTrace(pw); 176 ee.putMessage(STACK_TRACE, sw.toString()); 177 } catch (Exception e) { 178 ee.putMessage(STACK_TRACE, "(failed)"); 179 } 180 finally { 181 errors.add(ee); 182 pw.close(); 183 } 184 } 185 return (EjenError[]) errors.toArray(new EjenError[0]); 186 } 187 188 202 public static String toString(EjenError[] errors) { 203 if (errors == null) { 204 return "errors.null"; 205 } 206 207 StringBuffer sb = new StringBuffer (); 208 209 for (int i = 0; i < errors.length; i++) { 210 if (i > 0) { 211 sb.append(LINE_SEPARATOR); 212 } 213 sb.append('[').append(errors[i].getName()).append("] {").append(LINE_SEPARATOR); 214 Properties msgs = errors[i].getMessages(); 215 216 for (Enumeration e = msgs.propertyNames(); e.hasMoreElements();) { 217 String name = (String ) (e.nextElement()); 218 219 sb.append(" ").append(name).append(": ").append(msgs.getProperty(name)).append(LINE_SEPARATOR); 220 } 221 sb.append("}"); 222 } 223 return sb.toString(); 224 } 225 226 234 public static String toString(String file, String note, Throwable t) { 235 return toString(get(file, note, t, false)); 236 } 237 238 247 public static String toString(String file, String note, Throwable t, boolean printStackTrace) { 248 return toString(get(file, note, t, printStackTrace)); 249 } 250 251 256 public static class EjenError { 257 private String _name; 258 private Properties _messages; 259 260 264 public EjenError(String name) { 265 _name = name; 266 _messages = new Properties (); 267 } 268 269 273 public String getName() { 274 return _name; 275 } 276 277 281 public Properties getMessages() { 282 return _messages; 283 } 284 285 290 public void putMessage(String id, String message) { 291 _messages.setProperty(id, message); 292 } 293 } 294 } 295 | Popular Tags |