Skip to content

Commit

Permalink
refactor(filters4): XLIFF1,2 filters and abstract classes
Browse files Browse the repository at this point in the history
- Unnecessary static for eFactory
- Unnecessary silent override of eFactory
- Unnecessary nested blocks
- Bad style of commenting in if-elseif syntax
- Unnecessary static for toPair method

Signed-off-by: Hiroshi Miura <[email protected]>
  • Loading branch information
miurahr committed Oct 22, 2023
1 parent b2734fb commit 250b710
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 61 deletions.
6 changes: 0 additions & 6 deletions config/checkstyle/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,8 @@
checks="(InnerAssignment|LocalVariableName|ParameterName|MemberName|MissingSwitchDefault|MethodLength)"
lines="95,292,302,456,520,578,609,767"/>
<suppress files="MsOfficeFileFilter\.java" checks="(MemberName|OperatorWrap)" lines="49,50,190"/>
<suppress files="Xliff1Filter\.java" checks="AvoidNestedBlocks" lines="412"/>
<suppress files="SdlXliff\.java" checks="MemberName" lines="62,105,106,107"/>
<suppress files="AbstractXliffFilter\.java" checks="ConstantName" lines="246"/>
<suppress files="Xliff2Filter\.java" checks="RightCurly" lines="146"/>
<suppress files="Xliff2Filter\.java" checks="AvoidNestedBlocks" lines="266"/>
<suppress files="AbstractXmlFilter\.java" checks="(MissingSwitchDefault|ConstantName)" lines="325,380,464"/>


<!-- missing javadoc: temporary global suppression -->
<suppress checks="(DesignForExtension|MagicNumber)"
files=".*"/>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ <h1>OmegaT User Manual</h1>

<tr>
<td>
<p class="lang"><a href="ia/index.html">Interlingua</a></p>
<p class="lang"><a href="ia/index.html">interlingua</a></p>
</td>
<td>(<span class="out-of-date">3.5.0</span>)</td>
<td><a target="blank"
Expand Down
76 changes: 35 additions & 41 deletions src/org/omegat/filters4/AbstractZipFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;
Expand Down Expand Up @@ -72,18 +71,15 @@ public boolean isTargetEncodingVariable() {

@Override
public boolean isFileSupported(File inFile, Map<String, String> config, FilterContext context) {
try {
ZipFile file = new ZipFile(inFile);
try (ZipFile file = new ZipFile(inFile)) {
Enumeration<? extends ZipEntry> entries = file.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (acceptInternalFile(entry, context)) {
file.close();
return true;
}
}
file.close();
} catch (IOException e) {
} catch (IOException ignored) {
}
return false;
}
Expand Down Expand Up @@ -116,45 +112,43 @@ protected Comparator<ZipEntry> getEntryComparator() {
@Override
public void processFile(File inFile, File outFile, FilterContext fc)
throws IOException, TranslationException {
ZipFile zf = new ZipFile(inFile);
ZipOutputStream zipout = null;
if (outFile != null) {
zipout = new ZipOutputStream(new FileOutputStream(outFile));
try (ZipFile zf = new ZipFile(inFile)) {
if (outFile != null) {
try (ZipOutputStream zipout = new ZipOutputStream(new FileOutputStream(outFile))) {
processFileImpl(zf, zipout, fc);
}
} else {
processFileImpl(zf, null, fc);
}
}
}

try {
Enumeration<? extends ZipEntry> zipcontents = zf.entries();
List<ZipEntry> toTranslate = new LinkedList<>();
Comparator<ZipEntry> cmp = getEntryComparator();
while (zipcontents.hasMoreElements()) {
ZipEntry ze = zipcontents.nextElement();
if (mustTranslateInternalFile(ze, outFile != null, fc)) {
if ((cmp == null) || (outFile != null)) {
translateEntry(zf, zipout, fc, ze);
} else {
toTranslate.add(ze); // need sort before treatment
}
} else if (!mustDeleteInternalFile(ze, outFile != null, fc)) {
if (zipout != null) {
ZipEntry outEntry = new ZipEntry(ze.getName());
zipout.putNextEntry(outEntry);

org.apache.commons.io.IOUtils.copy(zf.getInputStream(ze), zipout);
zipout.closeEntry();
}
private void processFileImpl(ZipFile zf, ZipOutputStream zipout, FilterContext fc) throws IOException {
Enumeration<? extends ZipEntry> zipcontents = zf.entries();
List<ZipEntry> toTranslate = new LinkedList<>();
Comparator<ZipEntry> cmp = getEntryComparator();
while (zipcontents.hasMoreElements()) {
ZipEntry ze = zipcontents.nextElement();
if (mustTranslateInternalFile(ze, zipout != null, fc)) {
if ((cmp == null) || (zipout != null)) {
translateEntry(zf, zipout, fc, ze);
} else {
toTranslate.add(ze); // need sort before treatment
}
} else if (!mustDeleteInternalFile(ze, zipout != null, fc)) {
if (zipout != null) {
ZipEntry outEntry = new ZipEntry(ze.getName());
zipout.putNextEntry(outEntry);
org.apache.commons.io.IOUtils.copy(zf.getInputStream(ze), zipout);
zipout.closeEntry();
}
}
if (cmp != null) {
Collections.sort(toTranslate, cmp);
}
for (ZipEntry ze : toTranslate) {
translateEntry(zf, zipout, fc, ze);
}
} finally {
if (zipout != null) {
zipout.close();
}
zf.close();
}
if (cmp != null) {
toTranslate.sort(cmp);
}
for (ZipEntry ze : toTranslate) {
translateEntry(zf, zipout, fc, ze);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/org/omegat/filters4/xml/AbstractXmlFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ protected StartElement findEvent(File inputFile, Pattern path) throws IOExceptio
protected Map<String, List<XMLEvent>> tagsMap = new TreeMap<>();

protected static final Pattern OMEGAT_TAG = Pattern.compile("<(\\/?)([a-z]\\d+)\\/?>");
protected static final XMLEventFactory eFactory = XMLEventFactory.newInstance();
protected final XMLEventFactory eFactory = XMLEventFactory.newInstance();

/**
* Produces xliff content for the translated text. Note: must be called
Expand Down Expand Up @@ -535,7 +535,7 @@ protected List<ProtectedPart> buildProtectedParts(String src) {
}

/** Convert <xxx/> to <xxx></xxx> **/
protected static List<XMLEvent> toPair(StartElement ev) {
protected List<XMLEvent> toPair(StartElement ev) {
List<XMLEvent> l = new LinkedList<XMLEvent>();
l.add(ev);
l.add(eFactory.createEndElement(ev.getName(), null));
Expand Down
3 changes: 0 additions & 3 deletions src/org/omegat/filters4/xml/xliff/AbstractXliffFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,4 @@ protected void startStackElement(boolean reuse, StartElement stEl, char prefix,
tagStack.push("" + prefix + count);
}
}

protected static final javax.xml.stream.XMLEventFactory eFactory = javax.xml.stream.XMLEventFactory
.newInstance();
}
5 changes: 2 additions & 3 deletions src/org/omegat/filters4/xml/xliff/Xliff1Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ protected String buildTags(List<XMLEvent> srcList, boolean reuse) {
res.setLength(0);
res = saveBuf.pop();
break;
default: {
default:
String pop = tagStack.pop();
if (pop.equals("mark-protected")) { // isProtectedTag(start
// element) was true
Expand All @@ -428,7 +428,6 @@ protected String buildTags(List<XMLEvent> srcList, boolean reuse) {
res.append("</").append(pop).append(">");
}
}
}
}
}
return res.toString();
Expand Down Expand Up @@ -628,7 +627,7 @@ private void flushTranslations(XMLStreamWriter writer) throws XMLStreamException
* Builds target from OmegaT to XLIFF format. May be overridden in
* subclasses
**/
protected List<XMLEvent> restoreTags(String unitId, String path, String src, String tra) {
protected List<XMLEvent> restoreTags(String aUnitId, String path, String src, String tra) {
return restoreTags(tra);
}

Expand Down
8 changes: 3 additions & 5 deletions src/org/omegat/filters4/xml/xliff/Xliff2Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,9 @@ protected boolean processStartElement(StartElement startElement, XMLStreamWriter
default:
if (currentBuffer != null) {
currentBuffer.add(startElement);
}
// <target> must be before any other-namespace markup
else if (((ignoreScope == null || ignoreScope.startsWith("!")) && (segId != null))
} else if (((ignoreScope == null || ignoreScope.startsWith("!")) && (segId != null))
&& (!startElement.getName().getNamespaceURI().equals(namespace))) {
// <target> must be before any other-namespace markup
flushTranslations(writer);
}
}
Expand Down Expand Up @@ -263,13 +262,12 @@ protected String buildTags(List<XMLEvent> srcList, boolean reuse) {
case "ec":
break; // Should be empty!!!
case "pc":
default: {
default:
String pop = tagStack.pop();
tagsMap.put("/" + pop, Collections.singletonList(ev));
res.append("</").append(pop).append(">");
break;
}
}
}
}
return res.toString();
Expand Down

0 comments on commit 250b710

Please sign in to comment.