0

我有一堆充满枚举的 IDL 文件。我想要一种方法来解析 IDL 文件,提取枚举并基于这些创建本机 Java 枚举(也忽略 IDL 中的任何其他声明,如 const、联合或结构)。

我什至不知道从哪里开始。有没有为此目的而设计的好的 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

并且 out 会用枚举吐出这些源 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
}

这可行吗?

4

1 回答 1

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

}
于 2012-06-21T22:32:35.257 回答