001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.dependencyfinder.cli;
034:
035: import java.io.*;
036: import java.util.*;
037:
038: import com.jeantessier.commandline.*;
039: import com.jeantessier.diff.*;
040:
041: public class ListDiff extends Command {
042: public ListDiff() throws CommandLineException {
043: super ("ListDiff");
044: }
045:
046: protected void showSpecificUsage(PrintStream out) {
047: out.println();
048: out.println("Defaults is text output to the console.");
049: out.println();
050: }
051:
052: public static void main(String[] args) throws Exception {
053: new ListDiff().run(args);
054: }
055:
056: protected void populateCommandLineSwitches() {
057: super .populateCommandLineSwitches();
058: populateCommandLineSwitchesForXMLOutput(
059: ListDiffPrinter.DEFAULT_ENCODING,
060: ListDiffPrinter.DEFAULT_DTD_PREFIX);
061:
062: getCommandLine().addSingleValueSwitch("name");
063: getCommandLine().addSingleValueSwitch("old-label");
064: getCommandLine().addSingleValueSwitch("old", true);
065: getCommandLine().addSingleValueSwitch("new-label");
066: getCommandLine().addSingleValueSwitch("new", true);
067: getCommandLine().addToggleSwitch("compress");
068: }
069:
070: protected void doProcessing() throws Exception {
071: String line;
072:
073: getVerboseListener().print("Loading old list ...");
074: Collection<String> oldAPI = new TreeSet<String>();
075: BufferedReader oldIn = new BufferedReader(new FileReader(
076: getCommandLine().getSingleSwitch("old")));
077: while ((line = oldIn.readLine()) != null) {
078: oldAPI.add(line);
079: }
080:
081: getVerboseListener().print("Loading new list ...");
082: Collection<String> newAPI = new TreeSet<String>();
083: BufferedReader newIn = new BufferedReader(new FileReader(
084: getCommandLine().getSingleSwitch("new")));
085: while ((line = newIn.readLine()) != null) {
086: newAPI.add(line);
087: }
088:
089: ListDiffPrinter printer = new ListDiffPrinter(getCommandLine()
090: .getToggleSwitch("compress"), getCommandLine()
091: .getSingleSwitch("encoding"), getCommandLine()
092: .getSingleSwitch("dtd-prefix"));
093: printer.setName(getCommandLine().getSingleSwitch("name"));
094: printer.setOldVersion(getCommandLine().getSingleSwitch(
095: "old-label"));
096: printer.setNewVersion(getCommandLine().getSingleSwitch(
097: "new-label"));
098: if (getCommandLine().isPresent("indent-text")) {
099: printer.setIndentText(getCommandLine().getSingleSwitch(
100: "indent-text"));
101: }
102:
103: getVerboseListener().print("Computing removed elements ...");
104: for (String name : oldAPI) {
105: if (!newAPI.contains(name)) {
106: printer.remove(name);
107: }
108: }
109:
110: getVerboseListener().print("Computing added elements ...");
111: for (String name : newAPI) {
112: if (!oldAPI.contains(name)) {
113: printer.add(name);
114: }
115: }
116:
117: getVerboseListener().print("Printing results ...");
118: out.print(printer);
119: }
120: }
|