1 package org.objectweb.celtix.tools.utils; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.net.MalformedURLException ; 6 import java.net.URL ; 7 import java.util.ArrayList ; 8 import java.util.HashMap ; 9 import java.util.List ; 10 import java.util.Map ; 11 import java.util.StringTokenizer ; 12 import java.util.logging.Logger ; 13 14 import javax.wsdl.Message; 15 import javax.wsdl.Operation; 16 import javax.wsdl.Part; 17 import javax.xml.namespace.QName ; 18 19 import com.sun.tools.xjc.api.Property; 20 import com.sun.tools.xjc.api.S2JJAXBModel; 21 import com.sun.tools.xjc.api.TypeAndAnnotation; 22 import com.sun.xml.bind.api.JAXBRIContext; 23 24 import org.objectweb.celtix.common.logging.LogUtils; 25 import org.objectweb.celtix.tools.common.ProcessorEnvironment; 26 import org.objectweb.celtix.tools.common.ToolConstants; 27 import org.objectweb.celtix.tools.common.ToolException; 28 import org.objectweb.celtix.tools.processors.wsdl2.internal.ClassCollector; 29 30 public final class ProcessorUtil { 31 private static final Logger LOG = LogUtils.getL7dLogger(ProcessorUtil.class); 32 private ProcessorUtil() { 33 } 34 35 public static String resolvePartName(Part part) { 36 return mangleNameToVariableName(part.getName()); 37 } 38 39 public static String getPartType(Part part) { 40 String typeName; 41 if (part.getElementName() != null) { 42 typeName = part.getElementName().getLocalPart(); 43 } else if (part.getTypeName() != null) { 44 typeName = part.getTypeName().getLocalPart(); 45 } else { 46 typeName = "BadType"; 47 } 48 return typeName; 49 } 50 51 public static String resolvePartType(Part part) { 52 return mangleNameToClassName(getPartType(part)); 53 } 54 55 public static QName getElementName(Part part) { 56 if (part == null) { 57 return null; 58 } 59 QName elementName = part.getElementName(); 60 if (elementName == null) { 61 elementName = part.getTypeName(); 62 } 63 return elementName; 64 } 65 66 public static QName getMappedElementName(Part part, ProcessorEnvironment env) { 71 QName origin = getElementName(part); 72 if (origin == null) { 73 return null; 74 } 75 if (!env.hasNamespace(origin.getNamespaceURI())) { 76 return origin; 77 } 78 return new QName (env.getCustomizedNS(origin.getNamespaceURI()), origin.getLocalPart()); 79 } 80 81 public static String resolvePartType(Part part, ProcessorEnvironment env) { 82 if (env != null) { 83 return resolvePartType(part, (S2JJAXBModel)env.get(ToolConstants.RAW_JAXB_MODEL)); 84 } else { 85 return resolvePartType(part); 86 } 87 } 88 89 public static String resolvePartType(Part part, S2JJAXBModel jaxbModel) { 90 return resolvePartType(part, jaxbModel, false); 91 } 92 93 public static String resolvePartType(Part part, S2JJAXBModel jaxbModel, boolean fullName) { 94 if (jaxbModel == null) { 95 return resolvePartType(part); 96 } 97 com.sun.tools.xjc.api.Mapping mapping = jaxbModel.get(getElementName(part)); 98 if (mapping == null) { 99 return resolvePartType(part); 100 } 101 if (fullName) { 102 return mapping.getType().getTypeClass().fullName(); 103 } else { 104 return mapping.getType().getTypeClass().name(); 105 } 106 } 107 108 public static String resolvePartNamespace(Part part) { 109 QName qname = part.getElementName(); 110 if (qname == null) { 111 qname = part.getTypeName(); 112 } 113 if (qname != null) { 114 return qname.getNamespaceURI(); 115 } else { 116 return null; 117 } 118 } 119 120 public static String mangleNameToClassName(String clzName) { 121 return JAXBRIContext.mangleNameToClassName(clzName); 122 } 123 124 public static String mangleNameToVariableName(String vName) { 125 return JAXBRIContext.mangleNameToVariableName(vName); 126 } 127 128 public static String parsePackageName(String namespace, String defaultPackageName) { 129 String packageName = (defaultPackageName != null && defaultPackageName.trim().length() > 0) 130 ? defaultPackageName : null; 131 132 if (packageName == null) { 133 packageName = URIParserUtil.getPackageName(namespace); 134 } 135 return packageName; 136 } 137 138 public static String getAbsolutePath(String location) throws IOException { 139 if (location.startsWith("http://")) { 140 return location; 141 } else { 142 return resolvePath(new File (location).getCanonicalPath()); 143 } 144 } 145 146 public static URL getWSDLURL(String location) throws Exception { 147 if (location.startsWith("http://")) { 148 return new URL (location); 149 } else { 150 return new File (getAbsolutePath(location)).toURL(); 151 } 152 } 153 154 private static String resolvePath(String path) { 155 return path.replace('\\', '/'); 156 } 157 158 public static List <? extends Property> getBlock(Part part, ProcessorEnvironment env) 162 throws ToolException { 163 if (part == null) { 164 return new ArrayList <Property>(); 165 } 166 167 S2JJAXBModel jaxbModel = (S2JJAXBModel)env.get(ToolConstants.RAW_JAXB_MODEL); 168 169 QName element = getElementName(part); 171 172 if (element != null && jaxbModel != null) { 173 com.sun.tools.xjc.api.Mapping mapping = jaxbModel.get(element); 174 if (mapping != null) { 175 return mapping.getWrapperStyleDrilldown(); 176 } else { 177 org.objectweb.celtix.common.i18n.Message msg = 178 new org.objectweb.celtix.common.i18n.Message("ELEMENT_MISSING", 179 LOG, 180 new Object []{element.toString(), 181 part.getName()}); 182 throw new ToolException(msg); 183 } 185 } else { 186 return new ArrayList <Property>(); 187 } 188 } 189 190 public static URL [] pathToURLs(String path) { 191 StringTokenizer st = new StringTokenizer (path, File.pathSeparator); 192 URL [] urls = new URL [st.countTokens()]; 193 int count = 0; 194 while (st.hasMoreTokens()) { 195 File file = new File (st.nextToken()); 196 URL url = null; 197 try { 198 url = file.toURL(); 199 } catch (MalformedURLException e) { 200 e.printStackTrace(); 201 } 202 if (url != null) { 203 urls[count++] = url; 204 } 205 } 206 if (urls.length != count) { 207 URL [] tmp = new URL [count]; 208 System.arraycopy(urls, 0, tmp, 0, count); 209 urls = tmp; 210 } 211 return urls; 212 } 213 214 public static String classNameToFilePath(String className) { 215 String str; 216 if (className.indexOf(".") < 0) { 217 return className; 218 } else { 219 str = className.replaceAll("\\.", "/"); 220 } 221 return str; 222 } 223 224 public static String getFullClzName(Part part, ProcessorEnvironment env, boolean boxify, 228 ClassCollector collector) { 229 S2JJAXBModel jaxbModel = (S2JJAXBModel)env.get(ToolConstants.RAW_JAXB_MODEL); 230 231 QName xmlTypeName = getElementName(part); 232 String jtype = BuiltInTypesJavaMappingUtil.getJType(xmlTypeName, jaxbModel, boxify); 233 String namespace = xmlTypeName.getNamespaceURI(); 234 String type = resolvePartType(part, jaxbModel); 235 String userPackage = env.mapPackageName(namespace); 236 237 if (jtype == null) { 238 jtype = collector.getTypesFullClassName(parsePackageName(namespace, userPackage), type); 239 } 240 241 if (jtype == null) { 242 if (!type.equals(resolvePartType(part))) { 243 jtype = resolvePartType(part, jaxbModel, true); 244 } else { 245 jtype = parsePackageName(namespace, userPackage) + "." + type; 246 } 247 } 248 249 return jtype; 250 } 251 252 public static String getFullClzName(Part part, ProcessorEnvironment env, ClassCollector collector) { 253 return getFullClzName(part, env, false, collector); 254 255 } 256 257 public static String getFileOrURLName(String fileOrURL) { 258 try { 259 try { 260 return escapeSpace(new URL (fileOrURL).toExternalForm()); 261 } catch (MalformedURLException e) { 262 return new File (fileOrURL).getCanonicalFile().toURL().toExternalForm(); 263 } 264 } catch (Exception e) { 265 return fileOrURL; 266 } 267 } 268 269 private static String escapeSpace(String url) { 270 StringBuffer buf = new StringBuffer (); 271 for (int i = 0; i < url.length(); i++) { 272 if (url.charAt(i) == ' ') { 273 buf.append("%20"); 274 } else { 275 buf.append(url.charAt(i)); 276 } 277 } 278 return buf.toString(); 279 } 280 281 public static String absolutize(String name) { 282 try { 285 URL baseURL = new File (".").getCanonicalFile().toURL(); 286 return new URL (baseURL, name).toExternalForm(); 287 } catch (IOException e) { 288 } 290 return name; 291 } 292 293 public static String getHandlerConfigFileName(String name) { 294 return name + "_handler"; 295 } 296 297 public static String boxify(QName xmlTypeName, S2JJAXBModel jaxbModel) { 298 TypeAndAnnotation typeAndAnnotation = jaxbModel.getJavaType(xmlTypeName); 299 if (typeAndAnnotation == null) { 300 return null; 301 } 302 return typeAndAnnotation.getTypeClass().boxify().fullName(); 303 } 304 305 @SuppressWarnings ("unchecked") 306 public static boolean isWrapperStyle(Operation operation, ProcessorEnvironment env) throws ToolException { 307 308 Message inputMessage = operation.getInput() == null ? null : operation.getInput().getMessage(); 309 Message outputMessage = operation.getOutput() == null ? null : operation.getOutput().getMessage(); 310 311 Map <String , Part> inputParts = new HashMap <String , Part>(); 312 Map <String , Part> outputParts = new HashMap <String , Part>(); 313 314 if (inputMessage != null) { 315 inputParts = inputMessage.getParts(); 316 } 317 if (outputMessage != null) { 318 outputParts = outputMessage.getParts(); 319 } 320 321 if (inputParts.size() > 1 || outputParts.size() > 1) { 327 return false; 328 } 329 330 Part inputPart = null; 337 if (inputParts.size() == 1) { 338 inputPart = inputParts.values().iterator().next(); 339 if (inputPart != null) { 340 QName inputElement = inputPart.getElementName(); 341 if (inputElement == null) { 342 return false; 343 } else if (!operation.getName().equals(inputElement.getLocalPart())) { 344 return false; 345 } 346 } 347 } 348 Part outputPart = null; 353 if (outputParts.size() == 1) { 354 outputPart = outputParts.values().iterator().next(); 355 if (outputPart != null) { 356 QName outputElement = outputPart.getElementName(); 357 if (outputElement == null) { 358 return false; 359 } 360 } 361 } 362 363 if (ProcessorUtil.getBlock(inputPart, env) == null 368 || ProcessorUtil.getBlock(outputPart, env) == null) { 369 return false; 370 } 371 372 return true; 373 } 374 } 375 | Popular Tags |