KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > xml > ParDescriptorReader


1 package org.jbpm.bpel.xml;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import javax.xml.parsers.DocumentBuilder JavaDoc;
7
8 import org.w3c.dom.Element JavaDoc;
9 import org.xml.sax.InputSource JavaDoc;
10 import org.xml.sax.SAXException JavaDoc;
11
12 import org.jbpm.bpel.def.Import;
13 import org.jbpm.bpel.par.ParDefinition;
14 import org.jbpm.bpel.xml.util.NodeUtil;
15 import org.jbpm.jpdl.xml.Problem;
16
17 /**
18  * @author Alejandro Guízar
19  * @version $Revision: 1.4 $ $Date: 2005/06/23 20:45:04 $
20  */

21 public class ParDescriptorReader {
22   
23   private static final ParDescriptorReader INSTANCE = new ParDescriptorReader();
24   
25   private DocumentBuilder JavaDoc builder;
26   private ProblemHandler problemHandler;
27   
28   protected ParDescriptorReader() {
29     builder = NodeUtil.getDocumentBuilder();
30   }
31   
32   public void read(ParDefinition definition, InputSource JavaDoc input) {
33     try {
34       // read application descriptor
35
ErrorHandlerAdapter validationHandler = new ErrorHandlerAdapter(getProblemHandler());
36       builder.setErrorHandler(validationHandler);
37       Element JavaDoc definitionElem = builder.parse(input).getDocumentElement();
38       
39       if( !validationHandler.hasErrors() ) {
40         // process document location
41
String JavaDoc location = definitionElem.getAttribute(BpelConstants.ATTR_LOCATION);
42         definition.setLocation(location);
43         // private imports
44
Element JavaDoc importsElem = NodeUtil.getElement(definitionElem, BpelConstants.NS_VENDOR, BpelConstants.ELEM_IMPORTS);
45         if (importsElem != null) {
46           readImports(importsElem, definition);
47         }
48       }
49     }
50     catch (SAXException JavaDoc e) {
51       getProblemHandler().add(
52           new Problem( Problem.LEVEL_ERROR, "bar descriptor contains invalid xml", e) );
53     }
54     catch (IOException JavaDoc e) {
55       getProblemHandler().add(
56           new Problem( Problem.LEVEL_ERROR, "par descriptor is not readable", e) );
57     }
58   }
59
60   public void readImports(Element JavaDoc importsElem, ParDefinition definition) {
61     Iterator JavaDoc importElemIt = NodeUtil.getElements(importsElem, BpelConstants.NS_VENDOR);
62     while (importElemIt.hasNext()) {
63       Element JavaDoc importElem = (Element JavaDoc) importElemIt.next();
64       Import imp = readImport(importElem);
65       definition.addImport(imp);
66     }
67   }
68   
69   public Import readImport(Element JavaDoc importElem) {
70     Import imp = new Import();
71     imp.setNamespace(importElem.getAttribute(BpelConstants.ATTR_NAMESPACE));
72     imp.setLocation(importElem.getAttribute(BpelConstants.ATTR_LOCATION));
73     String JavaDoc importName = importElem.getLocalName();
74     if (BpelConstants.ELEM_WSDL.equals(importName)) {
75       imp.setType(Import.Type.WSDL);
76     }
77     else if (BpelConstants.ELEM_XML_SCHEMA.equals(importName)) {
78       imp.setType(Import.Type.XML_SCHEMA);
79     }
80     return imp;
81   }
82   
83   public void setProblemHandler(ProblemHandler problemHandler) {
84     this.problemHandler = problemHandler;
85   }
86   
87   protected ProblemHandler getProblemHandler() {
88     if(problemHandler == null) {
89       problemHandler = new DefaultProblemHandler();
90     }
91     return problemHandler;
92   }
93   
94   public static ParDescriptorReader getInstance() {
95     return INSTANCE;
96   }
97 }
98
Popular Tags