KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > ant > AntFacade


1 package org.enhydra.kelp.ant;
2
3 import org.enhydra.kelp.ant.node.AntProject;
4
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.StringTokenizer JavaDoc;
9
10 import org.apache.crimson.tree.TextNode;
11 import org.w3c.dom.Node JavaDoc;
12 import org.w3c.dom.NodeList JavaDoc;
13
14 import java.util.regex.Matcher JavaDoc;
15 import java.util.regex.Pattern JavaDoc;
16 //import org.apache.crimson.tree.ElementNode2;
17
//import org.apache.crimson.tree.ElementNode;
18
import org.w3c.dom.Element JavaDoc;
19 import org.w3c.dom.Text JavaDoc;
20
21
22
23 /**
24  * @author Tweety
25  *
26  */

27 public class AntFacade {
28
29         private static final String JavaDoc BUILD_XML = "build.xml";
30
31     private AntXMLUtil util = null;
32         private AntProject project = null;
33     private String JavaDoc projectRootPath = null;
34
35     public AntFacade(AntProject project) throws Exception JavaDoc {
36                 this.project = project;
37         this.projectRootPath = project.getWorkingPath();
38         this.util = new AntXMLUtil(projectRootPath + "/" + BUILD_XML);
39     }
40
41
42     public void setPropertyLocation(String JavaDoc name, String JavaDoc location) {
43         Node propertyNode = util.getProjectProperty(name);
44         Node propertyValueNode = propertyNode.getAttributes().getNamedItem("location");
45         propertyValueNode.setNodeValue(location);
46     }
47
48     public String JavaDoc getPropertyLocation(String JavaDoc name) {
49         Node propertyNode = util.getProjectProperty(name);
50         if (propertyNode == null) {
51             return null;
52         }
53         if (propertyNode.getAttributes().getNamedItem("location") == null) {
54             return null;
55         }
56         String JavaDoc result = propertyNode.getAttributes().getNamedItem("location").getNodeValue();
57         return this.expandString(result);
58     }
59
60
61
62     /*
63      * Special case:
64      * if name="xmlc.package.name" then {
65      * change value of property "content.package.dir" and replace "." with "/" }
66      * */

67     public void setPropertyValue(String JavaDoc name, String JavaDoc value) {
68         Node propertyNode = util.getProjectProperty(name);
69         Node propertyValueNode = propertyNode.getAttributes().getNamedItem("value");
70         propertyValueNode.setNodeValue(value);
71         if(name.equalsIgnoreCase("xmlc.package.name")) {
72 // Node xmlcPackagePropertyNode = util.getProjectProperty("content.package.dir");
73
// Node xmlcPackagePropertyValueNode = xmlcPackagePropertyNode.getAttributes().getNamedItem("value");
74
value = value.replace('.', '/');
75             Node contentPackagePropertyNode = util.getProjectProperty("content.package.dir");
76                 if(contentPackagePropertyNode!=null){//WebApp do not have property "content.package.dir"
77
Node contentPackageValueNode = contentPackagePropertyNode.getAttributes().getNamedItem("value");
78                     contentPackageValueNode.setNodeValue(value);
79                 }
80         }
81     }
82
83     public String JavaDoc getPropertyValue(String JavaDoc name) {
84         Node propertyNode = util.getProjectProperty(name);
85         if (propertyNode == null) {
86             return null;
87         }
88         if (propertyNode.getAttributes().getNamedItem("value") == null) {
89             return null;
90         }
91         String JavaDoc result = propertyNode.getAttributes().getNamedItem("value").getNodeValue();
92         return this.expandString(result);
93     }
94
95
96
97     /*
98      * Remove all inner include tags and add all strings as new includes
99      *
100      */

101     public void replacePatternset(String JavaDoc id, String JavaDoc[] includes) {
102         Node oldPatternsetNode = util.getPatternsetById(id);
103
104         Node patternsetNode = oldPatternsetNode.cloneNode(false);
105                 //remove all existing filter nodes
106
oldPatternsetNode.getParentNode().replaceChild(patternsetNode, oldPatternsetNode);
107
108                 Text JavaDoc newLineTextNode = util.document.createTextNode("\n");
109         Text JavaDoc tabTextNode = util.document.createTextNode(" ");
110
111         //add new includes:
112
for(int i = 0; i < includes.length; i++) {
113
114                         patternsetNode.appendChild(newLineTextNode.cloneNode(true));
115                         patternsetNode.appendChild(tabTextNode.cloneNode(true));
116                         patternsetNode.appendChild(tabTextNode.cloneNode(true));
117
118                         Element JavaDoc newIncludeNode = util.document.createElement("include");
119                         newIncludeNode.setAttribute("name", includes[i]);
120             patternsetNode.appendChild(newIncludeNode);
121         }
122                 //enter and tab for ending tag patternset
123
patternsetNode.appendChild(newLineTextNode.cloneNode(true));
124                 patternsetNode.appendChild(tabTextNode.cloneNode(true));
125         }
126
127
128     public String JavaDoc[] getPatternset(String JavaDoc id) {
129         Node patternsetNode = util.getPatternsetById(id);
130                 if(patternsetNode != null) {
131                     NodeList JavaDoc includeChilds = patternsetNode.getChildNodes();
132                     ArrayList JavaDoc list = new ArrayList JavaDoc();
133                     for(int i = 0; i < includeChilds.getLength(); i++) {
134                             if(includeChilds.item(i).getNodeName().equalsIgnoreCase("include"))
135                                     list.add(includeChilds.item(i).getAttributes().getNamedItem("name").getNodeValue());
136                     }
137
138                     String JavaDoc[] s = new String JavaDoc[list.size()];
139                     for(int i = 0; i < s.length; i++) {
140                             s[i] = this.expandString(list.get(i).toString());
141                     }
142                     return s;
143                 }
144                 return null;
145     }
146
147
148
149     /*
150      * <filterset id="replacements">
151      * replace means delete all fiter tags and create new from HashMap
152      */

153     public void replaceInputFilters(HashMap JavaDoc filters) {
154 // Node oldInputTargetNode = util.getTargetByName("input");
155
Node oldInputTargetNode = util.getFiltersetById("replacements");
156         NodeList JavaDoc childs = oldInputTargetNode.getChildNodes();
157
158                 Node inputTargetNode = oldInputTargetNode.cloneNode(false);
159
160                 //remove all existing filter nodes
161
Node first = oldInputTargetNode.getFirstChild();
162                 while(first != null) {
163                     if(first.getNodeName().equals("filter")) {
164                         first = first.getNextSibling();
165                         if(first.getNodeType() == Node.TEXT_NODE)
166                            first = first.getNextSibling();
167                     }
168                     else {
169                         inputTargetNode.appendChild(first.cloneNode(true));
170                         first = first.getNextSibling();
171                     }
172                 }
173                 oldInputTargetNode.getParentNode().replaceChild(inputTargetNode, oldInputTargetNode);
174
175                 Text JavaDoc newLineTextNode = util.document.createTextNode("\n");
176         Text JavaDoc tabTextNode = util.document.createTextNode(" ");
177
178         //add new filter nodes
179
Object JavaDoc[] keys = filters.keySet().toArray();
180         for(int i = 0; i < filters.size(); i++) {
181
182                         inputTargetNode.appendChild(newLineTextNode.cloneNode(true));
183                         inputTargetNode.appendChild(tabTextNode.cloneNode(true));
184                         inputTargetNode.appendChild(tabTextNode.cloneNode(true));
185
186                         Element JavaDoc newFilterNode = util.document.createElement("filter");
187                         newFilterNode.setAttribute("token", keys[i].toString());
188             newFilterNode.setAttribute("value", filters.get(keys[i]).toString());
189             inputTargetNode.appendChild(newFilterNode);
190         }
191                 inputTargetNode.appendChild(newLineTextNode.cloneNode(true));
192                 inputTargetNode.appendChild(tabTextNode.cloneNode(true));
193     }
194
195
196     /*
197      * When 'value' attribut contains string that begins with '${'
198      * property named in '${...}' is found, and its 'value' is placed
199      * into HashMap
200      */

201     public HashMap JavaDoc getInputFilters() {
202 // Node inputTargetNode = util.getTargetByName("input");
203
Node inputTargetNode = util.getFiltersetById("replacements");
204                 HashMap JavaDoc hash = new HashMap JavaDoc();
205                 if(inputTargetNode != null) {
206                     NodeList JavaDoc childs = inputTargetNode.getChildNodes();
207
208                     //find filter nodes and put them into HashMap:
209
for(int i = 0; i < childs.getLength(); i++) {
210                             if(childs.item(i).getNodeName().equals("filter")) {
211                                     String JavaDoc key = childs.item(i).getAttributes().getNamedItem("token").getNodeValue();
212                                     String JavaDoc value = childs.item(i).getAttributes().getNamedItem("value").getNodeValue();
213                                     //hash.put(key, this.expandString(value)); DACHA 28.2.2003
214
hash.put(key, value);
215                             }
216                     }
217                 }
218
219         return hash;
220     }
221
222     /*
223      * This method adds dependedName into depends list of target
224      * only if it not exists in the list
225      */

226     public void addDependedTarget(String JavaDoc name, String JavaDoc dependedName) {
227         Node targetNode = util.getTargetByName(name);
228         String JavaDoc dependsValue = targetNode.getAttributes().getNamedItem("depends").getNodeValue();
229         if(dependsValue.indexOf(dependedName) == -1) {
230             String JavaDoc newList = this.addStringToDependensList(dependsValue, dependedName/*, name*/);
231             targetNode.getAttributes().getNamedItem("depends").setNodeValue(newList);
232         }
233     }
234
235     /*
236      * This method removes dependedName from depends list of the target
237      */

238     public void removeDependedTarget(String JavaDoc name, String JavaDoc dependedName) {
239         Node targetNode = util.getTargetByName(name);
240         String JavaDoc dependsValue = targetNode.getAttributes().getNamedItem("depends").getNodeValue();
241         if(dependsValue.indexOf(dependedName) != -1) {
242             String JavaDoc newList = this.removeStringFromDependensList(dependsValue, dependedName);
243             targetNode.getAttributes().getNamedItem("depends").setNodeValue(newList);
244         }
245     }
246
247     /*
248      * @return true if dependedName is in the depends list
249      * otherwise, false
250      */

251     public boolean checkDependedTarget(String JavaDoc name, String JavaDoc dependedName) {
252         Node targetNode = util.getTargetByName(name);
253         String JavaDoc dependsValue = targetNode.getAttributes().getNamedItem("depends").getNodeValue();
254         if(dependsValue.indexOf(dependedName) != -1)
255             return true;
256         else
257             return false;
258     }
259
260
261
262     private String JavaDoc addStringToDependensList(String JavaDoc dependsList, String JavaDoc item) {
263
264         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(dependsList, ",");
265         HashMap JavaDoc hash = new HashMap JavaDoc();
266
267         while(tokenizer.hasMoreTokens()){
268             String JavaDoc token = tokenizer.nextToken().trim();
269             hash.put(this.getOrdinalForDependsString(token), token);
270         }
271         hash.put(this.getOrdinalForDependsString(item), item);
272
273         String JavaDoc newList = "";
274         if(hash.containsKey(new Integer JavaDoc(1))) newList += hash.get(new Integer JavaDoc(1)) + ", ";
275         if(hash.containsKey(new Integer JavaDoc(2))) newList += hash.get(new Integer JavaDoc(2)) + ", ";
276         if(hash.containsKey(new Integer JavaDoc(3))) newList += hash.get(new Integer JavaDoc(3)) + ", ";
277         if(hash.containsKey(new Integer JavaDoc(4))) newList += hash.get(new Integer JavaDoc(4)) + ", ";
278
279         if(newList.endsWith(", "))
280             newList = newList.substring(0, newList.length() - 2);
281
282         return newList;
283     }
284
285
286     /*
287      * @return index of the dependedName in list
288      *
289      * make: xmlc, dods, compile, deploy
290      * deploy: input, content, archive
291      */

292     private Integer JavaDoc getOrdinalForDependsString(String JavaDoc item) {
293         if(item.equalsIgnoreCase("xmlc") || item.equalsIgnoreCase("input"))
294             return new Integer JavaDoc(1);
295         if(item.equalsIgnoreCase("dods") || item.equalsIgnoreCase("content"))
296             return new Integer JavaDoc(2);
297         if(item.equalsIgnoreCase("compile") || item.equalsIgnoreCase("archive"))
298             return new Integer JavaDoc(3);
299         if(item.equalsIgnoreCase("deploy") || item.equalsIgnoreCase("makeWAR"))
300             return new Integer JavaDoc(4);
301
302         return new Integer JavaDoc(0);
303     }
304
305
306     private String JavaDoc removeStringFromDependensList(String JavaDoc dependsList, String JavaDoc item) {
307         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(dependsList, ",");
308         String JavaDoc newList = "";
309
310         while(tokenizer.hasMoreTokens()) {
311             String JavaDoc token = tokenizer.nextToken().trim();
312             if(!token.equalsIgnoreCase(item))
313                 newList += token + ", ";
314         }
315
316         if(newList.endsWith(", "))
317             newList = newList.substring(0, newList.length() - 2);
318
319         return newList;
320     }
321
322
323         public boolean save() {
324             return util.saveDocument();
325         }
326
327     String JavaDoc expandString(String JavaDoc str) {
328
329         String JavaDoc replacement = null;
330         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(str);
331
332         Pattern JavaDoc p = Pattern.compile("\\$\\{[^\\}]*\\}");
333         Matcher JavaDoc m = p.matcher(sb);
334
335         while(m.find()) {
336             String JavaDoc propName = m.group(0).substring(2, m.group(0).length()-1);
337             replacement = getProperty(propName);
338
339             if(replacement != null) {
340                 propName = "${" + propName + "}";
341                 sb.replace(sb.indexOf(propName), sb.indexOf(propName) + propName.length(), replacement);
342             }
343             //else ant variable is not resolved !!!
344
}
345
346         return sb.toString();
347     }
348
349
350     private String JavaDoc getProperty(String JavaDoc propertyName) {
351
352         String JavaDoc result = null;
353
354         Node propertyNode = util.getProjectProperty(propertyName);
355         try {
356             result = propertyNode.getAttributes().getNamedItem("location").getNodeValue();
357         } catch(NullPointerException JavaDoc e) {
358             result = null;
359         }
360
361
362         if(result == null) {
363             try {
364                 result = propertyNode.getAttributes().getNamedItem("value").getNodeValue();
365             } catch(NullPointerException JavaDoc e) {
366                 result = null;
367             }
368         }
369
370         if(result != null)
371             result = this.expandString(result);
372
373         return result;
374     }
375
376 }
377
Popular Tags