1 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 ; 26 import org.xml.sax.SAXNotSupportedException ; 27 import org.xml.sax.XMLReader ; 28 29 33 public class ParserSupports extends ProjectComponent implements Condition { 34 35 private String feature; 36 private String property; 37 private String value; 38 40 public static final String ERROR_BOTH_ATTRIBUTES = 41 "Property and feature attributes are exclusive"; 42 43 public static final String FEATURE = "feature"; 44 45 public static final String PROPERTY = "property"; 46 47 48 public static final String NOT_RECOGNIZED = 49 " not recognized: "; 50 51 public static final String NOT_SUPPORTED = 52 " not supported: "; 53 54 public static final String ERROR_NO_ATTRIBUTES = 55 "Neither feature or property are set"; 56 57 public static final String ERROR_NO_VALUE = 58 "A value is needed when testing for property support"; 59 60 64 public void setFeature(String feature) { 65 this.feature = feature; 66 } 67 68 72 public void setProperty(String property) { 73 this.property = property; 74 } 75 76 81 public void setValue(String value) { 82 this.value = value; 83 } 84 85 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 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 107 private XMLReader getReader() { 108 JAXPUtils.getParser(); 109 return JAXPUtils.getXMLReader(); 110 } 111 112 116 public boolean evalFeature() { 117 XMLReader 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 e) { 125 log(FEATURE + NOT_RECOGNIZED + feature, Project.MSG_VERBOSE); 126 return false; 127 } catch (SAXNotSupportedException e) { 128 log(FEATURE + NOT_SUPPORTED + feature, Project.MSG_VERBOSE); 129 return false; 130 } 131 return true; 132 } 133 134 138 public boolean evalProperty() { 139 XMLReader reader = getReader(); 140 try { 141 reader.setProperty(property, value); 142 } catch (SAXNotRecognizedException e) { 143 log(PROPERTY + NOT_RECOGNIZED + property, Project.MSG_VERBOSE); 144 return false; 145 } catch (SAXNotSupportedException e) { 146 log(PROPERTY + NOT_SUPPORTED + property, Project.MSG_VERBOSE); 147 return false; 148 } 149 return true; 150 } 151 } 152 | Popular Tags |