Coverage Report - org.omegat.filters2.text.TextFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
TextFilter
44%
30/68
35%
10/28
2.7
 
 1  
 /**************************************************************************
 2  
  OmegaT - Computer Assisted Translation (CAT) tool 
 3  
           with fuzzy matching, translation memory, keyword search, 
 4  
           glossaries, and translation leveraging into updated projects.
 5  
 
 6  
  Copyright (C) 2000-2006 Keith Godfrey and Maxym Mykhalchuk
 7  
                Home page: http://www.omegat.org/
 8  
                Support center: http://groups.yahoo.com/group/OmegaT/
 9  
 
 10  
  This program is free software; you can redistribute it and/or modify
 11  
  it under the terms of the GNU General Public License as published by
 12  
  the Free Software Foundation; either version 2 of the License, or
 13  
  (at your option) any later version.
 14  
 
 15  
  This program is distributed in the hope that it will be useful,
 16  
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 17  
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18  
  GNU General Public License for more details.
 19  
 
 20  
  You should have received a copy of the GNU General Public License
 21  
  along with this program; if not, write to the Free Software
 22  
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23  
  **************************************************************************/
 24  
 
 25  
 package org.omegat.filters2.text;
 26  
 
 27  
 import java.awt.Dialog;
 28  
 import java.io.BufferedReader;
 29  
 import java.io.BufferedWriter;
 30  
 import java.io.IOException;
 31  
 import java.io.Writer;
 32  
 import java.util.Map;
 33  
 
 34  
 import org.omegat.filters2.AbstractFilter;
 35  
 import org.omegat.filters2.Instance;
 36  
 import org.omegat.util.LinebreakPreservingReader;
 37  
 import org.omegat.util.Log;
 38  
 import org.omegat.util.OStrings;
 39  
 
 40  
 /**
 41  
  * Filter to support plain text files (in various encodings).
 42  
  * 
 43  
  * @author Keith Godfrey
 44  
  * @author Maxym Mykhalchuk
 45  
  */
 46  1190853747
 public class TextFilter extends AbstractFilter {
 47  
 
 48  
     /**
 49  
      * Text filter should segmentOn text into paragraphs on line breaks.
 50  
      */
 51  
     public static final String SEGMENT_BREAKS = "BREAKS";
 52  
     /**
 53  
      * Defult. Text filter should segmentOn text into paragraphs on empty lines.
 54  
      */
 55  
     public static final String SEGMENT_EMPTYLINES = "EMPTYLINES";
 56  
     /**
 57  
      * Text filter should not segmentOn text into paragraphs.
 58  
      */
 59  
     public static final String SEGMENT_NEVER = "NEVER";
 60  
 
 61  
     public static final String OPTION_SEGMENT_ON = "segmentOn";
 62  
 
 63  
     public String getFileFormatName() {
 64  13064848
         return OStrings.getString("TEXTFILTER_FILTER_NAME");
 65  
     }
 66  
 
 67  
     public Instance[] getDefaultInstances() {
 68  569283428
         return new Instance[] { new Instance("*.txt"), new Instance("*.txt1", "ISO-8859-1", "ISO-8859-1"),
 69  
                 new Instance("*.txt2", "ISO-8859-2", "ISO-8859-2"), new Instance("*.utf8", "UTF-8", "UTF-8") };
 70  
     }
 71  
 
 72  
     public boolean isSourceEncodingVariable() {
 73  0
         return true;
 74  
     }
 75  
 
 76  
     public boolean isTargetEncodingVariable() {
 77  0
         return true;
 78  
     }
 79  
 
 80  
     public void processFile(BufferedReader in, BufferedWriter out) throws IOException {
 81  
         // BOM (byte order mark) bugfix
 82  608505471
         in.mark(1);
 83  608505471
         int ch = in.read();
 84  608505471
         if (ch != 0xFEFF)
 85  608505471
             in.reset();
 86  
 
 87  608505471
         String segmentOn = processOptions.get(TextFilter.OPTION_SEGMENT_ON);
 88  608505471
         if (SEGMENT_BREAKS.equals(segmentOn)) {
 89  0
             processSegLineBreaks(in, out);
 90  608505471
         } else if (SEGMENT_NEVER.equals(segmentOn)) {
 91  0
             processNonSeg(in, out);
 92  
         } else {
 93  608505471
             processSegEmptyLines(in, out);
 94  
         }
 95  608505471
     }
 96  
 
 97  
     /** Process the file without segmenting it. */
 98  
     private void processNonSeg(BufferedReader in, Writer out) throws IOException {
 99  0
         StringBuffer segment = new StringBuffer();
 100  0
         char[] buf = new char[4096];
 101  
         int len;
 102  0
         while ((len = in.read(buf)) >= 0)
 103  0
             segment.append(buf, 0, len);
 104  0
         out.write(processEntry(segment.toString()));
 105  0
     }
 106  
 
 107  
     /** Processes the file segmenting on line breaks. */
 108  
     private void processSegLineBreaks(BufferedReader in, Writer out) throws IOException {
 109  0
         LinebreakPreservingReader lpin = new LinebreakPreservingReader(in);
 110  0
         String nontrans = "";
 111  
         String s;
 112  0
         while ((s = lpin.readLine()) != null) {
 113  0
             if (s.trim().length() == 0) {
 114  0
                 nontrans += s + lpin.getLinebreak();
 115  0
                 continue;
 116  
             }
 117  0
             String srcText = s;
 118  
 
 119  0
             out.write(nontrans);
 120  0
             nontrans = "";
 121  
 
 122  0
             String translation = processEntry(srcText);
 123  0
             out.write(translation);
 124  
 
 125  0
             nontrans += lpin.getLinebreak();
 126  0
         }
 127  
 
 128  0
         if (nontrans.length() != 0)
 129  0
             out.write(nontrans);
 130  0
     }
 131  
 
 132  
     /** Processes the file segmenting on line breaks. */
 133  
     private void processSegEmptyLines(BufferedReader in, Writer out) throws IOException {
 134  608505471
         LinebreakPreservingReader lpin = new LinebreakPreservingReader(in);
 135  608505471
         StringBuffer nontrans = new StringBuffer();
 136  608505471
         StringBuffer trans = new StringBuffer();
 137  
         String s;
 138  30504243519
         while ((s = lpin.readLine()) != null) {
 139  29895738048
             if (s.length() == 0) {
 140  6573891141
                 out.write(nontrans.toString());
 141  6573891141
                 nontrans.setLength(0);
 142  
 
 143  6573891141
                 out.write(processEntry(trans.toString()));
 144  6573891141
                 trans.setLength(0);
 145  6573891141
                 nontrans.append(lpin.getLinebreak());
 146  
             } else {
 147  23321846907
                 if (s.trim().length() == 0 && trans.length() == 0) {
 148  0
                     nontrans.append(s);
 149  0
                     nontrans.append(lpin.getLinebreak());
 150  
                 } else {
 151  23321846907
                     trans.append(s);
 152  23321846907
                     trans.append(lpin.getLinebreak());
 153  
                 }
 154  
             }
 155  
         }
 156  
 
 157  608505471
         if (nontrans.length() >= 0)
 158  608505471
             out.write(nontrans.toString());
 159  608505471
         if (trans.length() >= 0)
 160  608505471
             out.write(processEntry(trans.toString()));
 161  608505471
     }
 162  
 
 163  
     @Override
 164  
     public Map<String, String> changeOptions(Dialog parent, Map<String, String> config) {
 165  
         try {
 166  0
             TextOptionsDialog dialog = new TextOptionsDialog(parent, config);
 167  0
             dialog.setVisible(true);
 168  0
             if (TextOptionsDialog.RET_OK == dialog.getReturnStatus())
 169  0
                 return dialog.getOptions();
 170  
             else
 171  0
                 return null;
 172  0
         } catch (Exception e) {
 173  0
             Log.log("Text filter threw an exception:");
 174  0
             Log.log(e);
 175  0
             return null;
 176  
         }
 177  
     }
 178  
 
 179  
     /**
 180  
      * Returns true to indicate that Text filter has options.
 181  
      * 
 182  
      * @return True, because Text filter has options.
 183  
      */
 184  
     public boolean hasOptions() {
 185  0
         return true;
 186  
     }
 187  
 }