tags:

views:

62

answers:

3

Hello, I want to write a java code to read the input XML file and generate its equivalent Java code.

enter code here

<CodeMarkers classname="CodeGenerator" access="public">

<MethodMarker javadoc="Compares two strings" name="stringComparator" access="private" static="no" return="boolean">
    <Input name="str1" notnull="true"/>
    <Input name="str2" notnull="false"/>
    <MethodVariable name="success" type="boolean" initvalue="false"/>
    <JavaCode>
        <![CDATA[
        System.out.println("Executing comparator");
        if (str1.equals(str2)) success = BOOLEAN.TRUE;
        return success;
        ]]>
    </JavaCode>
</MethodMarker>

<MethodMarker javadoc="Database look for an employee id" name="getEmployeeId" access="public" static="yes" return="int">
    <Input name="employeeName" notnull="true"/>
    <MethodVariable name="empId" type="int"/>
    <DBInit>
        <SQLCode>
            <![CDATA[
            empId = SELECT emp_id FROM test.employee where emp_id = empId;
            return empId;
            ]]>
        </SQLCode>
    </DBInit>
</MethodMarker>

I need to write a java code to convert the above XML code into a java code ie
The first line of output java code should be public class CodeGenerator{}

enter code here
                  <![CDATA[
            empId = SELECT emp_id FROM test.employee where emp_id = empId;
            return empId;
            ]]>

Im not able to put the Lines under CDATA should be used as is in the method

Thanks Athreya

A: 

How the class should be created form that XML is up to You. You need some kind of factory that properly parse those settings and creat a java file.

That what You need is compile and load class file into you "program".

Tips for compilation: http://stackoverflow.com/questions/566239/options-for-dynamic-compilation-in-java-5

This might help for loading: http://www.javaworld.com/javaworld/jw-06-2006/jw-0612-dynamic.html

Vash
A: 

Q2: "How to get data under < ![CDATA][...]]"

For retriveing the character data (CDATA) You do something like this,

import org.w3c.dom.Element;

 public static String getCharacterDataFromElement(Element e) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
      CharacterData cd = (CharacterData) child;
      return cd.getData();
    }
    return "";
  } 
Vash
Thanx Vash, ill try as you've mentioned
Athreya
@user333521,cool but for the future if you accept some answer than set is as accepted this will remove the question from unanswered top.
Vash
A: 

If I understand correcly I would:

  1. Use an XML-Parser to read in the XML-definition ( start here: http://java.sun.com/javase/6/docs/api/javax/xml/parsers/DocumentBuilderFactory.html )
  2. Use a template engine of your choise to create the source code ( may be this is a good candidate http://www.stringtemplate.org/ )
  3. Use the java compiler to compile the generated code ( http://java.sun.com/javase/6/docs/api/javax/tools/JavaCompiler.html )
  4. Load your generated code into the JVM ( http://www.javaworld.com/javaworld/jw-06-2006/jw-0612-dynamic.html?page=3 )
Arne