001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.workflow.loader;
006:
007: import com.opensymphony.workflow.InvalidWorkflowDescriptorException;
008:
009: import org.w3c.dom.Document;
010: import org.w3c.dom.Element;
011:
012: import org.xml.sax.*;
013:
014: import java.io.*;
015:
016: import java.net.URL;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import javax.xml.parsers.*;
022:
023: /**
024: * The WorkflowLoader is responsible for creating a WorkflowDesciptor
025: * by loading the XML from various sources.
026: *
027: * @author <a href="mailto:plightbo@hotmail.com">Pat Lightbody</a>
028: */
029: public class WorkflowLoader {
030: //~ Methods ////////////////////////////////////////////////////////////////
031:
032: /**
033: * @deprecated please use {@link #load(java.io.InputStream, boolean)} instead.
034: */
035: public static WorkflowDescriptor load(final InputStream is)
036: throws SAXException, IOException,
037: InvalidWorkflowDescriptorException {
038: return load(is, null, true);
039: }
040:
041: public static WorkflowDescriptor load(final InputStream is,
042: boolean validate) throws SAXException, IOException,
043: InvalidWorkflowDescriptorException {
044: return load(is, null, validate);
045: }
046:
047: /**
048: * Load a workflow descriptor from a URL
049: */
050: public static WorkflowDescriptor load(final URL url,
051: boolean validate) throws SAXException, IOException,
052: InvalidWorkflowDescriptorException {
053: return load(url.openStream(), url, validate);
054: }
055:
056: private static WorkflowDescriptor load(InputStream is, URL url,
057: boolean validate) throws SAXException, IOException,
058: InvalidWorkflowDescriptorException {
059: DocumentBuilderFactory dbf = DocumentBuilderFactory
060: .newInstance();
061: dbf.setNamespaceAware(true);
062:
063: dbf.setValidating(validate);
064:
065: DocumentBuilder db;
066:
067: try {
068: db = dbf.newDocumentBuilder();
069: db.setEntityResolver(new DTDEntityResolver());
070: } catch (ParserConfigurationException e) {
071: throw new SAXException("Error creating document builder", e);
072: }
073:
074: db.setErrorHandler(new WorkflowErrorHandler(url));
075:
076: Document doc = db.parse(is);
077:
078: Element root = (Element) doc.getElementsByTagName("workflow")
079: .item(0);
080:
081: WorkflowDescriptor descriptor = DescriptorFactory.getFactory()
082: .createWorkflowDescriptor(root);
083:
084: if (validate) {
085: descriptor.validate();
086: }
087:
088: return descriptor;
089: }
090:
091: //~ Inner Classes //////////////////////////////////////////////////////////
092:
093: public static class AllExceptionsErrorHandler implements
094: ErrorHandler {
095: private final List exceptions = new ArrayList();
096:
097: public List getExceptions() {
098: return exceptions;
099: }
100:
101: public void error(SAXParseException exception) {
102: addMessage(exception);
103: }
104:
105: public void fatalError(SAXParseException exception) {
106: addMessage(exception);
107: }
108:
109: public void warning(SAXParseException exception) {
110: }
111:
112: private void addMessage(SAXParseException exception) {
113: exceptions
114: .add(exception.getMessage()
115: + " (line:"
116: + exception.getLineNumber()
117: + ((exception.getColumnNumber() > -1) ? (" col:" + exception
118: .getColumnNumber())
119: : "") + ')');
120: }
121: }
122:
123: public static class WorkflowErrorHandler implements ErrorHandler {
124: private URL url;
125:
126: public WorkflowErrorHandler(final URL url) {
127: this .url = url;
128: }
129:
130: public void error(SAXParseException exception)
131: throws SAXException {
132: throw new SAXException(getMessage(exception));
133: }
134:
135: public void fatalError(SAXParseException exception)
136: throws SAXException {
137: throw new SAXException(getMessage(exception));
138: }
139:
140: public void warning(SAXParseException exception)
141: throws SAXException {
142: }
143:
144: private String getMessage(SAXParseException exception) {
145: return exception.getMessage()
146: + " ("
147: + ((url != null) ? (" url=" + url + ' ') : "")
148: + "line:"
149: + exception.getLineNumber()
150: + ((exception.getColumnNumber() > -1) ? (" col:" + exception
151: .getColumnNumber())
152: : "") + ')';
153: }
154: }
155: }
|