KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > util > ant > CmsAntTaskReadXMLProperty


1 /*
2  * File : $Source$
3  * Date : $Date$
4  * Version: $Revision$
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.util.ant;
33
34 import java.io.File JavaDoc;
35
36 import org.apache.commons.digester.Digester;
37 import org.apache.tools.ant.BuildException;
38 import org.apache.tools.ant.Task;
39
40 import org.xml.sax.ErrorHandler JavaDoc;
41 import org.xml.sax.SAXParseException JavaDoc;
42
43 /**
44  * Ant task for reading a property from a XML attribute or element.<p>
45  *
46  * @author Michael Moossen
47  *
48  * @version $Revision$
49  *
50  * @since 6.0.0
51  */

52 public class CmsAntTaskReadXMLProperty extends Task {
53
54     /** target attribute. */
55     private String JavaDoc m_attribute; // optional
56

57     /** target element. */
58     private String JavaDoc m_element; // required
59

60     /** Destination property. */
61     private String JavaDoc m_property; // required
62

63     /** return value. */
64     private String JavaDoc m_value = ",";
65
66     /** absoulte path to the xml file. */
67     private String JavaDoc m_xmlFile; // required
68

69     /**
70      * Default constructor.<p>
71      */

72     public CmsAntTaskReadXMLProperty() {
73
74         super();
75     }
76
77     /**
78      * Run the task.<p>
79      *
80      * Sets the given property to <code>__ABORT__</code> if canceled, or to a list of selected
81      * modules if not.<p>
82      *
83      * @throws BuildException if something goes wrong
84      *
85      * @see org.apache.tools.ant.Task#execute()
86      */

87     public void execute() throws BuildException {
88
89         boolean isAttr = (m_attribute != null && m_attribute.trim().length() > 0);
90
91         // instantiate Digester and enable XML validation
92
Digester digester = new Digester();
93         digester.setValidating(false);
94         digester.setEntityResolver(null);
95         digester.setRuleNamespaceURI(null);
96         digester.setErrorHandler(new ErrorHandler JavaDoc() {
97
98             /**
99              * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
100              */

101             public void error(SAXParseException JavaDoc exception) {
102
103                 log(exception.getMessage(), exception.getLineNumber());
104             }
105
106             /**
107              * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
108              */

109             public void fatalError(SAXParseException JavaDoc exception) {
110
111                 log(exception.getMessage(), exception.getLineNumber());
112             }
113
114             /**
115              * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
116              */

117             public void warning(SAXParseException JavaDoc exception) {
118
119                 log(exception.getMessage(), exception.getLineNumber());
120             }
121
122         });
123
124         // add this class to the Digester
125
digester.push(this);
126         if (!isAttr) {
127             digester.addCallMethod(m_element, "setValue", 0);
128         } else {
129             digester.addCallMethod(m_element, "setValue", 1);
130             digester.addCallParam(m_element, 0, m_attribute);
131         }
132         // start the parsing process
133
try {
134             digester.parse(new File JavaDoc(getXmlFile()));
135         } catch (Exception JavaDoc e) {
136             throw new BuildException(e);
137         }
138
139         getProject().setProperty(m_property, m_value.substring(1));
140     }
141
142     /**
143      * Returns the optional XML attribute.<p>
144      *
145      * @return the optional XML attribute
146      */

147     public String JavaDoc getAttribute() {
148
149         return m_attribute;
150     }
151
152     /**
153      * Returns the XML element path.<p>
154      *
155      * @return the XML element path
156      */

157     public String JavaDoc getElement() {
158
159         return m_element;
160     }
161
162     /**
163      * Returns the property to store the user selection.<p>
164      *
165      * @return Returns the property
166      */

167     public String JavaDoc getProperty() {
168
169         return m_property;
170     }
171
172     /**
173      * Returns the return value.<p>
174      *
175      * @return the return value
176      */

177     public String JavaDoc getValue() {
178
179         return m_value;
180     }
181
182     /**
183      * Returns the xmlFile absolute path.<p>
184      *
185      * @return the xmlFile absolute path
186      */

187     public String JavaDoc getXmlFile() {
188
189         return m_xmlFile;
190     }
191
192     /**
193      * Sets the optional XML attribute.<p>
194      *
195      * @param attribute the optional XML attribute to set
196      */

197     public void setAttribute(String JavaDoc attribute) {
198
199         m_attribute = attribute;
200     }
201
202     /**
203      * Sets the XML element path.<p>
204      *
205      * @param element the XML element path to set
206      */

207     public void setElement(String JavaDoc element) {
208
209         m_element = element;
210     }
211
212     /**
213      * Sets the property for storing the selected value.<p>
214      *
215      * @param property The property to set
216      */

217     public void setProperty(String JavaDoc property) {
218
219         m_property = property;
220     }
221
222     /**
223      * Sets the return value.<p>
224      *
225      * @param value the return value to set
226      */

227     public void setValue(String JavaDoc value) {
228
229         if (m_value.length() > 1) {
230             m_value += ",";
231         }
232         m_value += value;
233     }
234
235     /**
236      * Sets the xmlFile absolute path.<p>
237      *
238      * @param xmlFile the xmlFile absolute path to set
239      */

240     public void setXmlFile(String JavaDoc xmlFile) {
241
242         m_xmlFile = xmlFile;
243     }
244 }
Popular Tags