import java.io.*; import java.util.*; //import BufferLoad; //import ProcedureStep; /** * This type is a section of text which explains how * to do something using a series of steps to do the * explaining. * * @see * @author http://bumble.sf.net */ public class HowTo extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- private boolean isGood; //-------------------------------------------- private String errors; //-------------------------------------------- private String title; //-------------------------------------------- private Vector steps; //-------------------------------------------- private Date startTime; //-------------------------------------------- private Date finishTime; //-------------------------------------------- public static String INDICATOR = "[howto]"; //-------------------------------------------- public static String TERMINATOR = "[/howto]"; public HowTo() { this.errors = ""; this.steps = new Vector(); this.title = ""; } //-------------------------------------------- public HowTo(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(HowTo.INDICATOR)) { return false; } //-- allow a single quote character to escape the //-- HowTo indicator. //-- if (sText.endsWith("'" + HowTo.INDICATOR)) { return false; } return true; } //-------------------------------------------- public void loadData(String sText) { this.startTime = new Date(); this.isGood = this.isHowTo(sText); if (!this.isGood) { this.finishTime = new Date(); return; } String sHowToContents = sText.trim().substring(HowTo.INDICATOR.length()); StringReader srText = new StringReader(sHowToContents); int iCurrentCharacter = 0; StringBuffer sbText = new StringBuffer(""); while (iCurrentCharacter != -1) { sbText.append((char)iCurrentCharacter); //-- System.out.println("sbText=" + sbText); if (ProcedureStep.isProcedureStep(sbText.toString())) { //-- System.out.println(""); this.steps.addElement(new ProcedureStep(sbText.toString())); sbText.setLength(0); sbText.append(ProcedureStep.INDICATOR); } //-- if Step if (ProcedureStep.recognize(sbText.toString())) { sbText.setLength(0); sbText.append(ProcedureStep.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 an HowTo */ public static boolean isHowTo(String sText) { if (sText.trim().length() == 0) { return false; } if (!sText.trim().startsWith(HowTo.INDICATOR)) { return false; } if (!sText.trim().endsWith(HowTo.TERMINATOR)) { return false; } //-- allow a single quote to escape the HowTo //-- terminator tag. if (sText.trim().endsWith("'" + HowTo.TERMINATOR)) { return false; } return true; } //-------------------------------------------- /** is there any data */ public boolean isEmpty() { if (this.steps.size() == 0) { return true; } else { return false; } } //-------------------------------------------- /** how many question and answers are there */ public int countItems() { return this.steps.size(); } //-------------------------------------------- /** how many answers are there */ public int countSteps() { return this.steps.size(); /* -- int iSteps = 0; Step stepCurrent = new Step(); Enumeration ii = steps.elements(); while (ii.hasMoreElements()) { stepCurrent = (Step)ii.nextElement(); iSteps++; } return iSteps; */ } //-------------------------------------------- public String toString() { StringBuffer sbReturn = new StringBuffer(""); ProcedureStep stepCurrent = new ProcedureStep(); sbReturn.append(HowTo.INDICATOR); sbReturn.append(NEWLINE); Enumeration ii = steps.elements(); while (ii.hasMoreElements()) { stepCurrent = (ProcedureStep)ii.nextElement(); sbReturn.append(stepCurrent.toString()); sbReturn.append(NEWLINE); } sbReturn.append(HowTo.TERMINATOR); return sbReturn.toString(); } //-------------------------------------------- public String printHtml() { StringBuffer sbReturn = new StringBuffer(""); return this.printHtml(true); } //-------------------------------------------- public String printHtml(boolean bIncludeIndex) { StringBuffer sbReturn = new StringBuffer(""); Enumeration ii; ProcedureStep stepCurrent = new ProcedureStep(); 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 = steps.elements(); while (ii.hasMoreElements()) { stepCurrent = (ProcedureStep)ii.nextElement(); sbReturn.append(NEWLINE); if (bIncludeIndex) { sbReturn.append(stepCurrent.printIndexedHtml(iIndex)); //sbReturn.append(""); //sbReturn.append("(*)"); } else { sbReturn.append(stepCurrent.printHtml()); } sbReturn.append(NEWLINE); sbReturn.append(NEWLINE); iIndex++; } sbReturn.append("
"); sbReturn.append(NEWLINE); sbReturn.append("(top)"); return sbReturn.toString(); } //-------------------------------------------- public String printDocBook() { StringBuffer sbReturn = new StringBuffer(""); Enumeration ii; ProcedureStep stepCurrent = new ProcedureStep(); int iIndex; iIndex = 1; if (!this.title.trim().equals("")) { //sbReturn.append("

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

"); //sbReturn.append(NEWLINE); } sbReturn.append(NEWLINE); sbReturn.append(""); sbReturn.append(NEWLINE); iIndex = 1; ii = steps.elements(); while (ii.hasMoreElements()) { stepCurrent = (ProcedureStep)ii.nextElement(); sbReturn.append(NEWLINE); sbReturn.append(stepCurrent.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("HowTo title >"); sbReturn.append(this.getTitle()); sbReturn.append(NEWLINE); sbReturn.append("Number of items >"); sbReturn.append(this.countItems()); sbReturn.append(NEWLINE); sbReturn.append("Number of answers>"); sbReturn.append(this.countSteps()); 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("HowTo 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 HowTo HowTo-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])); } HowTo HowToTest = new HowTo(BufferLoad.loadBuffer(args[0])); System.out.println(HowToTest.printHtml()); if (args.length == 2) { System.out.println(NEWLINE); System.out.println(HowToTest.toString()); System.out.println(NEWLINE); System.out.println(HowToTest.printReport()); } } //-- main() } //-- HowTo class