KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > ant > IvyVar


1 /*
2  * This file is subject to the licence found in LICENCE.TXT in the root directory of the project.
3  * Copyright Jayasoft 2005 - All rights reserved
4  *
5  * #SNAPSHOT#
6  */

7 package fr.jayasoft.ivy.ant;
8
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.net.URL JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Properties JavaDoc;
15
16 import org.apache.tools.ant.BuildException;
17
18 import fr.jayasoft.ivy.Ivy;
19
20 /**
21  * This task let user set ivy variables from ant.
22  *
23  * @author Xavier Hanin
24  */

25 public class IvyVar extends IvyTask {
26     private String JavaDoc _name;
27     private String JavaDoc _value;
28     
29     private File JavaDoc _file;
30     private String JavaDoc _url;
31     
32     private String JavaDoc _prefix;
33
34     public File JavaDoc getFile() {
35         return _file;
36     }
37     
38
39     public void setFile(File JavaDoc file) {
40         _file = file;
41     }
42     
43
44     public String JavaDoc getName() {
45         return _name;
46     }
47     
48
49     public void setName(String JavaDoc name) {
50         _name = name;
51     }
52     
53
54     public String JavaDoc getPrefix() {
55         return _prefix;
56     }
57     
58
59     public void setPrefix(String JavaDoc prefix) {
60         _prefix = prefix;
61     }
62     
63
64     public String JavaDoc getUrl() {
65         return _url;
66     }
67     
68
69     public void setUrl(String JavaDoc url) {
70         _url = url;
71     }
72     
73
74     public String JavaDoc getValue() {
75         return _value;
76     }
77     
78
79     public void setValue(String JavaDoc 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 JavaDoc props = new Properties JavaDoc();
89             InputStream JavaDoc is = null;
90             try {
91                 if (getFile() != null) {
92                     is = new FileInputStream JavaDoc(getFile());
93                 } else if (getUrl() != null) {
94                     is = new URL JavaDoc(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 JavaDoc 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 JavaDoc e) {}
104                 }
105             }
106             for (Iterator JavaDoc iter = props.keySet().iterator(); iter.hasNext();) {
107                 String JavaDoc name = (String JavaDoc)iter.next();
108                 String JavaDoc value = (String JavaDoc)props.get(name);
109                 ivy.setVariable(getVarName(name), value);
110             }
111         }
112     }
113
114
115     private String JavaDoc getVarName(String JavaDoc name) {
116         String JavaDoc 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