KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > classycle > PackageProcessor


1 /*
2  * Copyright (c) 2003-2006, Franz-Josef Elmer, All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * - Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */

25 package classycle;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import classycle.graph.AtomicVertex;
32 import classycle.graph.GraphProcessor;
33 import classycle.graph.Vertex;
34
35 /**
36  * Processor which extracts the package dependency graph from the class
37  * dependency graph.
38  *
39  * @author Franz-Josef Elmer
40  */

41 public class PackageProcessor extends GraphProcessor
42 {
43   private final HashMap JavaDoc _packageVertices = new HashMap JavaDoc();
44   private AtomicVertex[] _packageGraph;
45   
46   /**
47    * Returns the package graph after processing.
48    * @return can be <tt>null</tt> before processing.
49    */

50   public AtomicVertex[] getGraph()
51   {
52     return _packageGraph;
53   }
54   
55   protected void initializeProcessing(Vertex[] graph)
56   {
57     _packageVertices.clear();
58   }
59
60   protected void processBefore(Vertex vertex)
61   {
62   }
63
64   protected void processArc(Vertex tail, Vertex head)
65   {
66     PackageVertex tailPackage = getPackageVertex(tail);
67     PackageVertex headPackage = getPackageVertex(head);
68     tailPackage.addOutgoingArcTo(headPackage);
69   }
70
71   private PackageVertex getPackageVertex(Vertex vertex)
72   {
73     ClassAttributes classAttributes = (ClassAttributes) vertex.getAttributes();
74     String JavaDoc className = (classAttributes).getName();
75     int index = className.lastIndexOf('.');
76     String JavaDoc packageName = index < 0 ? "(default package)"
77                                    : className.substring(0, index);
78     PackageVertex result = (PackageVertex) _packageVertices.get(packageName);
79     if (result == null)
80     {
81       result = new PackageVertex(packageName);
82       if (vertex instanceof AtomicVertex
83           && ((AtomicVertex) vertex).isGraphVertex())
84       {
85         // not an external package
86
result.reset();
87       }
88       _packageVertices.put(packageName, result);
89     }
90     result.addClass(classAttributes);
91     return result;
92   }
93
94   protected void processAfter(Vertex vertex)
95   {
96   }
97
98   protected void finishProcessing(Vertex[] graph)
99   {
100     Iterator JavaDoc vertices = _packageVertices.values().iterator();
101     ArrayList JavaDoc list = new ArrayList JavaDoc();
102     
103     while (vertices.hasNext())
104     {
105       AtomicVertex vertex = (AtomicVertex) vertices.next();
106       if (vertex.isGraphVertex())
107       {
108         list.add(vertex);
109       }
110     }
111     _packageGraph = (AtomicVertex[]) list.toArray(new AtomicVertex[list.size()]);
112   }
113
114 }
115
Popular Tags