Skip to content

Commit 841b6cb

Browse files
committed
Add template examples.
1 parent 9d3f376 commit 841b6cb

File tree

18 files changed

+136
-109
lines changed

18 files changed

+136
-109
lines changed

.idea/gradle.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -626,32 +626,19 @@ this code would produce the following output:
626626
a = hello, b = 123, c = true
627627
```
628628

629-
## TemplateEncoder.Modifier
629+
## Custom Modifiers
630630
Modifiers are created by implementing the `TemplateEncoder.Modifier` interface, which defines the following method:
631631

632632
```java
633633
public Object apply(Object value, String argument, Locale locale);
634634
```
635635

636-
The first argument to this method represents the value to be modified, and the second is the optional argument value following the `=` character in the modifier string. If an argument is not specified, this value will be `null`. The third argument contains the template's locale.
636+
The first argument to this method represents the value to be modified, and the second is the optional argument value following the `=` character in the modifier string. If an argument is not specified, this value will be `null`. The third argument contains the encoder's locale.
637637

638-
TODO Example
639-
640-
For example, the following class implements a modifier that converts values to uppercase:
641-
642-
```java
643-
public class UppercaseModifier implements TemplateEncoder.Modifier {
644-
@Override
645-
public Object apply(Object value, String argument, Locale locale) {
646-
return value.toString().toUpperCase(locale);
647-
}
648-
}
649-
```
650-
651-
Custom modifiers are registered by adding them to the modifier map returned by `TemplateEncoder#getModifiers()`. The map key represents the name that is used to apply a modifier in a template document. For example:
638+
For example, the following code creates a modifier that converts values to uppercase:
652639

653640
```java
654-
TemplateEncoder.getModifiers().put("uppercase", new UppercaseModifier());
641+
TemplateEncoder.getModifiers().put("uppercase", (value, argument, locale) -> value.toString().toUpperCase(locale));
655642
```
656643

657644
Note that modifiers must be thread-safe, since they are shared and may be invoked concurrently by multiple encoder instances.

httprpc-test/src/main/java/org/httprpc/test/mongodb/RestaurantService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.httprpc.WebService;
2525
import org.httprpc.io.CSVEncoder;
2626
import org.httprpc.io.JSONEncoder;
27+
import org.httprpc.io.TemplateEncoder;
2728
import org.httprpc.io.XMLEncoder;
2829
import org.httprpc.RequestMethod;
2930
import org.httprpc.Response;
@@ -86,6 +87,13 @@ public void getRestaurants(String zipCode, String format) throws IOException {
8687
XMLEncoder xmlEncoder = new XMLEncoder();
8788

8889
xmlEncoder.write(cursorAdapter, getResponse().getOutputStream());
90+
} else if (format.equals("html")) {
91+
getResponse().setContentType("text/html");
92+
93+
TemplateEncoder templateEncoder = new TemplateEncoder(getClass().getResource("restaurants.html"));
94+
95+
templateEncoder.setBaseName(getClass().getPackage().getName() + ".restaurants");
96+
templateEncoder.write(cursorAdapter, getResponse().getOutputStream());
8997
} else {
9098
throw new UnsupportedOperationException();
9199
}

httprpc-test/src/main/java/org/httprpc/test/mysql/PetService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.httprpc.WebService;
3838
import org.httprpc.io.CSVEncoder;
3939
import org.httprpc.io.JSONEncoder;
40+
import org.httprpc.io.TemplateEncoder;
4041
import org.httprpc.io.XMLEncoder;
4142
import org.httprpc.RequestMethod;
4243
import org.httprpc.ResourcePath;
@@ -118,6 +119,13 @@ public void getPets(String owner, String format) throws SQLException, IOExceptio
118119
XMLEncoder xmlEncoder = new XMLEncoder();
119120

120121
xmlEncoder.write(resultSetAdapter, getResponse().getOutputStream());
122+
} else if (format.equals("html")) {
123+
getResponse().setContentType("text/html");
124+
125+
TemplateEncoder templateEncoder = new TemplateEncoder(getClass().getResource("pets.html"));
126+
127+
templateEncoder.setBaseName(getClass().getPackage().getName() + ".pets");
128+
templateEncoder.write(resultSetAdapter, getResponse().getOutputStream());
121129
} else {
122130
throw new UnsupportedOperationException();
123131
}

httprpc-test/src/main/resources/org/httprpc/test/mongodb/RestaurantService.properties

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<html>
2+
<head>
3+
<title>Restaurants</title>
4+
</head>
5+
<body>
6+
7+
<!-- {{#.}} -->
8+
<div>
9+
<span>{{@name}}: {{name}}</span><br/>
10+
<span>{{@address}}: {{address.building}} {{address.street}}, {{address.zipcode}}</span><br/>
11+
12+
<div>
13+
<!-- {{#grades}} -->
14+
<span>{{date:format=shortDate}}: {{grade}} ({{score}})</span><br/>
15+
<!-- {{/grades}} -->
16+
</div>
17+
</div>
18+
19+
<hr/>
20+
<!-- {{/.}} -->
21+
22+
</body>
23+
</html>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name = Name
2+
address = Address
3+
cuisine = Cuisine
4+
grades = Grades

httprpc-test/src/main/resources/org/httprpc/test/mysql/EmployeeService.properties

Lines changed: 0 additions & 7 deletions
This file was deleted.

httprpc-test/src/main/resources/org/httprpc/test/mysql/PetService.properties

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<html>
2+
<head>
3+
<title>Pets</title>
4+
</head>
5+
<body>
6+
7+
<table>
8+
<tr>
9+
<td>{{@name}}</td>
10+
<td>{{@species}}</td>
11+
<td>{{@sex}}</td>
12+
<td>{{@birth}}</td>
13+
</tr>
14+
15+
<!-- {{#.}} -->
16+
<tr>
17+
<td>{{name}}</td>
18+
<td>{{species}}</td>
19+
<td>{{sex}}</td>
20+
<td>{{birth}}</td>
21+
</tr>
22+
<!-- {{/.}} -->
23+
</table>
24+
25+
</body>
26+
</html>

0 commit comments

Comments
 (0)