3

I would like to create a class in Java based on the fields defined in my XML config file:

For example: if the XML file contains (the syntax has been maligned for posting):

<property name="agent_host"></property>
<property name="subsystem"></property>

then internally it will create a class Event such as Event(String agentHost, String subSystem) that the client can instantiate. Note: the client always knows that this class will be called "Event" but does not know how many "mandatory parameters" it needs to pass.

On the other hand if I have a XML file with:

<property name="agent_host"></property>

then it will create Event(String eventHost) that the client can use for instantiation.

7
  • 1
    Do you want to generate the code (the .java files) for static compilation? Or do you want to generate the code at runtime? Commented Sep 23, 2010 at 17:29
  • The code(class Event) should be available at runtime Commented Sep 23, 2010 at 17:31
  • In that case, how are the users of the class going to instantiate it (since they don't have a compile-time reference)? Do you anticipate their using reflection? Commented Sep 23, 2010 at 17:32
  • That will work.. reflection. But they already know that they need to create an instance of class Event. To make it easier, the number of parameters required by the class Event constructor should be equal to the number of property fields in the XML file Commented Sep 23, 2010 at 17:36
  • 1
    No, to be precise, they already know that they need to create an instance of a class named "Event", which is entirely different. I'm inclined to agree with @Bill below that this is not going to be a pleasant experience for either you or your consumers. Can you elaborate on what advantage you see in having this class get generated on the fly rather than using a loose data structure like a map? Commented Sep 23, 2010 at 17:38

3 Answers 3

3

Yes, you could use reflection, but what comes to my mind is working with a class that you could add property. Imagine a class that has one encapsulated HashMap using a String as a key (for the attribute name) and the value of the attribute so you could read the XML file and for evey property add the attribute to the class like. For this line:

<property name="subsystem" type="String">value123</property>


GenericClass c = new GenericClass();
c.addAttribute("subsystem", new String("value123"));
//and you could implement a get and set methods like this:
public String getAttributeValue(String attributeName)
{
   return internalHashMap.get(attributeName).toString();
}

Using this you could also implement a setAttributeValue will be quite simple I think

Sign up to request clarification or add additional context in comments.

4 Comments

Why a hashMap? I can also do this using a LinkList?
Just a note: a naive implementation of setAttributeValue() will probably allow the user of the class to set values to attributes that were not defined in the XML, therefore creating them. This might be a problem depending on the OP's needs.
Yea I know, so you should throw an Exception when trying to set one attribute that doesnt exist, an IlegallArgumentException would be ok I think... And yeah a Hashmap so you can Map the attribute name with the value it has. You could use a LinkedList but you you have to create a class that would encapsulate the attribute name and value. So, use a Hashmap, much more simple
Thank you so much. I had a similar problem and was about to over-engineer way to much. Simple class extending a Map with some helper methods does the trick for me!
1

This isn't really a class you want, it's data. Why not use a hashmap? I really dislike "Bean" style classes--they encourage bad coding (there is no place in a generated class to put actual code, so everything ends up being manipulated by external code anyway).

You can just load a hashmap from your XML and inject it into a real object--that way you don't have to worry about actually passing a hash around, you are passing a real object with real business methods and real type safety--it just HAPPENS to use a hashmap internally to store data instead of member variables.

I've done a lot more than this, but at some point you realize Hibernate does everything you want for you.

2 Comments

what if values have different types, say some are integer, some of them are strings and some are double. And that type information has to be preserved.
If it is just data, not directly used in code, then the type should not be enforced by the language, it doesn't make sense. If you need type info then track it as more data. If you are going to use it in code, then using a bean pattern leads to bad code in every case I've seen, you should use an annotated hibernate object instead -- one that can contain business logic and not just a bean.
1

I think the DynaBean from Commons-BeanUtils may be what you're looking for.

A DynaBean is a Java object that supports properties whose names and data types, as well as values, may be dynamically modified. To the maximum degree feasible, other components of the BeanUtils package will recognize such beans and treat them as standard JavaBeans for the purpose of retrieving and setting property values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.