001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package anttasks;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.util.StringTokenizer;
022:
023: import javax.xml.transform.TransformerException;
024:
025: import org.apache.tools.ant.BuildException;
026: import org.apache.tools.ant.Task;
027: import org.apache.xpath.XPathAPI;
028: import org.w3c.dom.Document;
029: import org.w3c.dom.Element;
030: import org.w3c.dom.NodeList;
031: import org.xml.sax.SAXException;
032:
033: /**
034: * Set the special attributes for the pooling.
035: *
036: * @since 2.1.5
037: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
038: * @version CVS $Id: PoolSetterTask.java 433543 2006-08-22 06:22:54Z crossley $
039: */
040: public final class PoolSetterTask extends Task {
041:
042: private File file;
043: private String element;
044: private boolean isSitemap = true;
045: private String poolMax = "32";
046:
047: public void setFile(File file) {
048: this .file = file;
049: }
050:
051: public void setElement(String element) {
052: this .element = element;
053: }
054:
055: public void setIsSitemap(boolean value) {
056: this .isSitemap = value;
057: }
058:
059: public void setPoolMax(int value) {
060: this .poolMax = String.valueOf(value);
061: }
062:
063: public void execute() throws BuildException {
064:
065: if (this .file == null) {
066: throw new BuildException("file attribute is required", this
067: .getLocation());
068: }
069: if (this .element == null) {
070: throw new BuildException("element attribute is required",
071: this .getLocation());
072: }
073:
074: try {
075: // load xml
076: final Document configuration = DocumentCache.getDocument(
077: this .file, this );
078:
079: // process recursive
080: boolean changed = false;
081: StringTokenizer st = new StringTokenizer(this .element);
082: while (st.hasMoreTokens()) {
083: String componentName = st.nextToken();
084: NodeList nodes;
085: if (this .isSitemap) {
086: int pos = componentName.indexOf(":");
087: nodes = XPathAPI.selectNodeList(configuration,
088: "*/*[local-name()='components']/*/*[local-name()='"
089: + componentName.substring(0, pos)
090: + "' and @name='"
091: + componentName.substring(pos + 1)
092: + "']");
093: } else {
094: nodes = XPathAPI.selectNodeList(configuration, "*/"
095: + componentName);
096: }
097: if (nodes != null && nodes.getLength() > 0) {
098: for (int i = 0; i < nodes.getLength(); i++) {
099: final Element e = (Element) nodes.item(i);
100: e
101: .setAttributeNS(null, "pool-max",
102: this .poolMax);
103: changed = true;
104: }
105: } else {
106: System.out.println("Component not found: "
107: + componentName);
108: }
109: }
110:
111: if (changed) {
112: // save xml
113: DocumentCache.writeDocument(this .file, configuration,
114: this );
115: }
116: DocumentCache.storeDocument(this .file, configuration, this );
117: } catch (TransformerException e) {
118: throw new BuildException("TransformerException: " + e);
119: } catch (SAXException e) {
120: throw new BuildException("SAXException: " + e);
121: } catch (IOException ioe) {
122: throw new BuildException("IOException: " + ioe);
123: }
124: }
125:
126: }
|