KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > wsdl > util > WsdlUtil


1 package org.jbpm.bpel.wsdl.util;
2
3 import java.io.*;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import javax.wsdl.Definition;
9 import javax.wsdl.Message;
10 import javax.wsdl.Part;
11 import javax.wsdl.WSDLException;
12 import javax.wsdl.extensions.ExtensibilityElement;
13 import javax.wsdl.factory.WSDLFactory;
14 import javax.xml.namespace.QName JavaDoc;
15
16 import org.apache.commons.collections.Predicate;
17 import org.apache.commons.collections.iterators.EmptyIterator;
18 import org.apache.commons.collections.iterators.FilterIterator;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.xml.sax.InputSource JavaDoc;
22
23 import org.jbpm.bpel.def.Import;
24 import org.jbpm.bpel.def.Import.Type;
25 import org.jbpm.bpel.wsdl.def.PartnerLinkType;
26 import org.jbpm.bpel.wsdl.def.Property;
27 import org.jbpm.bpel.wsdl.xml.WsdlFactoryImpl;
28
29 /**
30  * Utility methods for dealing with JWSDL objects.
31  * @author Alejandro Guízar
32  * @version $Revision: 1.1 $ $Date: 2005/06/23 02:22:46 $
33  */

34 public class WsdlUtil {
35   
36   private static final WSDLFactory factory = new WsdlFactoryImpl();
37   private static final Log log = LogFactory.getLog(WsdlUtil.class);
38
39   /* Supresses default constructor, ensuring non-instantiability */
40   private WsdlUtil() {
41   }
42   
43   public static WSDLFactory getFactory() {
44     return factory;
45   }
46   
47   public static Definition readText(String JavaDoc text) throws WSDLException {
48     return getFactory().newWSDLReader().readWSDL(null, new InputSource JavaDoc(new StringReader(text)));
49   }
50   
51   public static void writeFile(Definition def, File file) throws WSDLException {
52     OutputStream sink = null;
53     try {
54       // open a stream for writing
55
sink = new FileOutputStream(file);
56       Writer writer;
57       try {
58         // try to use UTF-8 encoding
59
writer = new OutputStreamWriter(sink, "UTF-8");
60       }
61       catch (UnsupportedEncodingException e) {
62         log.warn("UTF-8 encoding not supported", e);
63         // fall back to the platform's default encoding
64
writer = new OutputStreamWriter(sink);
65       }
66       // introduce a buffered writer to improve performance
67
getFactory().newWSDLWriter().writeWSDL(def, new BufferedWriter(writer));
68     }
69     catch (FileNotFoundException e) {
70       log.error(e);
71       throw new WSDLException(WSDLException.OTHER_ERROR,
72           "could not open file for writing: " + file, e);
73     }
74     finally {
75       if (sink != null) {
76         try {
77           sink.close();
78         }
79         catch (IOException e) {
80           log.warn("could not close file: " + file, e);
81         }
82       }
83     }
84   }
85
86   public static PartnerLinkType getPartnerLinkType(Definition def, QName JavaDoc name) {
87     // first look in local extensibility elements...
88
List JavaDoc extensions = def.getExtensibilityElements();
89     for (int i = 0, n = extensions.size(); i < n; i++) {
90       Object JavaDoc extension = extensions.get(i);
91       if (extension instanceof PartnerLinkType) {
92         PartnerLinkType plinkType = (PartnerLinkType) extension;
93         if (plinkType.getQName().equals(name)) return plinkType;
94       }
95     }
96     // ...then look in imported definitions
97
List JavaDoc imports = def.getImports(name.getNamespaceURI());
98     if (imports != null) {
99       for (int i = 0, n = imports.size(); i < n; i++) {
100         javax.wsdl.Import imp = (javax.wsdl.Import) imports.get(i);
101         PartnerLinkType plinkType = getPartnerLinkType(imp.getDefinition(), name);
102         if (plinkType != null) return plinkType;
103       }
104     }
105     return null;
106   }
107
108   public static Property getProperty(Definition def, QName JavaDoc name) {
109     // first look in local extensibility elements...
110
List JavaDoc extensions = def.getExtensibilityElements();
111     for (int i = 0, n = extensions.size(); i < n; i++) {
112       Object JavaDoc extension = extensions.get(i);
113       if (extension instanceof Property) {
114         Property property = (Property) extension;
115         if (property.getQName().equals(name)) return property;
116       }
117     }
118     // ...then look in imported definitions
119
List JavaDoc imports = def.getImports(name.getNamespaceURI());
120     if (imports != null) {
121       for (int i = 0, n = imports.size(); i < n; i++) {
122         javax.wsdl.Import imp = (javax.wsdl.Import) imports.get(i);
123         Property property = getProperty(imp.getDefinition(), name);
124         if (property != null) return property;
125       }
126     }
127     return null;
128   }
129   
130   public static ExtensibilityElement getExtension(List JavaDoc extensions, QName JavaDoc extensionType) {
131     if (extensions != null) {
132       Iterator JavaDoc extensionIt = extensions.iterator();
133       while (extensionIt.hasNext()) {
134         ExtensibilityElement extension = (ExtensibilityElement) extensionIt.next();
135         if (ExtensionTypePredicate.evaluate(extension, extensionType)) {
136           return extension;
137         }
138       }
139     }
140     return null;
141   }
142
143   public static Iterator JavaDoc getExtensions(List JavaDoc extensions, QName JavaDoc extensionType) {
144     return extensions != null ?
145         new FilterIterator(extensions.iterator(), new ExtensionTypePredicate(extensionType))
146         : EmptyIterator.INSTANCE;
147   }
148
149   public static QName JavaDoc getDocLitElementName(Message message) {
150     Map JavaDoc parts = message.getParts();
151     return parts.size() == 1 ? ((Part) parts.values().iterator().next()).getElementName() : null;
152   }
153   
154   public static Import createImport(Definition def) {
155     Import imp = new Import();
156     imp.setNamespace(def.getTargetNamespace());
157     imp.setType(Type.WSDL);
158     imp.setDocument(def);
159     return imp;
160   }
161
162   private static class ExtensionTypePredicate implements Predicate {
163
164     private final QName JavaDoc extensionType;
165     
166     ExtensionTypePredicate(QName JavaDoc type) {
167       extensionType = type;
168     }
169     
170     public boolean evaluate(Object JavaDoc arg) {
171       return evaluate((ExtensibilityElement) arg, extensionType);
172     }
173     
174     public static boolean evaluate(ExtensibilityElement extension, QName JavaDoc extensionType) {
175       return extension.getElementType().equals(extensionType);
176     }
177   }
178 }
179
Popular Tags