Skip to content

Commit

Permalink
Fixed JSON encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
rmraya committed Oct 14, 2020
1 parent 7008231 commit d87be6d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
Binary file modified lib/openxliff.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions src/com/maxprograms/converters/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ private Constants() {

public static final String TOOLID = "OpenXLIFF";
public static final String TOOLNAME = "OpenXLIFF Filters";
public static final String VERSION = "1.11.0";
public static final String BUILD = "20200925_1344";
public static final String VERSION = "1.11.1";
public static final String BUILD = "20201014_1434";

public static final String SUCCESS = "0";
public static final String ERROR = "1";
Expand Down
7 changes: 4 additions & 3 deletions src/com/maxprograms/converters/json/Json2Xliff.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
Expand Down Expand Up @@ -80,7 +81,7 @@ public static List<String> run(Map<String, String> params) {
segmenter = new Segmenter(initSegmenter, sourceLanguage, catalog);
}

JSONObject json = loadFile(inputFile);
JSONObject json = loadFile(inputFile, encoding);
parseJson(json);

if (segments.size() == 0) {
Expand Down Expand Up @@ -138,9 +139,9 @@ private static void writeString(FileOutputStream out, String string) throws IOEx
out.write(string.getBytes(StandardCharsets.UTF_8));
}

protected static JSONObject loadFile(String file) throws IOException {
protected static JSONObject loadFile(String file, String charset) throws IOException {
StringBuilder builder = new StringBuilder();
try (FileReader stream = new FileReader(new File(file))) {
try (FileReader stream = new FileReader(new File(file), Charset.forName(charset))) {
try (BufferedReader reader = new BufferedReader(stream)) {
String line = "";
while ((line = reader.readLine()) != null) {
Expand Down
14 changes: 9 additions & 5 deletions src/com/maxprograms/converters/json/Xliff2json.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.maxprograms.xml.Catalog;
import com.maxprograms.xml.Document;
import com.maxprograms.xml.Element;
import com.maxprograms.xml.PI;
import com.maxprograms.xml.SAXBuilder;
import com.maxprograms.xml.TextNode;
import com.maxprograms.xml.XMLNode;
Expand All @@ -42,15 +43,13 @@
public class Xliff2json {

private static Map<String, Element> segments;
private static String encoding;

private Xliff2json() {
// do not instantiate this class
// use run method instead
}

public static void main(String[] args) {
}

public static List<String> run(Map<String, String> params) {
List<String> result = new ArrayList<>();
String sklFile = params.get("skeleton");
Expand All @@ -60,13 +59,13 @@ public static List<String> run(Map<String, String> params) {

try {
loadSegments(xliffFile, catalog);
JSONObject json = Json2Xliff.loadFile(sklFile);
JSONObject json = Json2Xliff.loadFile(sklFile, encoding);
parseJson(json);

try (FileOutputStream out = new FileOutputStream(outputFile)) {
out.write(json.toString(4).getBytes(StandardCharsets.UTF_8));
}

result.add(Constants.SUCCESS);
} catch (IOException | SAXException | ParserConfigurationException | URISyntaxException e) {
Logger logger = System.getLogger(Xliff2json.class.getName());
Expand All @@ -86,6 +85,11 @@ private static void loadSegments(String xliffFile, String catalog)

Document doc = builder.build(xliffFile);
Element root = doc.getRootElement();
List<PI> encodings = root.getChild("file").getPI("encoding");
if (encodings.size() == 0) {
throw new IOException("Missing encoding");
}
encoding = encodings.get(0).getData();
Element body = root.getChild("file").getChild("body");
List<Element> units = body.getChildren("trans-unit");
Iterator<Element> i = units.iterator();
Expand Down

0 comments on commit d87be6d

Please sign in to comment.