1 package org.codehaus.nanning.xml;
2
3 import org.codehaus.nanning.AspectException;
4 import org.codehaus.nanning.config.*;
5 import org.w3c.dom.Document;
6 import org.w3c.dom.NodeList;
7 import org.w3c.dom.Node;
8 import org.w3c.dom.Element;
9 import org.xml.sax.SAXException;
10
11 import javax.xml.parsers.DocumentBuilder;
12 import javax.xml.parsers.DocumentBuilderFactory;
13 import javax.xml.parsers.ParserConfigurationException;
14 import java.io.ByteArrayInputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.net.URL;
18
19 public class AspectSystemParser {
20 public AspectSystem parse(InputStream input) throws IOException {
21 try {
22 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
23 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
24 Document document = documentBuilder.parse(input);
25
26 AspectSystem aspectSystem = new AspectSystem();
27
28 parseAspectSystem(document.getDocumentElement(), aspectSystem);
29
30 return aspectSystem;
31 } catch (ParserConfigurationException e) {
32 throw new AspectException(e);
33 } catch (SAXException e) {
34 throw new AspectException(e);
35 } catch (IOException e) {
36 throw new AspectException(e);
37 }
38 }
39
40 private void parseAspectSystem(Element element, AspectSystem aspectSystem) {
41 NodeList children = element.getChildNodes();
42 for (int i = 0; i < children.getLength(); i++) {
43 Node node = children.item(i);
44 if ("aspect".equals(node.getNodeName())) {
45 aspectSystem.addAspect(parseAspect((Element) node));
46 } else if ("interceptor".equals(node.getNodeName())) {
47 aspectSystem.addAspect(parseInterceptorAspect((Element) node));
48 } else if ("class".equals(node.getNodeName())) {
49 aspectSystem.addAspect(parseClass((Element) node));
50 } else if ("mixin".equals(node.getNodeName())) {
51 aspectSystem.addAspect(parseMixinAspect((Element) node));
52 }
53 }
54 }
55
56 private MixinAspect parseMixinAspect(Element element) {
57 return new MixinAspect(loadClass(element.getAttribute("interface")), loadClass(element.getAttribute("target")));
58 }
59
60 private ClassAspect parseClass(Element element) {
61 ClassAspect classAspect = new ClassAspect(loadClass(element.getAttribute("name")));
62 parseAspectSystem(element, classAspect);
63 return classAspect;
64 }
65
66 private Aspect parseInterceptorAspect(Element element) {
67 Class interceptorClass = loadClass(element.getAttribute("class"));
68
69 if (element.getElementsByTagName("pointcut").getLength() == 0) {
70 return new InterceptorAspect(interceptorClass, InterceptorAspect.PER_METHOD);
71 } else {
72 Element pointcutElement = (Element) element.getElementsByTagName("pointcut").item(0);
73 Pointcut pointcut = parsePointcut(pointcutElement);
74 return new InterceptorAspect(pointcut, interceptorClass,
75 InterceptorAspect.PER_METHOD);
76 }
77 }
78
79 private Pointcut parsePointcut(Element pointcutElement) {
80 return P.methodAttribute(pointcutElement.getAttribute("attribute"));
81 }
82
83 private Aspect parseAspect(Element element) {
84 return (Aspect) newInstance(element.getAttribute("class"));
85 }
86
87 private Class loadClass(String className) {
88 try {
89 return Class.forName(className);
90 } catch (ClassNotFoundException e) {
91 throw new AspectException(e);
92 }
93 }
94
95 private Object newInstance(String className) {
96 try {
97 Class aspectClass = Class.forName(className);
98 return aspectClass.newInstance();
99 } catch (Exception e) {
100 throw new AspectException(e);
101 }
102 }
103
104 public AspectSystem parse(String xmlAsString) throws IOException {
105 return parse(new ByteArrayInputStream(xmlAsString.getBytes()));
106 }
107
108 public AspectSystem parse(URL resource) throws IOException {
109 InputStream input = resource.openStream();
110 try {
111 return parse(input);
112 } finally {
113 input.close();
114 }
115 }
116 }
This page was automatically generated by Maven