1 17 package org.apache.servicemix.components.validation; 18 19 import java.io.IOException ; 20 import java.io.PrintWriter ; 21 import java.io.StringWriter ; 22 23 import javax.jbi.messaging.MessagingException; 24 import javax.xml.parsers.ParserConfigurationException ; 25 import javax.xml.transform.TransformerException ; 26 import javax.xml.transform.dom.DOMSource ; 27 28 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 29 import org.apache.servicemix.jbi.jaxp.StringSource; 30 import org.xml.sax.ErrorHandler ; 31 import org.xml.sax.SAXParseException ; 32 import org.xml.sax.SAXException ; 33 34 40 public class MessageAggregatingErrorHandler implements MessageAwareErrorHandler { 41 42 private static final String openCDATA = "<![CDATA["; 43 private static final String closeCDATA = "]]>"; 44 private static final String openError = "<error>"; 45 private static final String closeError = "</error>"; 46 private static final String openFatalError = "<fatalError>"; 47 private static final String closeFatalError = "</fataError>"; 48 private static final String openWarning = "<warning>"; 49 private static final String closeWarning = "</warning>"; 50 51 private String openRootElement; 52 private String closeRootElement; 53 54 57 private int warningCount; 58 59 62 private int errorCount; 63 64 67 private int fatalErrorCount; 68 69 72 private String rootPath; 73 74 77 private String namespace; 78 79 82 private boolean includeStackTraces; 83 84 87 private StringBuffer messages = new StringBuffer (); 88 89 private SourceTransformer sourceTransformer = new SourceTransformer(); 90 91 99 public MessageAggregatingErrorHandler(String rootPath, String namespace) throws IllegalArgumentException { 100 if (rootPath == null || rootPath.trim().length() == 0) { 101 throw new IllegalArgumentException ("rootPath must not be null or an empty string"); 102 } 103 this.rootPath = rootPath; 104 this.namespace = namespace; 105 createRootElementTags(); 106 } 107 108 118 public MessageAggregatingErrorHandler(String rootPath, String namespace, boolean includeStackTraces) throws IllegalArgumentException { 119 this(rootPath, namespace); 120 this.includeStackTraces = includeStackTraces; 121 } 122 123 126 public SourceTransformer getSourceTransformer() { 127 return sourceTransformer; 128 } 129 130 133 public void setSourceTransformer(SourceTransformer sourceTransformer) { 134 this.sourceTransformer = sourceTransformer; 135 } 136 137 146 private void createRootElementTags() { 147 151 String [] pathElements = rootPath.split("/"); 152 153 StringBuffer openRootElementSB = new StringBuffer ().append("<").append(pathElements[0]); 154 StringBuffer closeRootElementSB = new StringBuffer (); 155 156 if (namespace != null && namespace.trim().length() > 0) { 157 openRootElementSB.append(" xmlns=\"").append(namespace).append("\">"); 158 } else { 159 openRootElementSB.append(">"); 160 } 161 162 if (pathElements.length > 0) { 163 for (int i = 1, j = pathElements.length - 1; i < pathElements.length; i++, j--) { 164 openRootElementSB.append("<").append(pathElements[i]).append(">"); 165 closeRootElementSB.append("</").append(pathElements[j]).append(">"); 166 } 167 } 168 169 closeRootElementSB.append("</").append(pathElements[0]).append(">"); 171 172 openRootElement = openRootElementSB.toString(); 173 closeRootElement = closeRootElementSB.toString(); 174 } 175 176 179 public boolean hasErrors() { 180 return getErrorCount() > 0 || getFatalErrorCount() > 0; 181 } 182 183 186 public int getWarningCount() { 187 return warningCount; 188 } 189 190 193 public int getErrorCount() { 194 return errorCount; 195 } 196 197 200 public int getFatalErrorCount() { 201 return fatalErrorCount; 202 } 203 204 207 public void warning(SAXParseException e) throws SAXException { 208 ++warningCount; 209 210 messages.append(openWarning).append(openCDATA); 212 213 appendErrorMessage(e); 215 216 messages.append(closeCDATA).append(closeWarning); 218 } 219 220 223 public void error(SAXParseException e) throws SAXException { 224 ++errorCount; 225 226 messages.append(openError).append(openCDATA); 228 229 appendErrorMessage(e); 231 232 messages.append(closeCDATA).append(closeError); 234 } 235 236 239 public void fatalError(SAXParseException e) throws SAXException { 240 ++fatalErrorCount; 241 242 messages.append(openFatalError).append(openCDATA); 244 245 appendErrorMessage(e); 247 248 messages.append(closeCDATA).append(closeFatalError); 250 } 251 252 257 private void appendErrorMessage(SAXParseException e) { 258 if (includeStackTraces) { 259 StringWriter sw = new StringWriter (); 260 e.printStackTrace(new PrintWriter (sw)); 261 messages.append(sw.toString()); 262 } else { 263 messages.append(e.getLocalizedMessage()); 264 } 265 } 266 267 270 public boolean capturesMessages() { 271 return true; 272 } 273 274 277 public Object getMessagesAs(Class format) throws MessagingException { 278 if (format == DOMSource .class) { 279 return getDOMSource(); 280 } else if (format == StringSource.class) { 281 return getStringSource(); 282 } else if (format == String .class) { 283 return getMessagesWithRootElement(); 284 } 285 throw new MessagingException("Unsupported message format: " + format.getName()); 286 } 287 288 291 public boolean supportsMessageFormat(Class format) { 292 if (format == DOMSource .class) { 293 return true; 294 } else if (format == StringSource.class) { 295 return true; 296 } else if (format == String .class) { 297 return true; 298 } 299 return false; 300 } 301 302 307 private String getMessagesWithRootElement() { 308 return new StringBuffer ().append(openRootElement).append(messages).append(closeRootElement).toString(); 309 } 310 311 316 private StringSource getStringSource() { 317 return new StringSource(getMessagesWithRootElement()); 318 } 319 320 326 private DOMSource getDOMSource() throws MessagingException { 327 try { 328 return sourceTransformer.toDOMSource(getStringSource()); 329 } catch (ParserConfigurationException e) { 330 throw new MessagingException("Failed to create DOMSource for Schema validation messages: " + e, e); 331 } catch (IOException e) { 332 throw new MessagingException("Failed to create DOMSource for Schema validation messages: " + e, e); 333 } catch (SAXException e) { 334 throw new MessagingException("Failed to create DOMSource for Schema validation messages: " + e, e); 335 } catch (TransformerException e) { 336 throw new MessagingException("Failed to create DOMSource for Schema validation messages: " + e, e); 337 } 338 } 339 340 } 341 | Popular Tags |