From c1acdb6dc012d494f1ad6da4d0fc79aae164da67 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Thu, 29 Sep 2016 15:24:09 -0700 Subject: [PATCH] Refactoring policy parsing section; Preparing for parsing multiple policy files --- iotjava/iotpolicy/IoTCompiler.java | 63 +++++++++++++++++------------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java index 1107451..e79bea6 100644 --- a/iotjava/iotpolicy/IoTCompiler.java +++ b/iotjava/iotpolicy/IoTCompiler.java @@ -34,7 +34,6 @@ public class IoTCompiler { /** * Class properties */ - private String origInt; private ParseTreeHandler ptHandler; private InterfaceDecl intDecl; private CapabilityDecl capDecl; @@ -157,7 +156,6 @@ public class IoTCompiler { */ public IoTCompiler() { - origInt = null; ptHandler = new ParseTreeHandler(); intDecl = null; capDecl = null; @@ -175,10 +173,9 @@ public class IoTCompiler { } - public IoTCompiler(String _origInt, ParseNode _pnPol, ParseNode _pnReq) { + public IoTCompiler(String _intface, ParseNode _pnPol, ParseNode _pnReq) { - origInt = _origInt; - ptHandler = new ParseTreeHandler(_origInt, _pnPol, _pnReq); + ptHandler = new ParseTreeHandler(_intface, _pnPol, _pnReq); intDecl = null; capDecl = null; reqDecl = null; @@ -263,12 +260,12 @@ public class IoTCompiler { * The local interface has to be the input parameter for the stub and the stub * interface has to be the input parameter for the local class. */ - public void generateJavaLocalInterface() throws IOException { + public void generateJavaLocalInterface(String _intface) throws IOException { // Create a new directory createDirectory(dir); // Open a new file to write into - String intface = origInt; + String intface = _intface; FileWriter fw = new FileWriter(dir + "/" + intface + ".java"); pw = new PrintWriter(new BufferedWriter(fw)); // Pass in set of methods and get import classes @@ -311,12 +308,12 @@ public class IoTCompiler { * The local interface has to be the input parameter for the stub and the stub * interface has to be the input parameter for the local class. */ - public void generateCplusLocalInterface() throws IOException { + public void generateCplusLocalInterface(String _intface) throws IOException { // Create a new directory createDirectory(dir); // Open a new file to write into - String intface = origInt; + String intface = _intface; FileWriter fw = new FileWriter(dir + "/" + intface + ".hpp"); pw = new PrintWriter(new BufferedWriter(fw)); // Write file headers @@ -620,6 +617,27 @@ public class IoTCompiler { } + /** + * parseFile() prepares Lexer and Parser objects, then parses the file + */ + public static ParseNode parseFile(String file) { + + ParseNode pn = null; + try { + ComplexSymbolFactory csf = new ComplexSymbolFactory(); + ScannerBuffer lexer = + new ScannerBuffer(new Lexer(new BufferedReader(new FileReader(file)),csf)); + Parser parse = new Parser(lexer,csf); + pn = (ParseNode) parse.parse().value; + } catch (Exception e) { + System.out.println("IoTCompiler: ERROR parsing policy file!"); + e.printStackTrace(); + } + + return pn; + } + + /**================================================ * Helper functions to write stub codes into files **================================================ @@ -864,36 +882,27 @@ public class IoTCompiler { public static void main(String[] args) throws Exception { // Runtime options - if (args.length != 0) { + if (args.length > 1) { // Display help if ((args[0].equals("--help") || (args[0].equals("-h")))) { IoTCompiler.printUsage(); } else { // Parse main policy file - ComplexSymbolFactory csfPol = new ComplexSymbolFactory(); - ScannerBuffer lexerPol = - new ScannerBuffer(new Lexer(new BufferedReader(new FileReader(args[0])),csfPol)); - Parser parsePol = new Parser(lexerPol,csfPol); - ParseNode pnPol = (ParseNode) parsePol.parse().value; + ParseNode pnPol = IoTCompiler.parseFile(args[0]); // Parse "requires" policy file - ComplexSymbolFactory csfReq = new ComplexSymbolFactory(); - ScannerBuffer lexerReq = - new ScannerBuffer(new Lexer(new BufferedReader(new FileReader(args[1])),csfReq)); - Parser parseReq = new Parser(lexerReq,csfReq); - ParseNode pnReq = (ParseNode) parseReq.parse().value; + ParseNode pnReq = IoTCompiler.parseFile(args[1]); // Get interface name - String intFace = ParseTreeHandler.getOrigIntface(pnPol); - //System.out.println("IoTCompiler: Original interface: " + intFace); - IoTCompiler comp = new IoTCompiler(intFace, pnPol, pnReq); + String intface = ParseTreeHandler.getOrigIntface(pnPol); + IoTCompiler comp = new IoTCompiler(intface, pnPol, pnReq); // Generate all policy files if just policy file is provided comp.parsePolicyFile(); comp.getMethodsForIntface(); if (args.length == 2) { - comp.generateJavaLocalInterface(); + comp.generateJavaLocalInterface(intface); comp.generateJavaInterfaces(); comp.generateJavaStubClasses(); - comp.generateCplusLocalInterface(); + comp.generateCplusLocalInterface(intface); comp.generateCPlusInterfaces(); comp.generateCPlusStubClasses(); } else { @@ -906,11 +915,11 @@ public class IoTCompiler { } else throw new Error("IoTCompiler: ERROR - please provide after option: " + args[i]); if (args[i].equals("-java")) { - comp.generateJavaLocalInterface(); + comp.generateJavaLocalInterface(intface); comp.generateJavaInterfaces(); comp.generateJavaStubClasses(); } else if (args[i].equals("-cplus")) { - comp.generateCplusLocalInterface(); + comp.generateCplusLocalInterface(intface); comp.generateCPlusInterfaces(); comp.generateCPlusStubClasses(); } else -- 2.34.1