KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PoolSetterTask


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 import java.io.File JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
20
21 import javax.xml.transform.TransformerException JavaDoc;
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 JavaDoc;
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.NodeList JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30
31 /**
32  * Set the special attributes for the pooling.
33  *
34  * @since 2.1.5
35  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
36  * @version CVS $Id: PoolSetterTask.java 153376 2005-02-11 08:50:21Z cziegeler $
37  */

38 public final class PoolSetterTask extends Task {
39
40     private File JavaDoc file;
41     private String JavaDoc element;
42     private boolean isSitemap = true;
43     private String JavaDoc poolMax = "32";
44
45     public void setFile(File JavaDoc file) {
46         this.file = file;
47     }
48
49     public void setElement(String JavaDoc 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             // load xml
72
final Document JavaDoc configuration = DocumentCache.getDocument(this.file, this);
73
74             // process recursive
75
boolean changed = false;
76             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(this.element);
77             while ( st.hasMoreTokens() ) {
78                 String JavaDoc componentName = st.nextToken();
79                 NodeList JavaDoc 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 JavaDoc e = (Element JavaDoc)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                 // save xml
99
DocumentCache.writeDocument(this.file, configuration, this);
100             }
101             DocumentCache.storeDocument(this.file, configuration, this);
102        } catch (TransformerException JavaDoc e) {
103             throw new BuildException("TransformerException: " + e);
104         } catch (SAXException JavaDoc e) {
105             throw new BuildException("SAXException: " + e);
106         } catch (IOException JavaDoc ioe) {
107             throw new BuildException("IOException: " + ioe);
108         }
109     }
110
111 }
112
Popular Tags