diff --git a/src/eu/lixko/ext/forraceexpander/Main.java b/src/eu/lixko/ext/forraceexpander/Main.java index c836d1a..8a70cc6 100644 --- a/src/eu/lixko/ext/forraceexpander/Main.java +++ b/src/eu/lixko/ext/forraceexpander/Main.java @@ -1,11 +1,8 @@ package eu.lixko.ext.forraceexpander; -import eu.lixko.ext.forraceexpander.components.Node; import eu.lixko.ext.forraceexpander.components.Parent; -import eu.lixko.ext.forraceexpander.components.Property; import eu.lixko.ext.forraceexpander.exporter.TreeSerializerProvider; import eu.lixko.ext.forraceexpander.exporter.TreeTestSerializer; -import eu.lixko.ext.forraceexpander.importer.OrderImporter; public class Main { @@ -13,18 +10,16 @@ public class Main { public static void main(String[] args) { Parent main = new Parent("main"); - Parent child = new Parent("child1"); - child.addProperty("prop", "sumtext"); - - Property prop = new Property<>("property1"); - prop.setData("ayy lmao"); - - child.addCloneChild(prop); - - main.addCloneChild(child); - - System.out.println(">>> " + main.getChildByNameFirst("child1")); + main.createParent("child1") + .createProperty("tag1", 5).parent() + .attr("field1", "yes, nodes can have attributes") + .createProperty("tag2", "hello").parent() + .createProperty("somefloat", 2.0f); + main.createParent("child2") + .createProperty("another_tag", 10).parent() + .createProperty("tag2", "some string again"); + TreeSerializerProvider serializer = new TreeTestSerializer(); System.out.println(serializer.serialize(main)); diff --git a/src/eu/lixko/ext/forraceexpander/components/Address.java b/src/eu/lixko/ext/forraceexpander/components/Address.java index a42aae5..fc27967 100644 --- a/src/eu/lixko/ext/forraceexpander/components/Address.java +++ b/src/eu/lixko/ext/forraceexpander/components/Address.java @@ -1,7 +1,6 @@ package eu.lixko.ext.forraceexpander.components; import java.util.ArrayList; -import java.util.LinkedHashMap; public class Address extends SerializableComponent { private ArrayList addressLines = new ArrayList<>(); diff --git a/src/eu/lixko/ext/forraceexpander/components/Node.java b/src/eu/lixko/ext/forraceexpander/components/Node.java index 1c53aec..07cf3a8 100644 --- a/src/eu/lixko/ext/forraceexpander/components/Node.java +++ b/src/eu/lixko/ext/forraceexpander/components/Node.java @@ -1,48 +1,59 @@ package eu.lixko.ext.forraceexpander.components; +import java.io.Serializable; import java.util.HashMap; -public class Node { +public abstract class Node implements Cloneable { - public String type = "node"; - protected Node parent = null; + protected Parent parent = null; protected String name = ""; - protected HashMap properties = new HashMap<>(); + protected HashMap properties = new HashMap<>(); - public Node(Node parent, String name) { + public Node(Parent parent, String name) { this.parent = parent; this.name = name; } - public Node(Node src) { - this.parent = src.parent; - this.name = src.name; - this.properties = new HashMap(src.properties); - this.type = src.type; - } - public Node(String name) { this.name = name; } - public Node getParent() { + public Parent parent() { return this.parent; } - public String getName() { + public String name() { return this.name; } - public HashMap getProperties() { + public void name(String name) { + this.name = name; + } + + public HashMap getProperties() { return this.properties; } - public void addProperty(String name, String value) { + public Node attr(String name, Serializable value) { properties.put(name, value); + return this; } - public String getProperty(String name) { + public Serializable attr(String name) { return properties.get(name); } + public boolean hasAttr(String name) { + return properties.containsKey(name); + } + + @Override + public Node clone() throws CloneNotSupportedException { + Node n = (Node) super.clone(); + n.name = this.name; + n.parent = this.parent; + n.properties = new HashMap<>(this.properties); + return n; + } + } diff --git a/src/eu/lixko/ext/forraceexpander/components/Parent.java b/src/eu/lixko/ext/forraceexpander/components/Parent.java index ef16431..7be3faf 100644 --- a/src/eu/lixko/ext/forraceexpander/components/Parent.java +++ b/src/eu/lixko/ext/forraceexpander/components/Parent.java @@ -1,27 +1,19 @@ package eu.lixko.ext.forraceexpander.components; +import java.io.Serializable; import java.util.ArrayList; -public class Parent extends Node { +public class Parent extends Node implements Cloneable { private ArrayList children = new ArrayList<>(); - public Parent(Parent src) { - super(src); - for(Node c : src.children) { - this.children.add(new Node(c)); - } - } - public Parent(String name) { super(name); - this.type = "parent"; } - public Parent(Parent parent, String name) { + public Parent(String name, Parent parent) { super(name); this.parent = parent; - this.type = "parent"; } public ArrayList getChildren() { @@ -32,24 +24,59 @@ public class Parent extends Node { this.children.add(child); } - public void addCloneChild(Parent child) { - Parent p = new Parent(child); - p.parent = this; - this.children.add(p); + public void addCloneChild(Node child) { + try { + Node cc = child.clone(); + cc.parent = this; + this.children.add(cc); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + } } - public void addCloneChild(Property child) { - Property n = new Property<>(child); - n.parent = this; - this.children.add(n); + public Property createProperty(String name, T value) { + Property p = new Property(name, value, this); + this.addChild(p); + return p; } - public Node getChildByNameFirst(String name) { + public Parent createParent(String name) { + Parent p = new Parent(name, this); + this.addChild(p); + return p; + } + + public Node first(String name) { for(Node n : children) { - if(n.getName() == name) + if(n.name() == name) return n; } return null; } + public Parent firstParent(String name) { + for(Node n : children) { + if(!(n instanceof Parent)) + continue; + if(n.name() == name) + return (Parent) n; + } + return null; + } + + public Parent attr(String name, Serializable value) { + return (Parent) super.attr(name, value); + } + + @Override + public Parent clone() throws CloneNotSupportedException { + Node x = super.clone(); + Parent p = (Parent) x; + p.children = new ArrayList<>(); + for(Node c : this.children) { + p.children.add(c.clone()); + } + return p; + } + } diff --git a/src/eu/lixko/ext/forraceexpander/components/Property.java b/src/eu/lixko/ext/forraceexpander/components/Property.java index d64c131..9a0242d 100644 --- a/src/eu/lixko/ext/forraceexpander/components/Property.java +++ b/src/eu/lixko/ext/forraceexpander/components/Property.java @@ -1,17 +1,24 @@ package eu.lixko.ext.forraceexpander.components; -public class Property extends Node { +import java.io.Serializable; + +public class Property extends Node { private T data; - - public Property(Property src) { - super(src); - this.data = src.data; - } public Property(String name) { super(name); - this.type = "prop"; + } + + public Property(String name, T data) { + super(name); + this.data = data; + } + + public Property(String name, T data, Parent p) { + super(name); + this.data = data; + this.parent = p; } public T getData() { @@ -22,9 +29,18 @@ public class Property extends Node { this.data = data; } + @SuppressWarnings("unchecked") + public Property attr(String name, Serializable value) { + return (Property) super.attr(name, value); + } + @Override - public Object clone() throws CloneNotSupportedException { - return super.clone(); + @SuppressWarnings("unchecked") + public Property clone() throws CloneNotSupportedException { + Node x = super.clone(); + Property p = (Property) x; + p.data = this.data; + return p; } } diff --git a/src/eu/lixko/ext/forraceexpander/exporter/TreeTestSerializer.java b/src/eu/lixko/ext/forraceexpander/exporter/TreeTestSerializer.java index b15e27e..9cabcf8 100644 --- a/src/eu/lixko/ext/forraceexpander/exporter/TreeTestSerializer.java +++ b/src/eu/lixko/ext/forraceexpander/exporter/TreeTestSerializer.java @@ -1,5 +1,8 @@ package eu.lixko.ext.forraceexpander.exporter; +import java.io.Serializable; +import java.util.Map; + import eu.lixko.ext.forraceexpander.components.Node; import eu.lixko.ext.forraceexpander.components.Parent; import eu.lixko.ext.forraceexpander.components.Property; @@ -12,23 +15,44 @@ public class TreeTestSerializer implements TreeSerializerProvider { } public String serialize(Node n, int level) { - String res = ""; + String res = "\n"; for(int i = 0; i < level; i++) - res += "\t"; + res += '\t'; if(n instanceof Parent) { Parent p = (Parent) n; - res += "P " + p.getName() + ": \n"; + res += p.name(); + if(!p.getProperties().isEmpty()) { + res += " ("; + int i = p.getProperties().size(); + for (Map.Entry entry : p.getProperties().entrySet()) { + i--; + res += entry.getKey(); + if(entry.getValue() != null) { + res += ": "; + if(entry.getValue() instanceof String) + res += "\"" + entry.getValue() + "\""; + else + res += entry.getValue(); + res += i == 0 ? "" : ", "; + } + + } + res += ')'; + } + + res += ':'; for(Node c : p.getChildren()) { res += serialize(c, level + 1); } } else if(n instanceof Property) { Property p = (Property) n; - res += "O " + p.getName() + " = " + p.getData(); + res += p.getData().getClass().getSimpleName() + " " + p.name() + ": "; + if(p.getData() instanceof String) + res += "\"" + p.getData() + "\""; + else + res += p.getData(); } else { - System.out.println("EXC: " + n.getName() + " / " + n.type); - System.out.println(n.getClass()); - System.out.println("> " + (Property.class.isAssignableFrom(n.getClass()))); - res += "else\n"; + res += "else"; } return res; }