1 7 package fr.jayasoft.ivy.ant; 8 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.InputStream ; 12 import java.net.URL ; 13 import java.util.Iterator ; 14 import java.util.Properties ; 15 16 import org.apache.tools.ant.BuildException; 17 18 import fr.jayasoft.ivy.Ivy; 19 20 25 public class IvyVar extends IvyTask { 26 private String _name; 27 private String _value; 28 29 private File _file; 30 private String _url; 31 32 private String _prefix; 33 34 public File getFile() { 35 return _file; 36 } 37 38 39 public void setFile(File file) { 40 _file = file; 41 } 42 43 44 public String getName() { 45 return _name; 46 } 47 48 49 public void setName(String name) { 50 _name = name; 51 } 52 53 54 public String getPrefix() { 55 return _prefix; 56 } 57 58 59 public void setPrefix(String prefix) { 60 _prefix = prefix; 61 } 62 63 64 public String getUrl() { 65 return _url; 66 } 67 68 69 public void setUrl(String url) { 70 _url = url; 71 } 72 73 74 public String getValue() { 75 return _value; 76 } 77 78 79 public void setValue(String value) { 80 _value = value; 81 } 82 83 public void execute() throws BuildException { 84 Ivy ivy = getIvyInstance(); 85 if (getName() != null) { 86 ivy.setVariable(getVarName(getName()), getValue()); 87 } else { 88 Properties props = new Properties (); 89 InputStream is = null; 90 try { 91 if (getFile() != null) { 92 is = new FileInputStream (getFile()); 93 } else if (getUrl() != null) { 94 is = new URL (getUrl()).openStream(); 95 } else { 96 throw new BuildException("specify either name or file or url to ivy var task"); 97 } 98 props.load(is); 99 } catch (Exception ex) { 100 throw new BuildException("impossible to load variables from file: "+ex, ex); 101 } finally { 102 if (is != null) { 103 try {is.close();} catch (Exception e) {} 104 } 105 } 106 for (Iterator iter = props.keySet().iterator(); iter.hasNext();) { 107 String name = (String )iter.next(); 108 String value = (String )props.get(name); 109 ivy.setVariable(getVarName(name), value); 110 } 111 } 112 } 113 114 115 private String getVarName(String name) { 116 String prefix = getPrefix(); 117 if (prefix != null) { 118 if (prefix.endsWith(".")) { 119 return prefix + name; 120 } else { 121 return prefix + "." + name; 122 } 123 } 124 return name; 125 } 126 } 127 | Popular Tags |