Skip to content

Commit bb20b54

Browse files
committed
Merge branch 'master' of github.com:lawson89/Sierra
2 parents e83a662 + 01b3c82 commit bb20b54

3 files changed

Lines changed: 96 additions & 6 deletions

File tree

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: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.httprpc.sierra.VerticalAlignment;
2424

2525
import javax.swing.Icon;
26+
import javax.swing.JComponent;
2627
import javax.swing.JMenu;
2728
import javax.swing.JMenuBar;
2829
import javax.swing.JPanel;
@@ -34,16 +35,17 @@
3435
import java.awt.Color;
3536
import java.awt.Font;
3637
import java.awt.Image;
37-
import java.io.File;
38-
import java.io.FileOutputStream;
3938
import java.io.IOException;
4039
import java.io.Writer;
40+
import java.nio.file.Files;
41+
import java.nio.file.Path;
4142
import java.util.ArrayList;
4243
import java.util.Comparator;
4344
import java.util.HashMap;
4445
import java.util.HashSet;
4546
import java.util.List;
4647
import java.util.Map;
48+
import java.util.Properties;
4749

4850
import static org.httprpc.sierra.UILoader.*;
4951

@@ -251,6 +253,12 @@ private void declareAttributeList(String tag, Class<?> type, Writer writer) thro
251253
}
252254

253255
public static void main(String[] args) throws Exception {
256+
var workingPath = Path.of(System.getProperty("user.dir"));
257+
258+
if (args.length > 0) {
259+
applyBindings(workingPath.resolve(args[0]));
260+
}
261+
254262
var typeSet = new HashSet<Class<?>>();
255263

256264
var tags = new HashMap<Class<?>, String>();
@@ -271,15 +279,29 @@ public static void main(String[] args) throws Exception {
271279

272280
typeList.sort(Comparator.comparing(DTDEncoder::getDepth).thenComparing(Class::getCanonicalName));
273281

274-
var dtdEncoder = new DTDEncoder(typeList, tags);
282+
try (var outputStream = Files.newOutputStream(workingPath.resolve("sierra.dtd"))) {
283+
var dtdEncoder = new DTDEncoder(typeList, tags);
275284

276-
var file = new File(new File(System.getProperty("user.dir")), "sierra.dtd");
277-
278-
try (var outputStream = new FileOutputStream(file)) {
279285
dtdEncoder.write(null, outputStream);
280286
}
281287
}
282288

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+
283305
private static int getDepth(Class<?> type) {
284306
var depth = 0;
285307

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)