KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > classycle > renderer > XMLStrongComponentRenderer


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.graph.GraphAttributes;
30 import classycle.graph.NameAttributes;
31 import classycle.graph.StrongComponent;
32 import classycle.graph.Vertex;
33
34 /**
35  * XML renderer of a {@link StrongComponent}.
36  *
37  * @author Franz-Josef Elmer
38  */

39 public class XMLStrongComponentRenderer
40                           extends AbstractStrongComponentRenderer {
41   private final int _minimumSize;
42   
43   protected String JavaDoc getStrongComponentElementName()
44   {
45     return "cycle";
46   }
47   
48   protected String JavaDoc getNodesElementName()
49   {
50     return "classes";
51   }
52   
53   protected String JavaDoc getNodeElementName()
54   {
55     return "classRef";
56   }
57   
58   protected String JavaDoc getCenterNodesElementName()
59   {
60     return "centerClasses";
61   }
62   
63   protected String JavaDoc getBestFragmentersElementName()
64   {
65     return "bestFragmenters";
66   }
67   
68   private MessageFormat JavaDoc getStrongComponentElementTemplate()
69   {
70     return new MessageFormat JavaDoc(" <" + getStrongComponentElementName()
71                              + " name=\"{0}\" size=\"{1}\" longestWalk=\"{2}\""
72                              + " girth=\"{3}\" radius=\"{4}\" diameter=\"{5}\""
73                              + " bestFragmentSize=\"{6}\">\n");
74   }
75   
76   private MessageFormat JavaDoc getNodeElementTemplate()
77   {
78     return new MessageFormat JavaDoc(" <" + getNodeElementName()
79                              + " name=\"{0}\"/>\n");
80   }
81
82   private MessageFormat JavaDoc getNodeElementTemplateWithEccentricity()
83   {
84     return new MessageFormat JavaDoc(" <" + getNodeElementName()
85                              + " name=\"{0}\" eccentricity=\"{1}\""
86                              + " maximumFragmentSize=\"{2}\"/>\n");
87   }
88
89   /**
90    * Creates an instance for the specified minimum number vertices.
91    * @param minimumSize Minimum number of vertices the {@link StrongComponent}
92    * should have to be rendered.
93    */

94   public XMLStrongComponentRenderer(int minimumSize)
95   {
96     _minimumSize = minimumSize;
97   }
98
99   public String JavaDoc render(StrongComponent component)
100   {
101     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
102     if (component.getNumberOfVertices() >= _minimumSize)
103     {
104       String JavaDoc[] values = new String JavaDoc[7];
105       values[0] = createName(component);
106       values[1] = Integer.toString(component.getNumberOfVertices());
107       values[2] = Integer.toString(component.getLongestWalk());
108       GraphAttributes attributes = (GraphAttributes) component.getAttributes();
109       values[3] = Integer.toString(attributes.getGirth());
110       values[4] = Integer.toString(attributes.getRadius());
111       values[5] = Integer.toString(attributes.getDiameter());
112       values[6] = Integer.toString(attributes.getBestFragmentSize());
113       getStrongComponentElementTemplate().format(values, result, null);
114
115       renderClasses(component, result);
116       renderVertices(attributes.getCenterVertices(), result,
117                      getCenterNodesElementName());
118       renderVertices(attributes.getBestFragmenters(), result,
119                      getBestFragmentersElementName());
120       result.append(" </").append(getStrongComponentElementName())
121             .append(">\n");
122     }
123     return new String JavaDoc(result);
124   }
125
126   private void renderClasses(StrongComponent component, StringBuffer JavaDoc result)
127   {
128     result.append(" <").append(getNodesElementName()).append(">\n");
129     int[] eccentricities
130         = ((GraphAttributes) component.getAttributes()).getEccentricities();
131     int[] maximumFragmentSizes
132         = ((GraphAttributes) component.getAttributes())
133                                                   .getMaximumFragmentSizes();
134     String JavaDoc[] values = new String JavaDoc[3];
135     MessageFormat JavaDoc template = getNodeElementTemplateWithEccentricity();
136     for (int i = 0, n = component.getNumberOfVertices(); i < n; i++)
137     {
138       values[0] = ((NameAttributes) component.getVertex(i).getAttributes())
139                                                                   .getName();
140       values[1] = Integer.toString(eccentricities[i]);
141       values[2] = Integer.toString(maximumFragmentSizes[i]);
142       template.format(values, result, null);
143     }
144     result.append(" </").append(getNodesElementName()).append(">\n");
145   }
146   
147   private void renderVertices(Vertex[] vertices, StringBuffer JavaDoc result,
148                               String JavaDoc tagName)
149   {
150     result.append(" <").append(tagName).append(">\n");
151     String JavaDoc[] values = new String JavaDoc[1];
152     MessageFormat JavaDoc template = getNodeElementTemplate();
153     for (int i = 0; i < vertices.length; i++) {
154       values[0] = ((NameAttributes) vertices[i].getAttributes()).getName();
155       template.format(values, result, null);
156     }
157     result.append(" </").append(tagName).append(">\n");
158   }
159 } //class
Popular Tags