KickJava   Java API By Example, From Geeks To Geeks.

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


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.Document JavaDoc;
9 import org.w3c.dom.Element JavaDoc;
10 import org.xml.sax.InputSource JavaDoc;
11 import org.xml.sax.SAXException JavaDoc;
12
13 import org.jbpm.bpel.bar.BarApplication;
14 import org.jbpm.bpel.bar.BarPartnerLink;
15 import org.jbpm.bpel.bar.BarScope;
16 import org.jbpm.bpel.xml.util.NodeUtil;
17 import org.jbpm.jpdl.xml.Problem;
18
19 /**
20  * @author Juan Cantú
21  * @version $Revision: 1.5 $ $Date: 2005/06/23 20:45:04 $
22  */

23 public class BarDescriptorReader {
24   
25   private static final BarDescriptorReader INSTANCE = new BarDescriptorReader();
26   
27   private DocumentBuilder JavaDoc builder;
28   private ProblemHandler problemHandler;
29   
30   protected BarDescriptorReader() {
31     builder = NodeUtil.getDocumentBuilder();
32   }
33   
34   public void read(BarApplication application, InputSource JavaDoc input) {
35     try {
36       // read application descriptor
37
ErrorHandlerAdapter validationHandler = new ErrorHandlerAdapter(getProblemHandler());
38       builder.setErrorHandler(validationHandler);
39       Document JavaDoc barDocument = builder.parse(input);
40       
41       if( !validationHandler.hasErrors() ) {
42         Element JavaDoc applicationElem = barDocument.getDocumentElement();
43         // connection factory name
44
application.setConnectionFactoryName(readConnectionFactoryName(applicationElem));
45         // scope properties
46
readScope(applicationElem, application);
47       }
48     }
49     catch (SAXException JavaDoc e) {
50       getProblemHandler().add(
51           new Problem( Problem.LEVEL_ERROR, "par descriptor contains invalid xml", e) );
52     }
53     catch (IOException JavaDoc e) {
54       getProblemHandler().add(
55           new Problem( Problem.LEVEL_ERROR, "par descriptor is not readable", e) );
56     }
57   }
58   
59   // configuration elements //////////////////////////////////////////////////////////////
60

61   public void readScope(Element JavaDoc scopeElem, BarScope scope) {
62     // name
63
scope.setName(NodeUtil.getAttribute(scopeElem, BpelConstants.ATTR_NAME));
64     // scope-default destination
65
scope.setDestinationName(readDestination(scopeElem));
66     // partner links
67
Element JavaDoc partnerLinksElem = NodeUtil.getElement(scopeElem, BpelConstants.NS_VENDOR, BpelConstants.ELEM_PARTNER_LINKS);
68     if(partnerLinksElem != null) readPartnerLinks(partnerLinksElem, scope);
69     // subordinated scopes
70
Element JavaDoc scopesElem = NodeUtil.getElement(scopeElem, BpelConstants.NS_VENDOR, BpelConstants.ELEM_SCOPES);
71     if(scopesElem != null) readScopes(scopesElem, scope);
72   }
73   
74   public void readScopes(Element JavaDoc scopesElem, BarScope parentScope) {
75     Iterator JavaDoc scopeElemIt = NodeUtil.getElements(scopesElem, BpelConstants.NS_VENDOR, BpelConstants.ELEM_SCOPE);
76     while (scopeElemIt.hasNext()) {
77       BarScope scope = new BarScope();
78       readScope((Element JavaDoc) scopeElemIt.next(), scope);
79       parentScope.addScope(scope);
80     }
81   }
82   
83   public void readPartnerLinks(Element JavaDoc plinksElem, BarScope scope) {
84     Iterator JavaDoc plinkElemIt = NodeUtil.getElements(plinksElem, BpelConstants.NS_VENDOR, BpelConstants.ELEM_PARTNER_LINK);
85     while (plinkElemIt.hasNext()) {
86       BarPartnerLink plinkConfig = readPartnerLink((Element JavaDoc) plinkElemIt.next());
87       scope.addPartnerLink(plinkConfig);
88     }
89   }
90   
91   public BarPartnerLink readPartnerLink(Element JavaDoc partnerLinkElem) {
92     BarPartnerLink partnerLink = new BarPartnerLink();
93     // definition name
94
partnerLink.setName(partnerLinkElem.getAttribute(BpelConstants.ATTR_NAME));
95     // naming context name
96
partnerLink.setNamingContextName(readNamingContextName(partnerLinkElem));
97     // destination
98
partnerLink.setDestinationName(readDestination(partnerLinkElem));
99     return partnerLink;
100   }
101   
102   protected String JavaDoc readConnectionFactoryName(Element JavaDoc contextElem) {
103     Element JavaDoc factoryElem = NodeUtil.getElement(contextElem, BpelConstants.NS_VENDOR, BpelConstants.ELEM_CONNECTION_FACTORY);
104     return factoryElem.getAttribute(BpelConstants.ATTR_NAME);
105   }
106   
107   protected String JavaDoc readDestination(Element JavaDoc contextElem) {
108     Element JavaDoc destinationElem = NodeUtil.getElement(contextElem, BpelConstants.NS_VENDOR, BpelConstants.ELEM_DESTINATION);
109     return destinationElem != null ? destinationElem.getAttribute(BpelConstants.ATTR_NAME) : null;
110   }
111   
112   protected String JavaDoc readNamingContextName(Element JavaDoc contextElem) {
113     Element JavaDoc namingElem = NodeUtil.getElement(contextElem, BpelConstants.NS_VENDOR, BpelConstants.ELEM_NAMING_CONTEXT);
114     return namingElem != null ? namingElem.getAttribute(BpelConstants.ATTR_NAME) : null;
115   }
116   
117   public void setProblemHandler(ProblemHandler problemHandler) {
118     this.problemHandler = problemHandler;
119   }
120   
121   protected ProblemHandler getProblemHandler() {
122     if(problemHandler == null) {
123       problemHandler = new DefaultProblemHandler();
124     }
125     return problemHandler;
126   }
127   
128   public static BarDescriptorReader getInstance() {
129     return INSTANCE;
130   }
131 }
Popular Tags