-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some refactoring to reuse code to escape/unescape double quotes (#365)
- Loading branch information
Showing
5 changed files
with
62 additions
and
50 deletions.
There are no files selected for viewing
4 changes: 1 addition & 3 deletions
4
webapp/src/main/java/com/box/l10n/mojito/okapi/filters/JSFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
webapp/src/main/java/com/box/l10n/mojito/okapi/filters/RegexEscapeDoubleQuoteFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters