1 16 package com.blandware.atleap.common.parsers.rtf; 17 18 import com.blandware.atleap.common.parsers.exception.PlainTextExtractorException; 19 import com.blandware.atleap.common.parsers.SpecificPlainTextExtractor; 20 import com.blandware.atleap.common.Constants; 21 22 import java.io.InputStream ; 23 import java.io.StringWriter ; 24 import java.io.Writer ; 25 import java.util.List ; 26 27 35 public class RTFPlainTextExtractor implements SpecificPlainTextExtractor, 36 RTFParserDelegate { 37 private StringWriter buffer = null; 38 private boolean inIgnorableDestination; 39 private int ignorableDestBraceLevel; 40 private int braceLevel; 41 42 public RTFPlainTextExtractor() { 43 } 44 45 55 public void extract(InputStream input, Writer output, String encoding) 56 throws PlainTextExtractorException { 57 braceLevel = 0; 59 inIgnorableDestination = false; 60 buffer = new StringWriter (); 61 RTFParser parser = new RTFParser(input); 62 63 parser.setNewLine(Constants.EOL); 64 parser.setDelegate(this); 65 try { 66 parser.parse(); 67 output.write(buffer.toString()); 68 } catch (Exception e) { 69 throw new PlainTextExtractorException(e); 70 } 71 } 72 73 private void tryToWriteOutput(String str, int context) { 74 if (context == IN_DOCUMENT) { 75 if (!inIgnorableDestination) { 76 if (buffer != null) { 77 buffer.write(str); 78 } 79 } 80 } 81 } 82 83 public void text(String text, String style, int context) { 84 tryToWriteOutput(text, context); 85 } 86 87 public void controlSymbol(String controlSymbol, int context) { 88 if (controlSymbol.equals("\\*")) { 89 if (inIgnorableDestination) { 91 } else { 93 inIgnorableDestination = true; 94 ignorableDestBraceLevel = braceLevel; 95 } 96 } 97 } 98 99 public void controlWord(String controlWord, int value, int context) { 100 if (controlWord.equals("\\cell")) { 101 tryToWriteOutput(" ", context); 102 } else if (controlWord.equals("\\row")) { 103 tryToWriteOutput(Constants.EOL, context); 104 } else if (controlWord.equals("\\object") || controlWord.equals("\\pict")) { 105 if (inIgnorableDestination) { 107 } else { 109 inIgnorableDestination = true; 110 ignorableDestBraceLevel = braceLevel; 111 } 112 } 113 } 114 115 public void openGroup(int depth) { 116 braceLevel++; 117 } 118 119 public void closeGroup(int depth) { 120 braceLevel--; 121 if (inIgnorableDestination && braceLevel < ignorableDestBraceLevel) { 122 inIgnorableDestination = false; 123 } 124 } 125 126 public void styleList(List styles) { 127 } 128 129 public void startDocument() { 130 } 131 132 public void endDocument() { 133 } 134 135 138 public String getUsedEncoding() { 139 return null; 140 } 141 } 142 | Popular Tags |