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: */
018: package org.apache.tools.ant.taskdefs.condition;
019:
020: import org.apache.tools.ant.BuildException;
021: import org.apache.tools.ant.Project;
022: import org.apache.tools.ant.ProjectComponent;
023: import org.apache.tools.ant.util.JAXPUtils;
024:
025: import org.xml.sax.SAXNotRecognizedException;
026: import org.xml.sax.SAXNotSupportedException;
027: import org.xml.sax.XMLReader;
028:
029: /**
030: * Test for the XML parser supporting a particular feature
031: * @since Ant 1.7
032: */
033: public class ParserSupports extends ProjectComponent implements
034: Condition {
035:
036: private String feature;
037: private String property;
038: private String value;
039: // Error messages
040: /** error - combined attributes not allowed */
041: public static final String ERROR_BOTH_ATTRIBUTES = "Property and feature attributes are exclusive";
042: /** feature */
043: public static final String FEATURE = "feature";
044: /** property */
045: public static final String PROPERTY = "property";
046:
047: /** error - not recognized */
048: public static final String NOT_RECOGNIZED = " not recognized: ";
049: /** error - not supported */
050: public static final String NOT_SUPPORTED = " not supported: ";
051: /** error - missing attribute */
052: public static final String ERROR_NO_ATTRIBUTES = "Neither feature or property are set";
053: /** error - no value */
054: public static final String ERROR_NO_VALUE = "A value is needed when testing for property support";
055:
056: /**
057: * Feature to probe for.
058: * @param feature the feature to probe for.
059: */
060: public void setFeature(String feature) {
061: this .feature = feature;
062: }
063:
064: /**
065: * Property to probe for
066: * @param property the property to probe for.
067: */
068: public void setProperty(String property) {
069: this .property = property;
070: }
071:
072: /**
073: * Optional value to set.
074: * Converted to a boolean value when setting a property
075: * @param value the value to set.
076: */
077: public void setValue(String value) {
078: this .value = value;
079: }
080:
081: /** {@inheritDoc}. */
082: public boolean eval() throws BuildException {
083: if (feature != null && property != null) {
084: throw new BuildException(ERROR_BOTH_ATTRIBUTES);
085: }
086: if (feature == null && property == null) {
087: throw new BuildException(ERROR_NO_ATTRIBUTES);
088: }
089: //pick a value that is good for everything
090: if (feature != null) {
091: return evalFeature();
092: }
093: if (value == null) {
094: throw new BuildException(ERROR_NO_VALUE);
095: }
096: return evalProperty();
097: }
098:
099: /**
100: * Get our reader
101: * @return a reader
102: */
103: private XMLReader getReader() {
104: JAXPUtils.getParser();
105: return JAXPUtils.getXMLReader();
106: }
107:
108: /**
109: * Set a feature
110: * @return true if the feature could be set
111: */
112: public boolean evalFeature() {
113: XMLReader reader = getReader();
114: if (value == null) {
115: value = "true";
116: }
117: boolean v = Project.toBoolean(value);
118: try {
119: reader.setFeature(feature, v);
120: } catch (SAXNotRecognizedException e) {
121: log(FEATURE + NOT_RECOGNIZED + feature, Project.MSG_VERBOSE);
122: return false;
123: } catch (SAXNotSupportedException e) {
124: log(FEATURE + NOT_SUPPORTED + feature, Project.MSG_VERBOSE);
125: return false;
126: }
127: return true;
128: }
129:
130: /**
131: * Set a property
132: * @return true if the feature could be set
133: */
134: public boolean evalProperty() {
135: XMLReader reader = getReader();
136: try {
137: reader.setProperty(property, value);
138: } catch (SAXNotRecognizedException e) {
139: log(PROPERTY + NOT_RECOGNIZED + property,
140: Project.MSG_VERBOSE);
141: return false;
142: } catch (SAXNotSupportedException e) {
143: log(PROPERTY + NOT_SUPPORTED + property,
144: Project.MSG_VERBOSE);
145: return false;
146: }
147: return true;
148: }
149: }
|