Skip to content

Commit 1c184f3

Browse files
author
Yoshito Umaoka
committed
Test case for custom filter with metadata support
Added a custom resource filter supporting bundle and entry metadata.
1 parent 1160d7b commit 1c184f3

9 files changed

Lines changed: 493 additions & 5 deletions

File tree

gp-res-filter/.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<attribute name="maven.pomderived" value="true"/>
77
</attributes>
88
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/resource"/>
910
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
1011
<attributes>
1112
<attribute name="maven.pomderived" value="true"/>

gp-res-filter/pom.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45

56
<parent>
@@ -27,6 +28,15 @@
2728
<groupId>org.apache.maven.plugins</groupId>
2829
<artifactId>maven-javadoc-plugin</artifactId>
2930
</plugin>
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-surefire-plugin</artifactId>
34+
<configuration>
35+
<additionalClasspathElements>
36+
<additionalClasspathElement>${basedir}/src/test/resource</additionalClasspathElement>
37+
</additionalClasspathElements>
38+
</configuration>
39+
</plugin>
3040
</plugins>
3141
</build>
3242

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
/*
2+
* Copyright IBM Corp. 2018
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ibm.g11n.pipeline.custom;
17+
18+
import java.io.BufferedReader;
19+
import java.io.BufferedWriter;
20+
import java.io.File;
21+
import java.io.FileOutputStream;
22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.io.InputStreamReader;
25+
import java.io.OutputStream;
26+
import java.io.OutputStreamWriter;
27+
import java.io.PrintWriter;
28+
import java.nio.charset.StandardCharsets;
29+
import java.util.HashMap;
30+
import java.util.LinkedList;
31+
import java.util.List;
32+
import java.util.Map;
33+
import java.util.Map.Entry;
34+
35+
import org.junit.Test;
36+
37+
import com.ibm.g11n.pipeline.resfilter.FilterInfo.Type;
38+
import com.ibm.g11n.pipeline.resfilter.FilterOptions;
39+
import com.ibm.g11n.pipeline.resfilter.LanguageBundle;
40+
import com.ibm.g11n.pipeline.resfilter.LanguageBundleBuilder;
41+
import com.ibm.g11n.pipeline.resfilter.ResourceFilter;
42+
import com.ibm.g11n.pipeline.resfilter.ResourceFilterException;
43+
import com.ibm.g11n.pipeline.resfilter.ResourceString;
44+
45+
/**
46+
* Resource filter implementation for testing.
47+
*
48+
* @author Yoshito Umaoka
49+
*/
50+
public class MockResourceFilter extends ResourceFilter {
51+
52+
public static final String ID = "MOCK";
53+
public static final Type TYPE = Type.SINGLE;
54+
55+
// MockResource format
56+
//
57+
// Header lines
58+
//
59+
// ! lang
60+
// # bundle_comment
61+
// @ metadata_key = meadata_value
62+
// ---
63+
//
64+
// Resource entry lines
65+
//
66+
// # bundle_comment1
67+
// # bundle_comment2
68+
// @ metadata_key1 = medata_value1
69+
// @ metadata_key2 = medata_value2
70+
// resource_key = resource_value
71+
//
72+
73+
private static char LANGCODE_MARKER = '!';
74+
private static char COMMENT_MARKER = '#';
75+
private static char METADATA_MARKER = '@';
76+
private static char KEY_VALUE_SEPARATOR_CHAR = '=';
77+
private static String HEADER_SEPARATOR = "---";
78+
79+
@Override
80+
public LanguageBundle parse(InputStream inStream, FilterOptions options)
81+
throws IOException, ResourceFilterException {
82+
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, StandardCharsets.UTF_8));
83+
84+
LanguageBundleBuilder bb = new LanguageBundleBuilder(true);
85+
boolean inHeader = true;
86+
List<String> notes = null;
87+
Map<String, String> metadata = null;
88+
89+
while (true) {
90+
String line = reader.readLine();
91+
if (line == null) {
92+
break;
93+
}
94+
95+
line = line.trim();
96+
if (line.isEmpty()) {
97+
continue;
98+
}
99+
100+
int idx;
101+
if (inHeader) {
102+
if (line.charAt(0) == LANGCODE_MARKER) {
103+
// language code
104+
String langCode = line.substring(1).trim();
105+
bb.embeddedLanguageCode(langCode);
106+
} else if (line.charAt(0) == COMMENT_MARKER) {
107+
// bundle comment
108+
bb.addNote(line.substring(1).trim());
109+
} else if (line.charAt(0) == METADATA_MARKER) {
110+
// bundle metadata
111+
String kv = line.substring(1);
112+
idx = kv.indexOf(KEY_VALUE_SEPARATOR_CHAR);
113+
if (idx <= 0) {
114+
continue;
115+
}
116+
117+
String metaKey = kv.substring(0, idx).trim();
118+
if (metaKey.isEmpty()) {
119+
continue;
120+
}
121+
String metaVal = kv.substring(idx + 1).trim();
122+
bb.addMetadata(metaKey, metaVal);
123+
} else if (line.equals(HEADER_SEPARATOR)) {
124+
inHeader = false;
125+
}
126+
} else {
127+
if (line.charAt(0) == COMMENT_MARKER) {
128+
if (notes == null) {
129+
notes = new LinkedList<>();
130+
}
131+
notes.add(line.substring(1).trim());
132+
} else if (line.charAt(0) == METADATA_MARKER) {
133+
String kv = line.substring(1);
134+
idx = kv.indexOf(KEY_VALUE_SEPARATOR_CHAR);
135+
if (idx <= 0) {
136+
continue;
137+
}
138+
139+
String metaKey = kv.substring(0, idx).trim();
140+
if (metaKey.isEmpty()) {
141+
continue;
142+
}
143+
String metaVal = kv.substring(idx + 1).trim();
144+
if (metadata == null) {
145+
metadata = new HashMap<>();
146+
}
147+
metadata.put(metaKey, metaVal);
148+
} else {
149+
idx = line.indexOf(KEY_VALUE_SEPARATOR_CHAR);
150+
if (idx <= 0) {
151+
continue;
152+
}
153+
154+
String resKey = line.substring(0, idx).trim();
155+
String resVal = line.substring(idx + 1).trim();
156+
157+
bb.addResourceString(
158+
ResourceString
159+
.with(resKey, resVal)
160+
.notes(notes)
161+
.metadata(metadata));
162+
163+
// reset notes/metadata
164+
notes = null;
165+
metadata = null;
166+
}
167+
}
168+
}
169+
170+
return bb.build();
171+
}
172+
173+
@Override
174+
public void write(OutputStream outStream, LanguageBundle languageBundle, FilterOptions options)
175+
throws IOException, ResourceFilterException {
176+
177+
try (PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outStream, StandardCharsets.UTF_8)))) {
178+
String langCode = languageBundle.getEmbeddedLanguageCode();
179+
if (langCode != null) {
180+
StringBuilder langCodeLine = new StringBuilder();
181+
langCodeLine.append(LANGCODE_MARKER).append(langCode);
182+
writer.println(langCodeLine);
183+
}
184+
185+
List<String> bundleNotes = languageBundle.getNotes();
186+
for (String bundleNote : bundleNotes) {
187+
StringBuilder noteLine = new StringBuilder();
188+
noteLine.append(COMMENT_MARKER).append(bundleNote);
189+
writer.println(noteLine);
190+
}
191+
192+
Map<String, String> bundleMetadata = languageBundle.getMetadata();
193+
for (Entry<String, String> bundleMetaKV : bundleMetadata.entrySet()) {
194+
StringBuilder metadataLine = new StringBuilder();
195+
metadataLine.append(METADATA_MARKER)
196+
.append(bundleMetaKV.getKey()).append(KEY_VALUE_SEPARATOR_CHAR).append(bundleMetaKV.getValue());
197+
writer.println(metadataLine);
198+
}
199+
200+
writer.println(HEADER_SEPARATOR);
201+
202+
List<ResourceString> resStrings = languageBundle.getSortedResourceStrings();
203+
for (ResourceString resString : resStrings) {
204+
writer.println();
205+
206+
// write resource entry notes
207+
List<String> notes = resString.getNotes();
208+
for (String note : notes) {
209+
StringBuilder noteLine = new StringBuilder();
210+
noteLine.append(COMMENT_MARKER).append(note);
211+
writer.println(noteLine);
212+
}
213+
214+
// write resource entry metadata
215+
Map<String, String> metadata = resString.getMetadata();
216+
for (Entry<String, String> metaKV : metadata.entrySet()) {
217+
StringBuilder metadataLine = new StringBuilder();
218+
metadataLine.append(METADATA_MARKER)
219+
.append(metaKV.getKey()).append(KEY_VALUE_SEPARATOR_CHAR).append(metaKV.getValue());
220+
writer.println(metadataLine);
221+
}
222+
223+
// write resoruce entry key and value
224+
StringBuilder resKV = new StringBuilder();
225+
resKV.append(resString.getKey()).append(KEY_VALUE_SEPARATOR_CHAR).append(resString.getValue());
226+
writer.println(resKV);
227+
}
228+
}
229+
}
230+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright IBM Corp. 2018
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.ibm.g11n.pipeline.custom;
17+
18+
import java.util.Arrays;
19+
import java.util.Collections;
20+
import java.util.Iterator;
21+
import java.util.List;
22+
import java.util.Locale;
23+
24+
import com.ibm.g11n.pipeline.resfilter.FilterInfo;
25+
import com.ibm.g11n.pipeline.resfilter.MultiBundleResourceFilter;
26+
import com.ibm.g11n.pipeline.resfilter.ResourceFilter;
27+
import com.ibm.g11n.pipeline.resfilter.ResourceFilterProvider;
28+
29+
/**
30+
* Custom resource filter provider for testing.
31+
*
32+
* @author Yoshito Umaoka
33+
*/
34+
public class MockResourceFilterProvider extends ResourceFilterProvider{
35+
36+
private static final List<FilterInfo> FILTERS = Collections.unmodifiableList(
37+
Arrays.asList(
38+
new FilterInfo(MockResourceFilter.TYPE, MockResourceFilter.ID)));
39+
40+
@Override
41+
public Iterator<FilterInfo> getAvailableResourceFilters() {
42+
return FILTERS.iterator();
43+
}
44+
45+
@Override
46+
public ResourceFilter getResourceFilter(String id) {
47+
if (id.toUpperCase(Locale.ROOT).equals(MockResourceFilter.ID)) {
48+
return new MockResourceFilter();
49+
}
50+
return null;
51+
}
52+
53+
@Override
54+
public MultiBundleResourceFilter getMultiBundleResourceFilter(String id) {
55+
return null;
56+
}
57+
58+
}

0 commit comments

Comments
 (0)