KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > QueueProcessingManager


1 package spoon.support;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.LinkedList JavaDoc;
6 import java.util.Queue JavaDoc;
7
8 import spoon.processing.ProcessingManager;
9 import spoon.processing.Processor;
10 import spoon.processing.Severity;
11 import spoon.reflect.Factory;
12 import spoon.reflect.declaration.CtElement;
13 import spoon.support.util.Timer;
14 import spoon.support.visitor.ProcessingVisitor;
15
16 /**
17  * This processing manager implements a blocking processing policy that consists
18  * of applying the processors in a FIFO order until no processors remain to be
19  * applied.
20  */

21 public class QueueProcessingManager implements ProcessingManager {
22     Processor<?> current;
23
24     Factory factory;
25
26     Queue JavaDoc<Processor> processors;
27
28     ProcessingVisitor visitor;
29
30     /**
31      * Creates a new processing manager that maintains a queue of processors to
32      * be applied to a given factory.
33      *
34      * @param factory
35      * the factory on which the processing applies (contains the
36      * meta-model)
37      */

38     public QueueProcessingManager(Factory factory) {
39         super();
40         setFactory(factory);
41     }
42
43     public void addProcessor(Class JavaDoc<? extends Processor> type) {
44         try {
45             Processor<?> p = type.newInstance();
46             p.setFactory(factory);
47             addProcessor(p);
48         } catch (Exception JavaDoc e) {
49             factory
50                     .getEnvironment()
51                     .report(
52                             null,
53                             Severity.ERROR,
54                             "Unable to instantiate processor \""
55                                     + type.getName()
56                                     + "\" - Your processor should have a constructor with no arguments");
57         }
58     }
59
60     public boolean addProcessor(Processor<?> p) {
61         p.setFactory(getFactory());
62         return getProcessors().add(p);
63     }
64
65     @SuppressWarnings JavaDoc("unchecked")
66     public void addProcessor(String JavaDoc qualifiedName) {
67         try {
68             addProcessor((Class JavaDoc<? extends Processor>) Class
69                     .forName(qualifiedName));
70         } catch (ClassNotFoundException JavaDoc e) {
71             factory.getEnvironment().report(
72                     null,
73                     Severity.ERROR,
74                     "Unable to load processor \"" + qualifiedName
75                             + "\" - Check your classpath");
76         }
77     }
78
79     public Processor<?> getCurrentProcessor() {
80         return current;
81     }
82
83     public Factory getFactory() {
84         return factory;
85     }
86
87     public Queue JavaDoc<Processor> getProcessors() {
88         if (processors == null) {
89             processors = new LinkedList JavaDoc<Processor>();
90         }
91         return processors;
92     }
93
94     protected ProcessingVisitor getVisitor() {
95         if (visitor == null)
96             visitor = new ProcessingVisitor(getFactory());
97         return visitor;
98     }
99
100     public boolean isToBeApplied(Class JavaDoc<? extends Processor> type) {
101         for (Processor p : getProcessors()) {
102             if (p.getClass() == type) {
103                 return true;
104             }
105         }
106         return false;
107     }
108
109     public void process() {
110         Timer.start("process");
111         process(getFactory().Package().getAllRoots());
112         Timer.stop("process");
113     }
114
115     public void process(Collection JavaDoc<? extends CtElement> elements) {
116         Processor p;
117         while ((p = getProcessors().poll()) != null) {
118             current = p;
119             p.init();
120             p.process();
121             for (CtElement e : new ArrayList JavaDoc<CtElement>(elements))
122                 process(e, p);
123             p.processingDone();
124         }
125     }
126
127     public void process(CtElement element) {
128         Processor p;
129         while ((p = getProcessors().poll()) != null) {
130             current = p;
131             p.init();
132             p.process();
133             process(element, p);
134             p.processingDone();
135         }
136     }
137
138     protected void process(CtElement element, Processor<?> processor) {
139         getVisitor().setProcessor(processor);
140         getVisitor().scan(element);
141     }
142
143     public void setFactory(Factory factory) {
144         this.factory = factory;
145         factory.getEnvironment().setManager(this);
146     }
147
148 }
149
Popular Tags