Skip to content

Commit 28fed4a

Browse files
committed
Text blocks, Locales, Numbers & Math
1 parent 0494bb3 commit 28fed4a

File tree

6 files changed

+110
-19
lines changed

6 files changed

+110
-19
lines changed

Chapter01/P16_CustomTemplateProcessor4/pom.xml

+10-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@
1010
<maven.compiler.source>21</maven.compiler.source>
1111
<maven.compiler.target>21</maven.compiler.target>
1212

13-
<maven.compiler.plugin>3.6.1</maven.compiler.plugin>
13+
<maven.compiler.plugin>3.6.1</maven.compiler.plugin>
14+
<jackson.version>2.9.7</jackson.version>
1415
</properties>
15-
<name>P16_CustomTemplateProcessor4</name>
16+
<name>P16_CustomTemplateProcessor4</name>
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.fasterxml.jackson.core</groupId>
20+
<artifactId>jackson-databind</artifactId>
21+
<version>${jackson.version}</version>
22+
</dependency>
23+
</dependencies>
1624
<build>
1725
<plugins>
1826
<plugin>
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,51 @@
11
package modern.challenge;
22

3-
import java.util.List;
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import java.io.IOException;
6+
import java.util.regex.Pattern;
47

58
public class Main {
69

7-
public static void main(String[] args) {
8-
9-
PhoneProcessor pp = new PhoneProcessor();
10-
11-
List<String> names = List.of("Arita Ion", "Mark Jurg", "Paul Istrate");
12-
List<String> phones= List.of("244-815-0089", "0721-825-8892", "(045)655-9230");
13-
14-
StringBuilder xmlMessage = new StringBuilder();
15-
for (int i = 0; i < names.size(); i++) {
16-
17-
xmlMessage.append(pp."""
18-
<name>\{names.get(i)}</name>
19-
<phone>\{phones.get(i)}</phone>
20-
""");
21-
}
10+
private static final Pattern PHONE_PATTERN = Pattern.compile(
11+
"\\d{10}|(?:\\d{3}-){2}\\d{4}|\\(\\d{3}\\)\\d{3}-?\\d{4}");
12+
13+
public static void main(String[] args) throws IOException {
14+
15+
StringTemplate.Processor<JsonNode, IllegalArgumentException> pp
16+
= (StringTemplate st) -> {
17+
18+
var values = st.values().stream()
19+
.map(value -> {
20+
if (!PHONE_PATTERN.matcher((CharSequence) value).matches()) {
21+
return "Invalid phone number";
22+
}
23+
24+
return value;
25+
}).toList();
26+
27+
ObjectMapper mapper = new ObjectMapper();
28+
29+
try {
30+
return mapper.readTree(StringTemplate.interpolate(
31+
st.fragments(), values));
32+
} catch (IOException ex) {
33+
throw new RuntimeException(ex);
34+
}
35+
};
36+
37+
String workPhone = "072-825-90095"; // not valid
38+
String homePhone = "(040)234-9670";
39+
40+
JsonNode jsonMessage = pp.
41+
"""
42+
{ "contact": {
43+
"work": "\{workPhone}",
44+
"home": "\{homePhone}"
45+
}
46+
}
47+
""";
2248

23-
System.out.println(xmlMessage);
49+
System.out.println(jsonMessage);
2450
}
2551
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Writing a custom template processor:
2+
Introduce the API for writing a user-defined template processor. Next, provide a few examples of custom template processors.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>java.modern.challenge</groupId>
5+
<artifactId>P16_CustomTemplateProcessor5</artifactId>
6+
<version>1.0</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>21</maven.compiler.source>
11+
<maven.compiler.target>21</maven.compiler.target>
12+
13+
<maven.compiler.plugin>3.6.1</maven.compiler.plugin>
14+
</properties>
15+
<name>P16_CustomTemplateProcessor5</name>
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<groupId>org.apache.maven.plugins</groupId>
20+
<artifactId>maven-compiler-plugin</artifactId>
21+
<version>${maven.compiler.plugin}</version>
22+
<configuration>
23+
<compilerArgs>
24+
<arg>--enable-preview</arg>
25+
</compilerArgs>
26+
</configuration>
27+
</plugin>
28+
</plugins>
29+
</build>
30+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package modern.challenge;
2+
3+
import java.util.List;
4+
5+
public class Main {
6+
7+
public static void main(String[] args) {
8+
9+
PhoneProcessor pp = new PhoneProcessor();
10+
11+
List<String> names = List.of("Arita Ion", "Mark Jurg", "Paul Istrate");
12+
List<String> phones= List.of("244-815-0089", "0721-825-8892", "(045)655-9230");
13+
14+
StringBuilder xmlMessage = new StringBuilder();
15+
for (int i = 0; i < names.size(); i++) {
16+
17+
xmlMessage.append(pp."""
18+
<name>\{names.get(i)}</name>
19+
<phone>\{phones.get(i)}</phone>
20+
""");
21+
}
22+
23+
System.out.println(xmlMessage);
24+
}
25+
}

0 commit comments

Comments
 (0)