KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > condition > ParserSupports


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

18 package org.apache.tools.ant.taskdefs.condition;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.Project;
22 import org.apache.tools.ant.ProjectComponent;
23 import org.apache.tools.ant.util.JAXPUtils;
24
25 import org.xml.sax.SAXNotRecognizedException JavaDoc;
26 import org.xml.sax.SAXNotSupportedException JavaDoc;
27 import org.xml.sax.XMLReader JavaDoc;
28
29 /**
30  * Test for the XML parser supporting a particular feature
31  * @since Ant 1.7
32  */

33 public class ParserSupports extends ProjectComponent implements Condition {
34
35     private String JavaDoc feature;
36     private String JavaDoc property;
37     private String JavaDoc value;
38     // Error messages
39
/** error - combined attributes not allowed */
40     public static final String JavaDoc ERROR_BOTH_ATTRIBUTES =
41             "Property and feature attributes are exclusive";
42     /** feature */
43     public static final String JavaDoc FEATURE = "feature";
44     /** property */
45     public static final String JavaDoc PROPERTY = "property";
46
47     /** error - not recognized */
48     public static final String JavaDoc NOT_RECOGNIZED =
49             " not recognized: ";
50     /** error - not supported */
51     public static final String JavaDoc NOT_SUPPORTED =
52             " not supported: ";
53     /** error - missing attribute */
54     public static final String JavaDoc ERROR_NO_ATTRIBUTES =
55         "Neither feature or property are set";
56     /** error - no value */
57     public static final String JavaDoc ERROR_NO_VALUE =
58         "A value is needed when testing for property support";
59
60     /**
61      * Feature to probe for.
62      * @param feature the feature to probe for.
63      */

64     public void setFeature(String JavaDoc feature) {
65         this.feature = feature;
66     }
67
68     /**
69      * Property to probe for
70      * @param property the property to probe for.
71      */

72     public void setProperty(String JavaDoc property) {
73         this.property = property;
74     }
75
76     /**
77      * Optional value to set.
78      * Converted to a boolean value when setting a property
79      * @param value the value to set.
80      */

81     public void setValue(String JavaDoc value) {
82         this.value = value;
83     }
84
85     /** {@inheritDoc}. */
86     public boolean eval() throws BuildException {
87         if (feature != null && property != null) {
88             throw new BuildException(ERROR_BOTH_ATTRIBUTES);
89         }
90         if (feature == null && property == null) {
91             throw new BuildException(ERROR_NO_ATTRIBUTES);
92         }
93         //pick a value that is good for everything
94
if (feature != null) {
95             return evalFeature();
96         }
97         if (value == null) {
98             throw new BuildException(ERROR_NO_VALUE);
99         }
100         return evalProperty();
101     }
102
103     /**
104      * Get our reader
105      * @return a reader
106      */

107     private XMLReader JavaDoc getReader() {
108         JAXPUtils.getParser();
109         return JAXPUtils.getXMLReader();
110     }
111
112     /**
113      * Set a feature
114      * @return true if the feature could be set
115      */

116     public boolean evalFeature() {
117         XMLReader JavaDoc reader = getReader();
118         if (value == null) {
119             value = "true";
120         }
121         boolean v = Project.toBoolean(value);
122         try {
123             reader.setFeature(feature, v);
124         } catch (SAXNotRecognizedException JavaDoc e) {
125             log(FEATURE + NOT_RECOGNIZED + feature, Project.MSG_VERBOSE);
126             return false;
127         } catch (SAXNotSupportedException JavaDoc e) {
128             log(FEATURE + NOT_SUPPORTED + feature, Project.MSG_VERBOSE);
129             return false;
130         }
131         return true;
132     }
133
134     /**
135      * Set a property
136      * @return true if the feature could be set
137      */

138     public boolean evalProperty() {
139         XMLReader JavaDoc reader = getReader();
140         try {
141             reader.setProperty(property, value);
142         } catch (SAXNotRecognizedException JavaDoc e) {
143             log(PROPERTY + NOT_RECOGNIZED + property, Project.MSG_VERBOSE);
144             return false;
145         } catch (SAXNotSupportedException JavaDoc e) {
146             log(PROPERTY + NOT_SUPPORTED + property, Project.MSG_VERBOSE);
147             return false;
148         }
149         return true;
150     }
151 }
152
Popular Tags