1 package com.icl.saxon; 2 3 import org.xml.sax.SAXException ; 4 import javax.xml.transform.*; 5 import javax.xml.transform.dom.DOMLocator ; 6 import java.io.PrintStream ; 7 8 13 14 public class StandardErrorListener implements ErrorListener { 15 16 int recoveryPolicy = Controller.RECOVER_WITH_WARNINGS; 17 int warningCount = 0; 18 PrintStream errorOutput = System.err; 19 20 24 25 public void setErrorOutput(PrintStream writer) { 26 errorOutput = writer; 27 } 28 29 32 33 public void setRecoveryPolicy(int policy) { 34 recoveryPolicy = policy; 35 } 36 37 56 57 public void warning(TransformerException exception) 58 throws TransformerException { 59 60 if (recoveryPolicy==Controller.RECOVER_SILENTLY) { 61 return; 63 } 64 65 String message = ""; 66 if (exception.getLocator()!=null) { 67 message = getLocationMessage(exception) + "\n "; 68 } 69 message += getExpandedMessage(exception); 70 71 if (recoveryPolicy==Controller.RECOVER_WITH_WARNINGS) { 72 errorOutput.println("Recoverable error"); 73 errorOutput.println(message); 74 warningCount++; 75 if (warningCount > 25) { 76 System.err.println("No more warnings will be displayed"); 77 recoveryPolicy = Controller.RECOVER_SILENTLY; 78 warningCount = 0; 79 } 80 } else { 81 errorOutput.println("Recoverable error"); 82 errorOutput.println(message); 83 errorOutput.println("Processing terminated because error recovery is disabled"); 84 throw new TransformerException(exception); 85 } 86 } 87 88 107 108 public void error(TransformerException exception) throws TransformerException { 109 String message = "Error " + 111 getLocationMessage(exception) + 112 "\n " + 113 getExpandedMessage(exception); 114 errorOutput.println(message); 115 } 116 117 134 135 public void fatalError(TransformerException exception) throws TransformerException { 136 error(exception); 137 throw exception; 138 } 139 140 143 144 public static String getLocationMessage(TransformerException err) { 145 SourceLocator loc = err.getLocator(); 146 if (loc==null) { 147 return ""; 148 } else { 149 String locmessage = ""; 150 if (loc instanceof DOMLocator ) { 151 locmessage += "at " + ((DOMLocator )loc).getOriginatingNode().getNodeName() + " "; 152 } 153 int line = loc.getLineNumber(); 154 int column = loc.getColumnNumber(); 155 if (line<0 && column>0) { 156 locmessage += "at byte " + column + " "; 157 } else { 158 locmessage += "on line " + line + " "; 159 if (loc.getColumnNumber() != -1) { 160 locmessage += "column " + column + " "; 161 } 162 } 163 locmessage += "of " + loc.getSystemId() + ":"; 164 return locmessage; 165 } 166 } 167 168 171 172 public static String getExpandedMessage(TransformerException err) { 173 String message = ""; 174 Throwable e = err; 175 while (true) { 176 if (e == null) { 177 break; 178 } 179 String next = e.getMessage(); 180 if (!next.equals("TRaX Transform Exception") && !message.endsWith(next)) { 181 if (!message.equals("")) { 182 message += ": "; 183 } 184 message += e.getMessage(); 185 } 186 if (e instanceof TransformerException) { 187 e = ((TransformerException)e).getException(); 188 } else if (e instanceof SAXException ) { 189 e = ((SAXException )e).getException(); 190 } else { 191 break; 192 } 193 } 194 195 return message; 196 } 197 } 198 199 | Popular Tags |