1 16 17 import java.io.File ; 18 import java.io.IOException ; 19 import java.util.StringTokenizer ; 20 21 import javax.xml.transform.TransformerException ; 22 23 import org.apache.tools.ant.BuildException; 24 import org.apache.tools.ant.Task; 25 import org.apache.xpath.XPathAPI; 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.Element ; 28 import org.w3c.dom.NodeList ; 29 import org.xml.sax.SAXException ; 30 31 38 public final class PoolSetterTask extends Task { 39 40 private File file; 41 private String element; 42 private boolean isSitemap = true; 43 private String poolMax = "32"; 44 45 public void setFile(File file) { 46 this.file = file; 47 } 48 49 public void setElement(String element) { 50 this.element = element; 51 } 52 53 public void setIsSitemap(boolean value) { 54 this.isSitemap = value; 55 } 56 57 public void setPoolMax(int value) { 58 this.poolMax = String.valueOf(value); 59 } 60 61 public void execute() throws BuildException { 62 63 if (this.file == null) { 64 throw new BuildException("file attribute is required", this.getLocation()); 65 } 66 if (this.element == null) { 67 throw new BuildException("element attribute is required", this.getLocation()); 68 } 69 70 try { 71 final Document configuration = DocumentCache.getDocument(this.file, this); 73 74 boolean changed = false; 76 StringTokenizer st = new StringTokenizer (this.element); 77 while ( st.hasMoreTokens() ) { 78 String componentName = st.nextToken(); 79 NodeList nodes; 80 if (this.isSitemap) { 81 int pos = componentName.indexOf(":"); 82 nodes = XPathAPI.selectNodeList(configuration, "*/*[local-name()='components']/*/*[local-name()='"+componentName.substring(0,pos)+"' and @name='"+componentName.substring(pos+1)+"']"); 83 } else { 84 nodes = XPathAPI.selectNodeList(configuration, "*/"+componentName); 85 } 86 if (nodes != null && nodes.getLength() > 0) { 87 for(int i=0; i < nodes.getLength(); i++) { 88 final Element e = (Element )nodes.item(i); 89 e.setAttributeNS(null, "pool-max", this.poolMax); 90 changed = true; 91 } 92 } else { 93 System.out.println("Component not found: " + componentName); 94 } 95 } 96 97 if ( changed ) { 98 DocumentCache.writeDocument(this.file, configuration, this); 100 } 101 DocumentCache.storeDocument(this.file, configuration, this); 102 } catch (TransformerException e) { 103 throw new BuildException("TransformerException: " + e); 104 } catch (SAXException e) { 105 throw new BuildException("SAXException: " + e); 106 } catch (IOException ioe) { 107 throw new BuildException("IOException: " + ioe); 108 } 109 } 110 111 } 112 | Popular Tags |