Skip to content

Commit 346fae6

Browse files
committed
Update DTDEncoder.
1 parent e62d0b8 commit 346fae6

3 files changed

Lines changed: 63 additions & 19 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
subprojects {
1616
group = 'org.httprpc'
17-
version = '3.5'
17+
version = '3.5.1'
1818

1919
repositories {
2020
mavenCentral()

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

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.httprpc.sierra.VerticalAlignment;
2424

2525
import javax.swing.Icon;
26-
import javax.swing.JComponent;
2726
import javax.swing.JMenu;
2827
import javax.swing.JMenuBar;
2928
import javax.swing.JPanel;
@@ -270,7 +269,13 @@ public static void main(String[] args) throws Exception {
270269
}
271270
}
272271

273-
applyBindings(workingPath.resolve(args[0]), classLoader);
272+
var bindings = new Properties();
273+
274+
try (var inputStream = Files.newInputStream(workingPath.resolve(args[0]))) {
275+
bindings.load(inputStream);
276+
}
277+
278+
UILoader.bind(bindings, classLoader);
274279
}
275280

276281
var typeSet = new HashSet<Class<?>>();
@@ -300,22 +305,6 @@ public static void main(String[] args) throws Exception {
300305
}
301306
}
302307

303-
@SuppressWarnings("unchecked")
304-
private static void applyBindings(Path path, ClassLoader classLoader) throws IOException, ClassNotFoundException {
305-
var bindings = new Properties();
306-
307-
try (var inputStream = Files.newInputStream(path)) {
308-
bindings.load(inputStream);
309-
}
310-
311-
for (var entry : bindings.entrySet()) {
312-
var tag = (String)entry.getKey();
313-
var typeName = (String)entry.getValue();
314-
315-
UILoader.bind(tag, (Class<? extends JComponent>)classLoader.loadClass(typeName), () -> null);
316-
}
317-
}
318-
319308
private static int getDepth(Class<?> type) {
320309
var depth = 0;
321310

sierra/src/main/java/org/httprpc/sierra/UILoader.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,19 @@
6767
import java.awt.Image;
6868
import java.io.IOException;
6969
import java.io.InputStream;
70+
import java.lang.reflect.Constructor;
7071
import java.lang.reflect.Field;
7172
import java.lang.reflect.InvocationTargetException;
7273
import java.net.MalformedURLException;
7374
import java.net.URL;
7475
import java.nio.file.Path;
76+
import java.util.Arrays;
77+
import java.util.Comparator;
7578
import java.util.Deque;
7679
import java.util.HashMap;
7780
import java.util.LinkedList;
7881
import java.util.Map;
82+
import java.util.Properties;
7983
import java.util.ResourceBundle;
8084
import java.util.function.Supplier;
8185

@@ -1024,6 +1028,57 @@ public static <C extends JComponent> void bind(String tag, Class<C> type, Suppli
10241028
suppliers.put(tag, supplier);
10251029
}
10261030

1031+
/**
1032+
* Applies multiple bindings.
1033+
*
1034+
* @param bindings
1035+
* The bindings to apply.
1036+
*/
1037+
public static void bind(Properties bindings) throws ReflectiveOperationException {
1038+
bind(bindings, ClassLoader.getSystemClassLoader());
1039+
}
1040+
1041+
/**
1042+
* Applies multiple bindings.
1043+
*
1044+
* @param bindings
1045+
* The bindings to apply.
1046+
*
1047+
* @param classLoader
1048+
* The class loader that will be used to resolve the bindings.
1049+
*/
1050+
@SuppressWarnings("unchecked")
1051+
public static void bind(Properties bindings, ClassLoader classLoader) throws ClassNotFoundException {
1052+
if (bindings == null || classLoader == null) {
1053+
throw new IllegalArgumentException();
1054+
}
1055+
1056+
for (var entry : bindings.entrySet()) {
1057+
var tag = (String)entry.getKey();
1058+
var typeName = (String)entry.getValue();
1059+
1060+
var type = (Class<?>)classLoader.loadClass(typeName);
1061+
1062+
var constructors = type.getConstructors();
1063+
1064+
if (constructors.length == 0) {
1065+
throw new UnsupportedOperationException(String.format("%s cannot be instantiated.", typeName));
1066+
}
1067+
1068+
Arrays.sort(constructors, Comparator.comparing(Constructor::getParameterCount));
1069+
1070+
var constructor = constructors[0];
1071+
1072+
bind(tag, (Class<JComponent>)type, () -> {
1073+
try {
1074+
return (JComponent)constructor.newInstance(new Object[constructor.getParameterCount()]);
1075+
} catch (InstantiationException | IllegalAccessException | InvocationTargetException exception) {
1076+
throw new RuntimeException(exception);
1077+
}
1078+
});
1079+
}
1080+
}
1081+
10271082
/**
10281083
* Retrieves a named color.
10291084
*

0 commit comments

Comments
 (0)