简体   繁体   中英

Parse IDL enums to Java Enums

I've got a bunch of IDL files filled with enums. I would like a way to parse the IDL files, extract out the enums and create native Java enums based on these (also ignoring any other declarations in the IDL, like consts, unions, or structs).

I'm not even sure where to start with this. Are there any good Linux tools designed for this purpose? I'm assuming regex would be needed?

To explain what I mean, here is an example in 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

I would like to run a command... such as:

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

and out would spit these source java files with enums:

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
}

Is this feasible?

a very quick and dirty solution - i'm sure there are tons of parsers out there which would work better, nicer and whatever. But if your IDL files all look the same, why bother?

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM