1 29 30 package com.caucho.xtpdoc; 31 32 import javax.xml.stream.XMLStreamException; 33 import javax.xml.stream.XMLStreamWriter; 34 import java.io.FilterWriter ; 35 import java.io.IOException ; 36 import java.io.PrintWriter ; 37 import java.io.Writer ; 38 39 public class PreFormattedText extends FormattedText { 40 public PreFormattedText(Document document) 41 { 42 super(document); 43 } 44 45 public void writeHtml(XMLStreamWriter out) 46 throws XMLStreamException 47 { 48 out.writeStartElement("pre"); 49 50 super.writeHtml(out); 51 52 out.writeEndElement(); } 54 55 public void writeLaTeX(PrintWriter out) 56 throws IOException 57 { 58 super.writeLaTeX(new PrintWriter (new PreFormatFilterWriter(out))); 59 } 60 61 public void writeLaTeXEnclosed(PrintWriter out) 62 throws IOException 63 { 64 writeLaTeX(out); 65 } 66 67 public void writeLaTeXTop(PrintWriter out) 68 throws IOException 69 { 70 writeLaTeX(out); 71 } 72 73 private static class PreFormatFilterWriter extends FilterWriter { 74 public PreFormatFilterWriter(Writer out) 75 { 76 super(out); 77 } 78 79 public void write(char[] cbuf, int off, int len) 80 throws IOException 81 { 82 for (int i = off; i < len; i++) 83 filterChar(cbuf[i]); 84 } 85 86 public void write(int c) 87 throws IOException 88 { 89 filterChar(c); 90 } 91 92 public void write(String str, int off, int len) 93 throws IOException 94 { 95 for (int i = off; i < len; i++) 96 filterChar(str.charAt(i)); 97 } 98 99 private void filterChar(int ch) 100 throws IOException 101 { 102 switch (ch) { 103 case ' ': 104 case '\t': 105 super.write('\\'); 106 super.write(' '); 107 break; 108 case '\n': 109 super.write('\\'); 110 super.write('\\'); 111 break; 112 default: 113 super.write(ch); 114 break; 115 } 116 } 117 } 118 } 119 | Popular Tags |