I need to inject values from xml file into my java code using reflection mechanism, currently I implemented xml parsing mechanism in this way:
protected Map<String, BasicElement> container = new HashMap<String, BasicElement();
// contain element instances that appears at xml file
for each class I there is dedicated xml file, that define class values.
here is an example:
java class
public class MerchandiseDesignDialog extends EntityDesignDialog {
private TextField model;
private TextField name;
private TextField description;
private DropDownList attribute_1;
private DropDownList attribute_2;
private DropDownList attribute_3;
private DropDownList attribute_4;
private final static String NAME = "Name";
private final static String DESCRIPTION = "Description";
private final static String ATTRIBUE_1 = "Attribute (1)";
private final static String ATTRIBUE_2= "Attribute (2)";
private final static String ATTRIBUE_3 = "Attribute (3)";
private final static String ATTRIBUE_4 = "Attribute (4)";
private static final String XML_FILE_NAME = "/src/main/java/com/merchandiseDesignDialog.xml";
@Override
protected void initInnerElements() {
try {
this.name = (TextField)container.get(NAME);
this.description = (TextField) container.get(DESCRIPTION);
this.attribute_1 = (DropDownList)container.get(ATTRIBUE_1);
this.attribute_2 = (DropDownList)container.get(ATTRIBUE_2);
this.attribute_3 = (DropDownList)container.get(ATTRIBUE_3);
this.attribute_4 = (DropDownList)container.get(ATTRIBUE_4);
} catch (ClassCastException e) {
report.reportStatus(this.getClass().getName() + " : initializing elements failed ", Status.FAIL);
}
}
public TextField getName() {
this.find();
name.find(this);
return name;
}
public TextField getDescription() {
this.find();
description.find(this);
return description;
}
public DropDownList getAttribute_1() {
this.find();
attribute_1.find(this);
return attribute_1;
}
public DropDownList getAttribute_2() {
this.find();
attribute_2.find(this);
return attribute_2;
}
public DropDownList getAttribute_3() {
this.find();
attribute_3.find(this);
return attribute_3;
}
public DropDownList getAttribute_4() {
this.find();
attribute_4.find(this);
return attribute_4;
}
/**********************************************************
* ********************************************************
***********************************************************/
public MerchandiseDesignDialog setName(String nameValue) {
getName().insert(nameValue);
return this;
}
public MerchandiseDesignDialog setDescription(String descriptionValue) {
getDescription().insert(descriptionValue);
return this;
}
public MerchandiseDesignDialog selectAttribute_1(String attribute_1Val) throws NoSuchElementException {
getAttribute_1().select(attribute_1Val);
return this;
}
public MerchandiseDesignDialog selectAttribute_2(String attribute_2Val) throws NoSuchElementException {
getAttribute_2().select(attribute_2Val);
return this;
}
public MerchandiseDesignDialog selectAttribute_3(String attribute_3Val) throws NoSuchElementException {
getAttribute_3().select(attribute_3Val);
return this;
}
public MerchandiseDesignDialog selectAttribute_4(String attribute_4Val) throws NoSuchElementException {
getAttribute_4().select(attribute_4Val);
return this;
}
}
detecated Xml file
<ComplexElement type="WorkSpace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:/Users/Ali/Downloads/ElementsTutorial/complex_element.xsd">
<UIBasicElement type="textField" locatorType="name" locator="name" visibleName="Name"/>
<UIBasicElement type="textField" locatorType="name" locator="description" visibleName="Description"/>
<!-- *************************************************************************** -->
<UIBasicElement type="dropdown" locatorType="name" locator="attribute1TypeId" visibleName="Attribute (1)"/>
<!-- *************************************************************************** -->
<UIBasicElement type="dropdown" locatorType="name" locator="attribute2TypeId" visibleName="Attribute (2)"/>
<!-- *************************************************************************** -->
<UIBasicElement type="dropdown" locatorType="name" locator="attribute3TypeId" visibleName="Attribute (3)"/>
<!-- *************************************************************************** -->
<UIBasicElement type="dropdown" locatorType="name" locator="attribute4TypeId" visibleName="Attribute (4)"/>
</ComplexElement>
Code explanation, container include all xml file content, each xml element has a java instance. at each class from I should implement the following:
-
initInnerElements() method, that init each one of the class field, by getting it from container.
-
get method the invoke find methods before return each field.
- and operation method the invoke get method and do an operation.
after describe the current status, here is The Question:
How can I implement the same code using the following code structure:
@Page(path = "/src/main/java/com/merchandiseDesignDialog.xml")
public interface IMerchandiseDesignDialog {
@Entity(visibileName = "Name")
public TextField getName();
@Entity(visibileName = "Description")
public TextField getDescription() ;
@Entity(visibileName = "Attribute (1)")
public SRODropDownList getAttribute_1() ;
@Entity(visibileName = "Attribute (2)")
public SRODropDownList getAttribute_2();
@Entity(visibileName = "Attribute (3)")
public SRODropDownList getAttribute_3() ;
@Entity(visibileName = "Attribute (4)")
public SRODropDownList getAttribute_4() ;
/**********************************************************
* ********************************************************
***********************************************************/
public IMerchandiseDesignDialog setName(String nameValue);
public IMerchandiseDesignDialog setDescription(String descriptionValue);
public IMerchandiseDesignDialog selectAttribute_1(String attribute_1Val) throws NoSuchElementException ;
public IMerchandiseDesignDialog selectAttribute_2(String attribute_2Val) throws NoSuchElementException ;
public IMerchandiseDesignDialog selectAttribute_3(String attribute_3Val) throws NoSuchElementException ;
public IMerchandiseDesignDialog selectAttribute_4(String attribute_4Val) throws NoSuchElementException ;
}
And some how the magic is happen in daemon code, that I don't need to write at each class again and again. I need to replace that MerchandiseDesignDialog class, and use an interface that only describe the methods with annotations. I know that java reflection is the correct way but I don't know how to implement it.
Aucun commentaire:
Enregistrer un commentaire