Skip to content

Commit

Permalink
Use Jackson XML dataformat for JAXB data parser
Browse files Browse the repository at this point in the history
- Update TMXWriterTest to check with system line separator
  It is because XML parser can automatically change /r/n into
  XML entity 
/n when System.lineSeparator() is /n
- Update TMXReader2 to detect both "lang" and "xml:lang"
  some implementation return "lang" from "xml:lang" but
  other can return "xml:lang" as is.
- as a workaround for  FasterXML/jackson-modules-base#127
  Add  @JacksonXmlElementWrapper(useWrapping=false) for mask
  element for the parser of omegat.project file.

Signed-off-by: Hiroshi Miura <miurahr@linux.com>
  • Loading branch information
miurahr committed Dec 27, 2022
1 parent f8e79b6 commit 3414561
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 183 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ dependencies {
// JSON parser
implementation "com.fasterxml.jackson.core:jackson-core:2.13.4"
implementation "com.fasterxml.jackson.core:jackson-databind:2.13.4.2"
implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.13.4"
implementation 'com.fasterxml.jackson.module:jackson-module-jaxb-annotations:2.14.0'

}

// Test dependencies
Expand Down
29 changes: 17 additions & 12 deletions src/gen/core/project/Masks.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;

/**
* <p>Java class for masks complex type.
* <p>
* Java class for masks complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
* <p>
* The following schema fragment specifies the expected content contained within
* this class.
*
* <pre>
* &lt;complexType name="masks"&gt;
Expand All @@ -28,32 +33,32 @@
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "masks", propOrder = {
"mask"
})
@XmlType(name = "masks", propOrder = { "mask" })
public class Masks {

// workaround https://github.com/FasterXML/jackson-modules-base/issues/127
@JacksonXmlElementWrapper(useWrapping = false)
protected List<String> mask;

/**
* Gets the value of the mask property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the mask property.
* This accessor method returns a reference to the live list, not a
* snapshot. Therefore any modification you make to the returned list will
* be present inside the JAXB object. This is why there is not a
* <CODE>set</CODE> method for the mask property.
*
* <p>
* For example, to add a new item, do as follows:
*
* <pre>
* getMask().add(newItem);
* getMask().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link String }
* Objects of the following type(s) are allowed in the list {@link String }
*
*
*/
Expand Down
22 changes: 9 additions & 13 deletions src/org/omegat/convert/v20to21/Convert20to21.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
import java.io.Serializable;
import java.nio.charset.StandardCharsets;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;

import gen.core.filters.Files;
import gen.core.filters.Filter;
Expand All @@ -60,19 +60,16 @@ private Convert20to21() {
* old config file
* @param toFile
* new config file
* @throws Exception
*/
public static void convertFiltersConfig(final File fromFile, final File toFile) throws Exception {
if (!fromFile.exists()) {
return;
}
String c = read(fromFile);
org.omegat.convert.v20to21.data.Filters filters;
XMLDecoder xmldec = new XMLDecoder(new ByteArrayInputStream(c.getBytes("UTF-8")));
try {
try (XMLDecoder xmldec = new XMLDecoder(
new ByteArrayInputStream(c.getBytes(StandardCharsets.UTF_8)))) {
filters = (org.omegat.convert.v20to21.data.Filters) xmldec.readObject();
} finally {
xmldec.close();
}

Filters res = new Filters();
Expand Down Expand Up @@ -108,10 +105,9 @@ public static void convertFiltersConfig(final File fromFile, final File toFile)
convertTextFilter(res);
convertHTMLFilter2(res);

JAXBContext ctx = JAXBContext.newInstance(Filters.class);
Marshaller m = ctx.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(res, toFile);
XmlMapper mapper = new XmlMapper();
mapper.registerModule(new JaxbAnnotationModule());
mapper.writeValue(toFile, res);
}

/**
Expand All @@ -133,8 +129,8 @@ private static String read(File f) throws IOException {
}
String res = r.toString();
res = res.replace("org.omegat.filters2.master.Filters", "org.omegat.convert.v20to21.data.Filters");
res = res
.replace("org.omegat.filters2.master.OneFilter", "org.omegat.convert.v20to21.data.OneFilter");
res = res.replace("org.omegat.filters2.master.OneFilter",
"org.omegat.convert.v20to21.data.OneFilter");
res = res.replace("org.omegat.filters2.Instance", "org.omegat.convert.v20to21.data.Instance");
res = res.replace("org.omegat.filters2.html2.HTMLOptions",
"org.omegat.convert.v20to21.data.HTMLOptions");
Expand Down
105 changes: 54 additions & 51 deletions src/org/omegat/core/statistics/StatsResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@
import java.util.Set;
import java.util.TimeZone;

import javax.xml.bind.JAXB;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SequenceWriter;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
import org.omegat.util.OStrings;
import org.omegat.util.StaticUtils;
import org.omegat.util.gui.TextUtil;
Expand All @@ -55,46 +58,36 @@
*/
@XmlRootElement(name = "omegat-stats")
public class StatsResult {
public static final String[] HT_HEADERS = {
"",
OStrings.getString("CT_STATS_Segments"),
OStrings.getString("CT_STATS_Words"),
OStrings.getString("CT_STATS_Characters_NOSP"),
OStrings.getString("CT_STATS_Characters"),
OStrings.getString("CT_STATS_Files"),
};

private static final String[] HT_ROWS = {
OStrings.getString("CT_STATS_Total"),
OStrings.getString("CT_STATS_Remaining"),
OStrings.getString("CT_STATS_Unique"),
OStrings.getString("CT_STATS_Unique_Remaining"),
};
public static final String[] HT_HEADERS = { "", OStrings.getString("CT_STATS_Segments"),
OStrings.getString("CT_STATS_Words"), OStrings.getString("CT_STATS_Characters_NOSP"),
OStrings.getString("CT_STATS_Characters"), OStrings.getString("CT_STATS_Files"), };

private static final String[] HT_ROWS = { OStrings.getString("CT_STATS_Total"),
OStrings.getString("CT_STATS_Remaining"), OStrings.getString("CT_STATS_Unique"),
OStrings.getString("CT_STATS_Unique_Remaining"), };

private static final boolean[] HT_ALIGN = new boolean[] { false, true, true, true, true, true };

public static final String[] FT_HEADERS = {
OStrings.getString("CT_STATS_FILE_Name"),
OStrings.getString("CT_STATS_FILE_Total_Segments"),
OStrings.getString("CT_STATS_FILE_Remaining_Segments"),
OStrings.getString("CT_STATS_FILE_Unique_Segments"),
OStrings.getString("CT_STATS_FILE_Unique_Remaining_Segments"),
OStrings.getString("CT_STATS_FILE_Total_Words"),
OStrings.getString("CT_STATS_FILE_Remaining_Words"),
OStrings.getString("CT_STATS_FILE_Unique_Words"),
OStrings.getString("CT_STATS_FILE_Unique_Remaining_Words"),
OStrings.getString("CT_STATS_FILE_Total_Characters_NOSP"),
OStrings.getString("CT_STATS_FILE_Remaining_Characters_NOSP"),
OStrings.getString("CT_STATS_FILE_Unique_Characters_NOSP"),
OStrings.getString("CT_STATS_FILE_Unique_Remaining_Characters_NOSP"),
OStrings.getString("CT_STATS_FILE_Total_Characters"),
OStrings.getString("CT_STATS_FILE_Remaining_Characters"),
OStrings.getString("CT_STATS_FILE_Unique_Characters"),
OStrings.getString("CT_STATS_FILE_Unique_Remaining_Characters"),
};

private static final boolean[] FT_ALIGN = { false, true, true, true, true, true, true, true, true, true, true, true,
true, true, true, true, true, };
public static final String[] FT_HEADERS = { OStrings.getString("CT_STATS_FILE_Name"),
OStrings.getString("CT_STATS_FILE_Total_Segments"),
OStrings.getString("CT_STATS_FILE_Remaining_Segments"),
OStrings.getString("CT_STATS_FILE_Unique_Segments"),
OStrings.getString("CT_STATS_FILE_Unique_Remaining_Segments"),
OStrings.getString("CT_STATS_FILE_Total_Words"),
OStrings.getString("CT_STATS_FILE_Remaining_Words"),
OStrings.getString("CT_STATS_FILE_Unique_Words"),
OStrings.getString("CT_STATS_FILE_Unique_Remaining_Words"),
OStrings.getString("CT_STATS_FILE_Total_Characters_NOSP"),
OStrings.getString("CT_STATS_FILE_Remaining_Characters_NOSP"),
OStrings.getString("CT_STATS_FILE_Unique_Characters_NOSP"),
OStrings.getString("CT_STATS_FILE_Unique_Remaining_Characters_NOSP"),
OStrings.getString("CT_STATS_FILE_Total_Characters"),
OStrings.getString("CT_STATS_FILE_Remaining_Characters"),
OStrings.getString("CT_STATS_FILE_Unique_Characters"),
OStrings.getString("CT_STATS_FILE_Unique_Remaining_Characters"), };

private static final boolean[] FT_ALIGN = { false, true, true, true, true, true, true, true, true, true,
true, true, true, true, true, true, true, };

@JsonProperty("project")
private StatProjectProperties props;
Expand Down Expand Up @@ -122,6 +115,7 @@ public StatsResult() {

/**
* Constructor.
*
* @param total
* @param remaining
* @param unique
Expand All @@ -143,7 +137,9 @@ public StatsResult(StatCount total, StatCount remaining, StatCount unique, StatC

/**
* Update given hosStat with current stats data.
* @param hotStat StatisticsInfo data object.
*
* @param hotStat
* StatisticsInfo data object.
*/
public void updateStatisticsInfo(StatisticsInfo hotStat) {
hotStat.numberOfSegmentsTotal = total.segments;
Expand All @@ -167,6 +163,7 @@ public StatProjectProperties getProps() {

/**
* Return total number of segments.
*
* @return
*/
@XmlElement(name = "total")
Expand All @@ -176,6 +173,7 @@ public StatCount getTotal() {

/**
* Return remaining number of segments that needs translation.
*
* @return
*/
@XmlElement(name = "remaining")
Expand All @@ -185,6 +183,7 @@ public StatCount getRemaining() {

/**
* Return a number of unique segments.
*
* @return
*/
@XmlElement(name = "unique")
Expand All @@ -194,6 +193,7 @@ public StatCount getUnique() {

/**
* Return a number of remaining unique segments.
*
* @return
*/
@XmlElement(name = "unique-remaining")
Expand All @@ -203,6 +203,7 @@ public StatCount getRemainingUnique() {

/**
* return a statistics of each source/target files.
*
* @return
*/
@XmlElement(name = "files")
Expand All @@ -212,25 +213,25 @@ public List<FileData> getCounts() {

/**
* Return pretty printed statistics data.
*
* @return pretty-printed string.
*/
@JsonIgnore
public String getTextData() {
return OStrings.getString("CT_STATS_Project_Statistics") +
"\n\n" +
TextUtil.showTextTable(HT_HEADERS, getHeaderTable(), HT_ALIGN) +
"\n\n" +
return OStrings.getString("CT_STATS_Project_Statistics") + "\n\n"
+ TextUtil.showTextTable(HT_HEADERS, getHeaderTable(), HT_ALIGN) + "\n\n" +

// STATISTICS BY FILE
OStrings.getString("CT_STATS_FILE_Statistics") +
"\n\n" +
TextUtil.showTextTable(FT_HEADERS, getFilesTable(), FT_ALIGN);
OStrings.getString("CT_STATS_FILE_Statistics") + "\n\n"
+ TextUtil.showTextTable(FT_HEADERS, getFilesTable(), FT_ALIGN);
}

/**
* Return JSON expression of stats data.
*
* @return JSON string data.
* @throws IOException when export failed.
* @throws IOException
* when export failed.
*/
@JsonIgnore
public String getJsonData() throws IOException {
Expand All @@ -245,14 +246,16 @@ public String getJsonData() throws IOException {

/**
* Return XML expression of Stats data.
*
* @return XML expression of stats data as String.
*/
@JsonIgnore
public String getXmlData() {
public String getXmlData() throws JsonProcessingException {
setDate();
StringWriter result = new StringWriter();
JAXB.marshal(this, result);
return result.toString();
XmlMapper mapper = XmlMapper.xmlBuilder().enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME)
.build();
mapper.registerModule(new JaxbAnnotationModule());
return mapper.writeValueAsString(this);
}

private void setDate() {
Expand Down

0 comments on commit 3414561

Please sign in to comment.