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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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.aspectj.weaver.loadtime.definition.Definition.AdviceKind;
  22. import org.aspectj.weaver.loadtime.definition.Definition.DeclareAnnotationKind;
  23. import org.xml.sax.Attributes;
  24. import org.xml.sax.InputSource;
  25. import org.xml.sax.SAXException;
  26. import org.xml.sax.SAXParseException;
  27. import org.xml.sax.XMLReader;
  28. import org.xml.sax.helpers.DefaultHandler;
  29. import org.xml.sax.helpers.XMLReaderFactory;
  30. /**
  31. *
  32. * @author Alexandre Vasseur
  33. * @author A. Nevado
  34. * @author Andy Clement
  35. */
  36. public class DocumentParser extends DefaultHandler {
  37. /**
  38. * The current DTD public id. The matching dtd will be searched as a resource.
  39. */
  40. private final static String DTD_PUBLIC_ID = "-//AspectJ//DTD 1.5.0//EN";
  41. /**
  42. * The DTD alias, for better user experience.
  43. */
  44. private final static String DTD_PUBLIC_ID_ALIAS = "-//AspectJ//DTD//EN";
  45. private final static String ASPECTJ_ELEMENT = "aspectj";
  46. private final static String WEAVER_ELEMENT = "weaver";
  47. private final static String DUMP_ELEMENT = "dump";
  48. private final static String DUMP_BEFOREANDAFTER_ATTRIBUTE = "beforeandafter";
  49. private final static String DUMP_PERCLASSLOADERDIR_ATTRIBUTE = "perclassloaderdumpdir";
  50. private final static String INCLUDE_ELEMENT = "include";
  51. private final static String EXCLUDE_ELEMENT = "exclude";
  52. private final static String OPTIONS_ATTRIBUTE = "options";
  53. private final static String ASPECTS_ELEMENT = "aspects";
  54. private final static String ASPECT_ELEMENT = "aspect";
  55. private final static String CONCRETE_ASPECT_ELEMENT = "concrete-aspect";
  56. private final static String NAME_ATTRIBUTE = "name";
  57. private final static String SCOPE_ATTRIBUTE = "scope";
  58. private final static String REQUIRES_ATTRIBUTE = "requires";
  59. private final static String EXTEND_ATTRIBUTE = "extends";
  60. private final static String PRECEDENCE_ATTRIBUTE = "precedence";
  61. private final static String PERCLAUSE_ATTRIBUTE = "perclause";
  62. private final static String POINTCUT_ELEMENT = "pointcut";
  63. private final static String BEFORE_ELEMENT = "before";
  64. private final static String AFTER_ELEMENT = "after";
  65. private final static String AFTER_RETURNING_ELEMENT = "after-returning";
  66. private final static String AFTER_THROWING_ELEMENT = "after-throwing";
  67. private final static String AROUND_ELEMENT = "around";
  68. private final static String WITHIN_ATTRIBUTE = "within";
  69. private final static String EXPRESSION_ATTRIBUTE = "expression";
  70. private final static String DECLARE_ANNOTATION_ELEMENT = "declare-annotation";
  71. private final Definition definition;
  72. private boolean inAspectJ;
  73. private boolean inWeaver;
  74. private boolean inAspects;
  75. private Definition.ConcreteAspect activeConcreteAspectDefinition;
  76. private static Hashtable<String, Definition> parsedFiles = new Hashtable<String, Definition>();
  77. private static boolean CACHE;
  78. private static final boolean LIGHTPARSER;
  79. static {
  80. boolean value = false;
  81. try {
  82. value = System.getProperty("org.aspectj.weaver.loadtime.configuration.cache", "true").equalsIgnoreCase("true");
  83. } catch (Throwable t) {
  84. t.printStackTrace();
  85. }
  86. CACHE = value;
  87. value = false;
  88. try {
  89. value = System.getProperty("org.aspectj.weaver.loadtime.configuration.lightxmlparser", "false")
  90. .equalsIgnoreCase("true");
  91. } catch (Throwable t) {
  92. t.printStackTrace();
  93. }
  94. LIGHTPARSER = value;
  95. }
  96. private DocumentParser() {
  97. definition = new Definition();
  98. }
  99. public static Definition parse(final URL url) throws Exception {
  100. if (CACHE && parsedFiles.containsKey(url.toString())) {
  101. return parsedFiles.get(url.toString());
  102. }
  103. Definition def = null;
  104. if (LIGHTPARSER) {
  105. def = SimpleAOPParser.parse(url);
  106. } else {
  107. def = saxParsing(url);
  108. }
  109. if (CACHE && def.getAspectClassNames().size() > 0) {
  110. parsedFiles.put(url.toString(), def);
  111. }
  112. return def;
  113. }
  114. private static Definition saxParsing(URL url) throws SAXException, ParserConfigurationException, IOException {
  115. DocumentParser parser = new DocumentParser();
  116. XMLReader xmlReader = getXMLReader();
  117. xmlReader.setContentHandler(parser);
  118. xmlReader.setErrorHandler(parser);
  119. try {
  120. xmlReader.setFeature("http://xml.org/sax/features/validation", false);
  121. } catch (SAXException e) {
  122. // fine, the parser don't do validation
  123. }
  124. try {
  125. xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
  126. } catch (SAXException e) {
  127. // fine, the parser don't do validation
  128. }
  129. try {
  130. xmlReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
  131. } catch (SAXException e) {
  132. // fine, the parser don't do validation
  133. }
  134. xmlReader.setEntityResolver(parser);
  135. InputStream in = url.openStream();
  136. xmlReader.parse(new InputSource(in));
  137. return parser.definition;
  138. }
  139. private static XMLReader getXMLReader() throws SAXException, ParserConfigurationException {
  140. XMLReader xmlReader = null;
  141. /* Try this first for Java 5 */
  142. try {
  143. xmlReader = XMLReaderFactory.createXMLReader();
  144. }
  145. /* .. and ignore "System property ... not set" and then try this instead */
  146. catch (SAXException ex) {
  147. xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
  148. }
  149. return xmlReader;
  150. }
  151. public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
  152. if (publicId.equals(DTD_PUBLIC_ID) || publicId.equals(DTD_PUBLIC_ID_ALIAS)) {
  153. InputStream in = DocumentParser.class.getResourceAsStream("/aspectj_1_5_0.dtd");
  154. if (in == null) {
  155. System.err.println("AspectJ - WARN - could not read DTD " + publicId);
  156. return null;
  157. } else {
  158. return new InputSource(in);
  159. }
  160. } else {
  161. System.err.println("AspectJ - WARN - unknown DTD " + publicId + " - consider using " + DTD_PUBLIC_ID);
  162. return null;
  163. }
  164. }
  165. public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
  166. if (ASPECT_ELEMENT.equals(qName)) {
  167. String name = attributes.getValue(NAME_ATTRIBUTE);
  168. String scopePattern = replaceXmlAnd(attributes.getValue(SCOPE_ATTRIBUTE));
  169. String requiredType = attributes.getValue(REQUIRES_ATTRIBUTE);
  170. if (!isNull(name)) {
  171. definition.getAspectClassNames().add(name);
  172. if (scopePattern != null) {
  173. definition.addScopedAspect(name, scopePattern);
  174. }
  175. if (requiredType != null) {
  176. definition.setAspectRequires(name, requiredType);
  177. }
  178. }
  179. } else if (WEAVER_ELEMENT.equals(qName)) {
  180. String options = attributes.getValue(OPTIONS_ATTRIBUTE);
  181. if (!isNull(options)) {
  182. definition.appendWeaverOptions(options);
  183. }
  184. inWeaver = true;
  185. } else if (CONCRETE_ASPECT_ELEMENT.equals(qName)) {
  186. String name = attributes.getValue(NAME_ATTRIBUTE);
  187. String extend = attributes.getValue(EXTEND_ATTRIBUTE);
  188. String precedence = attributes.getValue(PRECEDENCE_ATTRIBUTE);
  189. String perclause = attributes.getValue(PERCLAUSE_ATTRIBUTE);
  190. if (!isNull(name)) {
  191. activeConcreteAspectDefinition = new Definition.ConcreteAspect(name, extend, precedence, perclause);
  192. // if (isNull(precedence) && !isNull(extend)) {// if no precedence, then extends must be there
  193. // m_lastConcreteAspect = new Definition.ConcreteAspect(name, extend);
  194. // } else if (!isNull(precedence)) {
  195. // // wether a pure precedence def, or an extendsANDprecedence def.
  196. // m_lastConcreteAspect = new Definition.ConcreteAspect(name, extend, precedence, perclause);
  197. // }
  198. definition.getConcreteAspects().add(activeConcreteAspectDefinition);
  199. }
  200. } else if (POINTCUT_ELEMENT.equals(qName) && activeConcreteAspectDefinition != null) {
  201. String name = attributes.getValue(NAME_ATTRIBUTE);
  202. String expression = attributes.getValue(EXPRESSION_ATTRIBUTE);
  203. if (!isNull(name) && !isNull(expression)) {
  204. activeConcreteAspectDefinition.pointcuts.add(new Definition.Pointcut(name, replaceXmlAnd(expression)));
  205. }
  206. } else if (DECLARE_ANNOTATION_ELEMENT.equals(qName) && activeConcreteAspectDefinition!=null) {
  207. String methodSig = attributes.getValue("method");
  208. String fieldSig = attributes.getValue("field");
  209. String typePat = attributes.getValue("type");
  210. String anno = attributes.getValue("annotation");
  211. if (isNull(anno)) {
  212. throw new SAXException("Badly formed <declare-annotation> element, 'annotation' value is missing");
  213. }
  214. if (isNull(methodSig) && isNull(fieldSig) && isNull(typePat)) {
  215. throw new SAXException("Badly formed <declare-annotation> element, need one of 'method'/'field'/'type' specified");
  216. }
  217. if (!isNull(methodSig)) {
  218. // declare @method
  219. activeConcreteAspectDefinition.declareAnnotations.add(new Definition.DeclareAnnotation(DeclareAnnotationKind.Method,
  220. methodSig, anno));
  221. } else if (!isNull(fieldSig)) {
  222. // declare @field
  223. activeConcreteAspectDefinition.declareAnnotations.add(new Definition.DeclareAnnotation(DeclareAnnotationKind.Field,
  224. fieldSig, anno));
  225. } else if (!isNull(typePat)) {
  226. // declare @type
  227. activeConcreteAspectDefinition.declareAnnotations.add(new Definition.DeclareAnnotation(DeclareAnnotationKind.Type,
  228. typePat, anno));
  229. }
  230. } else if (BEFORE_ELEMENT.equals(qName) && activeConcreteAspectDefinition != null) {
  231. String pointcut = attributes.getValue(POINTCUT_ELEMENT);
  232. String adviceClass = attributes.getValue("invokeClass");
  233. String adviceMethod = attributes.getValue("invokeMethod");
  234. if (!isNull(pointcut) && !isNull(adviceClass) && !isNull(adviceMethod)) {
  235. activeConcreteAspectDefinition.pointcutsAndAdvice.add(new Definition.PointcutAndAdvice(AdviceKind.Before,
  236. replaceXmlAnd(pointcut), adviceClass, adviceMethod));
  237. } else {
  238. throw new SAXException("Badly formed <before> element");
  239. }
  240. } else if (AFTER_ELEMENT.equals(qName) && activeConcreteAspectDefinition != null) {
  241. String pointcut = attributes.getValue(POINTCUT_ELEMENT);
  242. String adviceClass = attributes.getValue("invokeClass");
  243. String adviceMethod = attributes.getValue("invokeMethod");
  244. if (!isNull(pointcut) && !isNull(adviceClass) && !isNull(adviceMethod)) {
  245. activeConcreteAspectDefinition.pointcutsAndAdvice.add(new Definition.PointcutAndAdvice(AdviceKind.After,
  246. replaceXmlAnd(pointcut), adviceClass, adviceMethod));
  247. } else {
  248. throw new SAXException("Badly formed <after> element");
  249. }
  250. } else if (AROUND_ELEMENT.equals(qName) && activeConcreteAspectDefinition != null) {
  251. String pointcut = attributes.getValue(POINTCUT_ELEMENT);
  252. String adviceClass = attributes.getValue("invokeClass");
  253. String adviceMethod = attributes.getValue("invokeMethod");
  254. if (!isNull(pointcut) && !isNull(adviceClass) && !isNull(adviceMethod)) {
  255. activeConcreteAspectDefinition.pointcutsAndAdvice.add(new Definition.PointcutAndAdvice(AdviceKind.Around,
  256. replaceXmlAnd(pointcut), adviceClass, adviceMethod));
  257. } else {
  258. throw new SAXException("Badly formed <before> element");
  259. }
  260. } else if (ASPECTJ_ELEMENT.equals(qName)) {
  261. if (inAspectJ) {
  262. throw new SAXException("Found nested <aspectj> element");
  263. }
  264. inAspectJ = true;
  265. } else if (ASPECTS_ELEMENT.equals(qName)) {
  266. inAspects = true;
  267. } else if (INCLUDE_ELEMENT.equals(qName) && inWeaver) {
  268. String typePattern = getWithinAttribute(attributes);
  269. if (!isNull(typePattern)) {
  270. definition.getIncludePatterns().add(typePattern);
  271. }
  272. } else if (EXCLUDE_ELEMENT.equals(qName) && inWeaver) {
  273. String typePattern = getWithinAttribute(attributes);
  274. if (!isNull(typePattern)) {
  275. definition.getExcludePatterns().add(typePattern);
  276. }
  277. } else if (DUMP_ELEMENT.equals(qName) && inWeaver) {
  278. String typePattern = getWithinAttribute(attributes);
  279. if (!isNull(typePattern)) {
  280. definition.getDumpPatterns().add(typePattern);
  281. }
  282. String beforeAndAfter = attributes.getValue(DUMP_BEFOREANDAFTER_ATTRIBUTE);
  283. if (isTrue(beforeAndAfter)) {
  284. definition.setDumpBefore(true);
  285. }
  286. String perWeaverDumpDir = attributes.getValue(DUMP_PERCLASSLOADERDIR_ATTRIBUTE);
  287. if (isTrue(perWeaverDumpDir)) {
  288. definition.setCreateDumpDirPerClassloader(true);
  289. }
  290. } else if (EXCLUDE_ELEMENT.equals(qName) && inAspects) {
  291. String typePattern = getWithinAttribute(attributes);
  292. if (!isNull(typePattern)) {
  293. definition.getAspectExcludePatterns().add(typePattern);
  294. }
  295. } else if (INCLUDE_ELEMENT.equals(qName) && inAspects) {
  296. String typePattern = getWithinAttribute(attributes);
  297. if (!isNull(typePattern)) {
  298. definition.getAspectIncludePatterns().add(typePattern);
  299. }
  300. } else {
  301. throw new SAXException("Unknown element while parsing <aspectj> element: " + qName);
  302. }
  303. super.startElement(uri, localName, qName, attributes);
  304. }
  305. private String getWithinAttribute(Attributes attributes) {
  306. return replaceXmlAnd(attributes.getValue(WITHIN_ATTRIBUTE));
  307. }
  308. public void endElement(String uri, String localName, String qName) throws SAXException {
  309. if (CONCRETE_ASPECT_ELEMENT.equals(qName)) {
  310. activeConcreteAspectDefinition = null;
  311. } else if (ASPECTJ_ELEMENT.equals(qName)) {
  312. inAspectJ = false;
  313. } else if (WEAVER_ELEMENT.equals(qName)) {
  314. inWeaver = false;
  315. } else if (ASPECTS_ELEMENT.equals(qName)) {
  316. inAspects = false;
  317. }
  318. super.endElement(uri, localName, qName);
  319. }
  320. // TODO AV - define what we want for XML parser error - for now stderr
  321. public void warning(SAXParseException e) throws SAXException {
  322. super.warning(e);
  323. }
  324. public void error(SAXParseException e) throws SAXException {
  325. super.error(e);
  326. }
  327. public void fatalError(SAXParseException e) throws SAXException {
  328. super.fatalError(e);
  329. }
  330. private static String replaceXmlAnd(String expression) {
  331. // TODO AV do we need to handle "..)AND" or "AND(.." ?
  332. return LangUtil.replace(expression, " AND ", " && ");
  333. }
  334. private boolean isNull(String s) {
  335. return (s == null || s.length() <= 0);
  336. }
  337. private boolean isTrue(String s) {
  338. return (s != null && s.equals("true"));
  339. }
  340. /**
  341. * Turn off caching
  342. */
  343. public static void deactivateCaching() {
  344. CACHE = false;
  345. }
  346. }