1 16 package org.apache.axis.wsdl.fromJava; 17 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 import java.util.StringTokenizer ; 22 import java.net.URL ; 23 import java.net.MalformedURLException ; 24 25 30 public class Namespaces extends HashMap { 31 32 33 private int prefixCount = 1; 34 35 36 private HashMap namespacePrefixMap = null; 37 38 41 public Namespaces() { 42 43 super(); 44 45 namespacePrefixMap = new HashMap (); 46 } 47 48 55 public String getCreate(String key) { 56 57 Object value = super.get(key); 58 59 if (value == null) { 60 value = makeNamespaceFromPackageName(key); 61 62 put(key, value, null); 63 } 64 65 return (String ) value; 66 } 67 68 76 public String getCreate(String key, String prefix) { 77 78 Object value = super.get(key); 79 80 if (value == null) { 81 value = makeNamespaceFromPackageName(key); 82 83 put(key, value, prefix); 84 } 85 86 return (String ) value; 87 } 88 89 99 public Object put(Object key, Object value, String prefix) { 100 101 if (prefix != null) { 102 namespacePrefixMap.put(value, prefix); 103 } else { 104 getCreatePrefix((String ) value); 105 } 106 107 return super.put(key, value); 108 } 109 110 117 public void putAll(Map map) { 118 119 Iterator i = map.entrySet().iterator(); 120 121 while (i.hasNext()) { 122 Map.Entry entry = (Map.Entry ) i.next(); 123 124 put(entry.getKey(), entry.getValue(), null); 125 } 126 } 127 128 134 public String getCreatePrefix(String namespace) { 135 136 if (namespacePrefixMap.get(namespace) == null) { 137 namespacePrefixMap.put(namespace, "tns" + prefixCount++); 138 } 139 140 return (String ) namespacePrefixMap.get(namespace); 141 } 142 143 149 public void putPrefix(String namespace, String prefix) { 150 namespacePrefixMap.put(namespace, prefix); 151 } 152 153 159 public void putAllPrefix(Map map) { 160 161 Iterator i = map.entrySet().iterator(); 162 163 while (i.hasNext()) { 164 Map.Entry entry = (Map.Entry ) i.next(); 165 166 put(entry.getKey(), entry.getValue()); 167 } 168 } 169 170 177 public static String makeNamespace(String clsName) { 178 return makeNamespace(clsName, "http"); 179 } 180 181 189 public static String makeNamespace(String clsName, String protocol) { 190 191 if (clsName.startsWith("[L")) { 192 clsName = clsName.substring(2, clsName.length() - 1); 193 } 194 195 if (clsName.lastIndexOf('.') == -1) { 196 return protocol + "://" + "DefaultNamespace"; 197 } 198 199 String packageName = clsName.substring(0, clsName.lastIndexOf('.')); 200 201 return makeNamespaceFromPackageName(packageName, protocol); 202 } 203 204 209 public static String getPackage(String namespace) { 210 try { 211 URL url = new URL (namespace); 212 StringTokenizer st = new StringTokenizer (url.getHost(), "."); 213 String [] words = new String [st.countTokens()]; 214 for (int i = 0; i < words.length; ++i) { 215 words[i] = st.nextToken(); 216 } 217 StringBuffer sb = new StringBuffer (80); 218 for (int i = words.length - 1; i >= 0; --i) { 219 String word = words[i]; 220 if (i != words.length - 1) { 222 sb.append('.'); 223 } 224 sb.append(word); 225 } 226 String pkg = sb.toString(); 227 if (pkg.equals("DefaultNamespace")) { 228 return ""; 229 } 230 return pkg; 231 } catch (MalformedURLException e) { 232 } 233 return null; 234 } 235 236 242 private static String makeNamespaceFromPackageName(String packageName) { 243 return makeNamespaceFromPackageName(packageName, "http"); 244 } 245 246 253 private static String makeNamespaceFromPackageName(String packageName, 254 String protocol) { 255 256 if ((packageName == null) || packageName.equals("")) { 257 return protocol + "://" + "DefaultNamespace"; 258 } 259 260 StringTokenizer st = new StringTokenizer (packageName, "."); 261 String [] words = new String [st.countTokens()]; 262 263 for (int i = 0; i < words.length; ++i) { 264 words[i] = st.nextToken(); 265 } 266 267 StringBuffer sb = new StringBuffer (80); 268 269 for (int i = words.length - 1; i >= 0; --i) { 270 String word = words[i]; 271 272 if (i != words.length - 1) { 274 sb.append('.'); 275 } 276 277 sb.append(word); 278 } 279 280 return protocol + "://" + sb.toString(); 281 } 282 283 288 public Iterator getNamespaces() { 289 return namespacePrefixMap.keySet().iterator(); 290 } 291 } 292 | Popular Tags |