1 package net.sourceforge.sannotations.context;
2
3 import java.lang.annotation.Annotation;
4 import java.util.ArrayList;
5
6 import net.sourceforge.sannotations.utils.AnnotationBeanDefinitionReader;
7 import net.sourceforge.sannotations.utils.GenericAnnotationBeanReader;
8
9 import org.springframework.beans.factory.config.BeanDefinition;
10 import org.springframework.beans.factory.xml.BeanDefinitionParser;
11 import org.springframework.beans.factory.xml.ParserContext;
12 import org.w3c.dom.Element;
13 import org.w3c.dom.Node;
14 import org.w3c.dom.NodeList;
15
16 /***
17 * bean definition parser for the tag sa:annotation-autoload, it configures an {@link net.sourceforge.sannotations.utils.Scanner} instance and delegates the scanning job to the {@link net.sourceforge.sannotations.utils.AnnotationBeanDefinitionReader} class
18 * User: Urubatan Date: 27/10/2006 Time: 17:07:35
19 */
20 public class AnnotationAutoloadBeanDefinitionParser implements BeanDefinitionParser {
21
22 @SuppressWarnings("unchecked")
23 public BeanDefinition parse(Element element, ParserContext parserContext) {
24 boolean scanDirs = false;
25
26 final ArrayList<String> includePackages = new ArrayList<String>();
27 final ArrayList<String> excludePackages = new ArrayList<String>();
28 final String val = element.getAttribute("scanDirs");
29 if (val != null) {
30 scanDirs = "true".equalsIgnoreCase(val);
31 }
32
33
34
35
36 final NodeList childNodes = element.getChildNodes();
37 for (int i = 0; i < childNodes.getLength(); i++) {
38 final Node node = childNodes.item(i);
39 if (node.getNodeType() == Node.ELEMENT_NODE) {
40 final String localName = node.getLocalName();
41 if ("includePackage".equals(localName)) {
42 includePackages.add(node.getTextContent());
43 } else if ("excludePackage".equals(localName)) {
44 excludePackages.add(node.getTextContent());
45 } else if ("alternateAnnotation".equals(localName)){
46 Node annotationNode = node.getAttributes().getNamedItem("annotation");
47 String annotationType = annotationNode.getNodeValue();
48 try {
49 Class<Annotation> annotationClass = (Class<Annotation>) Class.forName(annotationType);
50 NodeList annChilds = node.getChildNodes();
51 for (int j = 0; j < annChilds.getLength(); j++) {
52 Node annNode = annChilds.item(j);
53 String annLocal = annNode.getLocalName();
54 if("beanReader".equals(annLocal)){
55 Node lazy = annNode.getAttributes().getNamedItem("lazy");
56 Node scope = annNode.getAttributes().getNamedItem("scope");
57 Node autowire = annNode.getAttributes().getNamedItem("autoWire");
58 GenericAnnotationBeanReader reader = new GenericAnnotationBeanReader();
59 reader.setAutoWireMode(autowire.getNodeValue());
60 reader.setScope(scope.getNodeValue());
61 reader.setLazy("true".equalsIgnoreCase(lazy.getNodeValue()));
62 AnnotationBeanDefinitionReader.register(annotationClass, reader);
63 }
64 }
65 } catch (ClassNotFoundException e) {
66 }
67 }
68 }
69 }
70 AnnotationBeanDefinitionReader.loadBeansWithAnnotation(parserContext.getRegistry(), scanDirs, includePackages, excludePackages);
71 return null;
72 }
73 }