Skip to content

Commit 01b3c82

Browse files
committed
Update DTDEncoder.
1 parent 2d1c417 commit 01b3c82

File tree

3 files changed

+91
-23
lines changed

3 files changed

+91
-23
lines changed

sierra-tools/dtd-encoder/build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ plugins {
1818

1919
dependencies {
2020
implementation project(':sierra')
21+
22+
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.1'
23+
testImplementation 'org.jfree:jfreechart:1.5.6'
24+
25+
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
26+
}
27+
28+
tasks.named('test', Test) {
29+
useJUnitPlatform()
2130
}
2231

2332
application {

sierra-tools/dtd-encoder/src/main/java/org/httprpc/sierra/tools/DTDEncoder.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@
3535
import java.awt.Color;
3636
import java.awt.Font;
3737
import java.awt.Image;
38-
import java.io.File;
39-
import java.io.FileInputStream;
40-
import java.io.FileOutputStream;
4138
import java.io.IOException;
4239
import java.io.Writer;
40+
import java.nio.file.Files;
41+
import java.nio.file.Path;
4342
import java.util.ArrayList;
4443
import java.util.Comparator;
4544
import java.util.HashMap;
@@ -253,24 +252,11 @@ private void declareAttributeList(String tag, Class<?> type, Writer writer) thro
253252
writer.append(";>\n");
254253
}
255254

256-
@SuppressWarnings("unchecked")
257255
public static void main(String[] args) throws Exception {
258-
if (args.length > 0) {
259-
var bindings = new Properties();
260-
261-
var directory = new File(System.getProperty("user.dir"));
262-
var file = new File(directory, args[0]);
263-
264-
try (var inputStream = new FileInputStream(file)) {
265-
bindings.load(inputStream);
266-
}
256+
var workingPath = Path.of(System.getProperty("user.dir"));
267257

268-
for (var entry : bindings.entrySet()) {
269-
var tag = (String)entry.getKey();
270-
var type = (Class<? extends JComponent>)Class.forName((String)entry.getValue());
271-
272-
UILoader.bind(tag, type, () -> null);
273-
}
258+
if (args.length > 0) {
259+
applyBindings(workingPath.resolve(args[0]));
274260
}
275261

276262
var typeSet = new HashSet<Class<?>>();
@@ -293,15 +279,29 @@ public static void main(String[] args) throws Exception {
293279

294280
typeList.sort(Comparator.comparing(DTDEncoder::getDepth).thenComparing(Class::getCanonicalName));
295281

296-
var dtdEncoder = new DTDEncoder(typeList, tags);
297-
298-
var file = new File(new File(System.getProperty("user.dir")), "sierra.dtd");
282+
try (var outputStream = Files.newOutputStream(workingPath.resolve("sierra.dtd"))) {
283+
var dtdEncoder = new DTDEncoder(typeList, tags);
299284

300-
try (var outputStream = new FileOutputStream(file)) {
301285
dtdEncoder.write(null, outputStream);
302286
}
303287
}
304288

289+
@SuppressWarnings("unchecked")
290+
private static void applyBindings(Path path) throws IOException, ClassNotFoundException {
291+
var bindings = new Properties();
292+
293+
try (var inputStream = Files.newInputStream(path)) {
294+
bindings.load(inputStream);
295+
}
296+
297+
for (var entry : bindings.entrySet()) {
298+
var tag = (String)entry.getKey();
299+
var type = (Class<? extends JComponent>)Class.forName((String)entry.getValue());
300+
301+
UILoader.bind(tag, type, () -> null);
302+
}
303+
}
304+
305305
private static int getDepth(Class<?> type) {
306306
var depth = 0;
307307

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package org.httprpc.sierra.tools;
16+
17+
import org.httprpc.kilo.io.TextDecoder;
18+
import org.jfree.chart.ChartPanel;
19+
import org.junit.jupiter.api.Test;
20+
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
23+
import java.util.Properties;
24+
25+
import static org.junit.jupiter.api.Assertions.*;
26+
27+
public class DTDEncoderTest {
28+
@Test
29+
public void testDTDEncoder() throws Exception {
30+
var workingPath = Path.of(System.getProperty("user.dir"));
31+
32+
var bindingsPath = workingPath.resolve("bindings.properties");
33+
var dtdPath = workingPath.resolve("sierra.dtd");
34+
35+
var properties = new Properties();
36+
37+
properties.put("chart-panel", ChartPanel.class.getName());
38+
39+
try {
40+
try (var outputStream = Files.newOutputStream(bindingsPath)) {
41+
properties.store(outputStream, "Test Bindings");
42+
}
43+
44+
DTDEncoder.main(new String[]{bindingsPath.getFileName().toString()});
45+
46+
String text;
47+
try (var inputStream = Files.newInputStream(dtdPath)) {
48+
var textDecoder = new TextDecoder();
49+
50+
text = textDecoder.read(inputStream);
51+
}
52+
53+
assertTrue(text.contains("chart-panel"));
54+
} finally {
55+
Files.deleteIfExists(bindingsPath);
56+
Files.deleteIfExists(dtdPath);
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)