Click here to Skip to main content
15,889,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to analyze Java source code and produce some kind of output based on its contents, I started by moving the source code to txt file so I can pars it, then I would search for specific word in each test case if I found it I will present it in the produced xml file, that words I'm looking for are Cell and Engine.
the coude that I copied to txt file for parsing purpose is down;
Java
public class GameTest {
    @Test
    public void testGetMonsters() {
        Cell cell11 = aBoard.getCell(1, 1);
        theEngine = new Engine(theGame);   
    }

    @Test
    public void testDxDyPossibleMove() {
        Cell cell11 = aBoard.getCell(1, 1);
    }  
}


The structure of the output xml file looks like:

HTML
<class name="GameTest">
    <testcase name="testDxDyPossibleMove" Object1="Cell" object2="Engine" />
    <testcase name="testDxDyPossibleMove" Object1="Cell" object2="null" />
</class>

since we found the word Cell and Engine in the first test case we present them as an attribute in the xml file object1 and object2 respectively, while in the second test case there is only Engine so that attribute for Cell word is assigned with null in the xml file.
Posted
Updated 25-Oct-11 6:25am
v2
Comments
Richard MacCutchan 25-Oct-11 12:26pm    
Do you have a question?
medoded2009 25-Oct-11 12:45pm    
My question how can I use java to produce that xml file from that txt file using java parser

1 solution

If the words Cell and Engine have to be Java types[^], as your usage sample suggests, there is no way around using a real parser that can parse the Java language. If the words can appear in any context you could go after them with regular expressions[^] making sure that the words you are searching for are delimited by word boundary characters ("\b").

This site can help you build a Java parser as there are already samples for the complete Java grammar: http://www.antlr.org/[^].

If you have any doubts or questions, feel free to leave a comment.

Cheers!

—MRB
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900