package jdiff;
import java.io.*;
import java.util.*;
/**
* Class to generate colored differences between two sections of HTML text.
*
* See the file LICENSE.txt for copyright details.
* @author Matthew Doar, mdoar@pobox.com
*/
class Diff {
/**
* Save the differences between the two strings in a DiffOutput object
* for later use.
*
* @param id A per-package unique identifier for each documentation
* change.
*/
static String saveDocDiffs(String pkgName, String className,
String oldDoc, String newDoc,
String id, String title) {
// Generate the string which will link to this set of diffs
if (noDocDiffs)
return "Documentation changed from ";
if (oldDoc == null || newDoc == null) {
return "Documentation changed from ";
}
// Generate the differences.
generateDiffs(pkgName, className, oldDoc, newDoc, id, title);
return "Documentation changed from ";
}
/**
* Generate the differences.
*/
static void generateDiffs(String pkgName, String className,
String oldDoc, String newDoc,
String id, String title) {
String[] oldDocWords = parseDoc(oldDoc);
String[] newDocWords = parseDoc(newDoc);
DiffMyers diff = new DiffMyers(oldDocWords, newDocWords);
DiffMyers.change script = diff.diff_2(false);
script = mergeDiffs(oldDocWords, newDocWords, script);
String text = "" + title + "
";
// Generate the differences in blockquotes to cope with unterminated
// HTML tags
text += "
";
text = addDiffs(oldDocWords, newDocWords, script, text);
text += "
");
diffFile.println("This file contains all the changes in documentation in the package " + currPkgName + " as colored differences.");
if (deleteEffect == 0)
diffFile.println("Deletions are shown like this, and");
else if (deleteEffect == 1)
diffFile.println("Deletions are shown like this, and");
if (insertEffect == 0)
diffFile.println("additions are shown in red like this.");
else if (insertEffect == 1)
diffFile.println("additions are shown like this.");
diffFile.println("
");
diffFile.println("
");
diffFile.println("If no deletions or additions are shown in an entry, the HTML tags will be what has changed. The new HTML tags are shown in the differences. ");
diffFile.println("If no documentation existed, and then some was added in a later version, this change is noted in the appropriate class pages of differences, but the change is not shown on this page. Only changes in existing text are shown here. ");
diffFile.println("Similarly, documentation which was inherited from another class or interface is not shown here.");
diffFile.println("
");
diffFile.println("
");
diffFile.println(" Note that an HTML error in the new documentation may cause the display of other documentation changes to be presented incorrectly. For instance, failure to close a <code> tag will cause all subsequent paragraphs to be displayed differently.");
diffFile.println("
");
diffFile.println("");
diffFile.println();
} catch(IOException e) {
System.out.println("IO Error while attempting to create " + fullDiffFileName);
System.out.println("Error: " + e.getMessage());
System.exit(1);
}
} // if (currPkgName == null || currPkgName.compareTo(diffOutput.pkgName_) != 0)
// Now add the documentation difference text
diffFile.println(diffOutput.text_);
// Separate with a horizontal line
if (i != docDiffsArr.length - 1 &&
diffOutput.className_ != null &&
docDiffsArr[i+1].className_ != null &&
diffOutput.className_.compareTo(docDiffsArr[i+1].className_) != 0)
diffFile.println("");
// else
// diffFile.println("");
} // for (i = 0;
if (currPkgName != null)
closeDiffFile(); // Close the existing file
// Emit the single file which is the index to all documentation changes
emitDocDiffIndex(fullReportFileName, docDiffsArr);
}
/**
* Emit the single file which is the index to all documentation changes.
*/
public static void emitDocDiffIndex(String fullReportFileName,
DiffOutput[] docDiffsArr) {
String fullDiffFileName = fullReportFileName +
JDiff.DIR_SEP + diffFileName + "index" +
HTMLReportGenerator.reportFileExt;
// Create the output file
try {
FileOutputStream fos = new FileOutputStream(fullDiffFileName);
diffFile = new PrintWriter(fos);
// Write the HTML header
diffFile.println("");
diffFile.println("");
diffFile.println("");
diffFile.println("");
diffFile.println("");
diffFile.println("");
// diffFile.println("");
diffFile.println("");
diffFile.println("");
diffFile.println("");
diffFile.println("");
diffFile.println("All Documentation Differences");
diffFile.println("");
diffFile.println("");
diffFile.println("");
// Write the navigation bar
diffFile.println("");
diffFile.println("
");
diffFile.println("
");
diffFile.println("
");
diffFile.println("
");
diffFile.println("
");
// Always have a link to the Javadoc files
diffFile.println("
");
diffFile.println();
// For each package and class, add the first DiffOutput to
// the hash table. Used when generating navigation bars.
boolean firstPackage = true; // Set for the first package
boolean firstClass = true; // Set for first class in a package
boolean firstCtor = true; // Set for first ctor in a class
boolean firstMethod = true; // Set for first method in a class
boolean firstField = true; // Set for first field in a class
for (int i = 0; i < docDiffsArr.length; i++) {
DiffOutput diffOutput = docDiffsArr[i];
String link = "";
// See if the package name changed
if (firstPackage || diffOutput.pkgName_.compareTo(docDiffsArr[i-1].pkgName_) != 0) {
if (firstPackage) {
firstPackage = false;
} else {
diffFile.println(" ");
}
firstClass = true;
firstCtor = true;
firstMethod = true;
firstField = true;
String id = diffOutput.pkgName_ + "!package";
firstDiffOutput.put(id, id);
if (diffOutput.className_ == null) {
diffFile.println("" + link + "Package " + diffOutput.pkgName_ + " ");
} else {
diffFile.println("" + "Package " + diffOutput.pkgName_ + " ");
}
}
// See if the class name changed
if (diffOutput.className_ != null &&
(firstClass ||
diffOutput.className_.compareTo(docDiffsArr[i-1].className_) != 0)) {
if (firstClass) {
firstClass = false;
} else {
diffFile.println(" ");
}
firstCtor = true;
firstMethod = true;
firstField = true;
String id = diffOutput.pkgName_ + "." + diffOutput.className_ + "!class";
firstDiffOutput.put(id, id);
if (diffOutput.id_.endsWith("!class")) {
diffFile.println(" Class " + link + diffOutput.className_ + " ");
} else {
diffFile.println(" Class " + diffOutput.className_ + " ");
}
}
// Work out what kind of member this is, and
// display it appropriately
if (diffOutput.className_ != null &&
!diffOutput.id_.endsWith("!class")) {
int ctorIdx = diffOutput.id_.indexOf(".ctor");
if (ctorIdx != -1) {
diffFile.println(" " + link + diffOutput.className_ + diffOutput.id_.substring(ctorIdx + 5) + " ");
} else {
int methodIdx = diffOutput.id_.indexOf(".dmethod.");
if (methodIdx != -1) {
diffFile.println(" " + "Method " + link + diffOutput.id_.substring(methodIdx + 9) + " ");
} else {
int fieldIdx = diffOutput.id_.indexOf(".field.");
if (fieldIdx != -1) {
diffFile.println(" " + "Field " + link + diffOutput.id_.substring(fieldIdx + 7) + " ");
}
} //if (methodIdx != -1)
} //if (ctorIdx != -1)
} //diffOutput.className_ != null
}
} catch(IOException e) {
System.out.println("IO Error while attempting to create " + fullDiffFileName);
System.out.println("Error: " + e.getMessage());
System.exit(1);
}
closeDiffFile();
}
/**
* Emit the HTML footer and close the diff file.
*/
public static void closeDiffFile() {
if (diffFile != null) {
// Write the HTML footer
diffFile.println();
diffFile.println("");
diffFile.println("");
diffFile.close();
}
}
/**
* Current file where documentation differences are written as colored
* differences.
*/
public static PrintWriter diffFile = null;
/**
* Base name of the current file where documentation differences are
* written as colored differences.
*/
public static String diffFileName = "docdiffs_";
/**
* The name of the current package, used to create diffFileName.
*/
private static String currPkgName = null;
/**
* If set, then do not generate colored diffs for documentation.
* Default is true.
*/
public static boolean noDocDiffs = true;
/**
* Define the type of emphasis for deleted words.
* 0 strikes the words through.
* 1 outlines the words in light grey.
*/
public static int deleteEffect = 0;
/**
* Define the type of emphasis for inserted words.
* 0 colors the words red.
* 1 outlines the words in yellow, like a highlighter.
*/
public static int insertEffect = 1;
/**
* For each package and class, the first DiffOutput is added to
* this hash table. Used when generating navigation bars.
*/
public static Hashtable firstDiffOutput = new Hashtable();
/**
* If set, then show changes in implementation-related modifiers such as
* native and synchronized. For more information, see
* https://java.sun.com/j2se/1.4.1/docs/tooldocs/solaris/javadoc.html#generatedapideclarations
*/
public static boolean showAllChanges = false;
/** The list of documentation differences. */
private static List docDiffs = new ArrayList(); // DiffOutput[]
/** Set to enable increased logging verbosity for debugging. */
private static boolean trace = false;
}