我写了一个Java类来解析一个bpel文本文件,然后返回某些单词出现次数的计数。我想将其转换为VB2008 Forms应用程序,以便其结果显示在TextBox而不是控制台上。问题是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.
答案 0 :(得分:1)
您可以使用String.Split代替StringTokenizer。对于您使用扫描仪,System.IO.StreamReader应该是合适的替代品。
由于您解析的内容看起来像XML文件,因此您可以考虑使用.NET的XML解析功能而不是字符串操作。