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.Element ; 9 import org.xml.sax.InputSource ; 10 import org.xml.sax.SAXException ; 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 21 public class ParDescriptorReader { 22 23 private static final ParDescriptorReader INSTANCE = new ParDescriptorReader(); 24 25 private DocumentBuilder builder; 26 private ProblemHandler problemHandler; 27 28 protected ParDescriptorReader() { 29 builder = NodeUtil.getDocumentBuilder(); 30 } 31 32 public void read(ParDefinition definition, InputSource input) { 33 try { 34 ErrorHandlerAdapter validationHandler = new ErrorHandlerAdapter(getProblemHandler()); 36 builder.setErrorHandler(validationHandler); 37 Element definitionElem = builder.parse(input).getDocumentElement(); 38 39 if( !validationHandler.hasErrors() ) { 40 String location = definitionElem.getAttribute(BpelConstants.ATTR_LOCATION); 42 definition.setLocation(location); 43 Element importsElem = NodeUtil.getElement(definitionElem, BpelConstants.NS_VENDOR, BpelConstants.ELEM_IMPORTS); 45 if (importsElem != null) { 46 readImports(importsElem, definition); 47 } 48 } 49 } 50 catch (SAXException e) { 51 getProblemHandler().add( 52 new Problem( Problem.LEVEL_ERROR, "bar descriptor contains invalid xml", e) ); 53 } 54 catch (IOException e) { 55 getProblemHandler().add( 56 new Problem( Problem.LEVEL_ERROR, "par descriptor is not readable", e) ); 57 } 58 } 59 60 public void readImports(Element importsElem, ParDefinition definition) { 61 Iterator importElemIt = NodeUtil.getElements(importsElem, BpelConstants.NS_VENDOR); 62 while (importElemIt.hasNext()) { 63 Element importElem = (Element ) importElemIt.next(); 64 Import imp = readImport(importElem); 65 definition.addImport(imp); 66 } 67 } 68 69 public Import readImport(Element importElem) { 70 Import imp = new Import(); 71 imp.setNamespace(importElem.getAttribute(BpelConstants.ATTR_NAMESPACE)); 72 imp.setLocation(importElem.getAttribute(BpelConstants.ATTR_LOCATION)); 73 String 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 |