|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object thinwire.util.XOD
public final class XOD
Xml Object Document (XOD) generates objects from the XML file, and associates them with a Map. XODs can be used to create instances of plain old java objects (POJO). For example, a Dialog can be created as follows:
<?xml version="1.0"?> <xod> <include file="classes.xml"/> <Dialog id="dialog"> <title>XOD Demo File</title> <width>300</width> <height>200</height> <children> <TextField id="name"> <x>90</x> <y>5</y> <width>80</width> <height>25</height> </TextField> <Label> <text>Name:</text> <labelFor>name</labelFor> <alignX>RIGHT</alignX> <x>5</x> <y>5</y> <width>80</width> <height>25</height> </Label> </children> </Dialog> </xod>
XOD xod = new XOD(); xod.execute("dialog.xml"); Dialog dialog = (Dialog)xod.getObjectMap().get("dialog"); dialog.setVisible(true);
<xod> <alias name="Button" class="thinwire.ui.Button"/> <alias name="CheckBox" class="thinwire.ui.CheckBox"/> <alias name="Dialog" class="thinwire.ui.Dialog"/> <alias name="Divider" class="thinwire.ui.Divider"/> <alias name="DropDownGridBox" class="thinwire.ui.DropDownGridBox"/> <alias name="Frame" class="thinwire.ui.Frame"/> <alias name="GridBox" class="thinwire.ui.GridBox"/> <alias name="Image" class="thinwire.ui.Image"/> <alias name="Label" class="thinwire.ui.Label"/> <alias name="Menu" class="thinwire.ui.Menu"/> <alias name="Panel" class="thinwire.ui.Panel"/> <alias name="RadioButton" class="thinwire.ui.RadioButton"/> <alias name="RadioButton.Group" class="thinwire.ui.RadioButton$Group"/> <alias name="TabFolder" class="thinwire.ui.TabFolder"/> <alias name="TabSheet" class="thinwire.ui.TabSheet"/> <alias name="TextArea" class="thinwire.ui.TextArea"/> <alias name="TextField" class="thinwire.ui.TextField"/> </xod>
In general, an XOD file can contain tags of the form
<com.mypackage.XXXX>where XXXX is a class, and such a tag can be thought of as an instruction to create an instance of the XXXX class.
But the full class names are long. To allow for shorter tags in your xod files, you can define an alias:
<alias name="Button" class="thinwire.ui.Button"/>Once you've included this alias in your file, you can construct an object using it:
<Button id="button_ok"> <text>OK</text> <x>73</x> <y>301</y> <width>84</width> <height>30</height> </Button>
E.g. Since the thinwire.ui.Button class has a setText method, you can specify the text for your button with a <text> element.
<Button id="button_ok"> .... <text>OK</text> .... </Button>
In the case of these standard types, the XOD class will make the appropriate conversion while processing a xod file. E.g. When it comes across
<Button id="Search_btn"> ..... <x>73</x> ..... </Button>the XOD engine will convert "73" to an int and assign the x property of the new Button that int value.
In the case of non-literal types, XOD has three special strategies. First, it will check to see if the class type of the property you are assigning to has a 'valueOf' method that returns the appropriate type. If it does, it will call that method and assign the returned value to the property. E.g. The Label class has an alignX property of type thinwire.ui.AlignX. This class has a 'valueOf' method. When XOD processes the above demo file and comes across
<Label> ..... <alignX>RIGHT</alignX> ..... </Label>it calls the static 'valueOf' on AlignX, which returns the appropriate constant AlignX.RIGHT. Second, it will check to see if the class type has a static final field (i.e. constant) that is named the same as the value specified in the property tag. If it finds a constant, it will be assed to the property. Third, it looks for an object defined elsewhere in the XOD, an object whose id in the XOD matches the property value. If it finds one, it assigns the object as a value for the property. E.g. When it comes across
<Label> ..... <labelFor>name</labelFor> ..... </Label>then XOD engine discovers that there is a 'name' object defined previously, and it assigns this object to the Label's labelFor property.
When an XML element represents a property with a Collection type, the content of the element represents the members of the collection. E.g. When XOD comes across
<children> <Label> .... </Label> <Label> .... </Label> <Label> .... </Label> <Divider> .... </Divider> <Divider> .... </Divider> <TextField id="name"> .... </TextField> .... </children>it constructs Label, Divider, and TextField components and makes these components children of the Dialog.
Constructor Summary | |
---|---|
XOD()
Create a new XOD. |
|
XOD(java.lang.String uri)
Create a new XOD. |
Method Summary | |
---|---|
void |
execute(java.lang.String uri)
Executes a XOD file, adding the created objects to the map and list. |
java.util.Map<java.lang.String,java.lang.Class> |
getAliasMap()
|
java.lang.String |
getObjectId(java.lang.Object o)
Gets the id that is associated to the specified object. |
java.util.Map<java.lang.String,java.lang.Object> |
getObjectMap()
The object map contains references to previously defined objects identified by id. |
java.util.List<java.lang.Object> |
getObjects()
|
java.util.Map<java.lang.String,java.lang.String> |
getPropertyMap()
|
java.util.List<java.lang.Object> |
getRootObjects()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public XOD()
public XOD(java.lang.String uri)
uri
- the name of the XML Object Document file to execute.Method Detail |
---|
public java.util.Map<java.lang.String,java.lang.Class> getAliasMap()
public java.util.Map<java.lang.String,java.lang.String> getPropertyMap()
public java.util.Map<java.lang.String,java.lang.Object> getObjectMap()
public java.util.List<java.lang.Object> getObjects()
public java.util.List<java.lang.Object> getRootObjects()
public java.lang.String getObjectId(java.lang.Object o)
o
- the object to retrieve the id for.
public void execute(java.lang.String uri)
uri
- the name of the XML Object Document file.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |