Skip to content

Commit 4dc0616

Browse files
hive-botclaude
andcommitted
fix: store trimmed glossary terms and check every path that sets one
GlossaryEntries.put() validated a trimmed copy but stored the untrimmed terms, which were then sent. It now stores the trimmed terms, trims with Character.isWhitespace() like spoken terms, checks terms set through replaceAll() and Map.Entry.setValue(), and prints the invalid code point in hexadecimal. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a689507 commit 4dc0616

5 files changed

Lines changed: 172 additions & 27 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
254254
- `translateDocumentDownload()` maps an error response without body by its
255255
status, for example to `QuotaExceededException` or
256256
`DocumentNotReadyException`, instead of a plain `DeepLException`.
257+
- `GlossaryEntries` stores and sends trimmed terms, trims all whitespace
258+
(`Character.isWhitespace()`) like spoken terms, checks target terms set
259+
through `replaceAll()` or `Map.Entry.setValue()`, and names an invalid
260+
character in hexadecimal (`U+001F`).
257261
- Translation memory and translation memory job IDs are now URL-encoded in
258262
request paths.
259263
- The style rule and custom instruction methods now URL-encode the style rule

‎README.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,11 @@ class Example { // Continuing class Example from above
501501

502502
To construct the GlossaryEntries, you can insert entries using typical Map
503503
functions like `put()`. The `fromTsv()` function allows creating GlossaryEntries
504-
from TSV data.
504+
from TSV data. Each term is trimmed of leading and trailing whitespace, then
505+
checked: a term that is empty or contains a control character throws an
506+
`IllegalArgumentException`. The trimmed terms are stored and sent. Entries can
507+
also be changed with `replaceAll()` or `Map.Entry.setValue()`, which check the
508+
new terms the same way.
505509

506510
You can also create a glossary using a glossary downloaded from the DeepL
507511
website by using `createMultilingualGlossaryFromCsv()` with either a CSV file,

‎deepl-java/src/main/java/com/deepl/api/GlossaryEntries.java‎

Lines changed: 123 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import com.deepl.api.utils.*;
77
import java.util.*;
8+
import java.util.function.BiFunction;
89
import org.jetbrains.annotations.*;
910

1011
/** Stores the entries of a glossary. */
@@ -45,8 +46,6 @@ public static GlossaryEntries fromTsv(String tsv) {
4546
} else {
4647
String sourceTerm = trimWhitespace(splitLine[0]);
4748
String targetTerm = trimWhitespace(splitLine[1]);
48-
validateGlossaryTerm(sourceTerm);
49-
validateGlossaryTerm(targetTerm);
5049
if (result.containsKey(sourceTerm)) {
5150
throw new IllegalArgumentException(
5251
String.format(
@@ -98,17 +97,33 @@ public String get(Object key) {
9897
}
9998

10099
/**
101-
* Adds the given source term and target term to the glossary entries.
100+
* Adds the given source term and target term to the glossary entries. Both terms are trimmed of
101+
* leading and trailing whitespace, then checked with {@link #validateGlossaryTerm}; the trimmed
102+
* terms are stored.
102103
*
103104
* @param sourceTerm key with which the specified value is to be associated
104105
* @param targetTerm value to be associated with the specified key
105106
* @return The previous target term associated with this source term, or null if this source term
106107
* was not present.
107108
*/
108109
public String put(String sourceTerm, String targetTerm) throws IllegalArgumentException {
109-
validateGlossaryTerm(sourceTerm);
110-
validateGlossaryTerm(targetTerm);
111-
return entries.put(sourceTerm, targetTerm);
110+
String source = checkedTerm(sourceTerm);
111+
return entries.put(source, checkedTerm(targetTerm));
112+
}
113+
114+
/**
115+
* Replaces each target term with the result of the given function. Every new term is trimmed and
116+
* checked like in {@link #put}; if any is invalid, no entry is changed.
117+
*/
118+
@Override
119+
public void replaceAll(BiFunction<? super String, ? super String, ? extends String> function) {
120+
Map<String, String> replaced = new HashMap<>();
121+
for (Map.Entry<String, String> entryPair : entries.entrySet()) {
122+
replaced.put(
123+
entryPair.getKey(),
124+
checkedTerm(function.apply(entryPair.getKey(), entryPair.getValue())));
125+
}
126+
entries.putAll(replaced);
112127
}
113128

114129
@Override
@@ -140,10 +155,101 @@ public Collection<String> values() {
140155
return entries.values();
141156
}
142157

158+
/**
159+
* Returns a view of the entries. {@link Map.Entry#setValue} trims and checks the new target term
160+
* like {@link #put}.
161+
*/
143162
@NotNull
144163
@Override
145164
public Set<Entry<String, String>> entrySet() {
146-
return entries.entrySet();
165+
Set<Entry<String, String>> backing = entries.entrySet();
166+
return new AbstractSet<Entry<String, String>>() {
167+
@Override
168+
public @NotNull Iterator<Entry<String, String>> iterator() {
169+
Iterator<Entry<String, String>> iterator = backing.iterator();
170+
return new Iterator<Entry<String, String>>() {
171+
@Override
172+
public boolean hasNext() {
173+
return iterator.hasNext();
174+
}
175+
176+
@Override
177+
public Entry<String, String> next() {
178+
return new CheckedEntry(iterator.next());
179+
}
180+
181+
@Override
182+
public void remove() {
183+
iterator.remove();
184+
}
185+
};
186+
}
187+
188+
@Override
189+
public int size() {
190+
return backing.size();
191+
}
192+
193+
@Override
194+
public boolean contains(Object o) {
195+
return backing.contains(o);
196+
}
197+
198+
@Override
199+
public boolean remove(Object o) {
200+
return backing.remove(o);
201+
}
202+
203+
@Override
204+
public void clear() {
205+
backing.clear();
206+
}
207+
};
208+
}
209+
210+
/** An entry of the backing map whose setValue checks the new target term. */
211+
private static final class CheckedEntry implements Map.Entry<String, String> {
212+
private final Map.Entry<String, String> entry;
213+
214+
CheckedEntry(Map.Entry<String, String> entry) {
215+
this.entry = entry;
216+
}
217+
218+
@Override
219+
public String getKey() {
220+
return entry.getKey();
221+
}
222+
223+
@Override
224+
public String getValue() {
225+
return entry.getValue();
226+
}
227+
228+
@Override
229+
public String setValue(String value) {
230+
return entry.setValue(checkedTerm(value));
231+
}
232+
233+
@Override
234+
public boolean equals(Object o) {
235+
return entry.equals(o);
236+
}
237+
238+
@Override
239+
public int hashCode() {
240+
return entry.hashCode();
241+
}
242+
243+
@Override
244+
public String toString() {
245+
return entry.toString();
246+
}
247+
}
248+
249+
/** Validates the given term and returns it trimmed. */
250+
private static String checkedTerm(String term) {
251+
validateGlossaryTerm(term);
252+
return trimWhitespace(term);
147253
}
148254

149255
/**
@@ -165,7 +271,7 @@ public static void validateGlossaryTerm(String term) throws IllegalArgumentExcep
165271
if ((ch <= 31) || (128 <= ch && ch <= 159) || ch == '\u2028' || ch == '\u2029') {
166272
throw new IllegalArgumentException(
167273
String.format(
168-
"Term '%s' contains invalid character: '%c' (U+%04d)", term, ch, (int) ch));
274+
"Term '%s' contains invalid character: '%c' (U+%04X)", term, ch, (int) ch));
169275
}
170276
}
171277
}
@@ -188,30 +294,21 @@ public String toTsv() {
188294
}
189295

190296
/**
191-
* Strips whitespace characters from the beginning and end of the given string. Implemented here
192-
* because String.strip() is not available in Java 8.
297+
* Strips whitespace as defined by {@link Character#isWhitespace} from the beginning and end of
298+
* the given string, like spoken terms. Implemented here because String.strip() is not in Java 8.
193299
*
194300
* @param input String to have whitespace trimmed.
195301
* @return Input string with whitespace removed from ends.
196302
*/
197303
private static String trimWhitespace(String input) {
198-
int left = 0;
199-
for (; left < input.length(); left++) {
200-
char ch = input.charAt(left);
201-
if (ch != ' ' && ch != '\t') {
202-
break;
203-
}
304+
int start = 0;
305+
int end = input.length();
306+
while (start < end && Character.isWhitespace(input.charAt(start))) {
307+
start++;
204308
}
205-
if (left >= input.length()) {
206-
return "";
207-
}
208-
int right = input.length() - 1;
209-
for (; left < right; right--) {
210-
char ch = input.charAt(right);
211-
if (ch != ' ' && ch != '\t') {
212-
break;
213-
}
309+
while (end > start && Character.isWhitespace(input.charAt(end - 1))) {
310+
end--;
214311
}
215-
return input.substring(left, right + 1);
312+
return input.substring(start, end);
216313
}
217314
}

‎deepl-java/src/offlineTest/java/com/deepl/api/GlossariesTest.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
import static org.junit.jupiter.api.Assertions.assertEquals;
1414
import static org.junit.jupiter.api.Assertions.assertFalse;
1515
import static org.junit.jupiter.api.Assertions.assertThrows;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
1617

1718
import java.io.File;
1819
import java.nio.file.Path;
1920
import java.util.Collections;
2021
import java.util.List;
22+
import java.util.Map;
2123
import org.junit.jupiter.api.Test;
2224
import org.junit.jupiter.api.extension.RegisterExtension;
2325
import org.junit.jupiter.api.io.TempDir;
@@ -205,4 +207,30 @@ void throwsIllegalArgumentWhenTsvEntriesAreInvalid() {
205207
IllegalArgumentException.class,
206208
() -> new GlossaryEntries(Collections.singletonMap("A", "B\tC")));
207209
}
210+
211+
@Test
212+
void namesTheInvalidCharacterInHexadecimal() {
213+
IllegalArgumentException thrown =
214+
assertThrows(
215+
IllegalArgumentException.class,
216+
() -> new GlossaryEntries(Collections.singletonMap("A", "B\u001fC")));
217+
assertTrue(thrown.getMessage().contains("U+001F"), thrown.getMessage());
218+
}
219+
220+
@Test
221+
void rejectsInvalidTargetTermsSetThroughReplaceAll() {
222+
GlossaryEntries entries = new GlossaryEntries(Collections.singletonMap("a", "b"));
223+
assertThrows(IllegalArgumentException.class, () -> entries.replaceAll((k, v) -> "x\ty"));
224+
assertEquals("b", entries.get("a"));
225+
entries.replaceAll((k, v) -> " c ");
226+
assertEquals("c", entries.get("a"));
227+
}
228+
229+
@Test
230+
void rejectsInvalidTargetTermsSetThroughEntrySetValue() {
231+
GlossaryEntries entries = new GlossaryEntries(Collections.singletonMap("a", "b"));
232+
Map.Entry<String, String> entry = entries.entrySet().iterator().next();
233+
assertThrows(IllegalArgumentException.class, () -> entry.setValue("x\ty"));
234+
assertEquals("b", entries.get("a"));
235+
}
208236
}

‎deepl-java/src/offlineTest/java/com/deepl/api/MultilingualGlossariesTest.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,18 @@ void sendsTsvEntriesWhenReplacingADictionary() throws Exception {
233233
assertEquals(3, dictionary.getEntryCount());
234234
}
235235

236+
@Test
237+
void sendsTrimmedTermsWhenReplacingADictionary() throws Exception {
238+
wm.stubFor(
239+
put("/v3/glossaries/g1/dictionaries")
240+
.withRequestBody(
241+
equalToJson(
242+
"{\"source_lang\":\"en\",\"target_lang\":\"de\","
243+
+ "\"entries\":\"key1\\tvalue1\",\"entries_format\":\"tsv\"}"))
244+
.willReturn(okJson(DICTIONARY)));
245+
client.replaceMultilingualGlossaryDictionary("g1", "en", "de", entries(" key1\n", "value1 "));
246+
}
247+
236248
@Test
237249
void sendsTheIdOfAGlossaryObjectWhenReplacingADictionary() throws Exception {
238250
wm.stubFor(

0 commit comments

Comments
 (0)