Skip to content

Commit 21f7286

Browse files
committed
Update examples.
1 parent 41a7759 commit 21f7286

File tree

2 files changed

+62
-59
lines changed

2 files changed

+62
-59
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -542,9 +542,7 @@ csvEncoder.write(rows, System.out);
542542
```
543543

544544
## TemplateEncoder
545-
The `TemplateEncoder` class transforms an object hierarchy (known as a "data dictionary") into an output format using a [template document](template-reference.md). Template syntax is based loosely on the [Mustache](https://mustache.github.io) specification and supports most Mustache features.
546-
547-
For example, this code applies a template named _example.html_ to a map instance:
545+
The `TemplateEncoder` class transforms an object hierarchy (known as a "data dictionary") into an output format using a [template document](template-reference.md). Template syntax is based loosely on the [Mustache](https://mustache.github.io) specification and supports most Mustache features. For example:
548546

549547
```java
550548
var map = mapOf(

kilo-test/src/test/java/org/httprpc/kilo/examples/Examples.java

Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,29 @@ public interface Example {
4545
public static void main(String[] args) {
4646
execute("Math Service 1", Examples::mathService1);
4747
execute("Math Service 2", Examples::mathService2);
48+
4849
execute("JSON Encoder", Examples::jsonEncoder);
4950
execute("Text Encoder", Examples::textEncoder);
5051
execute("CSV Encoder", Examples::csvEncoder);
52+
5153
execute("Template Encoder", Examples::templateEncoder);
52-
execute("Adapt Bean", Examples::adaptBean);
53-
execute("Coerce Bean", Examples::coerceBean);
54-
execute("Interface Proxy", Examples::interfaceProxy);
55-
execute("Required Property 1", Examples::requiredProperty1);
56-
execute("Required Property 2", Examples::requiredProperty2);
57-
execute("Element Adapter", Examples::elementAdapter);
58-
execute("Collections", Examples::collections);
5954
execute("Variables", Examples::variables);
6055
execute("Repeating Sections", Examples::repeatingSections);
6156
execute("Conditional Sections", Examples::conditionalSections);
6257
execute("Inverted Sections", Examples::invertedSections);
6358
execute("Resources", Examples::resources);
6459
execute("Includes", Examples::includes);
6560
execute("Comments", Examples::comments);
61+
62+
execute("Adapt Bean", Examples::adaptBean);
63+
execute("Coerce Bean", Examples::coerceBean);
64+
execute("Interface Proxy", Examples::interfaceProxy);
65+
execute("Required Property 1", Examples::requiredProperty1);
66+
execute("Required Property 2", Examples::requiredProperty2);
67+
68+
execute("Element Adapter", Examples::elementAdapter);
69+
70+
execute("Collections", Examples::collections);
6671
}
6772

6873
private static void execute(String label, Example example) {
@@ -164,6 +169,55 @@ public static void templateEncoder() throws Exception {
164169
templateEncoder.write(map, System.out);
165170
}
166171

172+
public static void variables() throws IOException {
173+
templateExample("variables");
174+
}
175+
176+
public static void repeatingSections() throws IOException {
177+
templateExample("repeating-sections");
178+
}
179+
180+
public static void conditionalSections() throws IOException {
181+
templateExample("conditional-sections");
182+
}
183+
184+
public static void invertedSections() throws IOException {
185+
templateExample("inverted-sections");
186+
}
187+
188+
public static void resources() throws IOException {
189+
templateExample("resources");
190+
}
191+
192+
public static void includes() throws IOException {
193+
templateExample("includes");
194+
}
195+
196+
public static void comments() throws IOException {
197+
templateExample("comments");
198+
}
199+
200+
private static void templateExample(String name) throws IOException {
201+
var jsonDecoder = new JSONDecoder();
202+
203+
Object dictionary;
204+
try (var inputStream = Examples.class.getResourceAsStream(String.format("%s.json", name))) {
205+
dictionary = jsonDecoder.read(new InputStreamReader(inputStream));
206+
}
207+
208+
var templateEncoder = new TemplateEncoder(Examples.class, String.format("%s.html", name));
209+
210+
try {
211+
var resourceBundle = ResourceBundle.getBundle(String.format("%s.%s", Examples.class.getPackageName(), name));
212+
213+
templateEncoder.setResourceBundle(resourceBundle);
214+
} catch (MissingResourceException exception) {
215+
// No-op
216+
}
217+
218+
templateEncoder.write(dictionary, System.out);
219+
}
220+
167221
public static void adaptBean() {
168222
var course = new Course();
169223

@@ -282,53 +336,4 @@ public static void collections() {
282336

283337
System.out.println(set.contains("a")); // true
284338
}
285-
286-
public static void variables() throws IOException {
287-
templateExample("variables");
288-
}
289-
290-
public static void repeatingSections() throws IOException {
291-
templateExample("repeating-sections");
292-
}
293-
294-
public static void conditionalSections() throws IOException {
295-
templateExample("conditional-sections");
296-
}
297-
298-
public static void invertedSections() throws IOException {
299-
templateExample("inverted-sections");
300-
}
301-
302-
public static void resources() throws IOException {
303-
templateExample("resources");
304-
}
305-
306-
public static void includes() throws IOException {
307-
templateExample("includes");
308-
}
309-
310-
public static void comments() throws IOException {
311-
templateExample("comments");
312-
}
313-
314-
private static void templateExample(String name) throws IOException {
315-
var jsonDecoder = new JSONDecoder();
316-
317-
Object dictionary;
318-
try (var inputStream = Examples.class.getResourceAsStream(String.format("%s.json", name))) {
319-
dictionary = jsonDecoder.read(new InputStreamReader(inputStream));
320-
}
321-
322-
var templateEncoder = new TemplateEncoder(Examples.class, String.format("%s.html", name));
323-
324-
try {
325-
var resourceBundle = ResourceBundle.getBundle(String.format("%s.%s", Examples.class.getPackageName(), name));
326-
327-
templateEncoder.setResourceBundle(resourceBundle);
328-
} catch (MissingResourceException exception) {
329-
// No-op
330-
}
331-
332-
templateEncoder.write(dictionary, System.out);
333-
}
334339
}

0 commit comments

Comments
 (0)