XML to JAVA code

Hi

I am trying to write a utility that read an xml file and produce java code based on the data in it.

XML data

<State name="ABC" DecisionMethod="ABCDecision"/>            
<State name="DEF" DecisionMethod="DEFDecision"/>            
<State name="GHI" DecisionMethod="GHIDecision"/>            

Java code code should be something like this :

State ABCState = new State("ABC", this, getMethod("ABCDecision"));
State DEFState = new State("DEF", this, getMethod("DEFDecision"));
State GHIState = new State("GHI", this, getMethod("GHIDecision"));

public void ABCDecision(){}
public void DEFDecision(){}
public void GHIDecision(){}


The idea is to not hard code the states and their methods but just provide the XML definiation of them and the utility will read it and create a file called "TestData.java" which will be used by another program as input to data.

has any got an idea if it can be done and how (get  me started).

thanks

SC
scorpio05678Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

durgaprasad_jCommented:
hi,

IMO, If you can parse the xml file and get the attribute values, you can write the java file.
for parsing see this http://www.w3schools.com/dom/dom_examples.asp 

I dont know any of the existing tools for doing this.

Hope this helps
0
Mayank SAssociate Director - Product EngineeringCommented:
0
ashok3sepCommented:
Hi,
Hope this link does what you need or atleast help you with Ideas.....

http://www-106.ibm.com/developerworks/library/x-quick/

regards,

Freedom
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

scorpio05678Author Commented:
Hi guys

Thanks for the support and links. i have read through all the 3 links  but none of them is suiteable to what i am doing, the closest one was IBM's but it uses some thing else.

Any way  I managed to solve the problem by long winded way (i.e parse the file (using Quick and Dirty parser) into map, then create an array for each entry of the map with all the required fields and then print them to the file).

Now i am faced with another problem: i am trying to create a thread of an object and want them to wait untill the last thread is created and initialised before i can run them. It is very important to have all the thread in waiting state (ie is blocked) and than be triggred by the last thread.

I have created a class called ThreadController which takes a custom object of type Communicating XMachine and the number of maximum allowed and then create a thread of it.

"Communicating XMachine" implments runnable and have a method called run.

When my main program call Run on this ThreadController, threads are created fine but when i try to tell the newly formed thread to wait (in synchronized incount() )i get this error:
"java.lang.IllegalMonitorStateException: current thread not owner".


my code for this ThreadController class is as follows:
************************************************
public class ThreadController extends Thread
{
            int threadCount = 0;
            int maxAllowed = 0;
            Thread thisThread= null;
            Set CXMMachineSet;
            public ThreadController(Set machineSet,int maxAllowed)
            {
      this.maxAllowed = maxAllowed;
      this.CXMMachineSet = machineSet;
             }
             public void run()
             {
      if (!CXMMachineSet.isEmpty() )
      {
           createThread();
      }
              }
             public void createThread()
             {
       Iterator thisMachineIterator = CXMMachineSet.iterator();
       CommunicatingXMachine tempMachine;
        while (thisMachineIterator.hasNext())
         {
                 tempMachine = (CommunicatingXMachine)thisMachineIterator.next();
                 thisThread = new Thread(tempMachine);
                 thisThread.setName("CXM " + tempMachine.getKey());
                           incCount();      
         }
      }
      public synchronized  void incCount()
      {
                   try
         {
                 if (this.threadCount < this.maxAllowed)
                 {
            this.threadCount ++;                        
            thisThread.wait();
                  }
                  else
                  {
            notifyAll();
                   }
            }
            catch(Exception exp)
            {
            System.out.println(exp.toString());
             }
      }
}

any one have any idea how to deal with this thing ?

thanks
SC
0
scorpio05678Author Commented:
forgot to mention that point has been increased. (125 -> 200)

thanks

SC
0
durgaprasad_jCommented:
>>It is very important to have all the thread in waiting state (ie is blocked) and than be triggred by the last thread.

I think you need CyclicBarrier for this purpose.

for this you can use java.util.concurrent.CyclicBarrier [it is in j2se 5.0 ].

see this link http://java.sun.com/developer/JDCTechTips/2005/tt0216.html#1

just go down a bit , you will find an example about CyclicBarrier.

Hope this helps
0
scorpio05678Author Commented:
hi mate

I have solved it my self. Solution as follows:

General Solution is: In the run method of my customized thread object i pass the control to the Barrier class where the count is increased and then the control is returned back to the run method where it execuate the wait(). Once all the threads(except the last one) have called the Barrier class then we fell into the loop where the size is equal to the max no of threads then wakeup method is called on all the threads in reverse order which notify each thread in turn.

Note the reference to the Barrier class is passed to my customised thread in the constructor and their is only one instance of the barrier class for the whole program.

Java code for the Barrier.java

****************************************
package pkgGlobal;


import pkgCommunicatingXMachine.CommunicatingXMachine;
import java.util.*;
import java.util.logging.Logger;



public class Barrier
{
      public int maxAllowed = 0;
      protected GlobalInterface thisGlobalInterface = Glue.getthisGlobalInterfaceImp();
      ArrayList CXMSet = new ArrayList();
      private Logger logger = Logger.getLogger("Barrier");

      public Barrier()
      {
            
      }
      public void setMax(int maxAllowed)
      {
            try
            {
                  this.maxAllowed = maxAllowed;
            }
            catch (Exception exp)
            {
                  System.out.println("Barrier -> setMax " + exp.toString());
            }
      }
      
      /**
       *
       */
      public boolean incCount(CommunicatingXMachine thisMachineRef)
      {
            CommunicatingXMachine thisMac= null;            
            try
            {                        
                  if (CXMSet.size() < this.maxAllowed)
                  {
                        CXMSet.add(thisMachineRef);
                        System.out.println("Barrier -> IncCount : Waiting Machines= " + CXMSet.size() + " Max Allowed= "+ this.maxAllowed);
                  }
                  if (CXMSet.size() == this.maxAllowed)
                  {
                        for(int i=CXMSet.size(); i>0; i--)      
                        {
                               thisMac = (CommunicatingXMachine)CXMSet.get(i-1);
                               //logger.severe(thisMac.toString());
                               thisMac.wakeup();                              
                        }                        
                  }
      
                  return true;
            }
            catch (Exception exp)
            {
                  System.out.println("Barrier -> incCount " + exp.toString());
                  System.out.println("Machine ID : " + thisMac.getKey());                  
                  return false;
            }
      }
}

****************************************
Run and Wake method in my Customised thread object class.

public void wakup()
{
      notify();
}
public void run()
{
     thisBarrier.incCount(this);
     wait();
     ...............................
     ..............................
}
cheers.

SC.
0
moduloCommented:
PAQed with points refunded (200)

modulo
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.