001: package vqwiki.lex;
002:
003: import java.io.IOException;
004:
005: import java.io.Reader;
006:
007: import java.io.StringReader;
008:
009: import java.util.List;
010: import org.apache.log4j.Logger;
011: import vqwiki.Environment;
012:
013: /**
014: * Parser used to implement MediaWiki syntax.
015: */
016: public class MediaWikiParser extends AbstractParser {
017:
018: private static final Logger logger = Logger
019: .getLogger(MediaWikiParser.class);
020: private static final String PARSER_BUNDLE = "mediaWikiParser";
021: private static final String PARSER_VERSION = "1.0";
022: private static final String PARSER_NAME = "MediaWiki Parser";
023:
024: private MakeTableOfContents toc = new MakeTableOfContents();
025:
026: /**
027: *
028: */
029: public MediaWikiParser() {
030: super (
031: new ParserInfo(PARSER_NAME, PARSER_VERSION,
032: PARSER_BUNDLE));
033: }
034:
035: /**
036: * Parse raw text and return a list of all found topic names.
037: */
038: public List getTopics(String rawtext, String virtualwiki)
039: throws Exception {
040: StringReader reader = new StringReader(rawtext);
041: BackLinkLex lexer = new BackLinkLex(reader);
042: while (lexer.yylex() != null) {
043: }
044: reader.close();
045: return lexer.getLinks();
046: }
047:
048: /**
049: * Utility method for executing a lexer parse.
050: * FIXME - this is copy & pasted here and in DefaultWikiParser
051: */
052: protected StringBuffer lex(Lexer lexer) throws IOException {
053: StringBuffer contents = new StringBuffer();
054: while (true) {
055: String line = lexer.yylex();
056: if (line == null) {
057: break;
058: }
059: contents.append(line);
060: }
061: return contents;
062: }
063:
064: /**
065: * Parse text for online display.
066: */
067: public String parseHTML(String rawtext, String virtualwiki)
068: throws IOException {
069: StringBuffer contents = new StringBuffer();
070: Reader raw = new StringReader(rawtext.toString());
071: contents = this .parseSyntax(raw, virtualwiki);
072: raw = new StringReader(contents.toString());
073: contents = this .parseParagraphs(raw, virtualwiki);
074: contents = MakeTableOfContents.addTableOfContents(this .toc,
075: contents);
076: return contents.toString();
077: }
078:
079: /**
080: * Parse raw text and return something suitable for export
081: */
082: public String parseExportHTML(String rawtext, String virtualWiki)
083: throws IOException {
084: // this won't work yet - need to allow parsing differently if
085: // external specified to syntax lexer
086: StringBuffer contents = new StringBuffer();
087: Reader raw = new StringReader(rawtext.toString());
088: contents = this .parseSyntax(raw, virtualWiki);
089: raw = new StringReader(contents.toString());
090: contents = this .parseParagraphs(raw, virtualWiki);
091: contents = MakeTableOfContents.addTableOfContents(this .toc,
092: contents);
093: return contents.toString();
094: }
095:
096: /**
097: *
098: */
099: private StringBuffer parseSyntax(Reader raw, String virtualWiki)
100: throws IOException {
101: MediaWikiSyntax lexer = new MediaWikiSyntax(raw);
102: lexer.setVirtualWiki(virtualWiki);
103: lexer.setTOC(this .toc);
104: return this .lex(lexer);
105: }
106:
107: /**
108: *
109: */
110: private StringBuffer parseParagraphs(Reader raw, String virtualWiki)
111: throws IOException {
112: MediaWikiHTML lexer = new MediaWikiHTML(raw);
113: lexer.setVirtualWiki(virtualWiki);
114: return this.lex(lexer);
115: }
116: }
|