?
Solved

XML to JAVA code

Posted on 2005-02-24
10
Medium Priority
?
259 Views
Last Modified: 2013-12-14
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
0
Comment
Question by:scorpio05678
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 6

Expert Comment

by:durgaprasad_j
ID: 13395048
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 13400566
0
 
LVL 5

Expert Comment

by:ashok3sep
ID: 13440549
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:scorpio05678
ID: 13444173
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
 

Author Comment

by:scorpio05678
ID: 13444183
forgot to mention that point has been increased. (125 -> 200)

thanks

SC
0
 
LVL 6

Expert Comment

by:durgaprasad_j
ID: 13448524
>>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
 

Author Comment

by:scorpio05678
ID: 13657957
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
 

Accepted Solution

by:
modulo earned 0 total points
ID: 13747248
PAQed with points refunded (200)

modulo
Community Support Moderator
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question