1 5 package com.opensymphony.workflow.loader; 6 7 import com.opensymphony.workflow.InvalidWorkflowDescriptorException; 8 9 import org.w3c.dom.Document ; 10 import org.w3c.dom.Element ; 11 12 import org.xml.sax.*; 13 14 import java.io.*; 15 16 import java.net.URL ; 17 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 import javax.xml.parsers.*; 22 23 24 31 public class WorkflowLoader { 32 34 public static WorkflowDescriptor load(final InputStream is) throws SAXException, IOException, InvalidWorkflowDescriptorException { 35 return load(is, null); 36 } 37 38 41 public static WorkflowDescriptor load(final URL url) throws SAXException, IOException, InvalidWorkflowDescriptorException { 42 return load(url.openStream(), url); 43 } 44 45 private static WorkflowDescriptor load(InputStream is, URL url) throws SAXException, IOException, InvalidWorkflowDescriptorException { 46 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 47 dbf.setNamespaceAware(true); 48 dbf.setValidating(true); 49 50 DocumentBuilder db; 51 52 try { 53 db = dbf.newDocumentBuilder(); 54 db.setEntityResolver(new DTDEntityResolver()); 55 } catch (ParserConfigurationException e) { 56 throw new SAXException("Error creating document builder", e); 57 } 58 59 db.setErrorHandler(new WorkflowErrorHandler(url)); 60 61 Document doc = db.parse(is); 62 63 Element root = (Element ) doc.getElementsByTagName("workflow").item(0); 64 65 WorkflowDescriptor descriptor = new WorkflowDescriptor(root); 66 descriptor.validate(); 67 68 return descriptor; 69 } 70 71 73 public static class AllExceptionsErrorHandler implements ErrorHandler { 74 private final List exceptions = new ArrayList (); 75 76 public List getExceptions() { 77 return exceptions; 78 } 79 80 public void error(SAXParseException exception) { 81 addMessage(exception); 82 } 83 84 public void fatalError(SAXParseException exception) { 85 addMessage(exception); 86 } 87 88 public void warning(SAXParseException exception) { 89 } 90 91 private void addMessage(SAXParseException exception) { 92 exceptions.add(exception.getMessage() + " (line:" + exception.getLineNumber() + ((exception.getColumnNumber() > -1) ? (" col:" + exception.getColumnNumber()) : "") + ")"); 93 } 94 } 95 96 public static class WorkflowErrorHandler implements ErrorHandler { 97 private URL url; 98 99 public WorkflowErrorHandler(final URL url) { 100 this.url = url; 101 } 102 103 public void error(SAXParseException exception) throws SAXException { 104 throw new SAXException(getMessage(exception)); 105 } 106 107 public void fatalError(SAXParseException exception) throws SAXException { 108 throw new SAXException(getMessage(exception)); 109 } 110 111 public void warning(SAXParseException exception) throws SAXException { 112 } 113 114 private String getMessage(SAXParseException exception) { 115 return exception.getMessage() + " (" + ((url != null) ? (" url=" + url + " ") : "") + "line:" + exception.getLineNumber() + ((exception.getColumnNumber() > -1) ? (" col:" + exception.getColumnNumber()) : "") + ")"; 116 } 117 } 118 } 119 | Popular Tags |