1 16 package org.apache.axis.wsdl.toJava; 17 18 import org.apache.axis.utils.JavaUtils; 19 20 import java.io.File ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.StringTokenizer ; 25 import java.util.Vector ; 26 27 31 public class Namespaces extends HashMap { 32 33 34 private String root; 35 36 37 private String defaultPackage = null; 38 39 40 private static final char[] pkgSeparators = {'.', ':'}; 41 42 43 private static final char javaPkgSeparator = pkgSeparators[0]; 44 45 46 private Map pkg2NamespacesMap = new HashMap (); 47 48 55 private static String normalizePackageName(String pkg, char separator) { 56 57 for (int i = 0; i < pkgSeparators.length; i++) { 58 pkg = pkg.replace(pkgSeparators[i], separator); 59 } 60 61 return pkg; 62 } 63 64 69 public Namespaces(String root) { 70 71 super(); 72 73 this.root = root; 74 } 76 81 private Namespaces(Namespaces clone) { 82 83 super(clone); 84 85 this.root = clone.root; 86 this.defaultPackage = clone.defaultPackage; 87 } 89 94 public Object clone() { 95 return new Namespaces(this); 96 } 98 105 public String getCreate(String key) { 106 return getCreate(key, true); 107 } 109 117 String getCreate(String key, boolean create) { 118 119 if (defaultPackage != null) { 120 put(key, defaultPackage); 121 return defaultPackage; 122 } 123 124 String value = (String ) super.get(key); 125 126 if ((value == null) && create) { 127 value = normalizePackageName((String ) Utils.makePackageName(key), 128 javaPkgSeparator); 129 130 put(key, value); 131 } 132 133 return (String ) value; 134 } 136 143 public String getAsDir(String key) { 144 145 if (defaultPackage != null) { 146 return toDir(defaultPackage); 147 } 148 149 String pkg = (String ) get(key); 150 151 return toDir(pkg); 152 } 154 161 public String toDir(String pkg) { 162 163 String dir = null; 164 165 if (pkg != null) { 166 pkg = normalizePackageName(pkg, File.separatorChar); 167 } 168 169 if (root == null) { 170 dir = pkg; 171 } else { 172 dir = root + File.separatorChar + pkg; 173 } 174 175 return (dir == null) 176 ? "" 177 : dir + File.separatorChar; 178 } 180 186 public void putAll(Map map) { 187 188 Iterator i = map.entrySet().iterator(); 189 190 while (i.hasNext()) { 191 Map.Entry entry = (Map.Entry ) i.next(); 192 Object key = entry.getKey(); 193 String pkg = (String ) entry.getValue(); 194 195 pkg = javify(pkg); 196 197 put(key, pkg); 198 } 199 } 201 208 private String javify(String pkg) { 209 210 StringTokenizer st = new StringTokenizer (pkg, "."); 211 212 pkg = ""; 213 214 while (st.hasMoreTokens()) { 215 String token = st.nextToken(); 216 217 if (JavaUtils.isJavaKeyword(token)) { 218 token = JavaUtils.makeNonJavaKeyword(token); 219 } 220 221 pkg = pkg + token; 222 223 if (st.hasMoreTokens()) { 224 pkg = pkg + '.'; 225 } 226 } 227 228 return pkg; 229 } 231 236 public void mkdir(String pkg) { 237 238 String pkgDirString = toDir(pkg); 239 File packageDir = new File (pkgDirString); 240 241 packageDir.mkdirs(); 242 } 244 249 public void setDefaultPackage(String defaultPackage) { 250 this.defaultPackage = defaultPackage; 251 } 252 253 public Object put(Object key, Object value) { 254 Vector v = null; 256 if (!pkg2NamespacesMap.containsKey(value)) { 257 v = new Vector (); 258 } else { 259 v = (Vector )pkg2NamespacesMap.get(value); 260 } 261 if (!v.contains(key)) { 263 v.add(key); 264 } 265 pkg2NamespacesMap.put(value, v); 266 267 return super.put(key, value); 268 } 269 270 public Map getPkg2NamespacesMap() { 271 return pkg2NamespacesMap; 272 } 273 } | Popular Tags |