0

I have a Maven & Spring based Java web application

In src/main/resources, I have one XML file.

sourceconfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<sourceConfig area="Defects">
    <adapterObject>jAdapter</adapterObject>
    <resultObject>jsonObject</resultObject>
</sourceConfig>

In I have a POJO for this SourceConfig.java

@XmlRootElement
public class SourceConfig {
    String area;
    String adapterObject;
    String resultObject;

    public String getArea() {
        return area;
    }

    @XmlAttribute
    public void setArea(String area) {
        this.area = area;
    }

    public String getAdapterObject() {
        return adapterObject;
    }

    @XmlElement
    public void setAdapterObject(String adapterObject) {
        this.adapterObject = adapterObject;
    }

    public String getResultObject() {
        return resultObject;
    }

    @XmlElement
    public void setResultObject(String resultObject) {
        this.resultObject = resultObject;
    }
}

I am able to parse the xml to object.

public class SourceAdapterConfig {

    public SourceConfig getConfigObject() throws JAXBException, IOException {
        JAXBContext jaxbContext = JAXBContext.newInstance(SourceConfig.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

        Resource resource=new ClassPathResource("sourceconfig.xml");
        File file=resource.getFile();

        SourceConfig sourceConfig = (SourceConfig) jaxbUnmarshaller.unmarshal(file);
        return sourceConfig;
    }
}

It is working fine.

But all are String. Some I want as object. For example, In XML I have mentioned <resultObject>jsonObject</resultObject>

I have a class com.myapp.config.JsonObject.java

So, instead of <resultObject>jsonObject</resultObject> If I mention class like this

<resultObject class="com.myapp.config.JsonObject">jsonObject</resultObject> 

or some other way to mention class, I should be able to get a JsonObject object in my SourceConfig How can I do that?

1 Answers1

0

use java reflection

Class theClass = Class.forName("com.example.Test");
Test testObject = (Test)theClass.newInstance();

This will create an instance of com.example.Test.

In your context,

public class SourceAdapterConfig {

    private SourceConfig config;

    private SourceConfig getConfigObject() throws JAXBException, IOException {
        JAXBContext jaxbContext = JAXBContext.newInstance(SourceConfig.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

        Resource resource=new ClassPathResource("sourceconfig.xml");
        File file=resource.getFile();

        SourceConfig sourceConfig = (SourceConfig) jaxbUnmarshaller.unmarshal(file);
        return sourceConfig;
    }

    public SourceAdapterConfig(){
        config = getConfigObject();
    }

    public Object getAdapterObject(){
        String adapterClassName = config.getAdapterObject();
        Class theClass = Class.forName(adapterClassName);
        return theClass.newInstance();
 }
}

Usage:

SourceAdapterConfig config = new SourceAdapterConfig();
Object adapterObject = config.getAdapterObject();
Chamil
  • 805
  • 5
  • 12
  • The class can be changed. For another XML or another config, there will be another class. So I cannot know to which class I need type cast at run time. Here you are type casting it to Test. What if it's another class. If I know which class it is, there is no need of xml file. I can create object with `new` operator –  Mar 03 '14 at 08:58
  • You can avoid the casting and return it simply in Object type. Or you can write your method using generic. http://stackoverflow.com/questions/450807/how-do-i-make-the-method-return-type-generic – Chamil Mar 03 '14 at 09:00
  • I am sorry, where should I do that? After unmarshalling only? Is there any way that when it is parsing itself, it will be converted to Object while parsing XML to POJO? –  Mar 03 '14 at 09:20
  • If your adapter object is a simple POJO, you can do direct unmarshaling. Because marshaling/unmarshaling only saves the state of the object in a file. So you have to do it in the way I mentioned. – Chamil Mar 03 '14 at 09:37