1 18 package org.objectweb.kilim.repository; 19 20 import java.io.IOException ; 21 import java.io.OutputStream ; 22 import java.io.PrintStream ; 23 import java.io.PrintWriter ; 24 import java.io.StringWriter ; 25 26 import javax.xml.parsers.ParserConfigurationException ; 27 import org.objectweb.kilim.KilimException; 28 import org.xml.sax.SAXException ; 29 import org.xml.sax.SAXParseException ; 30 31 36 public class BasicParserErrorHandler implements ParserErrorHandler { 37 private PrintStream out; 38 private static final String BASE_HEADER = "Kilim Parser v1.0 "; 39 private String header = BASE_HEADER; 40 private String templateName; 41 42 45 public BasicParserErrorHandler() { 46 out = System.err; 47 } 48 49 53 public BasicParserErrorHandler(OutputStream os) { 54 out = new PrintStream (os); 55 } 56 57 61 public void setTemplateName(String aName) { 62 templateName = aName; 63 header = BASE_HEADER + " [ template : " + aName + " ] "; 64 } 65 66 69 public void handleIOException(IOException ioex) { 70 out.println(header + ioex.getLocalizedMessage()); 71 } 72 73 76 public void handleSAXException(SAXException saxex) { 77 if (saxex instanceof SAXParseException ) { 78 handleFatalSAXParseException((SAXParseException ) saxex); 79 } else { 80 out.println(header + saxex.getLocalizedMessage()); 81 } 82 } 83 84 87 public void handleParserConfigurationException(ParserConfigurationException pcex) { 88 out.println(header + pcex.getLocalizedMessage()); 89 } 90 91 94 public void handleWarningSAXParseException(SAXParseException saxpex) { 95 out.println(getParseErrorMessage(saxpex, "WARNING")); 96 } 97 98 101 public void handleErrorSAXParseException(SAXParseException saxpex) { 102 out.println(getParseErrorMessage(saxpex, "ERROR")); 103 } 104 105 108 public void handleFatalSAXParseException(SAXParseException saxpex) { 109 out.println(getParseErrorMessage(saxpex, "FATAL")); 110 } 111 112 115 public void handleKilimException(KilimException ex) { 116 out.println("Kilim Exception " + ex); 118 } 119 120 private String getParseErrorMessage(SAXParseException saxex, String level) { 121 StringBuffer msg = new StringBuffer (header + level); 122 int line = saxex.getLineNumber(); 123 int col = saxex.getColumnNumber(); 124 String publicId = saxex.getPublicId(); 125 String systemId = saxex.getSystemId(); 126 Exception embedded = saxex.getException(); 127 if (line > -1) { 128 msg.append(" at line " + line); 129 } 130 if (col > -1) { 131 msg.append(" at column " + col); 132 } 133 if (publicId != null) { 134 msg.append(" at public id " + publicId); 135 } 136 if (systemId != null) { 137 msg.append(" at system id " + systemId); 138 } 139 if (embedded != null) { 140 msg.append(" with embedded exception " + embedded); 141 } 142 msg.append(" : " + saxex.getLocalizedMessage()); 143 144 StringWriter strng_wrtr = new StringWriter (); 145 PrintWriter prt_wrtr = new PrintWriter (strng_wrtr); 146 return msg.toString(); 147 } 148 } 149 | Popular Tags |