KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > loader > WorkflowLoader


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.workflow.loader;
6
7 import com.opensymphony.workflow.InvalidWorkflowDescriptorException;
8
9 import org.w3c.dom.Document JavaDoc;
10 import org.w3c.dom.Element JavaDoc;
11
12 import org.xml.sax.*;
13
14 import java.io.*;
15
16 import java.net.URL JavaDoc;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.xml.parsers.*;
22
23
24 /**
25  * The WorkflowLoader is responsible for creating a WorkflowDesciptor
26  * by loading the XML from various sources.
27  *
28  * @author <a HREF="mailto:plightbo@hotmail.com">Pat Lightbody</a>
29  * @version $Revision: 1.6 $
30  */

31 public class WorkflowLoader {
32     //~ Methods ////////////////////////////////////////////////////////////////
33

34     public static WorkflowDescriptor load(final InputStream is) throws SAXException, IOException, InvalidWorkflowDescriptorException {
35         return load(is, null);
36     }
37
38     /**
39      * Load a workflow descriptor from a URL
40      */

41     public static WorkflowDescriptor load(final URL JavaDoc url) throws SAXException, IOException, InvalidWorkflowDescriptorException {
42         return load(url.openStream(), url);
43     }
44
45     private static WorkflowDescriptor load(InputStream is, URL JavaDoc 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 JavaDoc doc = db.parse(is);
62
63         Element JavaDoc root = (Element JavaDoc) doc.getElementsByTagName("workflow").item(0);
64
65         WorkflowDescriptor descriptor = new WorkflowDescriptor(root);
66         descriptor.validate();
67
68         return descriptor;
69     }
70
71     //~ Inner Classes //////////////////////////////////////////////////////////
72

73     public static class AllExceptionsErrorHandler implements ErrorHandler {
74         private final List JavaDoc exceptions = new ArrayList JavaDoc();
75
76         public List JavaDoc 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 JavaDoc url;
98
99         public WorkflowErrorHandler(final URL JavaDoc 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 JavaDoc 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