Link to home
Start Free TrialLog in
Avatar of scorpio05678
scorpio05678Flag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of durgaprasad_j
durgaprasad_j

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
Avatar of Mayank S
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
Avatar of scorpio05678

ASKER

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
forgot to mention that point has been increased. (125 -> 200)

thanks

SC
>>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
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.
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial