1 | /* |
2 | * Copyright (c) 2001-2009, 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.dependencyfinder.cli; |
34 | |
35 | import java.util.*; |
36 | import java.io.*; |
37 | |
38 | import org.apache.log4j.*; |
39 | |
40 | import com.jeantessier.commandline.*; |
41 | import com.jeantessier.dependency.*; |
42 | |
43 | public class ClassCohesion extends DependencyGraphCommand { |
44 | private static final String DEFAULT_ENCODING = "utf-8"; |
45 | private static final String DEFAULT_DTD_PREFIX = "http://depfind.sourceforge.net/dtd"; |
46 | private static final String DEFAULT_INDENT_TEXT = " "; |
47 | |
48 | protected void populateCommandLineSwitches() { |
49 | super.populateCommandLineSwitches(); |
50 | populateCommandLineSwitchesForXMLOutput(DEFAULT_ENCODING, DEFAULT_DTD_PREFIX, DEFAULT_INDENT_TEXT); |
51 | |
52 | getCommandLine().addToggleSwitch("csv"); |
53 | getCommandLine().addToggleSwitch("txt"); |
54 | getCommandLine().addToggleSwitch("xml"); |
55 | getCommandLine().addToggleSwitch("list"); |
56 | } |
57 | |
58 | protected Collection<CommandLineException> parseCommandLine(String[] args) { |
59 | Collection<CommandLineException> exceptions = super.parseCommandLine(args); |
60 | int modeSwitch = 0; |
61 | |
62 | if (getCommandLine().getToggleSwitch("csv")) { |
63 | modeSwitch++; |
64 | } |
65 | if (getCommandLine().getToggleSwitch("txt")) { |
66 | modeSwitch++; |
67 | } |
68 | if (getCommandLine().getToggleSwitch("xml")) { |
69 | modeSwitch++; |
70 | } |
71 | if (modeSwitch != 1) { |
72 | exceptions.add(new CommandLineException("Must have one and only one of -csv, -txt, or -xml")); |
73 | } |
74 | |
75 | return exceptions; |
76 | } |
77 | |
78 | public void doProcessing() throws Exception { |
79 | LCOM4Gatherer gatherer = new LCOM4Gatherer(); |
80 | |
81 | Logger.getLogger(OOMetrics.class).debug("Reading classes and computing metrics as we go ..."); |
82 | getVerboseListener().print("Reading classes and computing metrics as we go ..."); |
83 | gatherer.traverseNodes(loadGraph().getPackages().values()); |
84 | |
85 | Logger.getLogger(OOMetrics.class).debug("Printing results ..."); |
86 | getVerboseListener().print("Printing results ..."); |
87 | |
88 | if (getCommandLine().isPresent("csv")) { |
89 | printCSVFiles(gatherer.getResults()); |
90 | } else if (getCommandLine().isPresent("txt")) { |
91 | printTextFile(gatherer.getResults()); |
92 | } else if (getCommandLine().isPresent("xml")) { |
93 | printXMLFile(gatherer.getResults()); |
94 | } |
95 | |
96 | Logger.getLogger(OOMetrics.class).debug("Done."); |
97 | } |
98 | |
99 | private void printCSVFiles(Map<ClassNode, Collection<Collection<FeatureNode>>> results) throws IOException { |
100 | getOut().println("class, LCOM4"); |
101 | for (Map.Entry<ClassNode, Collection<Collection<FeatureNode>>> entry : results.entrySet()) { |
102 | getOut().println(entry.getKey().getName() + ", " + entry.getValue().size()); |
103 | } |
104 | } |
105 | |
106 | private void printTextFile(Map<ClassNode, Collection<Collection<FeatureNode>>> results) throws IOException { |
107 | String indentText = getCommandLine().getSingleSwitch("indent-text"); |
108 | |
109 | for (Map.Entry<ClassNode, Collection<Collection<FeatureNode>>> entry : results.entrySet()) { |
110 | getOut().println(entry.getKey().getName() + ": " + entry.getValue().size()); |
111 | if (entry.getValue().size() > 1 && getCommandLine().isPresent("list")) { |
112 | getOut().println(indentText + "--------"); |
113 | for (Collection<FeatureNode> component : entry.getValue()) { |
114 | for (FeatureNode feature : component) { |
115 | getOut().println(indentText + feature.getName().substring(feature.getClassNode().getName().length() + 1)); |
116 | } |
117 | getOut().println(indentText + "--------"); |
118 | } |
119 | } |
120 | } |
121 | } |
122 | |
123 | private void printXMLFile(Map<ClassNode, Collection<Collection<FeatureNode>>> results) throws IOException { |
124 | String indentText = getCommandLine().getSingleSwitch("indent-text"); |
125 | |
126 | getOut().println("<classes>"); |
127 | for (Map.Entry<ClassNode, Collection<Collection<FeatureNode>>> entry : results.entrySet()) { |
128 | if (entry.getValue().size() > 1) { |
129 | getOut().println(indentText + "<class name=\"" + entry.getKey().getName() + "\" lcom4=\"" + entry.getValue().size() + "\">"); |
130 | for (Collection<FeatureNode> component : entry.getValue()) { |
131 | getOut().println(indentText + indentText + "<component>"); |
132 | for (FeatureNode feature : component) { |
133 | getOut().println(indentText + indentText + indentText + "<feature name=\"" + feature.getName() + "\"/>"); |
134 | } |
135 | getOut().println(indentText + indentText + "</component>"); |
136 | } |
137 | getOut().println(indentText + "</class>"); |
138 | } else { |
139 | getOut().println(indentText + "<class name=\"" + entry.getKey().getName() + "\" lcom4=\"" + entry.getValue().size() + "\"/>"); |
140 | } |
141 | } |
142 | getOut().println("</classes>"); |
143 | } |
144 | |
145 | public static void main(String[] args) throws Exception { |
146 | new ClassCohesion().run(args); |
147 | } |
148 | } |