2009-10-29 50 views
0

我編寫了一個Java類,用於解析bpel文本文件,然後返回某些單詞出現次數的計數。我想將其轉換爲VB2008 Forms應用程序,以便其結果顯示在TextBox中而不是在控制檯上。問題是VB2008缺少Scanner和StringTokenizer類,它們在我當前的Java類中。我不確定如何在VB2008中獲得相同的功能(或更好)。有人可以幫助轉換這個班級。謝謝。將java類轉換爲vb 2008應用程序

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

回答

1

您可以使用String.Split而不是StringTokenizer。對於您使用掃描儀,System.IO.StreamReader應該是一個合適的替代品。

由於您正在解析的內容看起來像一個XML文件,因此您可以考慮使用.NET的XML解析功能而不是字符串操作。

+0

感謝Heinzi, 我試過StreamReader&Split,但得到不正確的結果。我想分割文件,然後計算「序列」的出現次數。任何想法如何做到這一點?見我的代碼: 昏暗參考譯文字符串= txtOpen.Text 昏暗ARR作爲字符串()= s.Split( 「 」) 昏暗SEARCHTERM作爲字符串=「 」 各S在ARR txtSplit.Multiline =真 txtSplit.Lines = ARR 接下來 昏暗matchQuery =從字在ARR凡word.ToLowerInvariant()= searchTerm.ToLowerInvariant()選擇一個單詞 昏暗算作整數= matchQuery.Count() txtWordOccurences.Multiline =真 txtWordOccurences.Text = count.ToString() – user198934

+0

我現在還沒有VB編譯器,所以我無法測試你的代碼,但這是我注意到的:嘗試使用s.Split(Nothing)而不是s.Split(「」) ,所以它分裂*所有* wh itespace charaters。 PS。你的「For Each s」循環沒有意義 - 你不使用「s」! – Heinzi