2012-06-21 181 views
0

我有一堆IDL文件充滿了枚舉。我想要一種方法來解析IDL文件,提取出枚舉並基於這些語句創建本地Java枚舉(也忽略IDL中的任何其他聲明,如常量,聯合或結構)。解析IDL枚舉到Java枚舉

我甚至不確定從哪裏開始。有沒有爲此目的而設計的好的Linux工具?我假設正則表達式將需要?

爲了說明我的意思,這裏是common.idl一個例子:

#ifndef __common_idl__ 
#define __common_idl__ 



module common_idl { 


    const short MAX_UNITS  = 99; 
    const short MAX_SIZE = 15; 

    enum Bearing_Type { 
    North_Bearing, 
    South_Bearing, 
    No_Bearing 
    }; 

    enum Bounds_Type { 
    Empty, 
    Full, 
    Above, 
    Between, 
    Below 
    }; 



    enum Identity_Type { 
     Pending, 
     Unknown, 
     Assumed_Friend, 
     Friend 
    }; 

    enum Status_Type { 
    No_Status, 
    To_Status, 
    To_Established, 
    From_Established 
    }; 

    enum Emergency_Type { 
     Reported, 
     Confirmed, 
     None  
    }; 

struct Console_Marker { 
    boolean        Marked; 
};  


    typedef unsigned short Index_Type; 
    typedef long   TQ_Type;     
    typedef unsigned short Count_Type;    
    typedef unsigned long Number_Of_Type;   
    typedef unsigned short Special_Index_Type; 
    typedef string<2>  Code_Type;     


}; 


#endif 

我想運行一個命令,如:

converter common.idl -package com.myproject -outputDir src

進出會吐出這些源java文件與枚舉:

src/com/myproject/Bearing_Type.java

package com.myproject; 

public enum Bearing_Type { 
    North_Bearing, 
    South_Bearing, 
    No_Bearing 
} 

的src/COM/myproject的/ Bounds_Type.java

package com.myproject; 

public enum Bounds_Type { 
    Empty, 
    Full, 
    Above, 
    Between, 
    Below 
} 

的src/COM/myproject的/ Emergency_Type.java

package com.myproject; 

public enum Emergency_Type { 
    Reported, 
    Confirmed, 
    None  
} 

的src/COM/myproject的/ Identity_Type.java

package com.myproject; 

public enum Identity_Type { 
    Pending, 
    Unknown, 
    Assumed_Friend, 
    Friend 
} 

的src/COM/myproject的/ Status_Type.java

package com.myproject; 

public enum Status_Type { 
    No_Status, 
    To_Status, 
    To_Established, 
    From_Established 
} 

這是可行的?

回答

1

一個非常快速和骯髒的解決方案 - 我敢肯定,有大量的解析器在那裏會更好,更好和任何工作。但是如果你的IDL文件看起來都一樣,爲什麼呢?

import java.io.File; 
import java.io.InputStream; 
import java.util.Scanner; 

public class IDLParserTest { 

    public static void main(String[] args) { 
     parse("common.idl"); 
    } 

    private static void parse(String file) { 
     InputStream is = IDLParserTest.class.getResourceAsStream(file); 
     Scanner scanner = new Scanner(is); 

     while (parseEnum(scanner)) 
      ; 
    } 

    protected static boolean parseEnum(Scanner scanner) { 
     if (!scanner.hasNext()) 
      return false; 
     String strEnum = scanner.findWithinHorizon("enum", 8192); 
     if (strEnum == null) 
      return false; 
     if (!strEnum.equals("enum")) 
      return true; 
     StringBuilder javaCode = new StringBuilder(); 
     String strClass = scanner.next(); 
     scanner.next(); // skip open bracket 
     javaCode.append("package com.mycompany; "); 
     javaCode.append("public enum " + strClass + " { "); 
     while (scanner.hasNext()) { 
      String f = scanner.next(); 
      // System.out.println("<<<" + f); 
      if (f.equals("};")) { 
       javaCode.append(" }"); 
       File outFile = new File(strClass + ".java"); 
       // TODO: Write javaCode.toString() to outFile 
       System.out.println("Wrote: " + outFile + ":" + javaCode.toString()); 
       return true; 
      } else { 
       javaCode.append(f); 
      } 
     } 
     return true; 
    } 

}