1 package org.jbpm.bpel.xml; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.net.URI ; 8 import java.net.URISyntaxException ; 9 import java.util.List ; 10 11 import javax.wsdl.xml.WSDLLocator; 12 import javax.xml.parsers.DocumentBuilder ; 13 import javax.xml.transform.Transformer ; 14 import javax.xml.transform.TransformerConfigurationException ; 15 import javax.xml.transform.TransformerException ; 16 import javax.xml.transform.TransformerFactory ; 17 import javax.xml.transform.dom.DOMSource ; 18 import javax.xml.transform.stream.StreamResult ; 19 import javax.xml.transform.stream.StreamSource ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.w3c.dom.Document ; 24 import org.xml.sax.InputSource ; 25 import org.xml.sax.SAXException ; 26 27 import org.jbpm.bpel.wsdl.xml.WsdlConstants; 28 import org.jbpm.bpel.xml.util.NodeUtil; 29 import org.jbpm.jpdl.xml.Problem; 30 31 35 public class ImportWsdlLocator implements WSDLLocator { 36 37 private final URI importBaseURI; 38 private String baseURI; 39 private String latestImportURI; 40 41 private DocumentBuilder builder; 42 private ProblemHandler problemHandler; 43 private boolean hasErrors; 44 45 private static final String UPGRADABLE_ELEMENTS_XPATH = 46 "*[namespace-uri(.) = '" + BpelConstants.NS_BPWS_1_1 + "' or" + 47 " namespace-uri(.) = '" + WsdlConstants.NS_PLNK_1_1 + "']"; 48 private static final String WSDL_1_1_TEMPLATE = "wsdl-1-1-converter.xsl"; 49 private static Transformer wsdlUpgrader = 50 NodeUtil.createUpgrader(BpelReader.class.getResource(WSDL_1_1_TEMPLATE)); 51 52 private static final Log log = LogFactory.getLog(ImportWsdlLocator.class); 53 54 public ImportWsdlLocator(String importBaseURI) { 55 builder = NodeUtil.getDocumentBuilder(); 56 try { 57 this.importBaseURI = new URI (importBaseURI); 58 } 59 catch (URISyntaxException e) { 60 throw new RuntimeException (e); 61 } 62 } 63 64 public void resolveBaseURI(String location) { 65 baseURI = importBaseURI.resolve(location).toString(); 66 } 67 68 public String getBaseURI() { 69 return baseURI; 70 } 71 72 public InputSource getBaseInputSource() { 73 return getInputSource(baseURI); 74 } 75 76 public InputSource getImportInputSource(String parentLocation, String importLocation) { 77 try { 78 URI importURI = new URI (importLocation); 79 return getInputSource(importURI.isAbsolute() ? importLocation 80 : new URI (parentLocation).resolve(importURI).toString()); 81 } 82 catch (URISyntaxException e) { 83 return null; 84 } 85 } 86 87 public String getLatestImportURI() { 88 return latestImportURI; 89 } 90 91 protected InputSource createInputSource(String importURI) { 92 return new InputSource (importURI); 93 } 94 95 private InputSource getInputSource(String importURI) { 96 InputSource inputSource = createInputSource(importURI); 97 try { 98 ErrorHandlerAdapter validationHandler = new ErrorHandlerAdapter(getProblemHandler()); 100 builder.setErrorHandler(validationHandler); 101 Document wsdlDocument = builder.parse(inputSource); 102 103 if( validationHandler.hasErrors() ) { 104 hasErrors = true; 105 } 106 else if (hasUpgradableElements(wsdlDocument)) { 107 ErrorListenerAdapter transformHandler = new ErrorListenerAdapter(getProblemHandler()); 108 wsdlUpgrader.setErrorListener(transformHandler); 109 ByteArrayOutputStream resultStream = new ByteArrayOutputStream (); 110 wsdlUpgrader.transform(new DOMSource (wsdlDocument), new StreamResult (resultStream)); 111 log.debug("upgraded wsdl document: " + importURI); 112 if( transformHandler.hasErrors() ) { 113 hasErrors = true; 114 } 115 else { 116 inputSource.setByteStream(new ByteArrayInputStream (resultStream.toByteArray())); 117 } 118 } 119 else { 120 InputStream parsedStream = inputSource.getByteStream(); 121 if (parsedStream != null) { 122 parsedStream.reset(); 123 } 124 } 125 latestImportURI = importURI; 126 } 127 catch (SAXException e) { 128 getProblemHandler().add( 129 new Problem(Problem.LEVEL_ERROR, "wsdl document contains invalid xml: " + importURI, e)); 130 } 131 catch (IOException e) { 132 getProblemHandler().add( 133 new Problem(Problem.LEVEL_ERROR, "unable to read wsdl document: " + importURI, e)); 134 } 135 catch (TransformerException e) { 136 getProblemHandler().add( 137 new Problem(Problem.LEVEL_ERROR, "unable to upgrade wsdl document: " + importURI, e)); 138 } 139 return inputSource; 140 } 141 142 public static Transformer createUpgrader() { 143 TransformerFactory factory = TransformerFactory.newInstance(); 144 Transformer upgrader = null; 145 InputStream stream = null; 146 try { 147 stream = ImportWsdlLocator.class.getResourceAsStream(WSDL_1_1_TEMPLATE); 148 upgrader = factory.newTransformer(new StreamSource (stream)); 149 } 150 catch (TransformerConfigurationException e) { 151 log.error("unable to create upgrader " + WSDL_1_1_TEMPLATE, e); 152 } 153 finally { 154 try { 155 stream.close(); 156 } catch (IOException e) { 157 log.error("unable to close xsl template " + WSDL_1_1_TEMPLATE, e); 158 } 159 } 160 161 return upgrader; 162 } 163 164 private static boolean hasUpgradableElements(Document wsdlDocument) { 165 List results = (List ) NodeUtil.evaluateXPath(UPGRADABLE_ELEMENTS_XPATH, 166 wsdlDocument.getDocumentElement(), null); 167 return !results.isEmpty(); 168 } 169 170 public ProblemHandler getProblemHandler() { 171 if(problemHandler == null) { 172 problemHandler = new DefaultProblemHandler(); 173 } 174 return problemHandler; 175 } 176 177 public void setProblemHandler(ProblemHandler problemHandler) { 178 this.problemHandler = problemHandler; 179 } 180 181 public boolean hasErrors() { 182 return hasErrors; 183 } 184 } | Popular Tags |