KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > definition > aspectj5 > DocumentParser


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.definition.aspectj5;
5
6 import org.xml.sax.Attributes JavaDoc;
7 import org.xml.sax.InputSource JavaDoc;
8 import org.xml.sax.SAXException JavaDoc;
9 import org.xml.sax.SAXParseException JavaDoc;
10 import org.xml.sax.XMLReader JavaDoc;
11 import org.xml.sax.helpers.DefaultHandler JavaDoc;
12 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
13
14 import com.tc.aspectwerkz.util.Strings;
15
16 import java.io.InputStream JavaDoc;
17 import java.net.URL JavaDoc;
18
19 /**
20  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
21  */

22 public class DocumentParser extends DefaultHandler JavaDoc {
23
24   /**
25    * The current DTD public id. The matching dtd will be searched as a resource.
26    */

27   private final static String JavaDoc DTD_PUBLIC_ID = "-//AspectJ//DTD 1.5.0//EN";
28
29   /**
30    * The DTD alias, for better user experience.
31    */

32   private final static String JavaDoc DTD_PUBLIC_ID_ALIAS = "-//AspectJ//DTD//EN";
33
34   /**
35    * A handler to the DTD stream so that we are only using one file descriptor
36    */

37   private final static InputStream JavaDoc DTD_STREAM = DocumentParser.class
38           .getResourceAsStream("/aspectj_1_5_0.dtd");
39
40   private final static String JavaDoc ASPECTJ_ELEMENT = "aspectj";
41   private final static String JavaDoc WEAVER_ELEMENT = "weaver";
42   private final static String JavaDoc DUMP_ELEMENT = "dump";
43   private final static String JavaDoc INCLUDE_ELEMENT = "include";
44   private final static String JavaDoc EXCLUDE_ELEMENT = "exclude";
45   private final static String JavaDoc OPTIONS_ATTRIBUTE = "options";
46   private final static String JavaDoc ASPECTS_ELEMENT = "aspects";
47   private final static String JavaDoc ASPECT_ELEMENT = "aspect";
48   private final static String JavaDoc CONCRETE_ASPECT_ELEMENT = "concrete-aspect";
49   private final static String JavaDoc NAME_ATTRIBUTE = "name";
50   private final static String JavaDoc EXTEND_ATTRIBUTE = "extends";
51   private final static String JavaDoc POINTCUT_ELEMENT = "pointcut";
52   private final static String JavaDoc WITHIN_ATTRIBUTE = "within";
53   private final static String JavaDoc 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 JavaDoc url) throws Exception JavaDoc {
68     InputStream JavaDoc in = null;
69     try {
70       DocumentParser parser = new DocumentParser();
71       XMLReader JavaDoc 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 JavaDoc e) {
78         // fine, the parser don't do validation
79
}
80       try {
81         xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", false);
82       } catch (SAXException JavaDoc e) {
83         // fine, the parser don't do validation
84
}
85       try {
86         xmlReader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
87       } catch (SAXException JavaDoc e) {
88         // fine, the parser don't do validation
89
}
90
91       xmlReader.setEntityResolver(parser);
92       in = url.openStream();
93       xmlReader.parse(new InputSource JavaDoc(in));
94       return parser.m_definition;
95     } finally {
96       try {
97         in.close();
98       } catch (Throwable JavaDoc t) {
99         // ignore
100
}
101     }
102   }
103
104   public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) {
105     if (publicId.equals(DTD_PUBLIC_ID) || publicId.equals(DTD_PUBLIC_ID_ALIAS)) {
106       InputStream JavaDoc 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 JavaDoc(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 JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
120     if (ASPECT_ELEMENT.equals(qName)) {
121       String JavaDoc 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 JavaDoc 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 JavaDoc name = attributes.getValue(NAME_ATTRIBUTE);
133       String JavaDoc 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 JavaDoc name = attributes.getValue(NAME_ATTRIBUTE);
140       String JavaDoc 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 JavaDoc("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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc typePattern = attributes.getValue(WITHIN_ATTRIBUTE);
168       if (!isNull(typePattern)) {
169         m_definition.getAspectExcludePatterns().add(typePattern);
170       }
171     } else {
172       throw new SAXException JavaDoc("Unknown element while parsing<aspectj> element: " + qName);
173     }
174     super.startElement(uri, localName, qName, attributes);
175   }
176
177   public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
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   // TODO AV - define what we want for XML parser error - for now stderr
191
public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
192     super.warning(e);
193   }
194
195   public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
196     super.error(e);
197   }
198
199   public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
200     super.fatalError(e);
201   }
202
203   private static String JavaDoc replaceXmlAnd(String JavaDoc expression) {
204     // TODO AV do we need to handle "..)AND" or "AND(.." ?
205
return Strings.replaceSubString(expression, " AND ", " && ");
206   }
207
208   private boolean isNull(String JavaDoc s) {
209     return s == null || s.length() <= 0;
210   }
211 }
212
Popular Tags