1 55 package org.jboss.axis.wsdl.fromJava; 56 57 import java.util.HashMap ; 58 import java.util.Iterator ; 59 import java.util.Map ; 60 import java.util.StringTokenizer ; 61 62 67 public class Namespaces extends HashMap 68 { 69 private int prefixCount = 1; 70 private HashMap namespacePrefixMap = null; 71 72 public Namespaces() 73 { 74 super(); 75 namespacePrefixMap = new HashMap (); 76 } 77 78 85 public String getCreate(String key) 86 { 87 Object value = super.get(key); 88 if (value == null) 89 { 90 value = makeNamespaceFromPackageName(key); 91 put(key, value, null); 92 } 93 return (String )value; 94 } 95 96 104 public String getCreate(String key, String prefix) 105 { 106 Object value = super.get(key); 107 if (value == null) 108 { 109 value = makeNamespaceFromPackageName(key); 110 put(key, value, prefix); 111 } 112 return (String )value; 113 } 114 115 125 public Object put(Object key, Object value, String prefix) 126 { 127 if (prefix != null) 128 namespacePrefixMap.put(value, prefix); 129 else 130 getCreatePrefix((String )value); 131 return super.put(key, value); 132 } 133 134 141 public void putAll(Map map) 142 { 143 Iterator i = map.entrySet().iterator(); 144 while (i.hasNext()) 145 { 146 Map.Entry entry = (Map.Entry )i.next(); 147 put(entry.getKey(), entry.getValue(), null); 148 } 149 } 150 151 157 public String getCreatePrefix(String namespace) 158 { 159 if (namespacePrefixMap.get(namespace) == null) 160 { 161 namespacePrefixMap.put(namespace, "tns" + prefixCount++); 162 } 163 return (String )namespacePrefixMap.get(namespace); 164 } 165 166 172 public void putPrefix(String namespace, String prefix) 173 { 174 namespacePrefixMap.put(namespace, prefix); 175 } 176 177 183 public void putAllPrefix(Map map) 184 { 185 Iterator i = map.entrySet().iterator(); 186 while (i.hasNext()) 187 { 188 Map.Entry entry = (Map.Entry )i.next(); 189 put(entry.getKey(), entry.getValue()); 190 } 191 } 192 193 200 public static String makeNamespace(String clsName) 201 { 202 return makeNamespace(clsName, "http"); 203 } 204 205 213 public static String makeNamespace(String clsName, String protocol) 214 { 215 if (clsName.lastIndexOf('.') == -1) 216 return protocol + "://" + "DefaultNamespace"; 217 String packageName = clsName.substring(0, clsName.lastIndexOf('.')); 218 return makeNamespaceFromPackageName(packageName, protocol); 219 } 220 221 private static String makeNamespaceFromPackageName(String packageName) 222 { 223 return makeNamespaceFromPackageName(packageName, "http"); 224 } 225 226 227 private static String makeNamespaceFromPackageName(String packageName, String protocol) 228 { 229 if (packageName == null || packageName.equals("")) 230 return protocol + "://" + "DefaultNamespace"; 231 StringTokenizer st = new StringTokenizer (packageName, "."); 232 String [] words = new String [st.countTokens()]; 233 for (int i = 0; i < words.length; ++i) 234 words[i] = st.nextToken(); 235 236 StringBuffer sb = new StringBuffer (80); 237 for (int i = words.length - 1; i >= 0; --i) 238 { 239 String word = words[i]; 240 if (i != words.length - 1) 242 sb.append('.'); 243 sb.append(word); 244 } 245 return protocol + "://" + sb.toString(); 246 } 247 248 253 public Iterator getNamespaces() 254 { 255 return namespacePrefixMap.keySet().iterator(); 256 } 257 } 258 | Popular Tags |