Skip to content

Commit

Permalink
some refactoring to reuse code to escape/unescape double quotes (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeeyi authored Dec 5, 2018
1 parent 7478936 commit 52f7b81
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.box.l10n.mojito.okapi.filters;

import static com.box.l10n.mojito.okapi.filters.MacStringsFilter.FILTER_CONFIG_ID;
import java.util.ArrayList;
import java.util.List;
import net.sf.okapi.common.filters.FilterConfiguration;
import net.sf.okapi.filters.regex.RegexFilter;

/**
* @author jyi
*/
public class JSFilter extends RegexFilter {
public class JSFilter extends RegexEscapeDoubleQuoteFilter {

public static final String FILTER_CONFIG_ID = "okf_regex@mojito";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,23 @@

import java.util.ArrayList;
import java.util.List;
import net.sf.okapi.common.Event;
import net.sf.okapi.common.EventType;
import net.sf.okapi.common.encoder.EncoderManager;
import net.sf.okapi.common.filters.FilterConfiguration;
import net.sf.okapi.common.resource.TextContainer;
import net.sf.okapi.common.resource.TextUnit;
import net.sf.okapi.filters.regex.RegexFilter;

/**
* Overrides {@link RegexFilter} to handle escape/unescape special characters
*
*
* @author jyi
*/
public class MacStringsFilter extends RegexFilter {
public class MacStringsFilter extends RegexEscapeDoubleQuoteFilter {

public static final String FILTER_CONFIG_ID = "okf_regex@mojito";

@Override
public String getName() {
return FILTER_CONFIG_ID;
}

@Override
public List<FilterConfiguration> getConfigurations() {
List<FilterConfiguration> list = new ArrayList<FilterConfiguration>();
Expand All @@ -36,36 +31,4 @@ public List<FilterConfiguration> getConfigurations() {
return list;
}

@Override
public Event next() {
Event event = super.next();

if (event.getEventType() == EventType.TEXT_UNIT) {
// if source has escaped double-quotes, unescape
TextUnit textUnit = (TextUnit) event.getTextUnit();
String sourceString = textUnit.getSource().toString();
String unescapedSourceString = unescape(sourceString);
TextContainer source = new TextContainer(unescapedSourceString);
textUnit.setSource(source);
}

return event;
}

private String unescape(String text) {
String unescapedText = text.replaceAll("(\\\\)(\")", "$2");
unescapedText = unescapedText.replaceAll("\\\\n", "\n");
unescapedText = unescapedText.replaceAll("\\\\r", "\r");
return unescapedText;
}

@Override
public EncoderManager getEncoderManager() {
EncoderManager encoderManager = super.getEncoderManager();
if (encoderManager == null) {
encoderManager = new EncoderManager();
}
encoderManager.setMapping(getMimeType(), "com.box.l10n.mojito.okapi.filters.MacStringsEncoder");
return encoderManager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.box.l10n.mojito.okapi.filters;

import net.sf.okapi.common.Event;
import net.sf.okapi.common.EventType;
import net.sf.okapi.common.encoder.EncoderManager;
import net.sf.okapi.common.resource.TextContainer;
import net.sf.okapi.common.resource.TextUnit;
import net.sf.okapi.filters.regex.RegexFilter;

/**
*
* @author jyi
*/
public class RegexEscapeDoubleQuoteFilter extends RegexFilter {

@Override
public Event next() {
Event event = super.next();
if (event.getEventType() == EventType.TEXT_UNIT) {
// if source has escaped double-quotes, unescape
TextUnit textUnit = (TextUnit) event.getTextUnit();
String sourceString = textUnit.getSource().toString();
String unescapedSourceString = unescape(sourceString);
TextContainer source = new TextContainer(unescapedSourceString);
textUnit.setSource(source);
}
return event;
}

protected String unescape(String text) {
String unescapedText = text.replaceAll("(\\\\)(\")", "$2");
unescapedText = unescapedText.replaceAll("\\\\n", "\n");
unescapedText = unescapedText.replaceAll("\\\\r", "\r");
return unescapedText;
}

@Override
public EncoderManager getEncoderManager() {
EncoderManager encoderManager = super.getEncoderManager();
if (encoderManager == null) {
encoderManager = new EncoderManager();
}
encoderManager.setMapping(getMimeType(), "com.box.l10n.mojito.okapi.filters.SimpleEncoder");
return encoderManager;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

/**
* Encoder to handle escaping \n, \r, double-quotes.
*
*
* @author jyi
*/
public class MacStringsEncoder implements IEncoder {
public class SimpleEncoder implements IEncoder {

private CharsetEncoder chsEnc;
private String lineBreak;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.box.l10n.mojito.okapi.ImportTranslationsFromLocalizedAssetStep.StatusForEqualTarget;
import com.box.l10n.mojito.okapi.InheritanceMode;
import com.box.l10n.mojito.okapi.Status;
import com.box.l10n.mojito.okapi.filters.MacStringsEncoder;
import com.box.l10n.mojito.okapi.filters.SimpleEncoder;
import com.box.l10n.mojito.okapi.filters.XMLEncoder;
import com.box.l10n.mojito.service.asset.AssetRepository;
import com.box.l10n.mojito.service.asset.AssetService;
Expand Down Expand Up @@ -1153,7 +1153,7 @@ public void testLocalizeAndroidStringsRemoveUntranslated() throws Exception {
}

/**
* This test is to test {@link MacStringsEncoder} with special characters
* This test is to test {@link SimpleEncoder} with special characters
*
* According to iOS specification in
* https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/LoadingResources/Strings/Strings.html,
Expand Down Expand Up @@ -2070,6 +2070,7 @@ public void testLocalizeTSFile() throws Exception {
+ " \"loginText\": \"Log In\",\n"
+ " // signup comment\n"
+ " \"signupText\": \"Sign up\",\n"
+ " \"quotedText\": \"Hello \\\"%s\\\"\",\n"
+ " \"noComment\": \"String with no comment\"\n"
+ " };\n"
+ "}\n"
Expand All @@ -2092,8 +2093,11 @@ public void testLocalizeTSFile() throws Exception {
textUnitSearcherParameters.setRepositoryIds(repo.getId());
textUnitSearcherParameters.setStatusFilter(StatusFilter.FOR_TRANSLATION);
List<TextUnitDTO> textUnitDTOs = textUnitSearcher.search(textUnitSearcherParameters);
assertEquals(3, textUnitDTOs.size());
assertEquals(4, textUnitDTOs.size());
for (TextUnitDTO textUnitDTO : textUnitDTOs) {
if ("quotedText".equals(textUnitDTO.getName())) {
assertEquals("Hello \"%s\"", textUnitDTO.getSource());
}
logger.debug("{}\n{}=[{}]", textUnitDTO.getComment(), textUnitDTO.getName(), textUnitDTO.getSource());
}

Expand Down

0 comments on commit 52f7b81

Please sign in to comment.