KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.bpel.xml;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.net.URI JavaDoc;
8 import java.net.URISyntaxException JavaDoc;
9 import java.util.List JavaDoc;
10
11 import javax.wsdl.xml.WSDLLocator;
12 import javax.xml.parsers.DocumentBuilder JavaDoc;
13 import javax.xml.transform.Transformer JavaDoc;
14 import javax.xml.transform.TransformerConfigurationException JavaDoc;
15 import javax.xml.transform.TransformerException JavaDoc;
16 import javax.xml.transform.TransformerFactory JavaDoc;
17 import javax.xml.transform.dom.DOMSource JavaDoc;
18 import javax.xml.transform.stream.StreamResult JavaDoc;
19 import javax.xml.transform.stream.StreamSource JavaDoc;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.w3c.dom.Document JavaDoc;
24 import org.xml.sax.InputSource JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
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 /**
32  * @author Alejandro Guízar
33  * @version $Revision: 1.6 $ $Date: 2005/06/23 20:45:04 $
34  */

35 public class ImportWsdlLocator implements WSDLLocator {
36   
37   private final URI JavaDoc importBaseURI;
38   private String JavaDoc baseURI;
39   private String JavaDoc latestImportURI;
40   
41   private DocumentBuilder JavaDoc builder;
42   private ProblemHandler problemHandler;
43   private boolean hasErrors;
44   
45   private static final String JavaDoc 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 JavaDoc WSDL_1_1_TEMPLATE = "wsdl-1-1-converter.xsl";
49   private static Transformer JavaDoc 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 JavaDoc importBaseURI) {
55     builder = NodeUtil.getDocumentBuilder();
56     try {
57       this.importBaseURI = new URI JavaDoc(importBaseURI);
58     }
59     catch (URISyntaxException JavaDoc e) {
60       throw new RuntimeException JavaDoc(e);
61     }
62   }
63   
64   public void resolveBaseURI(String JavaDoc location) {
65     baseURI = importBaseURI.resolve(location).toString();
66   }
67   
68   public String JavaDoc getBaseURI() {
69     return baseURI;
70   }
71   
72   public InputSource JavaDoc getBaseInputSource() {
73     return getInputSource(baseURI);
74   }
75
76   public InputSource JavaDoc getImportInputSource(String JavaDoc parentLocation, String JavaDoc importLocation) {
77     try {
78       URI JavaDoc importURI = new URI JavaDoc(importLocation);
79       return getInputSource(importURI.isAbsolute() ? importLocation
80           : new URI JavaDoc(parentLocation).resolve(importURI).toString());
81     }
82     catch (URISyntaxException JavaDoc e) {
83       return null;
84     }
85   }
86
87   public String JavaDoc getLatestImportURI() {
88     return latestImportURI;
89   }
90   
91   protected InputSource JavaDoc createInputSource(String JavaDoc importURI) {
92     return new InputSource JavaDoc(importURI);
93   }
94
95   private InputSource JavaDoc getInputSource(String JavaDoc importURI) {
96     InputSource JavaDoc inputSource = createInputSource(importURI);
97     try {
98       //parse wsdl document using an error handler that collects validation problems
99
ErrorHandlerAdapter validationHandler = new ErrorHandlerAdapter(getProblemHandler());
100       builder.setErrorHandler(validationHandler);
101       Document JavaDoc 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 JavaDoc resultStream = new ByteArrayOutputStream JavaDoc();
110         wsdlUpgrader.transform(new DOMSource JavaDoc(wsdlDocument), new StreamResult JavaDoc(resultStream));
111         log.debug("upgraded wsdl document: " + importURI);
112         if( transformHandler.hasErrors() ) {
113           hasErrors = true;
114         }
115         else {
116           inputSource.setByteStream(new ByteArrayInputStream JavaDoc(resultStream.toByteArray()));
117         }
118       }
119       else {
120         InputStream JavaDoc parsedStream = inputSource.getByteStream();
121         if (parsedStream != null) {
122           parsedStream.reset();
123         }
124       }
125       latestImportURI = importURI;
126     }
127     catch (SAXException JavaDoc e) {
128       getProblemHandler().add(
129           new Problem(Problem.LEVEL_ERROR, "wsdl document contains invalid xml: " + importURI, e));
130     }
131     catch (IOException JavaDoc e) {
132       getProblemHandler().add(
133           new Problem(Problem.LEVEL_ERROR, "unable to read wsdl document: " + importURI, e));
134     }
135     catch (TransformerException JavaDoc 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 JavaDoc createUpgrader() {
143     TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
144     Transformer JavaDoc upgrader = null;
145     InputStream JavaDoc stream = null;
146     try {
147       stream = ImportWsdlLocator.class.getResourceAsStream(WSDL_1_1_TEMPLATE);
148       upgrader = factory.newTransformer(new StreamSource JavaDoc(stream));
149     }
150     catch (TransformerConfigurationException JavaDoc e) {
151       log.error("unable to create upgrader " + WSDL_1_1_TEMPLATE, e);
152     }
153     finally {
154       try {
155         stream.close();
156       } catch (IOException JavaDoc 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 JavaDoc wsdlDocument) {
165     List JavaDoc results = (List JavaDoc) 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