KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependencyfinder > cli > ListDiff


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.dependencyfinder.cli;
34
35 import java.io.*;
36 import java.util.*;
37
38 import org.apache.log4j.*;
39
40 import com.jeantessier.commandline.*;
41 import com.jeantessier.dependencyfinder.*;
42 import com.jeantessier.diff.*;
43
44 public class ListDiff {
45     public static void showError(CommandLineUsage clu, String JavaDoc msg) {
46         System.err.println(msg);
47         showError(clu);
48     }
49
50     public static void showError(CommandLineUsage clu) {
51         System.err.println(clu);
52         System.err.println();
53         System.err.println("Defaults is text output to the console.");
54         System.err.println();
55     }
56
57     public static void showVersion() throws IOException {
58         Version version = new Version();
59         
60         System.err.print(version.getImplementationTitle());
61         System.err.print(" ");
62         System.err.print(version.getImplementationVersion());
63         System.err.print(" (c) ");
64         System.err.print(version.getCopyrightDate());
65         System.err.print(" ");
66         System.err.print(version.getCopyrightHolder());
67         System.err.println();
68         
69         System.err.print(version.getImplementationURL());
70         System.err.println();
71         
72         System.err.print("Compiled on ");
73         System.err.print(version.getImplementationDate());
74         System.err.println();
75     }
76
77     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
78         // Parsing the command line
79
CommandLine commandLine = new CommandLine(new NullParameterStrategy());
80         commandLine.addSingleValueSwitch("name");
81         commandLine.addSingleValueSwitch("old-label");
82         commandLine.addSingleValueSwitch("old", true);
83         commandLine.addSingleValueSwitch("new-label");
84         commandLine.addSingleValueSwitch("new", true);
85         commandLine.addToggleSwitch("compress");
86         commandLine.addSingleValueSwitch("encoding", ListDiffPrinter.DEFAULT_ENCODING);
87         commandLine.addSingleValueSwitch("dtd-prefix", ListDiffPrinter.DEFAULT_DTD_PREFIX);
88         commandLine.addSingleValueSwitch("indent-text");
89         commandLine.addToggleSwitch("time");
90         commandLine.addSingleValueSwitch("out");
91         commandLine.addToggleSwitch("help");
92         commandLine.addToggleSwitch("version");
93
94         CommandLineUsage usage = new CommandLineUsage("ListDiff");
95         commandLine.accept(usage);
96
97         try {
98             commandLine.parse(args);
99         } catch (IllegalArgumentException JavaDoc ex) {
100             showError(usage, ex.toString());
101             System.exit(1);
102         } catch (CommandLineException ex) {
103             showError(usage, ex.toString());
104             System.exit(1);
105         }
106
107         if (commandLine.getToggleSwitch("help")) {
108             showError(usage);
109         }
110         
111         if (commandLine.getToggleSwitch("version")) {
112             showVersion();
113         }
114
115         if (commandLine.getToggleSwitch("help") || commandLine.getToggleSwitch("version")) {
116             System.exit(1);
117         }
118
119         /*
120          * Beginning of main processing
121          */

122         
123         Date start = new Date();
124         
125         String JavaDoc line;
126         
127         Logger.getLogger(ListDiff.class).info("Loading data ...");
128         
129         Collection oldAPI = new TreeSet();
130         BufferedReader oldIn = new BufferedReader(new FileReader(commandLine.getSingleSwitch("old")));
131         while((line = oldIn.readLine()) != null) {
132             oldAPI.add(line);
133         }
134         
135         Collection newAPI = new TreeSet();
136         BufferedReader newIn = new BufferedReader(new FileReader(commandLine.getSingleSwitch("new")));
137         while((line = newIn.readLine()) != null) {
138             newAPI.add(line);
139         }
140         
141         ListDiffPrinter printer = new ListDiffPrinter(commandLine.getToggleSwitch("compress"), commandLine.getSingleSwitch("encoding"), commandLine.getSingleSwitch("dtd-prefix"));
142         printer.setName(commandLine.getSingleSwitch("name"));
143         printer.setOldVersion(commandLine.getSingleSwitch("old-label"));
144         printer.setNewVersion(commandLine.getSingleSwitch("new-label"));
145         if (commandLine.isPresent("indent-text")) {
146             printer.setIndentText(commandLine.getSingleSwitch("indent-text"));
147         }
148         
149         Iterator i;
150
151         i = oldAPI.iterator();
152         while (i.hasNext()) {
153             line = (String JavaDoc) i.next();
154             if (!newAPI.contains(line)) {
155                 printer.remove(line);
156             }
157         }
158
159         i = newAPI.iterator();
160         while (i.hasNext()) {
161             line = (String JavaDoc) i.next();
162             if (!oldAPI.contains(line)) {
163                 printer.add(line);
164             }
165         }
166
167         Logger.getLogger(ListDiff.class).info("Printing results ...");
168         
169         PrintWriter out;
170         if (commandLine.isPresent("out")) {
171             out = new PrintWriter(new FileWriter(commandLine.getSingleSwitch("out")));
172         } else {
173             out = new PrintWriter(new OutputStreamWriter(System.out));
174         }
175         out.print(printer);
176         out.close();
177
178         Date end = new Date();
179
180         if (commandLine.getToggleSwitch("time")) {
181             System.err.println(ListDiff.class.getName() + ": " + ((end.getTime() - (double) start.getTime()) / 1000) + " secs.");
182         }
183     }
184 }
185
Popular Tags