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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*******************************************************************************
  2. * Copyright (c) 2005 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Alexandre Vasseur initial implementation
  11. * Abraham Nevado - Lucierna simple caching strategy
  12. *******************************************************************************/
  13. package org.aspectj.weaver.loadtime.definition;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.net.URL;
  17. import java.util.Hashtable;
  18. import javax.xml.parsers.ParserConfigurationException;
  19. import javax.xml.parsers.SAXParserFactory;
  20. import org.aspectj.util.LangUtil;
  21. import org.xml.sax.Attributes;
  22. import org.xml.sax.InputSource;
  23. import org.xml.sax.SAXException;
  24. import org.xml.sax.SAXParseException;
  25. import org.xml.sax.XMLReader;
  26. import org.xml.sax.helpers.DefaultHandler;
  27. import org.xml.sax.helpers.XMLReaderFactory;
  28. /**
  29. * FIXME AV - doc, concrete aspect
  30. *
  31. * @author Alexandre Vasseur
  32. * @author A. Nevado
  33. * @author Andy Clement
  34. */
  35. public class DocumentParser extends DefaultHandler {
  36. /**
  37. * The current DTD public id. The matching dtd will be searched as a resource.
  38. */
  39. private final static String DTD_PUBLIC_ID = "-//AspectJ//DTD 1.5.0//EN";
  40. /**
  41. * The DTD alias, for better user experience.
  42. */
  43. private final static String DTD_PUBLIC_ID_ALIAS = "-//AspectJ//DTD//EN";
  44. private final static String ASPECTJ_ELEMENT = "aspectj";
  45. private final static String WEAVER_ELEMENT = "weaver";
  46. private final static String DUMP_ELEMENT = "dump";
  47. private final static String DUMP_BEFOREANDAFTER_ATTRIBUTE = "beforeandafter";
  48. private final static String DUMP_PERCLASSLOADERDIR_ATTRIBUTE = "perclassloaderdumpdir";
  49. private final static String INCLUDE_ELEMENT = "include";
  50. private final static String EXCLUDE_ELEMENT = "exclude";
  51. private final static String OPTIONS_ATTRIBUTE = "options";
  52. private final static String ASPECTS_ELEMENT = "aspects";
  53. private final static String ASPECT_ELEMENT = "aspect";
  54. private final static String CONCRETE_ASPECT_ELEMENT = "concrete-aspect";
  55. private final static String NAME_ATTRIBUTE = "name";
  56. private final static String SCOPE_ATTRIBUTE = "scope";
  57. private final static String REQUIRES_ATTRIBUTE = "requires";
  58. private final static String EXTEND_ATTRIBUTE = "extends";
  59. private final static String PRECEDENCE_ATTRIBUTE = "precedence";
  60. private final static String PERCLAUSE_ATTRIBUTE = "perclause";
  61. private final static String POINTCUT_ELEMENT = "pointcut";
  62. private final static String WITHIN_ATTRIBUTE = "within";
  63. private final static String EXPRESSION_ATTRIBUTE = "expression";
  64. private final Definition m_definition;
  65. private boolean m_inAspectJ;
  66. private boolean m_inWeaver;
  67. private boolean m_inAspects;
  68. private Definition.ConcreteAspect m_lastConcreteAspect;
  69. private static Hashtable<String, Definition> parsedFiles = new Hashtable<String, Definition>();
  70. private static final boolean CACHE;
  71. private static final boolean LIGHTPARSER;
  72. static {
  73. boolean value = false;
  74. try {
  75. value = System.getProperty("org.aspectj.weaver.loadtime.configuration.cache", "true").equalsIgnoreCase("true");
  76. } catch (Throwable t) {
  77. t.printStackTrace();
  78. }
  79. CACHE = value;
  80. value = false;
  81. try {
  82. value = System.getProperty("org.aspectj.weaver.loadtime.configuration.lightxmlparser", "false")
  83. .equalsIgnoreCase("true");
  84. } catch (Throwable t) {
  85. t.printStackTrace();
  86. }
  87. LIGHTPARSER = value;
  88. }
  89. private DocumentParser() {
  90. m_definition = new Definition();
  91. }
  92. public static Definition parse(final URL url) throws Exception {
  93. if (CACHE && parsedFiles.containsKey(url.toString())) {
  94. return parsedFiles.get(url.toString());
  95. }
  96. Definition def = null;
  97. if (LIGHTPARSER) {
  98. def = SimpleAOPParser.parse(url);
  99. } else {
  100. def = saxParsing(url);
  101. }
  102. if (CACHE && def.getAspectClassNames().size() > 0) {
  103. parsedFiles.put(url.toString(), def);
  104. }
  105. return def;
  106. }
  107. private static Definition saxParsing(URL url) throws SAXException, ParserConfigurationException, IOException {
  108. DocumentParser parser = new DocumentParser();
  109. XMLReader xmlReader = getXMLReader();
  110. xmlReader.setContentHandler(parser);
  111. xmlReader.setErrorHandler(parser);
  112. try {
  113. xmlReader.setFeature("http://xml.org/sax/features/validation", false);
  114. } catch (SAXException e) {
  115. // fine, the parser don't do validation
  116. }
  117. try {
  118. xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
  119. } catch (SAXException e) {
  120. // fine, the parser don't do validation
  121. }
  122. try {
  123. xmlReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
  124. } catch (SAXException e) {
  125. // fine, the parser don't do validation
  126. }
  127. xmlReader.setEntityResolver(parser);
  128. InputStream in = url.openStream();
  129. xmlReader.parse(new InputSource(in));
  130. return parser.m_definition;
  131. }
  132. private static XMLReader getXMLReader() throws SAXException, ParserConfigurationException {
  133. XMLReader xmlReader = null;
  134. /* Try this first for Java 5 */
  135. try {
  136. xmlReader = XMLReaderFactory.createXMLReader();
  137. }
  138. /* .. and ignore "System property ... not set" and then try this instead */
  139. catch (SAXException ex) {
  140. xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
  141. }
  142. return xmlReader;
  143. }
  144. public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
  145. if (publicId.equals(DTD_PUBLIC_ID) || publicId.equals(DTD_PUBLIC_ID_ALIAS)) {
  146. InputStream in = DocumentParser.class.getResourceAsStream("/aspectj_1_5_0.dtd");
  147. if (in == null) {
  148. System.err.println("AspectJ - WARN - could not read DTD " + publicId);
  149. return null;
  150. } else {
  151. return new InputSource(in);
  152. }
  153. } else {
  154. System.err.println("AspectJ - WARN - unknown DTD " + publicId + " - consider using " + DTD_PUBLIC_ID);
  155. return null;
  156. }
  157. }
  158. public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
  159. if (ASPECT_ELEMENT.equals(qName)) {
  160. String name = attributes.getValue(NAME_ATTRIBUTE);
  161. String scopePattern = replaceXmlAnd(attributes.getValue(SCOPE_ATTRIBUTE));
  162. String requiredType = attributes.getValue(REQUIRES_ATTRIBUTE);
  163. if (!isNull(name)) {
  164. m_definition.getAspectClassNames().add(name);
  165. if (scopePattern != null) {
  166. m_definition.addScopedAspect(name, scopePattern);
  167. }
  168. if (requiredType != null) {
  169. m_definition.setAspectRequires(name, requiredType);
  170. }
  171. }
  172. } else if (WEAVER_ELEMENT.equals(qName)) {
  173. String options = attributes.getValue(OPTIONS_ATTRIBUTE);
  174. if (!isNull(options)) {
  175. m_definition.appendWeaverOptions(options);
  176. }
  177. m_inWeaver = true;
  178. } else if (CONCRETE_ASPECT_ELEMENT.equals(qName)) {
  179. String name = attributes.getValue(NAME_ATTRIBUTE);
  180. String extend = attributes.getValue(EXTEND_ATTRIBUTE);
  181. String precedence = attributes.getValue(PRECEDENCE_ATTRIBUTE);
  182. String perclause = attributes.getValue(PERCLAUSE_ATTRIBUTE);
  183. if (!isNull(name)) {
  184. m_lastConcreteAspect = new Definition.ConcreteAspect(name, extend, precedence, perclause);
  185. // if (isNull(precedence) && !isNull(extend)) {// if no precedence, then extends must be there
  186. // m_lastConcreteAspect = new Definition.ConcreteAspect(name, extend);
  187. // } else if (!isNull(precedence)) {
  188. // // wether a pure precedence def, or an extendsANDprecedence def.
  189. // m_lastConcreteAspect = new Definition.ConcreteAspect(name, extend, precedence, perclause);
  190. // }
  191. m_definition.getConcreteAspects().add(m_lastConcreteAspect);
  192. }
  193. } else if (POINTCUT_ELEMENT.equals(qName) && m_lastConcreteAspect != null) {
  194. String name = attributes.getValue(NAME_ATTRIBUTE);
  195. String expression = attributes.getValue(EXPRESSION_ATTRIBUTE);
  196. if (!isNull(name) && !isNull(expression)) {
  197. m_lastConcreteAspect.pointcuts.add(new Definition.Pointcut(name, replaceXmlAnd(expression)));
  198. }
  199. } else if (ASPECTJ_ELEMENT.equals(qName)) {
  200. if (m_inAspectJ) {
  201. throw new SAXException("Found nested <aspectj> element");
  202. }
  203. m_inAspectJ = true;
  204. } else if (ASPECTS_ELEMENT.equals(qName)) {
  205. m_inAspects = true;
  206. } else if (INCLUDE_ELEMENT.equals(qName) && m_inWeaver) {
  207. String typePattern = getWithinAttribute(attributes);
  208. if (!isNull(typePattern)) {
  209. m_definition.getIncludePatterns().add(typePattern);
  210. }
  211. } else if (EXCLUDE_ELEMENT.equals(qName) && m_inWeaver) {
  212. String typePattern = getWithinAttribute(attributes);
  213. if (!isNull(typePattern)) {
  214. m_definition.getExcludePatterns().add(typePattern);
  215. }
  216. } else if (DUMP_ELEMENT.equals(qName) && m_inWeaver) {
  217. String typePattern = getWithinAttribute(attributes);
  218. if (!isNull(typePattern)) {
  219. m_definition.getDumpPatterns().add(typePattern);
  220. }
  221. String beforeAndAfter = attributes.getValue(DUMP_BEFOREANDAFTER_ATTRIBUTE);
  222. if (isTrue(beforeAndAfter)) {
  223. m_definition.setDumpBefore(true);
  224. }
  225. String perWeaverDumpDir = attributes.getValue(DUMP_PERCLASSLOADERDIR_ATTRIBUTE);
  226. if (isTrue(perWeaverDumpDir)) {
  227. m_definition.setCreateDumpDirPerClassloader(true);
  228. }
  229. } else if (EXCLUDE_ELEMENT.equals(qName) && m_inAspects) {
  230. String typePattern = getWithinAttribute(attributes);
  231. if (!isNull(typePattern)) {
  232. m_definition.getAspectExcludePatterns().add(typePattern);
  233. }
  234. } else if (INCLUDE_ELEMENT.equals(qName) && m_inAspects) {
  235. String typePattern = getWithinAttribute(attributes);
  236. if (!isNull(typePattern)) {
  237. m_definition.getAspectIncludePatterns().add(typePattern);
  238. }
  239. } else {
  240. throw new SAXException("Unknown element while parsing <aspectj> element: " + qName);
  241. }
  242. super.startElement(uri, localName, qName, attributes);
  243. }
  244. private String getWithinAttribute(Attributes attributes) {
  245. return replaceXmlAnd(attributes.getValue(WITHIN_ATTRIBUTE));
  246. }
  247. public void endElement(String uri, String localName, String qName) throws SAXException {
  248. if (CONCRETE_ASPECT_ELEMENT.equals(qName)) {
  249. m_lastConcreteAspect = null;
  250. } else if (ASPECTJ_ELEMENT.equals(qName)) {
  251. m_inAspectJ = false;
  252. } else if (WEAVER_ELEMENT.equals(qName)) {
  253. m_inWeaver = false;
  254. } else if (ASPECTS_ELEMENT.equals(qName)) {
  255. m_inAspects = false;
  256. }
  257. super.endElement(uri, localName, qName);
  258. }
  259. // TODO AV - define what we want for XML parser error - for now stderr
  260. public void warning(SAXParseException e) throws SAXException {
  261. super.warning(e);
  262. }
  263. public void error(SAXParseException e) throws SAXException {
  264. super.error(e);
  265. }
  266. public void fatalError(SAXParseException e) throws SAXException {
  267. super.fatalError(e);
  268. }
  269. private static String replaceXmlAnd(String expression) {
  270. // TODO AV do we need to handle "..)AND" or "AND(.." ?
  271. return LangUtil.replace(expression, " AND ", " && ");
  272. }
  273. private boolean isNull(String s) {
  274. return (s == null || s.length() <= 0);
  275. }
  276. private boolean isTrue(String s) {
  277. return (s != null && s.equals("true"));
  278. }
  279. }