mirror of
https://github.com/macocianradu/javaGUItoolkit.git
synced 2026-03-18 21:50:04 +00:00
created location placer factory
made debugger changeable from outside the code made possible to add more converters Made changeable FPS in window
This commit is contained in:
@@ -6,18 +6,19 @@ import guiTree.Helper.Debugger;
|
||||
import guiTree.Visual;
|
||||
import guiTree.Window;
|
||||
import org.w3c.dom.*;
|
||||
import parser.converters.ConverterInterface;
|
||||
|
||||
import javax.xml.parsers.*;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
|
||||
public class XAMLParser {
|
||||
private final static String packageGuiTree = "guiTree.";
|
||||
private final static String packageComponents = "guiTree.Components.";
|
||||
private final static String packageDecorations = "guiTree.Components.Decorations.";
|
||||
public class XMLParser {
|
||||
private static Map<String, Class<?>> classMap;
|
||||
private static Converter valueConverter = new Converter();
|
||||
|
||||
private static void setAttributes(Object object, NamedNodeMap attributeList){
|
||||
@@ -68,9 +69,12 @@ public class XAMLParser {
|
||||
public static Window parse(String filepath) throws Exception {
|
||||
Object rootObject;
|
||||
Debugger.log("Started", Debugger.Tag.PARSING);
|
||||
FileInputStream fileIS = new FileInputStream(new File("resources/" + filepath));
|
||||
InputStream fileIS = XMLParser.class.getClassLoader().getResourceAsStream(filepath);
|
||||
|
||||
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = builderFactory.newDocumentBuilder();
|
||||
Class<?>[] classes = getClasses();
|
||||
classMap = createClassTable(classes);
|
||||
Document xmlDocument = builder.parse(fileIS);
|
||||
|
||||
xmlDocument.normalize();
|
||||
@@ -117,18 +121,8 @@ public class XAMLParser {
|
||||
}
|
||||
|
||||
private static Object parseNode(Node parentNode)throws Exception{
|
||||
Class<?> parentClass;
|
||||
try {
|
||||
parentClass = Class.forName(packageComponents.concat(parentNode.getNodeName()));
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
try {
|
||||
parentClass = Class.forName(packageGuiTree.concat(parentNode.getNodeName()));
|
||||
}
|
||||
catch (ClassNotFoundException f) {
|
||||
parentClass = Class.forName(packageDecorations.concat(parentNode.getNodeName()));
|
||||
}
|
||||
}
|
||||
Class<?> parentClass = classMap.get(parentNode.getNodeName());
|
||||
|
||||
Debugger.log("Parsing " + parentClass, Debugger.Tag.PARSING);
|
||||
Object parentObject = parentClass.getDeclaredConstructor().newInstance();
|
||||
Debugger.log("Constructor called successfully for " + parentObject, Debugger.Tag.PARSING);
|
||||
@@ -161,6 +155,89 @@ public class XAMLParser {
|
||||
return parentObject;
|
||||
}
|
||||
|
||||
private static Class<?>[] getClasses()
|
||||
throws ClassNotFoundException, IOException {
|
||||
URL classRoot = XMLParser.class.getProtectionDomain().getCodeSource().getLocation();
|
||||
URL projectRoot = ClassLoader.getSystemResource("");
|
||||
File dir;
|
||||
ClassLoader classLoader = XMLParser.class.getClassLoader();
|
||||
|
||||
ArrayList<Class<?>> classes = new ArrayList<>();
|
||||
|
||||
String path = classRoot.getPath();
|
||||
if(path.indexOf('.') > 0) {
|
||||
if (path.substring(path.lastIndexOf('.')).equals(".jar")) {
|
||||
JarFile jarFile = new JarFile(classRoot.getPath());
|
||||
Enumeration<JarEntry> jarEntries = jarFile.entries();
|
||||
while (jarEntries.hasMoreElements()) {
|
||||
JarEntry jarEntry = jarEntries.nextElement();
|
||||
String filePath = jarEntry.getName();
|
||||
if (filePath.indexOf('.') < 0) {
|
||||
continue;
|
||||
}
|
||||
if (filePath.substring(filePath.lastIndexOf('.')).equals(".class")) {
|
||||
filePath = filePath.replaceAll("/", ".");
|
||||
classes.add(Class.forName(filePath.substring(0, filePath.lastIndexOf('.'))));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
dir = new File(classRoot.getFile());
|
||||
classes.addAll(findClasses(dir, ""));
|
||||
}
|
||||
if(!projectRoot.getPath().equals(classRoot.getPath())){
|
||||
dir = new File(projectRoot.getFile());
|
||||
classes.addAll(findClasses(dir, ""));
|
||||
}
|
||||
|
||||
return classes.toArray(new Class[0]);
|
||||
}
|
||||
|
||||
|
||||
private static List<Class<?>> findClasses(File directory, String packageName) throws ClassNotFoundException {
|
||||
Debugger.log("Getting Classes from Directory: " + directory.getName(), Debugger.Tag.PARSING);
|
||||
List<Class<?>> classes = new ArrayList<>();
|
||||
if (!directory.exists()) {
|
||||
return classes;
|
||||
}
|
||||
File[] files = directory.listFiles();
|
||||
if(files == null) {
|
||||
return classes;
|
||||
}
|
||||
for (File file : files) {
|
||||
if (file.isDirectory()) {
|
||||
assert !file.getName().contains(".");
|
||||
classes.addAll(findClasses(file, packageName + "." + file.getName()));
|
||||
} else if (file.getName().endsWith(".class")) {
|
||||
if(packageName.length() == 0) {
|
||||
classes.add(Class.forName(file.getName().substring(0, file.getName().length() - 6)));
|
||||
}
|
||||
else {
|
||||
classes.add(Class.forName(packageName.substring(1) + '.' + file.getName().substring(0, file.getName().length() - 6)));
|
||||
}
|
||||
}
|
||||
}
|
||||
return classes;
|
||||
}
|
||||
|
||||
private static Map<String, Class<?>> createClassTable(Class<?>[] classes) {
|
||||
Map<String, Class<?>> map = new HashMap<>();
|
||||
for(Class<?> c: classes) {
|
||||
if(c.getName().indexOf('.') >= 0) {
|
||||
map.put(c.getName().substring(c.getName().lastIndexOf('.') + 1), c);
|
||||
}
|
||||
else {
|
||||
map.put(c.getName(), c);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static void addConverter(ConverterInterface<?> converterInterface) {
|
||||
valueConverter.addConverter(converterInterface);
|
||||
}
|
||||
|
||||
private static void addVisual(Visual parentObject, Visual childObject){
|
||||
parentObject.addVisual(childObject);
|
||||
}
|
||||
@@ -7,4 +7,9 @@ public class BooleanConverter implements ConverterInterface<Boolean> {
|
||||
content = content.replaceAll(" ", "");
|
||||
return Boolean.valueOf(content);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getConversionClass() {
|
||||
return Boolean.class;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,4 +8,9 @@ public class ColorConverter implements ConverterInterface<Color> {
|
||||
content = content.replaceAll(" ", "");
|
||||
return Color.decode(content);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getConversionClass() {
|
||||
return Color.class;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,4 +30,8 @@ public class Converter {
|
||||
}
|
||||
throw new InvalidClassException(type.getName());
|
||||
}
|
||||
|
||||
public void addConverter(ConverterInterface<?> converterInterface) {
|
||||
converterTable.put(converterInterface.getConversionClass(), converterInterface);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,4 +4,5 @@ import java.io.InvalidClassException;
|
||||
|
||||
public interface ConverterInterface<T> {
|
||||
T convert(String content) throws InvalidClassException;
|
||||
Class<?> getConversionClass();
|
||||
}
|
||||
|
||||
@@ -16,4 +16,9 @@ public class DirectionConverter implements ConverterInterface<Slider.Direction>
|
||||
}
|
||||
throw new InvalidClassException(Slider.Direction.class.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getConversionClass() {
|
||||
return Slider.Direction.class;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,4 +7,9 @@ public class DoubleConverter implements ConverterInterface<Double>{
|
||||
content = content.replaceAll(" ", "");
|
||||
return Double.parseDouble(content);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getConversionClass() {
|
||||
return Double.class;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,4 +15,9 @@ public class FloatConverter implements ConverterInterface<Float> {
|
||||
|
||||
return Float.parseFloat(content);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getConversionClass() {
|
||||
return Float.class;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,4 +7,9 @@ public class IntegerConverter implements ConverterInterface<Integer> {
|
||||
content = content.replaceAll(" ", "");
|
||||
return Integer.parseInt(content);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getConversionClass() {
|
||||
return Integer.class;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,4 +6,9 @@ public class StringConverter implements ConverterInterface<String> {
|
||||
public String convert(String content) {
|
||||
return content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getConversionClass() {
|
||||
return String.class;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user