6 package com.google.appinventor.components.runtime.util;
8 import android.util.Log;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.Comparator;
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.List;
17 import java.util.TreeSet;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
34 private static class MappingOrder {
40 public void changeOrder(List<String> keys, String text) {
48 private static class MappingLongestStringFirstOrder
extends MappingOrder {
50 public void changeOrder(List<String> keys, String text) {
51 Collections.sort(keys,
new Comparator<String>() {
53 public int compare(String s, String t1) {
55 return Integer.compare(t1.length(), s.length());
70 private static class MappingEarliestOccurrenceFirstOrder
extends MappingOrder {
72 public void changeOrder(List<String> keys, String text) {
78 for (String key : keys) {
79 int firstIndex = text.indexOf(key);
83 if (firstIndex == -1) {
84 firstIndex = text.length() + occurrenceIndices.size();
88 occurrenceIndices.put(key, firstIndex);
91 Collections.sort(keys,
new Comparator<String>() {
93 public int compare(String s, String t1) {
95 int id1 = occurrenceIndices.get(s);
96 int id2 = occurrenceIndices.get(t1);
100 return Integer.compare(t1.length(), s.length());
103 return Integer.compare(id1, id2);
114 private static class Range {
126 public Range(
int start,
int end, String text) {
136 private static class RangeComparator
implements Comparator<Range> {
138 public int compare(Range r1, Range r2) {
141 int maxStart = Math.max(r1.start, r2.start);
142 int minEnd = Math.min(r1.end, r2.end);
147 if (maxStart < minEnd) {
153 return Integer.compare(r2.end, r1.end);
159 private static final boolean DEBUG =
false;
165 private static final MappingOrder mappingOrderDictionary =
new MappingOrder();
166 private static final MappingOrder mappingOrderLongestStringFirst =
new MappingLongestStringFirstOrder();
167 private static final MappingOrder mappingOrderEarliestOccurrence =
new MappingEarliestOccurrenceFirstOrder();
168 private static final Comparator<Range> rangeComparator =
new RangeComparator();
189 public static String
joinStrings(List<Object> listOfStrings, String separator) {
194 return join(listOfStrings, separator);
197 private static String join(List<Object> list, String separator)
199 StringBuilder sb =
new StringBuilder();
200 boolean first =
true;
201 for (Object item : list)
206 sb.append(separator);
207 sb.append(item.toString());
209 return sb.toString();
267 Iterator<
Map.Entry<Object, Object>> it = mappings.entrySet().
iterator();
274 List<String> keys =
new ArrayList<>();
276 while (it.hasNext()) {
277 Map.Entry<Object, Object> current = it.next();
282 String key = current.getKey().toString();
283 String value = current.getValue().toString();
287 if (!stringMappings.containsKey(key)) {
292 stringMappings.put(key, value);
299 order.changeOrder(keys, text);
302 return applyMappings(text, stringMappings, keys);
330 private static String applyMappings(String text,
Map<String, String> mappings, List<String> keys) {
335 TreeSet<Range> ranges =
new TreeSet<Range>(rangeComparator);
339 for (String key : keys) {
342 Pattern keyPattern = Pattern.compile(Pattern.quote(key));
343 Matcher matcher = keyPattern.matcher(text);
346 String replacement = mappings.get(key);
349 while (matcher.find()) {
351 int startId = matcher.start();
352 int endId = matcher.end();
358 Range range =
new Range(startId, endId, replacement);
370 for (Range range : ranges) {
375 String left = text.substring(0, range.start);
376 String middle = range.text;
377 String end = text.substring(range.end);
378 text = left + middle + end;