1 4 package com.tc.aspectwerkz.definition.aspectj5; 5 6 import org.xml.sax.Attributes ; 7 import org.xml.sax.InputSource ; 8 import org.xml.sax.SAXException ; 9 import org.xml.sax.SAXParseException ; 10 import org.xml.sax.XMLReader ; 11 import org.xml.sax.helpers.DefaultHandler ; 12 import org.xml.sax.helpers.XMLReaderFactory ; 13 14 import com.tc.aspectwerkz.util.Strings; 15 16 import java.io.InputStream ; 17 import java.net.URL ; 18 19 22 public class DocumentParser extends DefaultHandler { 23 24 27 private final static String DTD_PUBLIC_ID = "-//AspectJ//DTD 1.5.0//EN"; 28 29 32 private final static String DTD_PUBLIC_ID_ALIAS = "-//AspectJ//DTD//EN"; 33 34 37 private final static InputStream DTD_STREAM = DocumentParser.class 38 .getResourceAsStream("/aspectj_1_5_0.dtd"); 39 40 private final static String ASPECTJ_ELEMENT = "aspectj"; 41 private final static String WEAVER_ELEMENT = "weaver"; 42 private final static String DUMP_ELEMENT = "dump"; 43 private final static String INCLUDE_ELEMENT = "include"; 44 private final static String EXCLUDE_ELEMENT = "exclude"; 45 private final static String OPTIONS_ATTRIBUTE = "options"; 46 private final static String ASPECTS_ELEMENT = "aspects"; 47 private final static String ASPECT_ELEMENT = "aspect"; 48 private final static String CONCRETE_ASPECT_ELEMENT = "concrete-aspect"; 49 private final static String NAME_ATTRIBUTE = "name"; 50 private final static String EXTEND_ATTRIBUTE = "extends"; 51 private final static String POINTCUT_ELEMENT = "pointcut"; 52 private final static String WITHIN_ATTRIBUTE = "within"; 53 private final static String EXPRESSION_ATTRIBUTE = "expression"; 54 55 private final Definition m_definition; 56 57 private boolean m_inAspectJ; 58 private boolean m_inWeaver; 59 private boolean m_inAspects; 60 61 private Definition.ConcreteAspect m_lastConcreteAspect; 62 63 private DocumentParser() { 64 m_definition = new Definition(); 65 } 66 67 public static Definition parse(final URL url) throws Exception { 68 InputStream in = null; 69 try { 70 DocumentParser parser = new DocumentParser(); 71 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 72 xmlReader.setContentHandler(parser); 73 xmlReader.setErrorHandler(parser); 74 75 try { 76 xmlReader.setFeature("http://xml.org/sax/features/validation", false); 77 } catch (SAXException e) { 78 } 80 try { 81 xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false); 82 } catch (SAXException e) { 83 } 85 try { 86 xmlReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false); 87 } catch (SAXException e) { 88 } 90 91 xmlReader.setEntityResolver(parser); 92 in = url.openStream(); 93 xmlReader.parse(new InputSource (in)); 94 return parser.m_definition; 95 } finally { 96 try { 97 in.close(); 98 } catch (Throwable t) { 99 } 101 } 102 } 103 104 public InputSource resolveEntity(String publicId, String systemId) { 105 if (publicId.equals(DTD_PUBLIC_ID) || publicId.equals(DTD_PUBLIC_ID_ALIAS)) { 106 InputStream in = DTD_STREAM; 107 if (in == null) { 108 System.err.println("AspectJ - WARN - could not read DTD " + publicId); 109 return null; 110 } else { 111 return new InputSource (in); 112 } 113 } else { 114 System.err.println("AspectJ - WARN - unknown DTD " + publicId + " - consider using " + DTD_PUBLIC_ID); 115 return null; 116 } 117 } 118 119 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 120 if (ASPECT_ELEMENT.equals(qName)) { 121 String name = attributes.getValue(NAME_ATTRIBUTE); 122 if (!isNull(name)) { 123 m_definition.getAspectClassNames().add(name); 124 } 125 } else if (WEAVER_ELEMENT.equals(qName)) { 126 String options = attributes.getValue(OPTIONS_ATTRIBUTE); 127 if (!isNull(options)) { 128 m_definition.appendWeaverOptions(options); 129 } 130 m_inWeaver = true; 131 } else if (CONCRETE_ASPECT_ELEMENT.equals(qName)) { 132 String name = attributes.getValue(NAME_ATTRIBUTE); 133 String extend = attributes.getValue(EXTEND_ATTRIBUTE); 134 if (!isNull(name) && !isNull(extend)) { 135 m_lastConcreteAspect = new Definition.ConcreteAspect(name, extend); 136 m_definition.getConcreteAspects().add(m_lastConcreteAspect); 137 } 138 } else if (POINTCUT_ELEMENT.equals(qName) && m_lastConcreteAspect != null) { 139 String name = attributes.getValue(NAME_ATTRIBUTE); 140 String expression = attributes.getValue(EXPRESSION_ATTRIBUTE); 141 if (!isNull(name) && !isNull(expression)) { 142 m_lastConcreteAspect.pointcuts.add(new Definition.Pointcut(name, replaceXmlAnd(expression))); 143 } 144 } else if (ASPECTJ_ELEMENT.equals(qName)) { 145 if (m_inAspectJ) { 146 throw new SAXException ("Found nested <aspectj> element"); 147 } 148 m_inAspectJ = true; 149 } else if (ASPECTS_ELEMENT.equals(qName)) { 150 m_inAspects = true; 151 } else if (INCLUDE_ELEMENT.equals(qName) && m_inWeaver) { 152 String typePattern = attributes.getValue(WITHIN_ATTRIBUTE); 153 if (!isNull(typePattern)) { 154 m_definition.getIncludePatterns().add(typePattern); 155 } 156 } else if (EXCLUDE_ELEMENT.equals(qName) && m_inWeaver) { 157 String typePattern = attributes.getValue(WITHIN_ATTRIBUTE); 158 if (!isNull(typePattern)) { 159 m_definition.getExcludePatterns().add(typePattern); 160 } 161 } else if (DUMP_ELEMENT.equals(qName) && m_inWeaver) { 162 String typePattern = attributes.getValue(WITHIN_ATTRIBUTE); 163 if (!isNull(typePattern)) { 164 m_definition.getDumpPatterns().add(typePattern); 165 } 166 } else if (EXCLUDE_ELEMENT.equals(qName) && m_inAspects) { 167 String typePattern = attributes.getValue(WITHIN_ATTRIBUTE); 168 if (!isNull(typePattern)) { 169 m_definition.getAspectExcludePatterns().add(typePattern); 170 } 171 } else { 172 throw new SAXException ("Unknown element while parsing<aspectj> element: " + qName); 173 } 174 super.startElement(uri, localName, qName, attributes); 175 } 176 177 public void endElement(String uri, String localName, String qName) throws SAXException { 178 if (CONCRETE_ASPECT_ELEMENT.equals(qName)) { 179 m_lastConcreteAspect = null; 180 } else if (ASPECTJ_ELEMENT.equals(qName)) { 181 m_inAspectJ = false; 182 } else if (WEAVER_ELEMENT.equals(qName)) { 183 m_inWeaver = false; 184 } else if (ASPECTS_ELEMENT.equals(qName)) { 185 m_inAspects = false; 186 } 187 super.endElement(uri, localName, qName); 188 } 189 190 public void warning(SAXParseException e) throws SAXException { 192 super.warning(e); 193 } 194 195 public void error(SAXParseException e) throws SAXException { 196 super.error(e); 197 } 198 199 public void fatalError(SAXParseException e) throws SAXException { 200 super.fatalError(e); 201 } 202 203 private static String replaceXmlAnd(String expression) { 204 return Strings.replaceSubString(expression, " AND ", " && "); 206 } 207 208 private boolean isNull(String s) { 209 return s == null || s.length() <= 0; 210 } 211 } 212 | Popular Tags |