import java.io.*; import java.util.*; //import BufferLoad; //import Footnote, FootnoteID, FootnoteContents. // /** * A footnote section is the section of the document which * contains the footnotes. In my document format this is * at the end of the document and nothing is likely to * to come after the footnotes. Each footnote is begun * with some kind of identifier which could be a number * or some text. * * @see * @author http://bumble.sf.net */ public class FootnoteSection extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- private boolean isGood; //-------------------------------------------- private String errors; //-------------------------------------------- private String title; //-------------------------------------------- private Vector footnotes; //-------------------------------------------- private Date startTime; //-------------------------------------------- private Date finishTime; //-------------------------------------------- public static String INDICATOR = "Footnotes: "; //-------------------------------------------- public static String TERMINATOR = ""; public FootnoteSection() { this.errors = ""; this.footnotes = new Vector(); this.title = ""; } //-------------------------------------------- public FootnoteSection(String sText) { this(); this.loadData(sText); } //-------------------------------------------- /** return the number of milliseconds taken to load. */ public long getLoadDuration() { if (this.finishTime == null) { return 0; } if (this.startTime == null) { return 0; } return this.finishTime.getTime() - this.startTime.getTime(); } //-------------------------------------------- public void setTitle(String sTitle) { this.title = sTitle; } //-------------------------------------------- public String getTitle() { return this.title; } //-------------------------------------------- public static boolean recognize(String sText) { if (!sText.endsWith(FootnoteSection.INDICATOR)) { return false; } //-- allow a single quote character to escape the //-- FootnoteSection indicator. //-- if (sText.endsWith("'" + FootnoteSection.INDICATOR)) { return false; } return true; } //-------------------------------------------- public void loadData(String sText) { this.startTime = new Date(); this.isGood = this.isFootnoteSection(sText); if (!this.isGood) { this.finishTime = new Date(); return; } String sFootnoteSectionContents = sText.trim().substring(FootnoteSection.INDICATOR.length()); StringReader srText = new StringReader(sFootnoteSectionContents); int iCurrentCharacter = 0; StringBuffer sbText = new StringBuffer(""); boolean bDebug = false; while (iCurrentCharacter != -1) { sbText.append((char)iCurrentCharacter); if (bDebug) { System.out.println("sbText=" + sbText); } if (FootnoteSectionItem.isFootnote(sbText.toString())) { if (bDebug) { System.out.println(""); } this.footnotes.addElement(new Footnote(sbText.toString())); //-- //-- push the FootnoteSection term start token back on the //-- buffer since we use it as a delimiter //-- sbText.setLength(0); sbText.append(FootnoteID.INDICATOR); } //-- if Footnote if (FootnoteID.recognize(sbText.toString())) { if (bDebug) { System.out.println(""); } sbText.setLength(0); sbText.append(FootnoteID.INDICATOR); } try { iCurrentCharacter = srText.read(); } catch (IOException e) { this.errors = "An IO Exception occurred while reading " + e; this.finishTime = new Date(); return; } } //-- while srText.close(); this.finishTime = new Date(); } //-- m: loadData() //-------------------------------------------- /** checks if this looks like a FootnoteSection */ public static boolean isFootnoteSection(String sText) { if (sText.trim().length() == 0) { return false; } if (!sText.trim().startsWith(FootnoteSection.INDICATOR)) { return false; } // if (!sText.trim().endsWith(FootnoteSection.TERMINATOR)) // { return false; } return true; } //-------------------------------------------- /** is there any data */ public boolean isEmpty() { if (this.footnotes.size() == 0) { return true; } else { return false; } } //-------------------------------------------- /** how many items are there */ public int countItems() { return this.footnotes.size(); } //-------------------------------------------- /** how many Footnotes are there */ public int countFootnotes() { int iAnswers = 0; Footnote fnCurrent = new Footnote(); Enumeration ii = footnotes.elements(); while (ii.hasMoreElements()) { fnCurrent = (Footnote)ii.nextElement(); if (fnCurrent.hasFootnote()) { iAnswers++; } } return iAnswers; } //-------------------------------------------- public String toString() { StringBuffer sbReturn = new StringBuffer(""); Footnote fnCurrent = new Footnote(); sbReturn.append(FootnoteSection.INDICATOR); sbReturn.append(NEWLINE); Enumeration ii = footnotes.elements(); while (ii.hasMoreElements()) { fnCurrent = (Footnote)ii.nextElement(); sbReturn.append(fnCurrent.toString()); sbReturn.append(NEWLINE); } sbReturn.append(FootnoteSection.TERMINATOR); return sbReturn.toString(); } //-------------------------------------------- public String printHtml() { return this.printHtml(true); } //-------------------------------------------- public String printHtml(boolean bIncludeIndex) { StringBuffer sbReturn = new StringBuffer(""); Enumeration ii; Footnote fnCurrent = new Footnote(); int iIndex; iIndex = 1; if (!this.title.trim().equals("")) { sbReturn.append("

"); sbReturn.append(this.title); sbReturn.append("

"); sbReturn.append(NEWLINE); } sbReturn.append(""); sbReturn.append(NEWLINE); if (bIncludeIndex) { sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(NEWLINE); } //-- if sbReturn.append("
"); sbReturn.append(NEWLINE); iIndex = 1; ii = footnotes.elements(); while (ii.hasMoreElements()) { fnCurrent = (Footnote)ii.nextElement(); sbReturn.append(NEWLINE); if (bIncludeIndex) { sbReturn.append(fnCurrent.printIndexedHtml(iIndex)); } else { sbReturn.append(fnCurrent.printHtml()); } sbReturn.append(NEWLINE); iIndex++; } sbReturn.append("
"); sbReturn.append(NEWLINE); sbReturn.append("(top)"); return sbReturn.toString(); } //-------------------------------------------- public String printDocBook() { StringBuffer sbReturn = new StringBuffer(""); Enumeration ii; Footnote fnCurrent = new Footnote(); int iIndex; iIndex = 1; if (!this.title.trim().equals("")) { //--sbReturn.append(this.title.printDocBook()); sbReturn.append(NEWLINE); } sbReturn.append(NEWLINE); sbReturn.append("
"); sbReturn.append(NEWLINE); iIndex = 1; ii = footnotes.elements(); while (ii.hasMoreElements()) { fnCurrent = (Footnote)ii.nextElement(); sbReturn.append(NEWLINE); sbReturn.append(fnCurrent.printDocBook()); sbReturn.append(NEWLINE); iIndex++; } sbReturn.append(NEWLINE); sbReturn.append("
"); return sbReturn.toString(); } //-------------------------------------------- public String printReport() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append("Footnote section title >"); sbReturn.append(this.getTitle()); sbReturn.append(NEWLINE); sbReturn.append("Number of items >"); sbReturn.append(this.countItems()); sbReturn.append(NEWLINE); sbReturn.append("Number of footnotes>"); sbReturn.append(this.countFootnotes()); sbReturn.append(NEWLINE); sbReturn.append("Load time >"); sbReturn.append(this.getLoadDuration()); sbReturn.append(NEWLINE); sbReturn.append("last data good >"); sbReturn.append(this.isGood); sbReturn.append(NEWLINE); sbReturn.append("FootnoteSection is empty >"); sbReturn.append(this.isEmpty()); sbReturn.append(NEWLINE); sbReturn.append("error messages >"); sbReturn.append(this.errors); sbReturn.append(NEWLINE); return sbReturn.toString(); } //-------------------------------------------- public String print() { StringBuffer sbReturn = new StringBuffer(""); return this.toString(); } //-------------------------------------------- /** a main method for testing */ public static void main(String[] args) throws Exception { StringBuffer sbUsageMessage = new StringBuffer(""); sbUsageMessage.append("test usage: java FootnoteSection FootnoteSection-file [all]"); sbUsageMessage.append(NEWLINE); if (args.length == 0) { System.out.println(sbUsageMessage); System.exit(-1); } StringBuffer sbTest = new StringBuffer(""); sbTest.append(NEWLINE); if (args.length == 2) { System.out.println("Using data"); System.out.println(NEWLINE); System.out.println(BufferLoad.loadBuffer(args[0])); } FootnoteSection footTest = new FootnoteSection(BufferLoad.loadBuffer(args[0])); System.out.println(".printHtml()"); System.out.println(footTest.printHtml()); System.out.println(".printDocBook()"); System.out.println(footTest.printDocBook()); if (args.length == 2) { System.out.println(NEWLINE); System.out.println(".toString()"); System.out.println(footTest.toString()); System.out.println(".printReport()"); System.out.println(footTest.printReport()); } } //-- main() } //-- FootnoteSection class