Ashish
Ashish

Reputation: 3060

How to move to XML configurations

I am required to move some of our application configuration classes to XMLs. The classes mainly have enums, which are used by other classes. These enums are extensively used in our application. For instance, we have classes like

    enum ColumnType{
        type1("Type1"),type2("type2"),type3("type3")
    }

Also we need these types to instantiate classes. for instance,

    Processor p = new StringValueProcessor(ColumnType.type1);

How can I move this to an XML file without changing the dependecies in my application?

Edit: It is not mandatory to keep these enums and I don't want to compile the code against the classes created from xml. The config needs to be dynamic, that's the whole point of moving to XML, so that we can configure things in XML and there is no need of compiling and re-deploying. My main concern is to be able to restrict instances for all column types to one and make them accessible throughout my application.

Edit: After thinking over the design for some more time, I have narrowed down to two essential requirements. 1) I would define some xml tags with some properties and I would need to convert it to object 2) I would also define some tags (the way servlets are defined in web.xml) and I would need to initialise the corresponding class 3) I would further define some mapping tags which will map the objects created in step 1) to instances initialised in step 2). This should be converted to java HashMap, where there can be only one instance of objects defined in step 1) but there will be a new instance of objects defined in step 2) for each mapping. Is there a framework which can provide this functionality out-of-the-box?

Upvotes: 0

Views: 73

Answers (2)

JohnnyK
JohnnyK

Reputation: 1102

I think the answer your looking for is to use JAXB. It lets you turn XML into POJOs and vice-versa. It even has some functionality for using enums. All you have to do is add some annotations to your java and you can convert to and from XML.

By using annotations, you won't affect any existing functionality.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533590

You can use the XML files to generate the enums. This has to be done at compile/build time, or you cannot use them in your code like the second example (as they don't exist at compile time)

Why do you want to migrate the enums to XML?

Upvotes: 1

Related Questions