KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependency > NodeHandler


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.dependency;
34
35 import java.io.*;
36 import java.net.*;
37 import java.util.*;
38
39 import org.apache.log4j.*;
40
41 import org.xml.sax.*;
42 import org.xml.sax.helpers.*;
43
44 public class NodeHandler extends DefaultHandler {
45     private static final int PACKAGE = 1;
46     private static final int CLASS = 2;
47     private static final int FEATURE = 3;
48
49     private NodeFactory factory;
50
51     private int currentNodeType;
52     private int currentDependencyType;
53     private Attributes currentDependencyAttributes;
54     private Node currentNode;
55     private PackageNode currentPackage;
56     private Attributes currentPackageAttributes;
57     private ClassNode currentClass;
58     private Attributes currentClassAttributes;
59     private FeatureNode currentFeature;
60     private Attributes currentFeatureAttributes;
61     private StringBuffer JavaDoc currentName = new StringBuffer JavaDoc();
62
63     private HashSet dependencyListeners = new HashSet();
64
65     public NodeHandler() {
66         this(new NodeFactory());
67     }
68
69     public NodeHandler(NodeFactory factory) {
70         this.factory = factory;
71     }
72
73     public NodeFactory getFactory() {
74         return factory;
75     }
76
77     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes atts) throws SAXException {
78         Logger.getLogger(getClass()).debug("qName = " + qName);
79
80         for (int i=0; i<atts.getLength(); i++) {
81             Logger.getLogger(getClass()).debug(" " + atts.getQName(i) + ": " + atts.getValue(i));
82         }
83
84         currentName.delete(0, currentName.length());
85
86         if ("dependencies".equals(qName)) {
87             fireBeginSession();
88         } else if ("package".equals(qName)) {
89             currentNodeType = PACKAGE;
90             currentPackageAttributes = new AttributesImpl(atts);
91         } else if ("class".equals(qName)) {
92             currentNodeType = CLASS;
93             currentClassAttributes = new AttributesImpl(atts);
94         } else if ("feature".equals(qName)) {
95             currentNodeType = FEATURE;
96             currentFeatureAttributes = new AttributesImpl(atts);
97         } else if ("inbound".equals(qName) || "outbound".equals(qName)) {
98             if ("package".equals(atts.getValue("type"))) {
99                 currentDependencyType = PACKAGE;
100             } else if ("class".equals(atts.getValue("type"))) {
101                 currentDependencyType = CLASS;
102             } else if ("feature".equals(atts.getValue("type"))) {
103                 currentDependencyType = FEATURE;
104             }
105             currentDependencyAttributes = new AttributesImpl(atts);
106         }
107
108         Logger.getLogger(getClass()).debug(" current node type: " + currentNodeType);
109         Logger.getLogger(getClass()).debug(" current dependency type: " + currentDependencyType);
110     }
111
112     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException {
113         Logger.getLogger(getClass()).debug("qName = " + qName);
114
115         if ("dependencies".equals(qName)) {
116             fireEndSession();
117         } else if ("name".equals(qName)) {
118             Logger.getLogger(getClass()).debug(" Processing <name> tag:");
119             Logger.getLogger(getClass()).debug(" current name: " + currentName);
120             Logger.getLogger(getClass()).debug(" current node type: " + currentNodeType);
121
122             switch (currentNodeType) {
123                 case PACKAGE:
124                     currentPackage = getFactory().createPackage(currentName.toString(), isConfirmed(currentPackageAttributes));
125                     currentNode = currentPackage;
126                     break;
127                 case CLASS:
128                     currentClass = getFactory().createClass(currentName.toString(), isConfirmed(currentClassAttributes));
129                     currentNode = currentClass;
130                     fireBeginClass(currentClass.toString());
131                     break;
132                 case FEATURE:
133                     currentFeature = getFactory().createFeature(currentName.toString(), isConfirmed(currentFeatureAttributes));
134                     currentNode = currentFeature;
135                     break;
136             }
137         } else if ("outbound".equals(qName)) {
138             Logger.getLogger(getClass()).debug(" Processing <outbound> tag:");
139             Logger.getLogger(getClass()).debug(" current_name: " + currentName);
140             Logger.getLogger(getClass()).debug(" current_dependency_type: " + currentDependencyType);
141
142             Node other = null;
143             switch (currentDependencyType) {
144                 case PACKAGE:
145                     other = getFactory().createPackage(currentName.toString(), isConfirmed(currentDependencyAttributes));
146                     break;
147                 case CLASS:
148                     other = getFactory().createClass(currentName.toString(), isConfirmed(currentDependencyAttributes));
149                     break;
150                 case FEATURE:
151                     other = getFactory().createFeature(currentName.toString(), isConfirmed(currentDependencyAttributes));
152                     break;
153             }
154             currentNode.addDependency(other);
155             fireDependency(currentNode, other);
156         } else if ("inbound".equals(qName)) {
157             Logger.getLogger(getClass()).debug(" Processing <inbound> tag:");
158             Logger.getLogger(getClass()).debug(" current_name: " + currentName);
159             Logger.getLogger(getClass()).debug(" current_dependency_type: " + currentDependencyType);
160
161             Node other = null;
162             switch (currentDependencyType) {
163                 case PACKAGE:
164                     other = getFactory().createPackage(currentName.toString(), isConfirmed(currentDependencyAttributes));
165                     break;
166                 case CLASS:
167                     other = getFactory().createClass(currentName.toString(), isConfirmed(currentDependencyAttributes));
168                     break;
169                 case FEATURE:
170                     other = getFactory().createFeature(currentName.toString(), isConfirmed(currentDependencyAttributes));
171                     break;
172             }
173             other.addDependency(currentNode);
174             fireDependency(other, currentNode);
175         }
176     }
177
178     public void characters(char[] ch, int start, int length) throws SAXException {
179         currentName.append(ch, start, length);
180         Logger.getLogger(getClass()).debug("characters: \"" + new String JavaDoc(ch, start, length) + "\"");
181     }
182
183     public void addDependencyListener(DependencyListener listener) {
184         synchronized(dependencyListeners) {
185             dependencyListeners.add(listener);
186         }
187     }
188
189     public void removeDependencyListener(DependencyListener listener) {
190         synchronized(dependencyListeners) {
191             dependencyListeners.remove(listener);
192         }
193     }
194
195     protected void fireBeginSession() {
196         DependencyEvent event = new DependencyEvent(this);
197
198         HashSet listeners;
199         synchronized(dependencyListeners) {
200             listeners = (HashSet) dependencyListeners.clone();
201         }
202
203         Iterator i = listeners.iterator();
204         while(i.hasNext()) {
205             ((DependencyListener) i.next()).beginSession(event);
206         }
207     }
208     
209     protected void fireBeginClass(String JavaDoc classname) {
210         DependencyEvent event = new DependencyEvent(this, classname);
211
212         HashSet listeners;
213         synchronized(dependencyListeners) {
214             listeners = (HashSet) dependencyListeners.clone();
215         }
216
217         Iterator i = listeners.iterator();
218         while(i.hasNext()) {
219             ((DependencyListener) i.next()).beginClass(event);
220         }
221     }
222
223     protected void fireDependency(Node dependent, Node dependable) {
224         DependencyEvent event = new DependencyEvent(this, dependent, dependable);
225
226         HashSet listeners;
227         synchronized(dependencyListeners) {
228             listeners = (HashSet) dependencyListeners.clone();
229         }
230
231         Iterator i = listeners.iterator();
232         while(i.hasNext()) {
233             ((DependencyListener) i.next()).dependency(event);
234         }
235     }
236
237     protected void fireEndClass(String JavaDoc classname) {
238         DependencyEvent event = new DependencyEvent(this, classname);
239
240         HashSet listeners;
241         synchronized(dependencyListeners) {
242             listeners = (HashSet) dependencyListeners.clone();
243         }
244
245         Iterator i = listeners.iterator();
246         while(i.hasNext()) {
247             ((DependencyListener) i.next()).endClass(event);
248         }
249     }
250
251     protected void fireEndSession() {
252         DependencyEvent event = new DependencyEvent(this);
253
254         HashSet listeners;
255         synchronized(dependencyListeners) {
256             listeners = (HashSet) dependencyListeners.clone();
257         }
258
259         Iterator i = listeners.iterator();
260         while(i.hasNext()) {
261             ((DependencyListener) i.next()).endSession(event);
262         }
263     }
264
265     private boolean isConfirmed(Attributes atts) {
266         return atts.getValue("confirmed") == null || "yes".equalsIgnoreCase(atts.getValue("confirmed"));
267     }
268 }
269
Popular Tags