1 57 58 package org.apache.wsif.compiler.util; 59 60 import java.io.BufferedReader ; 61 import java.io.IOException ; 62 import java.io.Reader ; 63 import java.io.StringWriter ; 64 import java.util.Hashtable ; 65 import java.util.Iterator ; 66 import java.util.List ; 67 import java.util.Map ; 68 import java.util.Vector ; 69 70 import javax.wsdl.Definition; 71 import javax.wsdl.Import; 72 import javax.wsdl.Types; 73 import javax.wsdl.extensions.UnknownExtensibilityElement; 74 import javax.xml.namespace.QName ; 75 76 import org.apache.wsif.WSIFConstants; 77 import org.apache.wsif.WSIFException; 78 79 import com.ibm.wsdl.util.StringUtils; 80 81 84 public class Utils { 85 86 public static void addAllTypesElements(Definition def, List toList) { 87 Types types = def.getTypes(); 88 if (types != null) { 89 Iterator extEleIt = types.getExtensibilityElements().iterator(); 90 while (extEleIt.hasNext()) { 91 UnknownExtensibilityElement typesElement = 93 (UnknownExtensibilityElement) extEleIt.next(); 94 toList.add(typesElement); 95 } 96 } 97 98 Map imports = def.getImports(); 99 100 if (imports != null) { 101 Iterator valueIterator = imports.values().iterator(); 102 103 while (valueIterator.hasNext()) { 104 List importList = (List ) valueIterator.next(); 105 106 if (importList != null) { 107 Iterator importIterator = importList.iterator(); 108 109 while (importIterator.hasNext()) { 110 Import tempImport = (Import) importIterator.next(); 111 112 if (tempImport != null) { 113 Definition importedDef = tempImport.getDefinition(); 114 115 if (importedDef != null) { 116 addAllTypesElements(importedDef, toList); 117 } 118 } 119 } 120 } 121 } 122 } 123 } 124 125 public static List getAllTypesElements(Definition def) { 126 List ret = new Vector (); 127 128 addAllTypesElements(def, ret); 129 130 return ret; 131 } 132 133 public static String getPackageName(String fqClassName) { 134 String packageName = ""; 135 136 if (fqClassName != null) { 137 int index = fqClassName.lastIndexOf('.'); 138 139 if (index != -1) { 140 packageName = fqClassName.substring(0, index); 141 } 142 } 143 144 return packageName; 145 } 146 147 public static String getClassName(String fqClassName) { 148 if (fqClassName != null) { 149 int index = fqClassName.lastIndexOf('.'); 150 151 if (index != -1) { 152 fqClassName = fqClassName.substring(index + 1); 153 } 154 } 155 156 return fqClassName; 157 } 158 159 public static String queryJavaTypeName( 160 QName type, 161 String encodingStyleURI, 162 Hashtable typeReg) 163 throws IllegalArgumentException { 164 TypeMapping tm = (TypeMapping) typeReg.get(type); 165 166 if (tm != null) { 167 return tm.javaType; 168 } else if ( 169 encodingStyleURI != null 170 && encodingStyleURI.equals(WSIFConstants.NS_URI_LITERAL_XML)) { 171 return "org.w3c.dom.Element"; 172 } else { 173 throw new IllegalArgumentException ("No mapping was found for '" + type + "'."); 174 } 175 } 176 177 public static String getQuotedString(Reader source, int indent) 178 throws WSIFException { 179 String indentStr = StringUtils.getChars(indent, ' '); 180 181 try { 182 BufferedReader br = new BufferedReader (source); 183 StringWriter sw = new StringWriter (); 184 int count = 0; 185 String tempLine = null; 186 187 while ((tempLine = br.readLine()) != null) { 188 sw.write( 189 (count > 0 190 ? " + \"" + StringUtils.lineSeparatorStr + "\" +" + StringUtils.lineSeparator 191 : "") 192 + indentStr 193 + '\"' 194 + StringUtils.cleanString(tempLine) 195 + '\"'); 196 197 count++; 198 } 199 200 return sw.toString(); 201 } catch (IOException e) { 202 throw new WSIFException("Problem writing strings.", e); 203 } 204 } 205 206 public static String convertToObject(String sourceClassName, String expr) 207 throws WSIFException { 208 return convertClass(sourceClassName, expr, "java.lang.Object"); 209 } 210 211 public static String convertFromObject(String expr, String targetClassName) 212 throws WSIFException { 213 return convertClass("java.lang.Object", expr, targetClassName); 214 } 215 216 private static String convertClass( 217 String sourceClassName, 218 String expr, 219 String targetClassName) 220 throws WSIFException { 221 if (sourceClassName == null || targetClassName == null) { 222 throw new WSIFException( 223 "I was unable to convert an object from " 224 + sourceClassName 225 + " to " 226 + targetClassName 227 + "."); 228 } 229 230 String shortTargetClassName = getShortName(targetClassName); 231 232 if (sourceClassName.equals("java.lang.Object")) { 233 if (isPrimitive(targetClassName)) { 234 return "((" 235 + getWrapperClassName(targetClassName) 236 + ")" 237 + expr 238 + ")." 239 + shortTargetClassName 240 + "Value()"; 241 } else { 242 return "(" + targetClassName + ")" + expr; 243 } 244 } else if ( 245 isPrimitive(sourceClassName) && targetClassName.equals("java.lang.Object")) { 246 return "new " + getWrapperClassName(sourceClassName) + "(" + expr + ")"; 247 } else { 248 return expr; 250 } 251 } 252 253 private static String getShortName(String className) { 254 if (className.startsWith("java.lang.")) 255 return className.substring(10); 256 else 257 return className; 258 } 259 260 private static String getWrapperClassName(String primitiveClassName) { 261 if (primitiveClassName.equals("int")) 262 return "Integer"; 263 else if (primitiveClassName.equals("char")) 264 return "Character"; 265 else 266 return getCapitalized(primitiveClassName); 267 } 268 269 private static String getCapitalized(String className) { 270 return Character.toUpperCase(className.charAt(0)) + className.substring(1); 271 } 272 273 private static boolean isPrimitive(String className) { 274 String [] primNames = 275 { 276 "boolean", 277 "byte", 278 "char", 279 "short", 280 "int", 281 "long", 282 "float", 283 "double", 284 "void" }; 285 286 for (int i = 0; i < primNames.length; i++) { 287 if (primNames[i].equals(className)) { 288 return true; 289 } 290 } 291 return false; 292 } 293 } | Popular Tags |