001: package vqwiki.lex;
002:
003: import java.io.IOException;
004: import java.io.Reader;
005: import java.io.StringReader;
006: import java.util.List;
007:
008: import org.apache.log4j.Logger;
009: import vqwiki.Environment;
010:
011: /**
012: *
013: * Default lexer implementation for VQWiki.
014: *
015: */
016: public class DefaultWikiParser extends AbstractParser {
017:
018: private static final Logger logger = Logger
019: .getLogger(DefaultWikiParser.class);
020: private static final String PARSER_BUNDLE = "defaultParser";
021: private static final String PARSER_VERSION = "2.7.91";
022: private static final String PARSER_NAME = "Default Parser";
023:
024: /**
025: *
026: */
027: public DefaultWikiParser() {
028: super (
029: new ParserInfo(PARSER_NAME, PARSER_VERSION,
030: PARSER_BUNDLE));
031: }
032:
033: /**
034: *
035: */
036: protected DefaultWikiParser(ParserInfo parserInfo) {
037: super (parserInfo);
038: }
039:
040: /**
041: *
042: * Parse text for online display.
043: *
044: */
045: public String parseHTML(String rawtext, String virtualwiki)
046: throws IOException {
047: StringBuffer contents = new StringBuffer();
048: Reader raw = new StringReader(rawtext.toString());
049: contents = this .parseFormat(raw, virtualwiki);
050: raw = new StringReader(contents.toString());
051: contents = this .parseLayout(raw, virtualwiki);
052: raw = new StringReader(contents.toString());
053: contents = this .parseLinks(raw, virtualwiki);
054: // remove trailing returns at the end of the site.
055: return this .removeTrailingNewlines(contents.toString());
056: }
057:
058: /**
059: *
060: * Parse raw text and return something suitable for export
061: *
062: */
063: public String parseExportHTML(String rawtext, String virtualwiki)
064: throws IOException {
065: StringBuffer contents = new StringBuffer();
066: Reader raw = new StringReader(rawtext.toString());
067: contents = this .parseFormat(raw, virtualwiki);
068: contents = this .parseLayout(raw, virtualwiki);
069: raw = new StringReader(contents.toString());
070: contents = this .parseExportLinks(raw, virtualwiki);
071: // remove trailing returns at the end of the site.
072: return this .removeTrailingNewlines(contents.toString());
073: }
074:
075: /**
076: * Parse raw text and return a list of all found topic names.
077: */
078: public List getTopics(String rawtext, String virtualwiki)
079: throws Exception {
080: StringReader reader = new StringReader(rawtext);
081: BackLinkLex lexer = new BackLinkLex(reader);
082: while (lexer.yylex() != null) {
083: }
084: reader.close();
085: return lexer.getLinks();
086: }
087:
088: /**
089: *
090: *
091: *
092: */
093: private StringBuffer parseFormat(Reader raw, String virtualWiki)
094: throws IOException {
095: StringBuffer contents = new StringBuffer();
096: FormatLex lexer = new FormatLex(raw);
097: lexer.setVirtualWiki(virtualWiki);
098: boolean external = false;
099: String tag = null;
100: StringBuffer externalContents = null;
101: while (true) {
102: String line = null;
103: try {
104: line = lexer.yylex();
105: } catch (ArrayIndexOutOfBoundsException e) {
106: logger.debug(e);
107: }
108: logger.debug(line);
109: if (line == null) {
110: break;
111: }
112: if (line.startsWith("[<")) {
113: if (!external) {
114: external = true;
115: tag = line.substring(2, line.length() - 2);
116: logger.debug("External lex call (tag=" + tag + ")");
117: externalContents = new StringBuffer();
118: contents.append(line);
119: } else {
120: external = false;
121: String converted = LexExtender.getInstance()
122: .lexify(tag, externalContents.toString());
123: if (converted != null) {
124: contents.append(converted);
125: }
126: contents.append(line);
127: logger.debug("External ends");
128: }
129: } else {
130: if (!external) {
131: contents.append(line);
132: } else {
133: externalContents.append(line);
134: }
135: }
136: }
137: if (Environment.getInstance().isMakeToc()) {
138: contents = new StringBuffer(MakeTableOfContents
139: .addTableOfContents(contents.toString()));
140: }
141: return contents;
142: }
143:
144: /**
145: *
146: *
147: *
148: */
149: private StringBuffer parseLayout(Reader raw, String virtualWiki)
150: throws IOException {
151: LayoutLex lexer = new LayoutLex(raw);
152: lexer.setVirtualWiki(virtualWiki);
153: return this .lex(lexer);
154: }
155:
156: /**
157: *
158: *
159: *
160: */
161: private StringBuffer parseLinks(Reader raw, String virtualWiki)
162: throws IOException {
163: LinkLex lexer = new LinkLex(raw);
164: lexer.setVirtualWiki(virtualWiki);
165: return this .lex(lexer);
166: }
167:
168: /**
169: *
170: *
171: *
172: */
173: private StringBuffer parseExportLinks(Reader raw, String virtualWiki)
174: throws IOException {
175: HTMLLinkLex lexer = new HTMLLinkLex(raw);
176: lexer.setVirtualWiki(virtualWiki);
177: return this .lex(lexer);
178: }
179:
180: /**
181: *
182: *
183: *
184: */
185: private String removeTrailingNewlines(String content) {
186: // remove trailing returns at the end of the site.
187: // TODO better do this with StringBuffer, but actually no more
188: // time for a proper cleanup.
189: if (content.endsWith("<br/>\n")) {
190: content = content.substring(0, content.length() - 6);
191: while (content.endsWith("<br/>")) {
192: content = content.substring(0, content.length() - 5);
193: }
194: }
195: return content;
196: }
197:
198: /**
199: * Utility method for executing a lexer parse.
200: * FIXME - this is copy & pasted here and in MediaWikiParser
201: */
202: protected StringBuffer lex(Lexer lexer) throws IOException {
203: StringBuffer contents = new StringBuffer();
204: while (true) {
205: String line = lexer.yylex();
206: if (line == null) {
207: break;
208: }
209: contents.append(line);
210: }
211: return contents;
212: }
213: }
|