KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > DescriptorLoader


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

5 package com.opensymphony.workflow;
6
7 import com.opensymphony.workflow.loader.DTDEntityResolver;
8 import com.opensymphony.workflow.loader.WorkflowDescriptor;
9 import com.opensymphony.workflow.loader.WorkflowLoader;
10
11 import org.w3c.dom.Document JavaDoc;
12 import org.w3c.dom.Element JavaDoc;
13
14 import java.io.File JavaDoc;
15 import java.io.FileInputStream JavaDoc;
16
17 import java.net.URL JavaDoc;
18
19 import javax.xml.parsers.DocumentBuilder JavaDoc;
20 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
21
22
23 /**
24  * User: Hani Suleiman (hani@formicary.net)
25  * Date: Feb 12, 2003
26  * Time: 6:49:46 PM
27  */

28 public class DescriptorLoader {
29     //~ Methods ////////////////////////////////////////////////////////////////
30

31     public static WorkflowDescriptor getDescriptor(String JavaDoc url) throws Exception JavaDoc {
32         File JavaDoc file = new File JavaDoc(new URL JavaDoc(url).getFile());
33
34         if (!file.exists()) {
35             throw new IllegalArgumentException JavaDoc("file " + file + " does not exist");
36         }
37
38         DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
39         dbf.setNamespaceAware(true);
40         dbf.setValidating(true);
41
42         DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
43         db.setEntityResolver(new DTDEntityResolver());
44         db.setErrorHandler(new WorkflowLoader.WorkflowErrorHandler(new URL JavaDoc(url)));
45
46         Document JavaDoc doc = db.parse(new FileInputStream JavaDoc(file));
47         Element JavaDoc root = (Element JavaDoc) doc.getElementsByTagName("workflow").item(0);
48
49         WorkflowDescriptor descriptor = new WorkflowDescriptor(root);
50
51         return descriptor;
52     }
53 }
54
Popular Tags