1 package org.jbpm.bpel.xml; 2 3 import java.io.IOException ; 4 import java.util.Iterator ; 5 6 import javax.xml.parsers.DocumentBuilder ; 7 8 import org.w3c.dom.Document ; 9 import org.w3c.dom.Element ; 10 import org.xml.sax.InputSource ; 11 import org.xml.sax.SAXException ; 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 23 public class BarDescriptorReader { 24 25 private static final BarDescriptorReader INSTANCE = new BarDescriptorReader(); 26 27 private DocumentBuilder builder; 28 private ProblemHandler problemHandler; 29 30 protected BarDescriptorReader() { 31 builder = NodeUtil.getDocumentBuilder(); 32 } 33 34 public void read(BarApplication application, InputSource input) { 35 try { 36 ErrorHandlerAdapter validationHandler = new ErrorHandlerAdapter(getProblemHandler()); 38 builder.setErrorHandler(validationHandler); 39 Document barDocument = builder.parse(input); 40 41 if( !validationHandler.hasErrors() ) { 42 Element applicationElem = barDocument.getDocumentElement(); 43 application.setConnectionFactoryName(readConnectionFactoryName(applicationElem)); 45 readScope(applicationElem, application); 47 } 48 } 49 catch (SAXException e) { 50 getProblemHandler().add( 51 new Problem( Problem.LEVEL_ERROR, "par descriptor contains invalid xml", e) ); 52 } 53 catch (IOException e) { 54 getProblemHandler().add( 55 new Problem( Problem.LEVEL_ERROR, "par descriptor is not readable", e) ); 56 } 57 } 58 59 61 public void readScope(Element scopeElem, BarScope scope) { 62 scope.setName(NodeUtil.getAttribute(scopeElem, BpelConstants.ATTR_NAME)); 64 scope.setDestinationName(readDestination(scopeElem)); 66 Element partnerLinksElem = NodeUtil.getElement(scopeElem, BpelConstants.NS_VENDOR, BpelConstants.ELEM_PARTNER_LINKS); 68 if(partnerLinksElem != null) readPartnerLinks(partnerLinksElem, scope); 69 Element scopesElem = NodeUtil.getElement(scopeElem, BpelConstants.NS_VENDOR, BpelConstants.ELEM_SCOPES); 71 if(scopesElem != null) readScopes(scopesElem, scope); 72 } 73 74 public void readScopes(Element scopesElem, BarScope parentScope) { 75 Iterator scopeElemIt = NodeUtil.getElements(scopesElem, BpelConstants.NS_VENDOR, BpelConstants.ELEM_SCOPE); 76 while (scopeElemIt.hasNext()) { 77 BarScope scope = new BarScope(); 78 readScope((Element ) scopeElemIt.next(), scope); 79 parentScope.addScope(scope); 80 } 81 } 82 83 public void readPartnerLinks(Element plinksElem, BarScope scope) { 84 Iterator plinkElemIt = NodeUtil.getElements(plinksElem, BpelConstants.NS_VENDOR, BpelConstants.ELEM_PARTNER_LINK); 85 while (plinkElemIt.hasNext()) { 86 BarPartnerLink plinkConfig = readPartnerLink((Element ) plinkElemIt.next()); 87 scope.addPartnerLink(plinkConfig); 88 } 89 } 90 91 public BarPartnerLink readPartnerLink(Element partnerLinkElem) { 92 BarPartnerLink partnerLink = new BarPartnerLink(); 93 partnerLink.setName(partnerLinkElem.getAttribute(BpelConstants.ATTR_NAME)); 95 partnerLink.setNamingContextName(readNamingContextName(partnerLinkElem)); 97 partnerLink.setDestinationName(readDestination(partnerLinkElem)); 99 return partnerLink; 100 } 101 102 protected String readConnectionFactoryName(Element contextElem) { 103 Element factoryElem = NodeUtil.getElement(contextElem, BpelConstants.NS_VENDOR, BpelConstants.ELEM_CONNECTION_FACTORY); 104 return factoryElem.getAttribute(BpelConstants.ATTR_NAME); 105 } 106 107 protected String readDestination(Element contextElem) { 108 Element 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 readNamingContextName(Element contextElem) { 113 Element 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 |