You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DocumentParser.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*******************************************************************************
  2. * Copyright (c) 2005 Contributors.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Common Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/cpl-v10.html
  7. *
  8. * Contributors:
  9. * Alexandre Vasseur initial implementation
  10. *******************************************************************************/
  11. package org.aspectj.weaver.loadtime.definition;
  12. import org.xml.sax.Attributes;
  13. import org.xml.sax.ErrorHandler;
  14. import org.xml.sax.InputSource;
  15. import org.xml.sax.SAXException;
  16. import org.xml.sax.SAXNotRecognizedException;
  17. import org.xml.sax.SAXParseException;
  18. import org.xml.sax.XMLReader;
  19. import org.xml.sax.DTDHandler;
  20. import org.xml.sax.helpers.DefaultHandler;
  21. import org.xml.sax.helpers.XMLReaderFactory;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.net.URL;
  25. import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
  26. /**
  27. * FIXME AV - doc, concrete aspect
  28. *
  29. * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
  30. */
  31. public class DocumentParser extends DefaultHandler {
  32. /**
  33. * The current DTD public id. The matching dtd will be searched as a resource.
  34. */
  35. private final static String DTD_PUBLIC_ID = "-//AspectJ//DTD 1.5.0//EN";
  36. /**
  37. * The DTD alias, for better user experience.
  38. */
  39. private final static String DTD_PUBLIC_ID_ALIAS = "-//AspectJ//DTD//EN";
  40. /**
  41. * A handler to the DTD stream so that we are only using one file descriptor
  42. */
  43. private final static InputStream DTD_STREAM = DocumentParser.class.getResourceAsStream("/aspectj_1_5_0.dtd");
  44. private final static String ASPECTJ_ELEMENT = "aspectj";
  45. private final static String WEAVER_ELEMENT = "weaver";
  46. private final static String INCLUDE_ELEMENT = "include";
  47. private final static String EXCLUDE_ELEMENT = "exclude";
  48. private final static String OPTIONS_ATTRIBUTE = "options";
  49. private final static String ASPECTS_ELEMENT = "aspects";
  50. private final static String ASPECT_ELEMENT = "aspect";
  51. private final static String CONCRETE_ASPECT_ELEMENT = "concrete-aspect";
  52. private final static String NAME_ATTRIBUTE = "name";
  53. private final static String EXTEND_ATTRIBUTE = "extends";
  54. private final static String POINTCUT_ELEMENT = "pointcut";
  55. private final static String WITHIN_ATTRIBUTE = "within";
  56. private final static String EXPRESSION_ATTRIBUTE = "expression";
  57. private final Definition m_definition;
  58. private boolean m_inAspectJ;
  59. private boolean m_inWeaver;
  60. private boolean m_inAspects;
  61. private Definition.ConcreteAspect m_lastConcreteAspect;
  62. private DocumentParser() {
  63. m_definition = new Definition();
  64. }
  65. public static Definition parse(final URL url) throws Exception {
  66. InputStream in = null;
  67. try {
  68. DocumentParser parser = new DocumentParser();
  69. XMLReader xmlReader = XMLReaderFactory.createXMLReader();
  70. xmlReader.setContentHandler(parser);
  71. xmlReader.setErrorHandler(parser);
  72. try {
  73. xmlReader.setFeature("http://xml.org/sax/features/validation", false);
  74. xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
  75. xmlReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
  76. } catch (SAXNotRecognizedException e) {
  77. ;//fine, the parser don't do validation
  78. }
  79. xmlReader.setEntityResolver(parser);
  80. in = url.openStream();
  81. xmlReader.parse(new InputSource(in));
  82. return parser.m_definition;
  83. } finally {
  84. try {
  85. in.close();
  86. } catch (Throwable t) {
  87. ;
  88. }
  89. }
  90. }
  91. public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException {
  92. if (publicId.equals(DTD_PUBLIC_ID) || publicId.equals(DTD_PUBLIC_ID_ALIAS)) {
  93. InputStream in = DTD_STREAM;
  94. if (in == null) {
  95. return null;
  96. } else {
  97. return new InputSource(in);
  98. }
  99. } else {
  100. System.err.println(
  101. "AspectJ - WARN - unknown DTD "
  102. + publicId
  103. + " - consider using "
  104. + DTD_PUBLIC_ID
  105. );
  106. return null;
  107. }
  108. }
  109. public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
  110. if (ASPECT_ELEMENT.equals(qName)) {
  111. String name = attributes.getValue(NAME_ATTRIBUTE);
  112. if (!isNull(name)) {
  113. m_definition.getAspectClassNames().add(name);
  114. }
  115. } else if (WEAVER_ELEMENT.equals(qName)) {
  116. String options = attributes.getValue(OPTIONS_ATTRIBUTE);
  117. if (!isNull(options)) {
  118. m_definition.appendWeaverOptions(options);
  119. }
  120. m_inWeaver = true;
  121. } else if (CONCRETE_ASPECT_ELEMENT.equals(qName)) {
  122. String name = attributes.getValue(NAME_ATTRIBUTE);
  123. String extend = attributes.getValue(EXTEND_ATTRIBUTE);
  124. if (!isNull(name) && !isNull(extend)) {
  125. m_lastConcreteAspect = new Definition.ConcreteAspect(name, extend);
  126. m_definition.getConcreteAspects().add(m_lastConcreteAspect);
  127. }
  128. } else if (POINTCUT_ELEMENT.equals(qName) && m_lastConcreteAspect != null) {
  129. String name = attributes.getValue(NAME_ATTRIBUTE);
  130. String expression = attributes.getValue(EXPRESSION_ATTRIBUTE);
  131. if (!isNull(name) && !isNull(expression)) {
  132. m_lastConcreteAspect.pointcuts.add(new Definition.Pointcut(name, replaceXmlAnd(expression)));
  133. }
  134. } else if (ASPECTJ_ELEMENT.equals(qName)) {
  135. if (m_inAspectJ) {
  136. throw new SAXException("Found nested <aspectj> element");
  137. }
  138. m_inAspectJ = true;
  139. } else if (ASPECTS_ELEMENT.equals(qName)) {
  140. m_inAspects = true;
  141. } else if (INCLUDE_ELEMENT.equals(qName) && m_inWeaver) {
  142. String typePattern = attributes.getValue(WITHIN_ATTRIBUTE);
  143. if (!isNull(typePattern)) {
  144. m_definition.getIncludePatterns().add(typePattern);
  145. }
  146. } else if (EXCLUDE_ELEMENT.equals(qName) && m_inWeaver) {
  147. String typePattern = attributes.getValue(WITHIN_ATTRIBUTE);
  148. if (!isNull(typePattern)) {
  149. m_definition.getExcludePatterns().add(typePattern);
  150. }
  151. } else if (EXCLUDE_ELEMENT.equals(qName) && m_inAspects) {
  152. String typePattern = attributes.getValue(WITHIN_ATTRIBUTE);
  153. if (!isNull(typePattern)) {
  154. m_definition.getAspectExcludePatterns().add(typePattern);
  155. }
  156. } else {
  157. throw new SAXException("Unknown element while parsing <aspectj> element: " + qName);
  158. }
  159. super.startElement(uri, localName, qName, attributes);
  160. }
  161. public void endElement(String uri, String localName, String qName) throws SAXException {
  162. if (CONCRETE_ASPECT_ELEMENT.equals(qName)) {
  163. m_lastConcreteAspect = null;
  164. } else if (ASPECTJ_ELEMENT.equals(qName)) {
  165. m_inAspectJ = false;
  166. } else if (WEAVER_ELEMENT.equals(qName)) {
  167. m_inWeaver = false;
  168. } else if (ASPECTS_ELEMENT.equals(qName)) {
  169. m_inAspects = false;
  170. }
  171. super.endElement(uri, localName, qName);
  172. }
  173. //TODO AV - define what we want for XML parser error - for now stderr
  174. public void warning(SAXParseException e) throws SAXException {
  175. super.warning(e);
  176. }
  177. public void error(SAXParseException e) throws SAXException {
  178. super.error(e);
  179. }
  180. public void fatalError(SAXParseException e) throws SAXException {
  181. super.fatalError(e);
  182. }
  183. private static String replaceXmlAnd(String expression) {
  184. //TODO AV do we need to handle "..)AND" or "AND(.." ?
  185. //FIXME AV Java 1.4 code - if KO, use some Strings util
  186. return expression.replaceAll(" AND ", " && ");
  187. }
  188. private boolean isNull(String s) {
  189. return (s == null || s.length() <= 0);
  190. }
  191. }