initial parser working as intended

This commit is contained in:
Macocian Radu
2019-12-23 22:25:04 +02:00
parent 2e35474578
commit d1f0716fcb
16 changed files with 297 additions and 54 deletions

View File

@@ -4,6 +4,7 @@
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />

4
resources/company.xml Normal file
View File

@@ -0,0 +1,4 @@
<?xml version="1.0"?>
<Window Visible="true" Size="640, 480" PositionRelativeTo="null">
<Button BackgroundColor="#990000"/>
</Window>

View File

@@ -1,38 +1,22 @@
import guiTree.Window;
import parser.XAMLParser;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Window win = new Window(); try{
win.setVisible(true); Window window = XAMLParser.parse("company.xml");
win.setSize(640, 480); assert window != null;
win.setPositionRelativeTo(null); window.addWindowListener(new WindowAdapter() {
win.addWindowListener(new WindowAdapter() { @Override
@Override public void windowClosing(WindowEvent e) {
public void windowClosing(WindowEvent e) { window.dispose();
win.dispose(); }
} });
}); }catch (Exception e){
e.printStackTrace();
Button button = new Button();
win.addVisual(button);
int x = 20;
int y = 20;
while(true){
if(x > win.getWidth()){
x = 0;
y+=20;
}
if(y > win.getHeight()){
y = 0;
}
button.setLocation(x, y);
x+=10;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
} }
} }

View File

@@ -0,0 +1,9 @@
package converters;
public class BooleanConverter implements ConverterInterface<Boolean> {
@Override
public Boolean convert(String content) {
return Boolean.valueOf(content);
}
}

View File

@@ -0,0 +1,10 @@
package converters;
import java.awt.*;
public class ColorConverter implements ConverterInterface<Color> {
@Override
public Color convert(String content) {
return Color.decode(content);
}
}

View File

@@ -0,0 +1,31 @@
package converters;
import com.sun.jdi.InvalidTypeException;
import java.awt.*;
import java.util.HashMap;
public class Converter {
private HashMap<Class<?>, ConverterInterface<?>> converterTable;
public Converter(){
this.converterTable = new HashMap<>();
this.converterTable.put(Integer.class, new IntegerConverter());
this.converterTable.put(Float.class, new FloatConverter());
this.converterTable.put(Double.class, new DoubleConverter());
this.converterTable.put(String.class, new StringConverter());
this.converterTable.put(Boolean.class, new BooleanConverter());
this.converterTable.put(Integer.TYPE, new IntegerConverter());
this.converterTable.put(Float.TYPE, new FloatConverter());
this.converterTable.put(Double.TYPE, new DoubleConverter());
this.converterTable.put(Boolean.TYPE, new BooleanConverter());
this.converterTable.put(Color.class, new ColorConverter());
}
public Object objectCreatorFactory (Class<?> type, String content) throws InvalidTypeException {
if(this.converterTable.containsKey(type)) {
return this.converterTable.get(type).convert(content);
}
throw new InvalidTypeException();
}
}

View File

@@ -0,0 +1,5 @@
package converters;
public interface ConverterInterface<T> {
T convert(String content);
}

View File

@@ -0,0 +1,9 @@
package converters;
public class DoubleConverter implements ConverterInterface<Double>{
@Override
public Double convert(String content) {
return Double.parseDouble(content);
}
}

View File

@@ -0,0 +1,9 @@
package converters;
public class FloatConverter implements ConverterInterface<Float> {
@Override
public Float convert(String content) {
return Float.parseFloat(content);
}
}

View File

@@ -0,0 +1,9 @@
package converters;
public class IntegerConverter implements ConverterInterface<Integer> {
@Override
public Integer convert(String content) {
return Integer.parseInt(content);
}
}

View File

@@ -0,0 +1,9 @@
package converters;
public class StringConverter implements ConverterInterface<String> {
@Override
public String convert(String content) {
return content;
}
}

View File

@@ -1,3 +1,7 @@
package guiTree;
import guiTree.Visual;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
@@ -12,7 +16,7 @@ public class Button extends Visual {
public void paint(BufferedImage imageBuffer) public void paint(BufferedImage imageBuffer)
{ {
Graphics g = imageBuffer.getGraphics(); Graphics g = imageBuffer.getGraphics();
g.setColor(Color.BLUE); g.setColor(this.getBackgroundColor());
g.fillRect(0, 0, getWidth(), getHeight()); g.fillRect(0, 0, getWidth(), getHeight());
} }
} }

View File

@@ -1,3 +1,5 @@
package guiTree;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;

View File

@@ -1,3 +1,5 @@
package guiTree;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.ArrayList; import java.util.ArrayList;
@@ -16,13 +18,14 @@ public class Visual {
/*-------------------------------------------------------------------- /*--------------------------------------------------------------------
Attributes Attributes
---------------------------------------------------------------------*/ ---------------------------------------------------------------------*/
private int width; private String name;
private int height; private Integer width;
private int locationX; private Integer height;
private int locationY; private Integer locationX;
private Integer locationY;
private Color backgroundColor; private Color backgroundColor;
private Color foregroundColor; private Color foregroundColor;
private Boolean valid; private Boolean active;
/*-------------------------------------------------------------------- /*--------------------------------------------------------------------
@@ -33,13 +36,23 @@ public class Visual {
this.parent = null; this.parent = null;
this.backgroundColor = Color.WHITE; this.backgroundColor = Color.WHITE;
this.foregroundColor = Color.BLACK; this.foregroundColor = Color.BLACK;
valid = false;
this.active = this instanceof Window;
} }
/*-------------------------------------------------------------------- /*--------------------------------------------------------------------
Attributes Methods Attributes Methods
---------------------------------------------------------------------*/ ---------------------------------------------------------------------*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWidth() public int getWidth()
{ {
return this.width; return this.width;
@@ -50,13 +63,12 @@ public class Visual {
return this.height; return this.height;
} }
public void setSize(int width, int height){ public void setSize(Integer width, Integer height){
this.width = width; this.width = width;
this.height = height; this.height = height;
initializeImageBuffer(width, height); initializeImageBuffer(width, height);
this.valid = false;
this.revalidate(); this.revalidate();
} }
@@ -68,10 +80,9 @@ public class Visual {
return this.locationY; return this.locationY;
} }
public void setLocation(int x, int y){ public void setLocation(Integer x, Integer y){
this.locationX = x; this.locationX = x;
this.locationY = y; this.locationY = y;
this.valid = false;
this.revalidate(); this.revalidate();
} }
@@ -81,7 +92,6 @@ public class Visual {
public void setBackgroundColor(Color backgroundColor) { public void setBackgroundColor(Color backgroundColor) {
this.backgroundColor = backgroundColor; this.backgroundColor = backgroundColor;
this.valid = false;
this.revalidate(); this.revalidate();
} }
@@ -91,7 +101,6 @@ public class Visual {
public void setForegroundColor(Color foregroundColor) { public void setForegroundColor(Color foregroundColor) {
this.foregroundColor = foregroundColor; this.foregroundColor = foregroundColor;
this.valid = false;
this.revalidate(); this.revalidate();
} }
@@ -101,8 +110,8 @@ public class Visual {
} }
private void calculateInitialLocation(){ private void calculateInitialLocation(){
this.locationX = 200; this.locationX = 20;
this.locationY = 200; this.locationY = 50;
} }
@@ -116,9 +125,9 @@ public class Visual {
child.calculateInitialLocation(); child.calculateInitialLocation();
child.calculateInitialSize(); child.calculateInitialSize();
child.imageBuffer = new BufferedImage(child.getWidth(), child.getHeight(), BufferedImage.TYPE_3BYTE_BGR); child.imageBuffer = new BufferedImage(child.getWidth(), child.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
child.valid = false;
this.revalidate(); child.active = true;
child.revalidate();
} }
private void setParent(Visual parent) private void setParent(Visual parent)
@@ -142,16 +151,14 @@ public class Visual {
} }
public void revalidate() { public void revalidate() {
if(!this.active){
return;
}
initializeImageBuffer(width, height); initializeImageBuffer(width, height);
this.paint(imageBuffer); this.paint(imageBuffer);
for(Visual v:children){ for(Visual v:children){
if(!v.valid){
v.paint(v.imageBuffer);
}
this.imageBuffer.getGraphics().drawImage(v.imageBuffer, v.locationX, v.locationY, null); this.imageBuffer.getGraphics().drawImage(v.imageBuffer, v.locationX, v.locationY, null);
v.valid = true;
} }
this.valid = true;
if(!(this instanceof Window)){ if(!(this instanceof Window)){
this.parent.revalidate(); this.parent.revalidate();
} }
@@ -169,7 +176,7 @@ public class Visual {
Helper Methods Helper Methods
---------------------------------------------------------------------*/ ---------------------------------------------------------------------*/
private void initializeImageBuffer(int width, int height){ private void initializeImageBuffer(Integer width, Integer height){
this.imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); this.imageBuffer = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
this.imageBuffer.getGraphics().setColor(backgroundColor); this.imageBuffer.getGraphics().setColor(backgroundColor);
this.imageBuffer.getGraphics().fillRect(0, 0, width, height); this.imageBuffer.getGraphics().fillRect(0, 0, width, height);

View File

@@ -1,3 +1,7 @@
package guiTree;
import guiTree.Visual;
import java.awt.*; import java.awt.*;
import java.awt.event.WindowListener; import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener; import java.awt.event.WindowStateListener;
@@ -17,7 +21,7 @@ public class Window extends Visual {
} }
@Override @Override
public void setSize(int width, int height) public void setSize(Integer width, Integer height)
{ {
this.frame.setSize(width, height); this.frame.setSize(width, height);
super.setSize(width, height); super.setSize(width, height);
@@ -26,6 +30,7 @@ public class Window extends Visual {
public void setFrameImageBuffer(BufferedImage imageBuffer){ public void setFrameImageBuffer(BufferedImage imageBuffer){
this.frame.setImageBuffer(imageBuffer); this.frame.setImageBuffer(imageBuffer);
this.frame.repaint();
} }
public void setSize(Dimension dimension) { public void setSize(Dimension dimension) {
@@ -55,4 +60,8 @@ public class Window extends Visual {
public void setPositionRelativeTo(Component c){ public void setPositionRelativeTo(Component c){
frame.setLocationRelativeTo(c); frame.setLocationRelativeTo(c);
} }
public void setPositionRelativeTo(){
frame.setLocationRelativeTo(null);
}
} }

141
src/parser/XAMLParser.java Normal file
View File

@@ -0,0 +1,141 @@
package parser;
import com.sun.jdi.InvalidTypeException;
import converters.Converter;
import guiTree.Visual;
import guiTree.Window;
import org.w3c.dom.*;
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;
public class XAMLParser {
private final static String packageName = "guiTree.";
private static Converter valueConverter = new Converter();
private static void setAttributes(Object object, NamedNodeMap attributeList){
for(int i = 0; i < attributeList.getLength(); i++){
Node attribute = attributeList.item(i);
String methodName = "set";
methodName = methodName.concat(attribute.getNodeName());
List<Object> parameterList = convertStringToPrimitives(object, attribute.getNodeValue(), methodName);
Method method = getMethod(object, methodName, parameterList);
try {
assert method != null;
method.invoke(object, parameterList.toArray());
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
private static Method getMethod(Object object, String methodName, List<Object> parameterList){
Method method;
Class<?>[] args = new Class[parameterList.size()];
for(Object o: parameterList){
try {
args[parameterList.indexOf(o)] = o.getClass();
} catch (NullPointerException e) {
System.err.println("Null Pointer Exception: " + methodName + " with parameters + " + parameterList.toString());
}
}
try {
method = object.getClass().getMethod(methodName, args);
return method;
} catch (NoSuchMethodException e) {
System.err.println("Method does not exist: " + methodName + " with parameters + " + parameterList.toString());
e.printStackTrace();
}
return null;
}
private static List<Method> getMethodsFromName(Object object, String methodName){
Method[] methods = object.getClass().getMethods();
List<Method> returnMethods = new ArrayList<>();
for(Method method: methods){
if(method.getName().equals(methodName)){
returnMethods.add(method);
}
}
return returnMethods;
}
public static Window parse(String filepath) throws Exception {
Object rootObject;
FileInputStream fileIS = new FileInputStream(new File("resources/" + filepath));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(fileIS);
xmlDocument.normalize();
Element rootNode = xmlDocument.getDocumentElement();
rootObject = parseNode(rootNode);
if(rootObject instanceof Window) {
return (Window) rootObject;
}
return null;
}
private static List<Object> convertStringToPrimitives(Object object, String value, String methodName){
List<Object> primitiveAttributes = new ArrayList<>();
List<String> values = new ArrayList<>();
value = value.replaceAll(" ", "");
while (value.contains(",")) {
values.add(value.substring(0, value.indexOf(',')));
value = value.substring(value.indexOf(',') + 1);
}
values.add(value);
List<Method> methods = getMethodsFromName(object, methodName);
for(Method method: methods){
Class<?>[] types = method.getParameterTypes();
for(int i = 0; i < types.length; i++){
try{
primitiveAttributes.add(valueConverter.objectCreatorFactory(types[i], values.get(i)));
} catch (InvalidTypeException | NumberFormatException e){
primitiveAttributes.clear();
break;
}
}
if(primitiveAttributes.size() == types.length){
return primitiveAttributes;
}
}
return null;
}
private static Object parseNode(Node parentNode)throws Exception{
Class<?> parentClass = Class.forName(packageName.concat(parentNode.getNodeName()));
Object parentObject = parentClass.getDeclaredConstructor().newInstance();
setAttributes(parentObject, parentNode.getAttributes());
NodeList childList = parentNode.getChildNodes();
for(int index = 0; index < childList.getLength(); index++){
Node childNode = childList.item(index);
if(childNode.getNodeType() == Node.ELEMENT_NODE) {
Object childObject = parseNode(childNode);
if(parentObject instanceof Visual && childObject instanceof Visual) {
addVisual((Visual) parentObject, (Visual) childObject);
}
System.out.println("\nCurrent Element :" + childNode.getNodeName());
}
}
return parentObject;
}
private static void addVisual(Visual parentObject, Visual childObject){
parentObject.addVisual(childObject);
}
}