forked from box/mojito
-
Notifications
You must be signed in to change notification settings - Fork 4
I18N-2066 Rewrite Processor Implementation #469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
121dc46
I18N-2066 Rewrite Processor Implementation
DarKhaos a168d44
Fix SmartlingPullBatchFileJobTest
DarKhaos 3736b08
Refactor RewriteRuleProcessor to call ignoreOverlaps from the Aho-Cor…
DarKhaos 48ccef2
Merge branch 'master' of https://github.com/pinterest/mojito into I18…
DarKhaos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
112 changes: 112 additions & 0 deletions
112
webapp/src/main/java/com/box/l10n/mojito/service/rewriterule/RewriteRuleProcessor.java
This file contains hidden or 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,112 @@ | ||
| package com.box.l10n.mojito.service.rewriterule; | ||
|
|
||
| import com.box.l10n.mojito.entity.RewriteRule; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Comparator; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import org.ahocorasick.trie.Emit; | ||
| import org.ahocorasick.trie.Trie; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.util.Assert; | ||
| import org.springframework.util.StringUtils; | ||
|
|
||
| @Service | ||
| public class RewriteRuleProcessor { | ||
|
|
||
| private final RewriteRuleService rewriteRuleService; | ||
|
|
||
| public RewriteRuleProcessor(RewriteRuleService rewriteRuleService) { | ||
| this.rewriteRuleService = rewriteRuleService; | ||
| } | ||
|
|
||
| private String resolveVariables(String rewriteTo, String localeTag) { | ||
| Assert.notNull(rewriteTo, "rewriteTo must not be null"); | ||
| Assert.notNull(localeTag, "localeTag must not be null"); | ||
|
|
||
| Locale locale = Locale.forLanguageTag(localeTag); | ||
| String languageValue = locale.getLanguage() == null ? "" : locale.getLanguage(); | ||
| String countryValue = locale.getCountry() == null ? "" : locale.getCountry(); | ||
|
|
||
| return rewriteTo | ||
| .replace("$language", languageValue) | ||
| .replace("$country", countryValue) | ||
| .replace("$locale", localeTag); | ||
| } | ||
|
|
||
| private boolean isRepositorySpecific(RewriteRule rewriteRule) { | ||
| return rewriteRule.getRepository() != null; | ||
| } | ||
|
|
||
| private int getRulePriority(RewriteRule rewriteRule) { | ||
| // Rewrite rules currently only model scope-based precedence. | ||
| return this.isRepositorySpecific(rewriteRule) ? 1 : 0; | ||
| } | ||
|
|
||
| public String processExactMatches( | ||
| String content, Long repositoryId, Long localeId, String localeTag) { | ||
| Assert.notNull(repositoryId, "repositoryId must not be null"); | ||
| Assert.notNull(localeId, "localeId must not be null"); | ||
| Assert.notNull(localeTag, "localeTag must not be null"); | ||
|
|
||
| if (!StringUtils.hasLength(content)) { | ||
| return content; | ||
| } | ||
|
|
||
| List<RewriteRule> rewriteRules = | ||
| rewriteRuleService.findActiveRewriteRules(localeId, repositoryId).stream() | ||
| .filter(rewriteRule -> StringUtils.hasLength(rewriteRule.getRewriteFrom())) | ||
| .toList(); | ||
|
|
||
| if (rewriteRules.isEmpty()) { | ||
| return content; | ||
| } | ||
|
|
||
| Map<String, List<RewriteRule>> rulesByRewriteFrom = new HashMap<>(); | ||
| Trie.TrieBuilder trieBuilder = Trie.builder().ignoreOverlaps(); | ||
|
|
||
| rewriteRules.forEach( | ||
| rewriteRule -> { | ||
| trieBuilder.addKeyword(rewriteRule.getRewriteFrom()); | ||
| rulesByRewriteFrom | ||
| .computeIfAbsent(rewriteRule.getRewriteFrom(), key -> new ArrayList<>()) | ||
| .add(rewriteRule); | ||
| }); | ||
|
|
||
| Trie trie = trieBuilder.build(); | ||
| Collection<Emit> emits = trie.parseText(content); | ||
|
|
||
| if (emits.isEmpty()) { | ||
| return content; | ||
| } | ||
|
|
||
| List<Match> matches = new ArrayList<>(); | ||
| for (Emit emit : emits) { | ||
| List<RewriteRule> emitRules = rulesByRewriteFrom.get(emit.getKeyword()); | ||
| emitRules.sort( | ||
| (leftRule, rightRule) -> | ||
| Integer.compare(this.getRulePriority(rightRule), this.getRulePriority(leftRule))); | ||
| RewriteRule emitRule = emitRules.getFirst(); | ||
| matches.add(new Match(emit.getStart(), emit.getEnd(), emitRule)); | ||
| } | ||
|
|
||
| matches.sort(Comparator.comparingInt(Match::start)); | ||
|
|
||
| StringBuilder rewrittenContent = new StringBuilder(); | ||
| int currentIndex = 0; | ||
|
|
||
| for (Match selectedCandidate : matches) { | ||
| rewrittenContent.append(content, currentIndex, selectedCandidate.start()); | ||
| rewrittenContent.append(resolveVariables(selectedCandidate.rule().getRewriteTo(), localeTag)); | ||
| currentIndex = selectedCandidate.end() + 1; | ||
| } | ||
|
|
||
| rewrittenContent.append(content, currentIndex, content.length()); | ||
| return rewrittenContent.toString(); | ||
| } | ||
|
|
||
| private record Match(int start, int end, RewriteRule rule) {} | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.