| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 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 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | 1190853747 | public class TextFilter extends AbstractFilter { |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public static final String SEGMENT_BREAKS = "BREAKS"; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public static final String SEGMENT_EMPTYLINES = "EMPTYLINES"; |
| 56 | |
|
| 57 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
public boolean hasOptions() { |
| 185 | 0 | return true; |
| 186 | |
} |
| 187 | |
} |