tags:

views:

120

answers:

1

I wrote a Java class that parses a bpel text file and then returns a count of the number of occurences of certain words. I wanted to convert it to VB2008 Forms application, so that its results are displayed in a TextBox and not on the console. The problem is that VB2008 lacks Scanner and StringTokenizer classes, which are in my current Java class. Am not sure how to get the same functionality (or better) in VB2008. Can someone out there help to convert this class. Thank you.

The Java class is as follows:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;

public class StringParser 
{
private int ctrlFlowStructures;
private String filePath;
private ArrayList<String> activities;   
final String[] ctrlFlowsArray={"sequence","if","while","repeatUntil","forEach", "pick","flow"};

public StringParser(String path)         
{
    filePath=path;
    ctrlFlowStructures =0;
    activities=new ArrayList<String>();
}    

//count number of occurences of words in ctrlFlowStructureArray
public int countCtrlFlowStructures ()
{    
    Scanner input=null;
    StringTokenizer st;
    String line=null;
    String openingComment="!--";
    String closingComment="--";
    int c=0;

    try
    {
        input=new Scanner( new FileInputStream(filePath));
    }

    catch(FileNotFoundException e)
    {
        System.out.println("Problem opening files.");
        System.exit(0);
    }        

    while(input.hasNextLine())
    {
        line=input.nextLine();
        line.trim(); 
        st= new StringTokenizer(line, " <,>\"",false);    
        String temp=null;                 
        while (st.hasMoreTokens())
        {  
            temp=st.nextToken();             

            //eliminate comments
            if(temp.equals(openingComment)||temp.equalsIgnoreCase("documentation"))
            {
                c=1;
            }
            if(temp.equals(closingComment)||temp.equalsIgnoreCase("/documentation"))
            {
                c=2;
            }
            if(c==0||c==2)
            {               
                for(int i=0;i< ctrlFlowsArray.length;i++)
                if(temp.equalsIgnoreCase(ctrlFlowsArray [i]))
                {
                    ctrlFlowStructures ++;                     
                }
            }
        }  
    } 
    input.close();   
    return ctrlFlowStructures;
}

//display control flow structures
public void display()
{
    int openingComment=0; //number of occurrence of an activity
    for(int i=0;i< ctrlFlowsArray.length;i++)
    {           
        for (int j=0;j<activities.size();j++)
        {               
            if(ctrlFlowsArray [i].equalsIgnoreCase(activities.get(j)))
            {
                openingComment++;
            }               
        }
        if(openingComment>0)
        {
            System.out.println(ctrlFlowsArray [i]+" = "+openingComment);
            openingComment=0;
        }
    }
}
public static void main(String[] args)    
{
    StringParser sp=new StringParser("c:\\MyFile1.bpel");
    int a = sp.countCtrlFlowStructures();        
    System.out.println(" The number of control-flow structure(s) = " + a);         
}
}

And this is the code snippet of the MyFile1.bpel file that was parsed:

    <sequence>
    <documentation>
        The sequence includes several activities which are executed in lexical order.
    </documentation>

    <receive
        name="start"
        partnerLink="MyProcess"
        operation="operation1"
        portType="ns1:portType1"
        variable="inputVar"
        createInstance="yes">

        <documentation>
            The Receive activity makes the process to wait for the incoming message to arrive.
        </documentation>
    </receive>

    <assign name="Assign1">
        <documentation>
            The Assign activity copies data from the input variable to the output variable.
        </documentation>

        <copy>
            <from>$inputVar.inputType/ns2:paramA</from>
            <to>$outputVar.resultType/ns2:paramA</to>
        </copy>
    </assign>

    <reply
        name="end"
        partnerLink="MyProcess"
        operation="operation1"
        portType="ns1:portType1"
        variable="outputVar">

        <documentation>
            The Reply activity returns a message from the process to the  partner which initiated the communication.
        </documentation>
    </reply>
</sequence>

Result:

The number of control-flow structure(s) = 1.
+1  A: 

You can use String.Split instead of StringTokenizer. For your use of Scanner, System.IO.StreamReader should be a suitable replacement.

Since what you are parsing looks like an XML file, you might consider using the XML parsing features of .NET instead of string operations.

Heinzi
I don't have a VB compiler with me right now so I cannot test your code, but this is what I noticed: Try using s.Split(Nothing) instead of s.Split(" "), so that it splits on *all* whitespace charaters. PS. Your "For Each s" loop does not make sense -- you don't use "s"!
Heinzi