将java类转换为vb 2008应用程序

发布于 2024-08-09 12:18:05 字数 4479 浏览 3 评论 0原文

我编写了一个 Java 类,它解析 bpel 文本文件,然后返回某些单词出现次数的计数。我想将其转换为 VB2008 Forms 应用程序,以便其结果显示在文本框中而不是控制台上。问题是VB2008缺少Scanner和StringTokenizer类,它们在我当前的Java类中。我不确定如何在 VB2008 中获得相同的功能(或更好的功能)。有人可以帮助转换这个类吗?谢谢。

Java 类如下:

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);         
}
}

这是解析的 MyFile1.bpel 文件的代码片段:

    <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>

结果:

The number of control-flow structure(s) = 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2024-08-16 12:18:05

您可以使用 String.Split 而不是 StringTokenizer。对于扫描程序的使用,System.IO.StreamReader 应该是合适的替代品。

由于您正在解析的内容看起来像 XML 文件,因此您可能会考虑使用 .NET 的 XML 解析功能而不是字符串操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文