1 16 17 package org.apache.axis.tools.ant.wsdl; 18 19 import org.apache.tools.ant.BuildException; 20 import org.apache.tools.ant.Project; 21 import org.apache.tools.ant.ProjectComponent; 22 23 import java.io.BufferedInputStream ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.IOException ; 27 import java.util.Enumeration ; 28 import java.util.HashMap ; 29 import java.util.Properties ; 30 31 35 public class NamespaceMapping implements Mapper { 36 37 38 private String namespace = null; 39 private String packageName = null; 40 private File mappingFile; 41 42 45 public NamespaceMapping() { 46 } 47 48 52 public void setNamespace(String value) { 53 namespace = value; 54 } 55 56 60 public void setPackage(String value) { 61 packageName = value; 62 } 63 64 69 public void setFile(File file) { 70 mappingFile = file; 71 } 72 73 81 protected void map(ProjectComponent owner, 82 HashMap map, 83 String packName, 84 String nspace, 85 boolean packageIsKey) { 86 owner.log("mapping "+nspace+" to "+packName, Project.MSG_VERBOSE); 87 if(packageIsKey) { 88 map.put(packName,nspace); 89 } else { 90 map.put(nspace, packName); 91 } 92 } 93 94 97 private void validate() { 98 if (mappingFile != null) { 99 if (namespace != null || packageName != null) { 100 throw new BuildException( 101 "Namespace or Package cannot be used with a File attribute"); 102 } 103 } else { 104 if (namespace == null) { 105 throw new BuildException("namespace must be defined"); 106 } 107 if (packageName == null) { 108 throw new BuildException("package must be defined"); 109 } 110 } 111 } 112 113 120 protected void mapFile(ProjectComponent owner, HashMap map, boolean packageIsKey) throws BuildException { 121 Properties props = loadMappingPropertiesFile(); 122 Enumeration keys = props.keys(); 123 while (keys.hasMoreElements()) { 124 String packageName = (String ) keys.nextElement(); 125 String namespace = props.getProperty(packageName); 126 map(owner, map, packageName, namespace, packageIsKey); 127 } 128 } 129 130 135 private Properties loadMappingPropertiesFile() throws BuildException { 136 Properties props = new Properties (); 137 FileInputStream instr = null; 138 try { 139 instr = new FileInputStream (mappingFile); 140 props.load(new BufferedInputStream (instr)); 141 } catch (IOException e) { 142 throw new BuildException("Failed to load " + mappingFile, e); 143 } finally { 144 if (instr != null) { 145 try { 146 instr.close(); 147 } catch (IOException e) { 148 } 149 } 150 } 151 return props; 152 } 153 154 155 162 public void execute(ProjectComponent owner, HashMap map, boolean packageIsKey) throws BuildException { 163 validate(); 164 if (mappingFile != null) { 165 mapFile(owner, map,packageIsKey); 166 } else { 167 map(owner, map, packageName, namespace, packageIsKey); 168 } 169 } 170 171 172 } | Popular Tags |