-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.in
197 lines (158 loc) · 5.31 KB
/
README.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
## jxtrand
Utility classes for XML string resources, and referential integrity for
string resources.
## Features
* Define string resources as XML in a standardized format.
* Developer-friendly message formatting API.
* Maven plugin for generating type-safe string resource accessors; no more `MissingResourceException` errors!
* Written in pure Java 17.
* [OSGi](https://www.osgi.org/) ready.
* [JPMS](https://en.wikipedia.org/wiki/Java_Platform_Module_System) ready.
* ISC license.
* High-coverage automated test suite.
## Motivation
Java exposes string resources that can be localized using the `ResourceBundle`
class. This can be backed by properties in a line-oriented text format. This
text format is somewhat convenient for developers, but suffers from being
line-oriented and whitespace sensitive; it becomes awkward to add significant
whitespace in strings. Java properties can also be represented in an XML-based
format that handles this better, but there is no built-in support for
loading a `ResourceBundle` from an XML file. Additionally, the actual logic
used by the `ResourceBundle` class to find property files is confusing and
complex.
Lastly, Java applications using localized strings have the property names
as string constants in the code. This means the code is free to drift out
of sync with the property files; code can refer to properties that do not
exist, and properties can become unused without much in the way of tool support
to detect unused properties.
## Usage
### XML Files
The `jxtrand` package provides a convenient abstract class to load resources
from XML property files. For example, an application could define the following
class:
```
/**
* An example where the resource is exported.
*/
public final class ExampleStrings0 extends JXTAbstractStrings
{
/**
* Construct an example.
*
* @param locale The locale
*
* @throws IOException On I/O errors
*/
public ExampleStrings0(final Locale locale)
throws IOException
{
super(
locale,
ExampleStrings0.class,
"/com/io7m/jxtrand/examples",
"Messages"
);
}
}
```
The class takes a `Locale` as an argument, and will look in the directory
`/com/io7m/jxtrand/examples` in the module containing `ExampleStrings0.class`;
`jxtrand` is module-aware.
Given:
```
final var lang = locale.getLanguage();
final var country = locale.getCountry();
final var ex = locale.getVariant();
```
The lookup procedure will look at the following files in order:
```
/com/io7m/jxtrand/examples/Messages_${lang}_${country}_${ex}.xml
/com/io7m/jxtrand/examples/Messages_${lang}_${country}.xml
/com/io7m/jxtrand/examples/Messages_${lang}.xml
/com/io7m/jxtrand/examples/Messages.xml
```
### Modules
When using `jxtrand` in a modular context, the `jxtrand` will need to be
able to search, reflectively, for resources inside application modules.
Therefore, those modules need to be open for reflection to `jxtrand`. The
following is typically necessary in module descriptors (assuming that
resource files are kept in `/com/io7m/example/internal`:
```
module com.io7m.example
{
opens com.io7m.example.internal
to com.io7m.jxtrand.vanilla;
exports com.io7m.example;
}
```
### Resource Compiler
The `jxtrand` package provides a simple resource compiler that can produce
`enum` classes from string property files (in XML or line-based formats).
Given a property file such as:
```
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="error.01">Error 1!</entry>
<entry key="example2">Example 2</entry>
</properties>
```
The compiler can produce an `enum` file such as:
```
public enum Strings implements JXTStringConstantType
{
ERROR_01 {
@Override
public String propertyName()
{
return "error.01";
}
},
EXAMPLE2 {
@Override
public String propertyName()
{
return "example2";
}
}
}
```
These constants can be used directly with instances of the `JXTStringsType`
class:
```
JXTStringsType resources;
String formatted = resources.format(ERROR_01);
```
### Maven Plugin
The `jxtrand` package publishes a Maven plugin to perform resource compilation
as part of a build. Use a plugin execution similar to the following:
```
<build>
<plugins>
<plugin>
<groupId>${project.groupId}</groupId>
<artifactId>com.io7m.jxtrand.maven_plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<id>generate-resources-0</id>
<goals>
<goal>generateSources</goal>
</goals>
<configuration>
<inputFile>${project.basedir}/src/main/resources/com/io7m/jxtrand/examples/internal/Messages.xml</inputFile>
<packageName>com.io7m.jxtrand.examples</packageName>
<className>GeneratedStrings</className>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
```
In the above example, the file `src/main/resources/com/io7m/jxtrand/examples/internal/Messages.xml`
would be used to generate a Java class `com.io7m.jxtrand.examples.GeneratedStrings`. The
class is generated as a plain Java source file and is placed in
`${project.build.directory}/generated-sources/jxtrand` by default
(and the `jxtrand` directory is registered as a generated source directory
automatically).