123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- /*******************************************************************************
- * Copyright (c) 2005 Contributors.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Common Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/cpl-v10.html
- *
- * Contributors:
- * Alexandre Vasseur initial implementation
- *******************************************************************************/
- package org.aspectj.weaver.loadtime.definition;
-
- import org.xml.sax.Attributes;
- import org.xml.sax.ErrorHandler;
- import org.xml.sax.InputSource;
- import org.xml.sax.SAXException;
- import org.xml.sax.SAXNotRecognizedException;
- import org.xml.sax.SAXParseException;
- import org.xml.sax.XMLReader;
- import org.xml.sax.DTDHandler;
- import org.xml.sax.helpers.DefaultHandler;
- import org.xml.sax.helpers.XMLReaderFactory;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URL;
-
- import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
-
- /**
- * FIXME AV - doc, concrete aspect
- *
- * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
- */
- public class DocumentParser extends DefaultHandler {
-
- /**
- * The current DTD public id. The matching dtd will be searched as a resource.
- */
- private final static String DTD_PUBLIC_ID = "-//AspectJ//DTD 1.5.0//EN";
-
- /**
- * The DTD alias, for better user experience.
- */
- private final static String DTD_PUBLIC_ID_ALIAS = "-//AspectJ//DTD//EN";
-
- /**
- * A handler to the DTD stream so that we are only using one file descriptor
- */
- private final static InputStream DTD_STREAM = DocumentParser.class.getResourceAsStream("/aspectj_1_5_0.dtd");
-
- private final static String ASPECTJ_ELEMENT = "aspectj";
- private final static String WEAVER_ELEMENT = "weaver";
- private final static String INCLUDE_ELEMENT = "include";
- private final static String EXCLUDE_ELEMENT = "exclude";
- private final static String OPTIONS_ATTRIBUTE = "options";
- private final static String ASPECTS_ELEMENT = "aspects";
- private final static String ASPECT_ELEMENT = "aspect";
- private final static String CONCRETE_ASPECT_ELEMENT = "concrete-aspect";
- private final static String NAME_ATTRIBUTE = "name";
- private final static String EXTEND_ATTRIBUTE = "extends";
- private final static String POINTCUT_ELEMENT = "pointcut";
- private final static String WITHIN_ATTRIBUTE = "within";
- private final static String EXPRESSION_ATTRIBUTE = "expression";
-
-
- private final Definition m_definition;
-
- private boolean m_inAspectJ;
- private boolean m_inWeaver;
- private boolean m_inAspects;
-
- private Definition.ConcreteAspect m_lastConcreteAspect;
-
- private DocumentParser() {
- m_definition = new Definition();
- }
-
- public static Definition parse(final URL url) throws Exception {
- InputStream in = null;
- try {
- DocumentParser parser = new DocumentParser();
-
- XMLReader xmlReader = XMLReaderFactory.createXMLReader();
- xmlReader.setContentHandler(parser);
- xmlReader.setErrorHandler(parser);
-
- try {
- xmlReader.setFeature("http://xml.org/sax/features/validation", false);
- xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
- xmlReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
- } catch (SAXNotRecognizedException e) {
- ;//fine, the parser don't do validation
- }
-
- xmlReader.setEntityResolver(parser);
- in = url.openStream();
- xmlReader.parse(new InputSource(in));
- return parser.m_definition;
- } finally {
- try {
- in.close();
- } catch (Throwable t) {
- ;
- }
- }
- }
-
- public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException {
- if (publicId.equals(DTD_PUBLIC_ID) || publicId.equals(DTD_PUBLIC_ID_ALIAS)) {
- InputStream in = DTD_STREAM;
- if (in == null) {
- return null;
- } else {
- return new InputSource(in);
- }
- } else {
- System.err.println(
- "AspectJ - WARN - unknown DTD "
- + publicId
- + " - consider using "
- + DTD_PUBLIC_ID
- );
- return null;
- }
- }
-
- public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
- if (ASPECT_ELEMENT.equals(qName)) {
- String name = attributes.getValue(NAME_ATTRIBUTE);
- if (!isNull(name)) {
- m_definition.getAspectClassNames().add(name);
- }
- } else if (WEAVER_ELEMENT.equals(qName)) {
- String options = attributes.getValue(OPTIONS_ATTRIBUTE);
- if (!isNull(options)) {
- m_definition.appendWeaverOptions(options);
- }
- m_inWeaver = true;
- } else if (CONCRETE_ASPECT_ELEMENT.equals(qName)) {
- String name = attributes.getValue(NAME_ATTRIBUTE);
- String extend = attributes.getValue(EXTEND_ATTRIBUTE);
- if (!isNull(name) && !isNull(extend)) {
- m_lastConcreteAspect = new Definition.ConcreteAspect(name, extend);
- m_definition.getConcreteAspects().add(m_lastConcreteAspect);
- }
- } else if (POINTCUT_ELEMENT.equals(qName) && m_lastConcreteAspect != null) {
- String name = attributes.getValue(NAME_ATTRIBUTE);
- String expression = attributes.getValue(EXPRESSION_ATTRIBUTE);
- if (!isNull(name) && !isNull(expression)) {
- m_lastConcreteAspect.pointcuts.add(new Definition.Pointcut(name, replaceXmlAnd(expression)));
- }
- } else if (ASPECTJ_ELEMENT.equals(qName)) {
- if (m_inAspectJ) {
- throw new SAXException("Found nested <aspectj> element");
- }
- m_inAspectJ = true;
- } else if (ASPECTS_ELEMENT.equals(qName)) {
- m_inAspects = true;
- } else if (INCLUDE_ELEMENT.equals(qName) && m_inWeaver) {
- String typePattern = attributes.getValue(WITHIN_ATTRIBUTE);
- if (!isNull(typePattern)) {
- m_definition.getIncludePatterns().add(typePattern);
- }
- } else if (EXCLUDE_ELEMENT.equals(qName) && m_inWeaver) {
- String typePattern = attributes.getValue(WITHIN_ATTRIBUTE);
- if (!isNull(typePattern)) {
- m_definition.getExcludePatterns().add(typePattern);
- }
- } else if (EXCLUDE_ELEMENT.equals(qName) && m_inAspects) {
- String typePattern = attributes.getValue(WITHIN_ATTRIBUTE);
- if (!isNull(typePattern)) {
- m_definition.getAspectExcludePatterns().add(typePattern);
- }
- } else {
- throw new SAXException("Unknown element while parsing <aspectj> element: " + qName);
- }
- super.startElement(uri, localName, qName, attributes);
- }
-
- public void endElement(String uri, String localName, String qName) throws SAXException {
- if (CONCRETE_ASPECT_ELEMENT.equals(qName)) {
- m_lastConcreteAspect = null;
- } else if (ASPECTJ_ELEMENT.equals(qName)) {
- m_inAspectJ = false;
- } else if (WEAVER_ELEMENT.equals(qName)) {
- m_inWeaver = false;
- } else if (ASPECTS_ELEMENT.equals(qName)) {
- m_inAspects = false;
- }
- super.endElement(uri, localName, qName);
- }
-
- //TODO AV - define what we want for XML parser error - for now stderr
- public void warning(SAXParseException e) throws SAXException {
- super.warning(e);
- }
-
- public void error(SAXParseException e) throws SAXException {
- super.error(e);
- }
-
- public void fatalError(SAXParseException e) throws SAXException {
- super.fatalError(e);
- }
-
-
- private static String replaceXmlAnd(String expression) {
- //TODO AV do we need to handle "..)AND" or "AND(.." ?
- //FIXME AV Java 1.4 code - if KO, use some Strings util
- return expression.replaceAll(" AND ", " && ");
- }
-
- private boolean isNull(String s) {
- return (s == null || s.length() <= 0);
- }
-
-
- }
|