Skip to content

Xml Hander

avurro edited this page Sep 3, 2015 · 5 revisions

As it may be compact, the configuration in XML file format, there is always the possibility of error-prone writing it, to eliminate these risks and speed up its completion, the framework exposes the class XmlHandler, with whom you can create or modify the configuration file.

IMPORTANT! XmlHandler handles the configuration but not the conversions

Constructors

The empty constructor defines a jmapper.xml file positioned at the application root:
new XmlHandler().
If exists an xml file that you want to handle, pass it to the constructor, as follows:
new XmlHandler("jmapper.xml").

Each method invoked on its instance will modify the file.

IMPORTANT! In this case, you do not need to declare the full path but only the file name, JMapper finds it recursively.

Methods

XmlHandler has a number of utility methods.
Most of its methods use two javaBean: Attribute and Global:

class Attribute {                  class Global {

    String name;                       String value;
    String value;                      String[] attributes;
    String[] attributes;               Class<?>[] classes;
    Class<?>[] classes;                String[] excluded;
    
    // getters and setters...          // getters and setters...
}                                  }

The Attribute class reflects the parameters of the @JMap annotation plus a string: name, the name of the current attribute.
The Global class reflects the parameters of the @JGlobalMap.

addAttributes

addAttributes lets you to add one or more attributes in a class that is present in the xml configuration file.

String[] attributes = {"field2Class1","field2Class2","field2Class3"};
Class<?>[]  classes = {Class1.class,Class2.class,Class3.class};
Attribute attribute = new Attribute("field2", attributes, classes);

xmlHandler.addAttributes(MyClass.class, attribute);

deleteAttributes

deleteAttributes eliminates attributes related to an existing class specifying their names.

xmlHandler.deleteAttributes(MyClass.class, "field2");

addGlobal

addGlobal permits to add a global mapping in a class that is present in the xml configuration file.

String[] excluded = {"field1Class1","field1Class2","field1Class3"};
Class<?>[]  globalClasses = {Class1.class,Class2.class,Class3.class};
Global global = new Global("globalMapping", globalClasses, excluded);

xmlHandler.addGlobal(MyClass.class, global);

deleteGlobal

deleteGlobal eliminates the global mapping related to an existing class specifying class name.

xmlHandler.deleteGlobal(MyClass.class);

overrideGlobal

overrideGlobal allows you to override an existing global mapping specifying the class name.

String[] excluded = {"field1Class1","field1Class2","field1Class3"};
Class<?>[]  globalClasses = {Class1.class,Class2.class,Class3.class};
Global global = new Global("globalMapping", globalClasses, excluded);

xmlHandler.overrideGlobal(MyClass.class, global);

addClass

addClass allows you to add, into the file configuration, the class passed in input.
There are many signatures of this method:

String[] attributes = {"field2Class1","field2Class2","field2Class3"};
Class<?>[]  classes = {Class1.class,Class2.class,Class3.class};
Attribute attribute = new Attribute("field2", attributes, classes);

String[] excluded = {"field1Class1","field1Class2","field1Class3"};
Class<?>[]  globalClasses = {Class1.class,Class2.class,Class3.class};
Global global = new Global("globalMapping", globalClasses, excluded);

xmlHandler.addClass(MyClass.class, attribute);
// or xmlHandler.addClass(MyClass.class, global);
// or xmlHandler.addClass(MyClass.class, global, attribute);

deleteClass

Deletes a class present in the Xml file.

xmlHandler.deleteClass(MyClass.class);

overrideClass

Override the class replacing the actual configuration with a new one given as input.

String[] attributes = {"field2Class1","field2Class2","field2Class3"};
Class<?>[]  classes = {Class1.class,Class2.class,Class3.class};
Attribute attribute = new Attribute("field2", attributes, classes);

String[] excluded = {"field1Class1","field1Class2","field1Class3"};
Class<?>[]  globalClasses = {Class1.class,Class2.class,Class3.class};
Global global = new Global("globalMapping", globalClasses, excluded);
		
xmlHandler.overrideClass(MyClass.class, attribute);
// or xmlHandler.overrideClass(MyClass.class, global);
// or xmlHandler.overrideClass(MyClass.class, global, attribute);

addAnnotatedClass

addAnnotatedClass allows you to add in the xml configuration file to one or more classes annotated passed as input to the method, not specifying any class the same will be done for all classes configured with annotation xmlHandler.addAnnotatedClass().

xmlHandler.addAnnotatedClass(AnnotatedClass.class);

addAnnotatedClassAll

Same logic as the previous method with the difference that the function will be effective even for the innerClass present.

deleteAnnotatedClasses

deleteAnnotatedClasses eliminates from xml file all the annotated classes.

xmlHandler.deleteAnnotatedClasses();

overrideAnnotatedClass

overrideAnnotatedClass allows you to update the xml configuration starting from the annotation of the classes passed as input, not specifying any parameters the same will be done for all annotated classes xmlHandler.overrideAnnotatedClass().

xmlHandler.overrideAnnotatedClass(AnnotatedClass.class);

overrideAnnotatedClassAll

Same logic as the previous method with the difference that the function will be effective even for the inner classes present.

fromXmlToAnnotation

This method allows to annotate a class starting from its xml configuration.
Not defining any class will be executed the same for all the classes present in the xml configuration file xmlHandler.fromXmlToAnnotation().

xmlHandler.fromXmlToAnnotation(MyClass.class);

fromXmlToAnnotationAll

Same logic as the previous method with the difference that the function will be effective even for the innerClass present.

cleanAnnotatedClass

This method eliminates the annotations from classes, without parameters will do the same for all annotated classes ```xmlHandler.cleanAnnotatedClass()```. ```java xmlHandler.cleanAnnotatedClass(AnnotatedClass.class); ```

cleanAnnotatedClassAll

Same logic as the previous method with the difference that the function will be effective even for the innerClass present.
Clone this wiki locally