KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > classycle > renderer > TemplateBasedClassRenderer


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.renderer;
26
27 import java.text.MessageFormat JavaDoc;
28
29 import classycle.ClassAttributes;
30 import classycle.graph.AtomicVertex;
31 import classycle.graph.NameAttributes;
32 import classycle.graph.StrongComponent;
33
34 /**
35  * Renderer of an {@link AtomicVertex} with
36  * {@link ClassAttributes}. The renderer is based on a
37  * <tt>java.text.MessageFormat</tt> template. The variables in the
38  * template have the following meaning:
39  * <table border=1 cellpadding=5>
40  * <tr><th>Variable index</th><th>Description</th></tr>
41  * <tr><td>0</td><td>fully-qualified class name</td></tr>
42  * <tr><td>1</td><td>class type</td></tr>
43  * <tr><td>2</td><td>size of the class file in bytes</td></tr>
44  * <tr><td>3</td>
45  * <td><tt>true</tt> if inner class otherwise <tt>false</tt></td></tr>
46  * <tr><td>4</td><td>Number of incoming arcs</td></tr>
47  * <tr><td>5</td>
48  * <td>Number of outgoing arcs to other vertices in the graph</td></tr>
49  * <tr><td>6</td><td>Number of outgoing arcs to external vertices</td></tr>
50  * <tr><td>7</td><td>Layer index</td></tr>
51  * <tr><td>8</td><td>Name of the cycle or empty string</td></tr>
52  * </table>
53  *
54  * @author Franz-Josef Elmer
55  */

56 public class TemplateBasedClassRenderer implements AtomicVertexRenderer
57 {
58   private MessageFormat JavaDoc _format;
59
60   /** Creates an instance for the specified template. */
61   public TemplateBasedClassRenderer(String JavaDoc template)
62   {
63     _format = new MessageFormat JavaDoc(template);
64   }
65
66   /**
67    * Renderes the specified vertex. It is assumed that the vertex attributes
68    * are of the type {@link classycle.ClassAttributes}.
69    * @param vertex Vertex to be rendered.
70    * @return the rendered vertex.
71    */

72   public String JavaDoc render(AtomicVertex vertex, StrongComponent cycle,
73                        int layerIndex)
74   {
75     String JavaDoc[] values = new String JavaDoc[9];
76     NameAttributes attributes = (NameAttributes) vertex.getAttributes();
77     values[0] = attributes.getName();
78     values[2] = Integer.toString(attributes.getSize());
79     if (attributes instanceof ClassAttributes)
80     {
81       ClassAttributes ca = (ClassAttributes) attributes;
82       values[1] = ca.getType();
83       values[3] = ca.isInnerClass() ? "true" : "false";
84     } else
85     {
86       values[1] = "";
87       values[3] = "";
88     }
89     values[4] = Integer.toString(vertex.getNumberOfIncomingArcs());
90     int usesInternal = 0;
91     int usesExternal = 0;
92     for (int i = 0, n = vertex.getNumberOfOutgoingArcs(); i < n; i++)
93     {
94       if (((AtomicVertex) vertex.getHeadVertex(i)).isGraphVertex())
95       {
96         usesInternal++;
97       } else
98       {
99         usesExternal++;
100       }
101     }
102     values[5] = Integer.toString(usesInternal);
103     values[6] = Integer.toString(usesExternal);
104     values[7] = Integer.toString(layerIndex);
105     values[8] = cycle == null ? ""
106                           : AbstractStrongComponentRenderer.createName(cycle);
107     return _format.format(values);
108   }
109 }
Popular Tags