It is important to remember that Simple XML must be able to follow any structure that you can logically generate using classes. That way, you can simply create a BaseClass that uses the error interface and apply the Decorator pattern so that it passes all of this through a specific error class without any implementing objects that needed to know what they were given.
That probably doesn't make sense. How about what I just show you ... okay ... I just left and implemented exactly what I was thinking about, and here are the results ( full code link ):
Main file:
package com.massaiolir.simple.iface; import java.io.File; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; public class Main { public static void main(String[] args) throws Exception { Serializer serial = new Persister(); ResC resc = serial.read(ResC.class, new File("data/testdata.xml")); System.out.println(" == Printing out all of the error text. == "); System.out.println(resc.getErrorText()); System.out.println(resc.conRes.getErrorText()); System.out.println(resc.conRes.conList.getErrorText()); for (Con con : resc.conRes.conList.cons) { System.out.println(con.getErrorText()); } System.out.println(" == Finished printing out all of the error text. == "); } }
It is simply simple and displays the results.
BaseObject.java class:
package com.massaiolir.simple.iface; import org.simpleframework.xml.Element; public class BaseObject implements Error { @Element(name = "Err", required = false, type = ConcreteError.class) private Error err; @Override public String getErrorText() { return err.getErrorText(); } @Override public void setErrorText(String errorText) { err.setErrorText(errorText); } }
This is the class that should be distributed if it wants to "Err".
Error Interface:
package com.massaiolir.simple.iface; public interface Error { void setErrorText(String errorText); String getErrorText(); }
ConcreteError class:
package com.massaiolir.simple.iface; import org.simpleframework.xml.Attribute; public class ConcreteError implements Error { @Attribute private String text; @Override public String getErrorText() { return text; } @Override public void setErrorText(String errorText) { this.text = errorText; } }
Actual implementation classes after this point. You will see that they are pretty trivial because the real work is handled in the classes above.
Con class:
package com.massaiolir.simple.iface; public class Con extends BaseObject { }
ConList Class:
package com.massaiolir.simple.iface; import java.util.ArrayList; import org.simpleframework.xml.ElementList; public class ConList extends BaseObject { @ElementList(entry = "Con", inline = true) public ArrayList<Con> cons; }
ConRes Class:
package com.massaiolir.simple.iface; import org.simpleframework.xml.Element; public class ConRes extends BaseObject { @Element(name = "ConList") public ConList conList; }
ResC Class:
package com.massaiolir.simple.iface; import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; @Root public class ResC extends BaseObject { @Element(name = "ConRes") public ConRes conRes; }
And thatβs all there is. Pretty simple. I was able to break it all in ten minutes. In fact, it took me longer to write this answer than it took me to write the code that I give you. If you do not understand anything about the code I just wrote, please let me know. Hope this helps you understand how you can do something like this.