001: /*
002: * Copyright (c) 1998-2000 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Emil Ong
028: */
029:
030: package com.caucho.xtpdoc;
031:
032: import javax.xml.stream.XMLStreamException;
033: import javax.xml.stream.XMLStreamWriter;
034: import java.io.FilterWriter;
035: import java.io.IOException;
036: import java.io.PrintWriter;
037: import java.io.Writer;
038:
039: public class PreFormattedText extends FormattedText {
040: public PreFormattedText(Document document) {
041: super (document);
042: }
043:
044: public void writeHtml(XMLStreamWriter out)
045: throws XMLStreamException {
046: out.writeStartElement("pre");
047:
048: super .writeHtml(out);
049:
050: out.writeEndElement(); // pre
051: }
052:
053: public void writeLaTeX(PrintWriter out) throws IOException {
054: super
055: .writeLaTeX(new PrintWriter(new PreFormatFilterWriter(
056: out)));
057: }
058:
059: public void writeLaTeXEnclosed(PrintWriter out) throws IOException {
060: writeLaTeX(out);
061: }
062:
063: public void writeLaTeXTop(PrintWriter out) throws IOException {
064: writeLaTeX(out);
065: }
066:
067: private static class PreFormatFilterWriter extends FilterWriter {
068: public PreFormatFilterWriter(Writer out) {
069: super (out);
070: }
071:
072: public void write(char[] cbuf, int off, int len)
073: throws IOException {
074: for (int i = off; i < len; i++)
075: filterChar(cbuf[i]);
076: }
077:
078: public void write(int c) throws IOException {
079: filterChar(c);
080: }
081:
082: public void write(String str, int off, int len)
083: throws IOException {
084: for (int i = off; i < len; i++)
085: filterChar(str.charAt(i));
086: }
087:
088: private void filterChar(int ch) throws IOException {
089: switch (ch) {
090: case ' ':
091: case '\t':
092: super .write('\\');
093: super .write(' ');
094: break;
095: case '\n':
096: super .write('\\');
097: super .write('\\');
098: break;
099: default:
100: super.write(ch);
101: break;
102: }
103: }
104: }
105: }
|