1 12 package org.eclipse.ant.internal.core.contentDescriber; 13 14 import java.io.IOException ; 15 import java.io.StringReader ; 16 17 import javax.xml.parsers.ParserConfigurationException ; 18 import javax.xml.parsers.SAXParser ; 19 import javax.xml.parsers.SAXParserFactory ; 20 21 import org.xml.sax.Attributes ; 22 import org.xml.sax.InputSource ; 23 import org.xml.sax.SAXException ; 24 import org.xml.sax.SAXNotRecognizedException ; 25 import org.xml.sax.SAXNotSupportedException ; 26 import org.xml.sax.XMLReader ; 27 import org.xml.sax.helpers.DefaultHandler ; 28 29 37 public final class AntHandler extends DefaultHandler { 38 43 private class StopParsingException extends SAXException { 44 47 private static final long serialVersionUID = 1L; 48 49 53 public StopParsingException() { 54 super((String ) null); 55 } 56 } 57 58 private static final String DEFAULT_ATTRIBUTE= "default"; private static final String PROJECT = "project"; private static final String TARGET= "target"; private static final String MACRODEF= "macrodef"; private static final String TASKDEF= "taskdef"; private static final String TYPEDEF= "typedef"; private static final String PROPERTY= "property"; private static final String CLASSPATH= "classpath"; private static final String PATH= "path"; private static final String IMPORT= "import"; 69 74 private String fTopElementFound = null; 75 private SAXParserFactory fFactory; 76 77 private boolean fDefaultAttributeFound= false; 78 private boolean fTargetFound = false; 79 private boolean fAntElementFound = false; 80 81 private int fLevel= -1; 82 83 92 private final SAXParser createParser(SAXParserFactory parserFactory) throws ParserConfigurationException , SAXException , SAXNotRecognizedException , SAXNotSupportedException { 93 final SAXParser parser = parserFactory.newSAXParser(); 95 final XMLReader reader = parser.getXMLReader(); 96 try { 98 reader.setFeature("http://xml.org/sax/features/validation", false); reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } catch (SAXNotRecognizedException e) { 102 } catch (SAXNotSupportedException e) { 104 } 106 return parser; 107 } 108 109 private SAXParserFactory getFactory() { 110 synchronized (this) { 111 if (fFactory != null) { 112 return fFactory; 113 } 114 fFactory= SAXParserFactory.newInstance(); 115 fFactory.setNamespaceAware(true); 116 } 117 return fFactory; 118 } 119 120 protected boolean parseContents(InputSource contents) throws IOException , ParserConfigurationException , SAXException { 121 try { 123 fFactory = getFactory(); 124 if (fFactory == null) { 125 return false; 126 } 127 final SAXParser parser = createParser(fFactory); 128 contents.setSystemId("/"); parser.parse(contents, this); 131 } catch (StopParsingException e) { 132 } 134 return true; 135 } 136 137 143 public InputSource resolveEntity(String publicId, String systemId) throws SAXException { 144 return new InputSource (new StringReader ("")); } 146 147 153 public final void startElement(final String uri, final String elementName, final String qualifiedName, final Attributes attributes) throws SAXException { 154 fLevel++; 155 if (fTopElementFound == null) { 156 fTopElementFound = elementName; 157 if (!hasRootProjectElement()) { 158 throw new StopParsingException(); 159 } 160 if (attributes != null) { 161 fDefaultAttributeFound= attributes.getValue(DEFAULT_ATTRIBUTE) != null; 162 if (fDefaultAttributeFound) { 163 throw new StopParsingException(); 164 } 165 } 166 } 167 if (fLevel == 1 && TARGET.equals(elementName)) { 168 fTargetFound= true; 169 throw new StopParsingException(); 170 } 171 172 if (fLevel == 1 && (MACRODEF.equals(elementName) 174 || TASKDEF.equals(elementName) || TYPEDEF.equals(elementName) 175 || PROPERTY.equals(elementName)|| CLASSPATH.equals(elementName) 176 || PATH.equals(elementName) || IMPORT.equals(elementName))) { 177 fAntElementFound= true; 178 throw new StopParsingException(); 179 } 180 } 181 182 185 public void endElement(String uri, String localName, String qName) throws SAXException { 186 super.endElement(uri, localName, qName); 187 fLevel--; 188 } 189 190 protected boolean hasProjectDefaultAttribute() { 191 return fDefaultAttributeFound; 192 } 193 194 protected boolean hasRootProjectElement() { 195 return PROJECT.equals(fTopElementFound); 196 } 197 198 protected boolean hasTargetElement() { 199 return fTargetFound; 200 } 201 202 protected boolean hasAntElement() { 203 return fAntElementFound; 204 } 205 } | Popular Tags |