import java.io.*; import java.util.*; //import BufferLoad; //import TextLink; //import PlainText; //import EndText; //import DocumentElement; //import PlainList; //import BlankLine; /** * This class represents some text which contains linked * text as well as ordinary text. The text can also contain * blank lines, example blocks which are indicated by text * such as 'for example:', * * * @see PlainText, TextLink * @author http://bumble.sf.net */ public class MixedText extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- private boolean isGood; //-------------------------------------------- private String errors; //-------------------------------------------- private Vector textElements; //-------------------------------------------- public MixedText() { this.errors = ""; this.errors = ""; this.textElements = new Vector(); } //-------------------------------------------- public MixedText(String sText) { this(); this.loadData(sText); } //-------------------------------------------- public int countElements() { return this.textElements.size(); } //-------------------------------------------- public void loadData(String sText) { this.isGood = this.isMixedText(sText); if (!this.isGood) { return; } StringReader srText = new StringReader(sText); String sTextLinkStart = ""; String sContent = ""; String sToken = ""; boolean bDebug = false; boolean bInExampleBlock = false; int iCurrentCharacter = 0; StringBuffer sbText = new StringBuffer(""); while (iCurrentCharacter != -1) { sbText.append((char)iCurrentCharacter); if (bDebug) { System.out.println("sbText=" + sbText); } if (TextLink.recognize(sbText.toString())) { //-- remove the link suffix if (bDebug) { System.out.println(""); } //-- I dont want to link links if they are in example //-- blocks //-- if (!bInExampleBlock) { sToken = TextLink.startToken(sbText.toString()); sContent = EndText.removeSuffix(sbText.toString(), sToken); this.textElements.addElement(new PlainText(sContent)); sbText.setLength(0); sbText.append(sToken); } } if (Example.recognize(sbText.toString())) { //-- remove the example indicator ("for example:" etc) if (bDebug) { System.out.println(""); } sToken = Example.startToken(sbText.toString()); sContent = EndText.removeSuffix(sbText.toString(), sToken); this.textElements.addElement(new PlainText(sContent)); sbText.setLength(0); sbText.append(sToken); bInExampleBlock = true; } if (BlankLine.recognize(sbText.toString())) { if (bDebug) { System.out.println(""); } //-- this test should or could be outside the //-- recognize test. //-- if (!bInExampleBlock) { this.textElements.addElement( new PlainText(sbText.toString().trim())); this.textElements.addElement(new BlankLine()); sbText.setLength(0); } } //-- if blank line if (Example.isExample(sbText.toString())) { if (bDebug) { System.out.println(""); } this.textElements .addElement(new Example(sbText.toString())); sbText.setLength(0); bInExampleBlock = false; } //-- if example if (TextLink.isTextLink(sbText.toString())) { if (bDebug) { System.out.println(""); } if (!bInExampleBlock) { this.textElements .addElement(new TextLink(sbText.toString())); //-- stop the text link from swallowing the end //-- character delimiter. //-- sToken = TextLink.returnEndToken(sbText.toString()); sbText.setLength(0); sbText.append(sToken); } } //-- if text link try { iCurrentCharacter = srText.read(); } catch (IOException e) { this.errors = "An IO Exception occurred while reading " + e; return; } } //-- while more characters if (TextLink.isTextLink(sbText.toString())) { if (bDebug) { System.out.println(""); } this.textElements .addElement(new TextLink(sbText.toString())); sbText.setLength(0); } else if (Example.isExample(sbText.toString())) { if (bDebug) { System.out.println(""); } this.textElements .addElement(new Example(sbText.toString())); sbText.setLength(0); } else { if (bDebug) { System.out.println(""); } this.textElements .addElement(new PlainText(sbText.toString())); sbText.setLength(0); } srText.close(); } //-- m: loadData() //-------------------------------------------- /** mixed text does not really have any structure */ public static boolean isMixedText(String sText) { return true; } //-------------------------------------------- public String toString() { StringBuffer sbReturn = new StringBuffer(""); DocumentElement deCurrent; Enumeration ee = this.textElements.elements(); while (ee.hasMoreElements()) { deCurrent = (DocumentElement)ee.nextElement(); sbReturn.append(deCurrent.toString()); } sbReturn.append(NEWLINE); return sbReturn.toString(); } //-------------------------------------------- public String printHtml() { StringBuffer sbReturn = new StringBuffer(""); DocumentElement deCurrent; Enumeration ee = this.textElements.elements(); while (ee.hasMoreElements()) { deCurrent = (DocumentElement)ee.nextElement(); sbReturn.append(deCurrent.printHtml()); } return sbReturn.toString(); } //-------------------------------------------- public String printDocBook() { StringBuffer sbReturn = new StringBuffer(""); DocumentElement deCurrent; Enumeration ee = this.textElements.elements(); while (ee.hasMoreElements()) { deCurrent = (DocumentElement)ee.nextElement(); sbReturn.append(deCurrent.printDocBook()); } return sbReturn.toString(); } //-------------------------------------------- public String printReport() { StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(""); sbReturn.append(NEWLINE); sbReturn.append("Number of elements>"); sbReturn.append(this.countElements()); 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 MixedText text-file [show]"); sbUsageMessage.append(NEWLINE); sbUsageMessage.append(""); sbUsageMessage.append(NEWLINE); if (args.length == 0) { System.out.println(sbUsageMessage); System.exit(-1); } StringBuffer sbTest = new StringBuffer(""); //-- show all data, not just the html. if (args.length == 2) { System.out.println("Using data"); System.out.println(BufferLoad.loadBuffer(args[0])); } MixedText document = new MixedText(BufferLoad.loadBuffer(args[0])); if (args.length == 2) { System.out.println(".toString()"); System.out.println(document.toString()); System.out.println(".printReport()"); System.out.println(document.printReport()); } System.out.println(".printHtml()"); System.out.println(document.printHtml()); } //-- main() } //-- FAQ class