1 package org.enhydra.kelp.ant; 2 3 import org.enhydra.kelp.ant.node.AntProject; 4 5 6 import java.util.ArrayList ; 7 import java.util.HashMap ; 8 import java.util.StringTokenizer ; 9 10 import org.apache.crimson.tree.TextNode; 11 import org.w3c.dom.Node ; 12 import org.w3c.dom.NodeList ; 13 14 import java.util.regex.Matcher ; 15 import java.util.regex.Pattern ; 16 import org.w3c.dom.Element ; 19 import org.w3c.dom.Text ; 20 21 22 23 27 public class AntFacade { 28 29 private static final String BUILD_XML = "build.xml"; 30 31 private AntXMLUtil util = null; 32 private AntProject project = null; 33 private String projectRootPath = null; 34 35 public AntFacade(AntProject project) throws Exception { 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 name, String location) { 43 Node propertyNode = util.getProjectProperty(name); 44 Node propertyValueNode = propertyNode.getAttributes().getNamedItem("location"); 45 propertyValueNode.setNodeValue(location); 46 } 47 48 public String getPropertyLocation(String 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 result = propertyNode.getAttributes().getNamedItem("location").getNodeValue(); 57 return this.expandString(result); 58 } 59 60 61 62 67 public void setPropertyValue(String name, String 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 value = value.replace('.', '/'); 75 Node contentPackagePropertyNode = util.getProjectProperty("content.package.dir"); 76 if(contentPackagePropertyNode!=null){ Node contentPackageValueNode = contentPackagePropertyNode.getAttributes().getNamedItem("value"); 78 contentPackageValueNode.setNodeValue(value); 79 } 80 } 81 } 82 83 public String getPropertyValue(String 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 result = propertyNode.getAttributes().getNamedItem("value").getNodeValue(); 92 return this.expandString(result); 93 } 94 95 96 97 101 public void replacePatternset(String id, String [] includes) { 102 Node oldPatternsetNode = util.getPatternsetById(id); 103 104 Node patternsetNode = oldPatternsetNode.cloneNode(false); 105 oldPatternsetNode.getParentNode().replaceChild(patternsetNode, oldPatternsetNode); 107 108 Text newLineTextNode = util.document.createTextNode("\n"); 109 Text tabTextNode = util.document.createTextNode(" "); 110 111 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 newIncludeNode = util.document.createElement("include"); 119 newIncludeNode.setAttribute("name", includes[i]); 120 patternsetNode.appendChild(newIncludeNode); 121 } 122 patternsetNode.appendChild(newLineTextNode.cloneNode(true)); 124 patternsetNode.appendChild(tabTextNode.cloneNode(true)); 125 } 126 127 128 public String [] getPatternset(String id) { 129 Node patternsetNode = util.getPatternsetById(id); 130 if(patternsetNode != null) { 131 NodeList includeChilds = patternsetNode.getChildNodes(); 132 ArrayList list = new ArrayList (); 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 [] s = new String [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 153 public void replaceInputFilters(HashMap filters) { 154 Node oldInputTargetNode = util.getFiltersetById("replacements"); 156 NodeList childs = oldInputTargetNode.getChildNodes(); 157 158 Node inputTargetNode = oldInputTargetNode.cloneNode(false); 159 160 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 newLineTextNode = util.document.createTextNode("\n"); 176 Text tabTextNode = util.document.createTextNode(" "); 177 178 Object [] 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 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 201 public HashMap getInputFilters() { 202 Node inputTargetNode = util.getFiltersetById("replacements"); 204 HashMap hash = new HashMap (); 205 if(inputTargetNode != null) { 206 NodeList childs = inputTargetNode.getChildNodes(); 207 208 for(int i = 0; i < childs.getLength(); i++) { 210 if(childs.item(i).getNodeName().equals("filter")) { 211 String key = childs.item(i).getAttributes().getNamedItem("token").getNodeValue(); 212 String value = childs.item(i).getAttributes().getNamedItem("value").getNodeValue(); 213 hash.put(key, value); 215 } 216 } 217 } 218 219 return hash; 220 } 221 222 226 public void addDependedTarget(String name, String dependedName) { 227 Node targetNode = util.getTargetByName(name); 228 String dependsValue = targetNode.getAttributes().getNamedItem("depends").getNodeValue(); 229 if(dependsValue.indexOf(dependedName) == -1) { 230 String newList = this.addStringToDependensList(dependsValue, dependedName); 231 targetNode.getAttributes().getNamedItem("depends").setNodeValue(newList); 232 } 233 } 234 235 238 public void removeDependedTarget(String name, String dependedName) { 239 Node targetNode = util.getTargetByName(name); 240 String dependsValue = targetNode.getAttributes().getNamedItem("depends").getNodeValue(); 241 if(dependsValue.indexOf(dependedName) != -1) { 242 String newList = this.removeStringFromDependensList(dependsValue, dependedName); 243 targetNode.getAttributes().getNamedItem("depends").setNodeValue(newList); 244 } 245 } 246 247 251 public boolean checkDependedTarget(String name, String dependedName) { 252 Node targetNode = util.getTargetByName(name); 253 String 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 addStringToDependensList(String dependsList, String item) { 263 264 StringTokenizer tokenizer = new StringTokenizer (dependsList, ","); 265 HashMap hash = new HashMap (); 266 267 while(tokenizer.hasMoreTokens()){ 268 String token = tokenizer.nextToken().trim(); 269 hash.put(this.getOrdinalForDependsString(token), token); 270 } 271 hash.put(this.getOrdinalForDependsString(item), item); 272 273 String newList = ""; 274 if(hash.containsKey(new Integer (1))) newList += hash.get(new Integer (1)) + ", "; 275 if(hash.containsKey(new Integer (2))) newList += hash.get(new Integer (2)) + ", "; 276 if(hash.containsKey(new Integer (3))) newList += hash.get(new Integer (3)) + ", "; 277 if(hash.containsKey(new Integer (4))) newList += hash.get(new Integer (4)) + ", "; 278 279 if(newList.endsWith(", ")) 280 newList = newList.substring(0, newList.length() - 2); 281 282 return newList; 283 } 284 285 286 292 private Integer getOrdinalForDependsString(String item) { 293 if(item.equalsIgnoreCase("xmlc") || item.equalsIgnoreCase("input")) 294 return new Integer (1); 295 if(item.equalsIgnoreCase("dods") || item.equalsIgnoreCase("content")) 296 return new Integer (2); 297 if(item.equalsIgnoreCase("compile") || item.equalsIgnoreCase("archive")) 298 return new Integer (3); 299 if(item.equalsIgnoreCase("deploy") || item.equalsIgnoreCase("makeWAR")) 300 return new Integer (4); 301 302 return new Integer (0); 303 } 304 305 306 private String removeStringFromDependensList(String dependsList, String item) { 307 StringTokenizer tokenizer = new StringTokenizer (dependsList, ","); 308 String newList = ""; 309 310 while(tokenizer.hasMoreTokens()) { 311 String 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 expandString(String str) { 328 329 String replacement = null; 330 StringBuffer sb = new StringBuffer (str); 331 332 Pattern p = Pattern.compile("\\$\\{[^\\}]*\\}"); 333 Matcher m = p.matcher(sb); 334 335 while(m.find()) { 336 String 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 } 345 346 return sb.toString(); 347 } 348 349 350 private String getProperty(String propertyName) { 351 352 String result = null; 353 354 Node propertyNode = util.getProjectProperty(propertyName); 355 try { 356 result = propertyNode.getAttributes().getNamedItem("location").getNodeValue(); 357 } catch(NullPointerException e) { 358 result = null; 359 } 360 361 362 if(result == null) { 363 try { 364 result = propertyNode.getAttributes().getNamedItem("value").getNodeValue(); 365 } catch(NullPointerException 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 |