001: package org.enhydra.kelp.ant;
002:
003: import org.enhydra.kelp.ant.node.AntProject;
004:
005: import java.util.ArrayList;
006: import java.util.HashMap;
007: import java.util.StringTokenizer;
008:
009: import org.w3c.dom.Node;
010: import org.w3c.dom.NodeList;
011:
012: import java.util.regex.Matcher;
013: import java.util.regex.Pattern;
014: import org.w3c.dom.Element;
015: import org.w3c.dom.Text;
016:
017: /**
018: * @author Tweety
019: *
020: */
021: public class AntFacade {
022:
023: private static final String BUILD_XML = "build.xml";
024:
025: private AntXMLUtil util = null;
026: private String projectRootPath = null;
027:
028: public AntFacade(AntProject project) throws Exception {
029: this .projectRootPath = project.getWorkingPath();
030: this .util = new AntXMLUtil(projectRootPath + "/" + BUILD_XML);
031: }
032:
033: public void setPropertyLocation(String name, String location) {
034: Node propertyNode = util.getProjectProperty(name);
035: Node propertyValueNode = propertyNode.getAttributes()
036: .getNamedItem("location");
037: propertyValueNode.setNodeValue(location);
038: }
039:
040: public String getPropertyLocation(String name) {
041: Node propertyNode = util.getProjectProperty(name);
042: if (propertyNode == null) {
043: return null;
044: }
045: if (propertyNode.getAttributes().getNamedItem("location") == null) {
046: return null;
047: }
048: String result = propertyNode.getAttributes().getNamedItem(
049: "location").getNodeValue();
050: return this .expandString(result);
051: }
052:
053: /*
054: * Special case:
055: * if name="xmlc.package.name" then {
056: * change value of property "content.package.dir" and replace "." with "/" }
057: * */
058: public void setPropertyValue(String name, String value) {
059: Node propertyNode = util.getProjectProperty(name);
060: Node propertyValueNode = propertyNode.getAttributes()
061: .getNamedItem("value");
062: propertyValueNode.setNodeValue(value);
063: if (name.equalsIgnoreCase("xmlc.package.name")) {
064: // Node xmlcPackagePropertyNode = util.getProjectProperty("content.package.dir");
065: // Node xmlcPackagePropertyValueNode = xmlcPackagePropertyNode.getAttributes().getNamedItem("value");
066: value = value.replace('.', '/');
067: Node contentPackagePropertyNode = util
068: .getProjectProperty("content.package.dir");
069: if (contentPackagePropertyNode != null) {//WebApp do not have property "content.package.dir"
070: Node contentPackageValueNode = contentPackagePropertyNode
071: .getAttributes().getNamedItem("value");
072: contentPackageValueNode.setNodeValue(value);
073: }
074: }
075: }
076:
077: public String getPropertyValue(String name) {
078: Node propertyNode = util.getProjectProperty(name);
079: if (propertyNode == null) {
080: return null;
081: }
082: if (propertyNode.getAttributes().getNamedItem("value") == null) {
083: return null;
084: }
085: String result = propertyNode.getAttributes().getNamedItem(
086: "value").getNodeValue();
087: return this .expandString(result);
088: }
089:
090: /*
091: * Remove all inner include tags and add all strings as new includes
092: *
093: */
094: public void replacePatternset(String id, String[] includes) {
095: Node oldPatternsetNode = util.getPatternsetById(id);
096:
097: Node patternsetNode = oldPatternsetNode.cloneNode(false);
098: //remove all existing filter nodes
099: oldPatternsetNode.getParentNode().replaceChild(patternsetNode,
100: oldPatternsetNode);
101:
102: Text newLineTextNode = util.document.createTextNode("\n");
103: Text tabTextNode = util.document.createTextNode(" ");
104:
105: //add new includes:
106: for (int i = 0; i < includes.length; i++) {
107:
108: patternsetNode.appendChild(newLineTextNode.cloneNode(true));
109: patternsetNode.appendChild(tabTextNode.cloneNode(true));
110: patternsetNode.appendChild(tabTextNode.cloneNode(true));
111:
112: Element newIncludeNode = util.document
113: .createElement("include");
114: newIncludeNode.setAttribute("name", includes[i]);
115: patternsetNode.appendChild(newIncludeNode);
116: }
117: //enter and tab for ending tag patternset
118: patternsetNode.appendChild(newLineTextNode.cloneNode(true));
119: patternsetNode.appendChild(tabTextNode.cloneNode(true));
120: }
121:
122: public String[] getPatternset(String id) {
123: Node patternsetNode = util.getPatternsetById(id);
124: if (patternsetNode != null) {
125: NodeList includeChilds = patternsetNode.getChildNodes();
126: ArrayList list = new ArrayList();
127: for (int i = 0; i < includeChilds.getLength(); i++) {
128: if (includeChilds.item(i).getNodeName()
129: .equalsIgnoreCase("include"))
130: list.add(includeChilds.item(i).getAttributes()
131: .getNamedItem("name").getNodeValue());
132: }
133:
134: String[] s = new String[list.size()];
135: for (int i = 0; i < s.length; i++) {
136: s[i] = this .expandString(list.get(i).toString());
137: }
138: return s;
139: }
140: return null;
141: }
142:
143: /*
144: * <filterset id="replacements">
145: * replace means delete all fiter tags and create new from HashMap
146: */
147: public void replaceInputFilters(HashMap filters) {
148: // Node oldInputTargetNode = util.getTargetByName("input");
149: Node oldInputTargetNode = util.getFiltersetById("replacements");
150:
151: Node inputTargetNode = oldInputTargetNode.cloneNode(false);
152:
153: //remove all existing filter nodes
154: Node first = oldInputTargetNode.getFirstChild();
155: while (first != null) {
156: if (first.getNodeName().equals("filter")) {
157: first = first.getNextSibling();
158: if (first.getNodeType() == Node.TEXT_NODE)
159: first = first.getNextSibling();
160: } else {
161: inputTargetNode.appendChild(first.cloneNode(true));
162: first = first.getNextSibling();
163: }
164: }
165: oldInputTargetNode.getParentNode().replaceChild(
166: inputTargetNode, oldInputTargetNode);
167:
168: Text newLineTextNode = util.document.createTextNode("\n");
169: Text tabTextNode = util.document.createTextNode(" ");
170:
171: //add new filter nodes
172: Object[] keys = filters.keySet().toArray();
173: for (int i = 0; i < filters.size(); i++) {
174:
175: inputTargetNode
176: .appendChild(newLineTextNode.cloneNode(true));
177: inputTargetNode.appendChild(tabTextNode.cloneNode(true));
178: inputTargetNode.appendChild(tabTextNode.cloneNode(true));
179:
180: Element newFilterNode = util.document
181: .createElement("filter");
182: newFilterNode.setAttribute("token", keys[i].toString());
183: newFilterNode.setAttribute("value", filters.get(keys[i])
184: .toString());
185: inputTargetNode.appendChild(newFilterNode);
186: }
187: inputTargetNode.appendChild(newLineTextNode.cloneNode(true));
188: inputTargetNode.appendChild(tabTextNode.cloneNode(true));
189: }
190:
191: /*
192: * When 'value' attribut contains string that begins with '${'
193: * property named in '${...}' is found, and its 'value' is placed
194: * into HashMap
195: */
196: public HashMap getInputFilters() {
197: // Node inputTargetNode = util.getTargetByName("input");
198: Node inputTargetNode = util.getFiltersetById("replacements");
199: HashMap hash = new HashMap();
200: if (inputTargetNode != null) {
201: NodeList childs = inputTargetNode.getChildNodes();
202:
203: //find filter nodes and put them into HashMap:
204: for (int i = 0; i < childs.getLength(); i++) {
205: if (childs.item(i).getNodeName().equals("filter")) {
206: String key = childs.item(i).getAttributes()
207: .getNamedItem("token").getNodeValue();
208: String value = childs.item(i).getAttributes()
209: .getNamedItem("value").getNodeValue();
210: //hash.put(key, this.expandString(value)); DACHA 28.2.2003
211: hash.put(key, value);
212: }
213: }
214: }
215:
216: return hash;
217: }
218:
219: /*
220: * This method adds dependedName into depends list of target
221: * only if it not exists in the list
222: */
223: public void addDependedTarget(String name, String dependedName) {
224: Node targetNode = util.getTargetByName(name);
225: String dependsValue = targetNode.getAttributes().getNamedItem(
226: "depends").getNodeValue();
227: if (dependsValue.indexOf(dependedName) == -1) {
228: String newList = this .addStringToDependensList(
229: dependsValue, dependedName/*, name*/);
230: targetNode.getAttributes().getNamedItem("depends")
231: .setNodeValue(newList);
232: }
233: }
234:
235: /*
236: * This method removes dependedName from depends list of the target
237: */
238: public void removeDependedTarget(String name, String dependedName) {
239: Node targetNode = util.getTargetByName(name);
240: String dependsValue = targetNode.getAttributes().getNamedItem(
241: "depends").getNodeValue();
242: if (dependsValue.indexOf(dependedName) != -1) {
243: String newList = this .removeStringFromDependensList(
244: dependsValue, dependedName);
245: targetNode.getAttributes().getNamedItem("depends")
246: .setNodeValue(newList);
247: }
248: }
249:
250: /*
251: * @return true if dependedName is in the depends list
252: * otherwise, false
253: */
254: public boolean checkDependedTarget(String name, String dependedName) {
255: Node targetNode = util.getTargetByName(name);
256: String dependsValue = targetNode.getAttributes().getNamedItem(
257: "depends").getNodeValue();
258: if (dependsValue.indexOf(dependedName) != -1)
259: return true;
260: else
261: return false;
262: }
263:
264: private String addStringToDependensList(String dependsList,
265: String item) {
266:
267: StringTokenizer tokenizer = new StringTokenizer(dependsList,
268: ",");
269: HashMap hash = new HashMap();
270:
271: while (tokenizer.hasMoreTokens()) {
272: String token = tokenizer.nextToken().trim();
273: hash.put(this .getOrdinalForDependsString(token), token);
274: }
275: hash.put(this .getOrdinalForDependsString(item), item);
276:
277: String newList = "";
278: if (hash.containsKey(new Integer(1)))
279: newList += hash.get(new Integer(1)) + ", ";
280: if (hash.containsKey(new Integer(2)))
281: newList += hash.get(new Integer(2)) + ", ";
282: if (hash.containsKey(new Integer(3)))
283: newList += hash.get(new Integer(3)) + ", ";
284: if (hash.containsKey(new Integer(4)))
285: newList += hash.get(new Integer(4)) + ", ";
286:
287: if (newList.endsWith(", "))
288: newList = newList.substring(0, newList.length() - 2);
289:
290: return newList;
291: }
292:
293: /*
294: * @return index of the dependedName in list
295: *
296: * make: xmlc, dods, compile, deploy
297: * deploy: input, content, archive
298: */
299: private Integer getOrdinalForDependsString(String item) {
300: if (item.equalsIgnoreCase("xmlc")
301: || item.equalsIgnoreCase("compile"))
302: return new Integer(1);
303: if (item.equalsIgnoreCase("dods")
304: || item.equalsIgnoreCase("content"))
305: return new Integer(2);
306: if (item.equalsIgnoreCase("input")
307: || item.equalsIgnoreCase("archive"))
308: return new Integer(3);
309: if (item.equalsIgnoreCase("deploy")
310: || item.equalsIgnoreCase("makeWAR"))
311: return new Integer(4);
312:
313: return new Integer(0);
314: }
315:
316: private String removeStringFromDependensList(String dependsList,
317: String item) {
318: StringTokenizer tokenizer = new StringTokenizer(dependsList,
319: ",");
320: String newList = "";
321:
322: while (tokenizer.hasMoreTokens()) {
323: String token = tokenizer.nextToken().trim();
324: if (!token.equalsIgnoreCase(item))
325: newList += token + ", ";
326: }
327:
328: if (newList.endsWith(", "))
329: newList = newList.substring(0, newList.length() - 2);
330:
331: return newList;
332: }
333:
334: public boolean save() {
335: return util.saveDocument();
336: }
337:
338: String expandString(String str) {
339:
340: String replacement = null;
341: StringBuffer sb = new StringBuffer(str);
342:
343: Pattern p = Pattern.compile("\\$\\{[^\\}]*\\}");
344: Matcher m = p.matcher(sb);
345:
346: while (m.find()) {
347: String propName = m.group(0).substring(2,
348: m.group(0).length() - 1);
349: replacement = getProperty(propName);
350:
351: if (replacement != null) {
352: propName = "${" + propName + "}";
353: sb.replace(sb.indexOf(propName), sb.indexOf(propName)
354: + propName.length(), replacement);
355: }
356: //else ant variable is not resolved !!!
357: }
358:
359: return sb.toString();
360: }
361:
362: private String getProperty(String propertyName) {
363:
364: String result = null;
365:
366: Node propertyNode = util.getProjectProperty(propertyName);
367: try {
368: result = propertyNode.getAttributes().getNamedItem(
369: "location").getNodeValue();
370: } catch (NullPointerException e) {
371: result = null;
372: }
373:
374: if (result == null) {
375: try {
376: result = propertyNode.getAttributes().getNamedItem(
377: "value").getNodeValue();
378: } catch (NullPointerException e) {
379: result = null;
380: }
381: }
382:
383: if (result != null)
384: result = this.expandString(result);
385:
386: return result;
387: }
388:
389: }
|