001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.htmltowiki;
021:
022: import java.io.IOException;
023: import java.io.Writer;
024: import java.util.regex.Matcher;
025: import java.util.regex.Pattern;
026:
027: /**
028: * Part of the XHtmlToWikiTranslator
029: *
030: * @author Sebastian Baltes (sbaltes@gmx.com)
031: */
032: public class WhitespaceTrimWriter extends Writer {
033:
034: private StringBuffer m_result = new StringBuffer();
035:
036: private StringBuffer m_buffer = new StringBuffer();
037:
038: private boolean m_trimMode = true;
039:
040: private static final Pattern ONLINE_PATTERN = Pattern.compile(
041: ".*?\\n\\s*?", Pattern.MULTILINE);
042:
043: private boolean m_currentlyOnLineBegin = true;
044:
045: public void flush() {
046: if (m_buffer.length() > 0) {
047: String s = m_buffer.toString();
048: s = s.replaceAll("\r\n", "\n");
049: if (m_trimMode) {
050: s = s.replaceAll(
051: "(\\w+) \\[\\?\\|Edit\\.jsp\\?page=\\1\\]",
052: "[$1]");
053: s = s.replaceAll("\n{2,}", "\n\n");
054: s = s.replaceAll("\\p{Blank}+", " ");
055: s = s.replaceAll("[ ]*\n[ ]*", "\n");
056: s = replacePluginNewlineBackslashes(s);
057: }
058: m_result.append(s);
059: m_buffer = new StringBuffer();
060: }
061: }
062:
063: private String replacePluginNewlineBackslashes(String s) {
064: Pattern p = Pattern
065: .compile(
066: "\\{\\{\\{(.*?)\\}\\}\\}|\\{\\{(.*?)\\}\\}|\\[\\{(.*?)\\}\\]",
067: Pattern.DOTALL + Pattern.MULTILINE);
068: Matcher m = p.matcher(s);
069: StringBuffer sb = new StringBuffer();
070: while (m.find()) {
071: String groupEscaped = m.group().replaceAll("\\\\|\\$",
072: "\\\\$0");
073: if (m.group(3) != null) {
074: m.appendReplacement(sb, groupEscaped.replaceAll(
075: "\\\\\\\\\\\\\\\\", "\n"));
076: } else {
077: m.appendReplacement(sb, groupEscaped);
078: }
079: }
080: m.appendTail(sb);
081: s = sb.toString();
082: return s;
083: }
084:
085: public boolean isWhitespaceTrimMode() {
086: return m_trimMode;
087: }
088:
089: public void setWhitespaceTrimMode(boolean trimMode) {
090: if (m_trimMode != trimMode) {
091: flush();
092: m_trimMode = trimMode;
093: }
094: }
095:
096: public void write(char[] arg0, int arg1, int arg2)
097: throws IOException {
098: m_buffer.append(arg0, arg1, arg2);
099: m_currentlyOnLineBegin = ONLINE_PATTERN.matcher(m_buffer)
100: .matches();
101: }
102:
103: public void close() throws IOException {
104: }
105:
106: public String toString() {
107: flush();
108: return m_result.toString();
109: }
110:
111: public boolean isCurrentlyOnLineBegin() {
112: return m_currentlyOnLineBegin;
113: }
114: }
|