import java.io.*; import java.util.*; //import Question; //import Answer; /** * This class represents one item from a glossary which is * a kind of dictionary. An item will contain a glossary term * as well as its definition, although the definition may * be missing. * * @author matth3wbishopyahoo!com * @see Glossary, GlossaryTerm, Definition */ public class GlossaryItem extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- private boolean isGood; //-------------------------------------------- private String errors; //-------------------------------------------- private GlossaryTerm term; //-------------------------------------------- private Definition definition; //-------------------------------------------- public static String INDICATOR = GlossaryTerm.INDICATOR; //-------------------------------------------- public static String TERMINATOR = GlossaryTerm.INDICATOR; //-------------------------------------------- public GlossaryItem() { this.errors = ""; this.term = new GlossaryTerm(); this.definition = new Definition(); } //-------------------------------------------- public GlossaryItem(String sText) { this(); this.loadData(sText); } //-------------------------------------------- public GlossaryTerm getTerm() { return this.term; } //-------------------------------------------- public Definition getDefinition() { return this.definition; } //-------------------------------------------- public void setDefinition(String sDefinitionText) { this.definition = new Definition(sDefinitionText); } //-------------------------------------------- public void loadData(String sText) { StringReader srText = new StringReader(sText); boolean bDebug = false; this.isGood = this.isGlossaryItem(sText); if (!this.isGood) { return; } int iCurrentCharacter = 0; StringBuffer sbText = new StringBuffer(""); while (iCurrentCharacter != -1) { sbText.append((char)iCurrentCharacter); if (bDebug) { System.out.println("sbText=" + sbText); } if (GlossaryTerm.isGlossaryTerm(sbText.toString())) { if (bDebug) { System.out.println("found term"); } this.term = new GlossaryTerm(sbText.toString()); //-- a glossary item may be terminated either by //-- the next item or by the end of the glossary //-- //-- if (sText.trim().endsWith(GlossaryTerm.INDICATOR)) { //-- remove suffix this.definition = new Definition(sText.trim().substring( sbText.toString().length(), sText.trim().length() - GlossaryTerm.INDICATOR.length())); } else { this.definition = new Definition(sText.trim().substring( sbText.toString().length(), sText.trim().length() - Glossary.TERMINATOR.length())); } sbText.setLength(0); return; } try { iCurrentCharacter = srText.read(); } catch (IOException e) { this.errors = "An IO Exception occurred while reading " + e; return; } } //-- while srText.close(); } //-- m: loadData() //-------------------------------------------- public static boolean recognize(String sText) { if (!sText.endsWith(GlossaryItem.INDICATOR)) { return false; } if (sText.endsWith("'" + GlossaryItem.INDICATOR)) { return false; } return true; } //-------------------------------------------- public static String returnEndToken(String sText) { return GlossaryTerm.INDICATOR; } //-------------------------------------------- /** checks if this looks like an GlossaryItem */ public static boolean isGlossaryItem(String sText) { if (sText.trim().length() == 0) { return false; } if (!sText.trim().startsWith(GlossaryTerm.INDICATOR)) { return false; } if (sText.trim().equals(GlossaryTerm.INDICATOR)) { return false; } if (!sText.trim().endsWith(GlossaryTerm.INDICATOR) && !sText.trim().endsWith(Glossary.TERMINATOR)) { return false; } return true; } //-------------------------------------------- /** checks if there is an anwer */ public boolean hasDefinition() { if (this.definition == null) { return false; } /* if (this.definition.getContents().trim().length() == 0) { return false; } */ return true; } //-------------------------------------------- public String toString() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(this.term.toString()); sbReturn.append(NEWLINE); sbReturn.append(this.definition.toString()); sbReturn.append(NEWLINE); return sbReturn.toString(); } //-------------------------------------------- public String printHtml() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(this.term.printHtml()); sbReturn.append(NEWLINE); sbReturn.append(this.definition.printHtml()); return sbReturn.toString(); } //-------------------------------------------- /** prints html suitable for an indexed faq document. * An index is a list of the GlossaryTerms at the top of the * faq. */ public String printIndexedHtml(int iIndex) { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(this.term.printIndexedHtml(iIndex)); sbReturn.append(NEWLINE); sbReturn.append(this.definition.printHtml()); return sbReturn.toString(); } //-------------------------------------------- public String printDocBook() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append(this.term.printDocBook()); sbReturn.append(NEWLINE); sbReturn.append(this.definition.printDocBook()); sbReturn.append(NEWLINE); sbReturn.append(""); return sbReturn.toString(); } //-------------------------------------------- public String printReport() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append("GlossaryTerm :"); sbReturn.append(this.term.getContents()); sbReturn.append(NEWLINE); sbReturn.append("Definition :"); sbReturn.append(this.definition.getContents()); sbReturn.append(NEWLINE); sbReturn.append("start token :"); sbReturn.append(this.INDICATOR); sbReturn.append(NEWLINE); sbReturn.append("end token :"); sbReturn.append(this.TERMINATOR); sbReturn.append(NEWLINE); sbReturn.append("Errors :"); sbReturn.append(this.errors); sbReturn.append(NEWLINE); return sbReturn.toString(); } //-------------------------------------------- public String print() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(""); return sbReturn.toString(); } //-------------------------------------------- /** a main method for testing */ public static void main(String[] args) throws Exception { StringBuffer sbUsageMessage = new StringBuffer(""); sbUsageMessage.append("test usage: java GlossaryItem ."); sbUsageMessage.append(NEWLINE); sbUsageMessage.append(" tests from standard in"); sbUsageMessage.append(NEWLINE); if (args.length == 0) { System.out.println(sbUsageMessage); System.exit(-1); } StringBuffer sbTest = new StringBuffer(""); sbTest.append(" + an enhancement; "); sbTest.append(NEWLINE); sbTest.append(" a deliberate sabotage of standards"); sbTest.append(NEWLINE); sbTest.append(" +"); sbTest.append(NEWLINE); System.out.println("Using data:"); System.out.println(NEWLINE); System.out.println(sbTest); GlossaryItem rTest = new GlossaryItem(sbTest.toString()); System.out.println(".printHtml()"); System.out.println(rTest.printHtml()); System.out.println(".printDocBook()"); System.out.println(rTest.printDocBook()); System.out.println(".toString()"); System.out.println(rTest.toString()); System.out.println(".printReport()"); System.out.println(rTest.printReport()); } //-- main() } //-- GlossaryItem class