From 33376e45baccffcbead64d9fb841b96b1219f032 Mon Sep 17 00:00:00 2001 From: Kirill Liubun Date: Wed, 28 Sep 2016 15:19:08 +0300 Subject: [PATCH] Fixed Compiler classes --- .../org/kaaproject/kaa/avro/avrogen/Main.java | 7 +- .../kaa/avro/avrogen/compiler/CCompiler.java | 9 ++- .../kaa/avro/avrogen/compiler/Compiler.java | 73 ++++++++++++++----- 3 files changed, 65 insertions(+), 24 deletions(-) diff --git a/avrogen/src/main/java/org/kaaproject/kaa/avro/avrogen/Main.java b/avrogen/src/main/java/org/kaaproject/kaa/avro/avrogen/Main.java index f4e476b137..4f32a63c3d 100644 --- a/avrogen/src/main/java/org/kaaproject/kaa/avro/avrogen/Main.java +++ b/avrogen/src/main/java/org/kaaproject/kaa/avro/avrogen/Main.java @@ -17,6 +17,7 @@ package org.kaaproject.kaa.avro.avrogen; +import org.kaaproject.kaa.avro.avrogen.compiler.Compiler; import org.kaaproject.kaa.avro.avrogen.compiler.ObjectiveCCompiler; public class Main { @@ -27,10 +28,10 @@ public static void main(String[] args) { + "Need {FULL_PATH_TO_SCHEMA} {OUTPUT_PATH} {SOURCE_NAME}"); } - org.kaaproject.kaa.avro.avrogen.compiler.Compiler compiler = new ObjectiveCCompiler(args[0], args[1], args[2]); + Compiler compiler = new ObjectiveCCompiler(args[0], args[1], args[2]); compiler.generate(); - } catch (Exception e) { - System.err.println("Compilation failure: " + e.toString()); + } catch (Exception ex) { + System.err.println("Compilation failure: " + ex.toString()); } } } diff --git a/avrogen/src/main/java/org/kaaproject/kaa/avro/avrogen/compiler/CCompiler.java b/avrogen/src/main/java/org/kaaproject/kaa/avro/avrogen/compiler/CCompiler.java index f2e088d114..248d42cb1d 100644 --- a/avrogen/src/main/java/org/kaaproject/kaa/avro/avrogen/compiler/CCompiler.java +++ b/avrogen/src/main/java/org/kaaproject/kaa/avro/avrogen/compiler/CCompiler.java @@ -30,15 +30,18 @@ public class CCompiler extends Compiler { - public CCompiler(String schemaPath, String outputPath, String sourceName) throws KaaGeneratorException { + public CCompiler(String schemaPath, String outputPath, String sourceName) + throws KaaGeneratorException { super(schemaPath, outputPath, sourceName); } - public CCompiler(Schema schema, String sourceName, OutputStream hdrS, OutputStream srcS) throws KaaGeneratorException { + public CCompiler(Schema schema, String sourceName, OutputStream hdrS, OutputStream srcS) + throws KaaGeneratorException { super(schema, sourceName, hdrS, srcS); } - public CCompiler(List schemas, String sourceName, OutputStream hdrS, OutputStream srcS) throws KaaGeneratorException { + public CCompiler(List schemas, String sourceName, OutputStream hdrS, OutputStream srcS) + throws KaaGeneratorException { super(schemas, sourceName, hdrS, srcS); } diff --git a/avrogen/src/main/java/org/kaaproject/kaa/avro/avrogen/compiler/Compiler.java b/avrogen/src/main/java/org/kaaproject/kaa/avro/avrogen/compiler/Compiler.java index bb779e5530..f87d717bab 100644 --- a/avrogen/src/main/java/org/kaaproject/kaa/avro/avrogen/compiler/Compiler.java +++ b/avrogen/src/main/java/org/kaaproject/kaa/avro/avrogen/compiler/Compiler.java @@ -46,6 +46,9 @@ import java.util.Map; import java.util.Set; +/** + * The type Compiler. + */ public abstract class Compiler { private static final String DIRECTION_PROP = "direction"; @@ -71,7 +74,17 @@ private Compiler(String sourceName) throws KaaGeneratorException { initVelocityEngine(); } - public Compiler(Schema schema, String sourceName, OutputStream hdrS, OutputStream srcS) throws KaaGeneratorException { + + /** + * Instantiates a new Compiler. + * + * @param schema the schema that used to generate source files + * @param sourceName the name of source file + * @param hdrS the stream of header file + * @param srcS the stream of source file + */ + public Compiler(Schema schema, String sourceName, OutputStream hdrS, OutputStream srcS) + throws KaaGeneratorException { this(sourceName); this.schemas.add(schema); this.headerWriter = new PrintWriter(hdrS); @@ -79,7 +92,17 @@ public Compiler(Schema schema, String sourceName, OutputStream hdrS, OutputStrea prepareTemplates(false); } - public Compiler(List schemas, String sourceName, OutputStream hdrS, OutputStream srcS) throws KaaGeneratorException { + + /** + * Instantiates a new Compiler. + * + * @param schemas list of schemas that used to generate source files + * @param sourceName name of source files + * @param hdrS stream of the header file + * @param srcS stream of the source file + */ + public Compiler(List schemas, String sourceName, OutputStream hdrS, OutputStream srcS) + throws KaaGeneratorException { this(sourceName); this.schemas.addAll(schemas); this.headerWriter = new PrintWriter(hdrS); @@ -87,13 +110,23 @@ public Compiler(List schemas, String sourceName, OutputStream hdrS, Outp prepareTemplates(false); } - public Compiler(List schemas, String sourceName, OutputStream hdrS, OutputStream srcS, Set generatedSchemas) throws KaaGeneratorException { + + public Compiler(List schemas, String sourceName, OutputStream hdrS, OutputStream srcS, + Set generatedSchemas) throws KaaGeneratorException { this(schemas, sourceName, hdrS, srcS); this.generatedSchemas = new HashSet<>(generatedSchemas); } - public Compiler(String schemaPath, String outputPath, String sourceName) throws KaaGeneratorException { + /** + * Instantiates a new Compiler. + * + * @param schemaPath path to file that contains schema + * @param outputPath destination path for generated sources + * @param sourceName name of source files + */ + public Compiler(String schemaPath, String outputPath, String sourceName) + throws KaaGeneratorException { this(sourceName); try { this.schemas.add(new Schema.Parser().parse(new File(schemaPath))); @@ -106,16 +139,16 @@ public Compiler(String schemaPath, String outputPath, String sourceName) throws String headerPath = outputPath + File.separator + generatedSourceName + ".h"; String sourcePath = outputPath + File.separator + generatedSourceName + getSourceExtension(); - Files.move(new File(headerTemplateGen()).toPath() - , new File(headerPath).toPath(), StandardCopyOption.REPLACE_EXISTING); - Files.move(new File(sourceTemplateGen()).toPath() - , new File(sourcePath).toPath(), StandardCopyOption.REPLACE_EXISTING); + Files.move(new File(headerTemplateGen()).toPath(), + new File(headerPath).toPath(), StandardCopyOption.REPLACE_EXISTING); + Files.move(new File(sourceTemplateGen()).toPath(), + new File(sourcePath).toPath(), StandardCopyOption.REPLACE_EXISTING); this.headerWriter = new PrintWriter(new BufferedWriter(new FileWriter(headerPath, true))); this.sourceWriter = new PrintWriter(new BufferedWriter(new FileWriter(sourcePath, true))); - } catch (Exception e) { - LOG.error("Failed to create ouput path: ", e); - throw new KaaGeneratorException("Failed to create output path: " + e.toString()); + } catch (Exception ex) { + LOG.error("Failed to create ouput path: ", ex); + throw new KaaGeneratorException("Failed to create output path: " + ex.toString()); } } @@ -129,7 +162,8 @@ private void initVelocityEngine() { "org.apache.velocity.runtime.resource.loader.FileResourceLoader"); engine.addProperty("file.resource.loader.path", "/, ."); engine.setProperty("runtime.references.strict", true); - engine.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem"); + engine.setProperty("runtime.log.logsystem.class", + "org.apache.velocity.runtime.log.NullLogSystem"); } protected abstract String headerTemplateGen(); @@ -158,9 +192,9 @@ private void prepareTemplates(boolean toFile) throws KaaGeneratorException { } else { writeToStream(hdrWriter, srcWriter); } - } catch (Exception e) { - LOG.error("Failed to prepare source templates: ", e); - throw new KaaGeneratorException("Failed to prepare source templates: " + e.toString()); + } catch (Exception ex) { + LOG.error("Failed to prepare source templates: ", ex); + throw new KaaGeneratorException("Failed to prepare source templates: " + ex.toString()); } } @@ -180,6 +214,9 @@ private void writeToFile(StringWriter hdrWriter, StringWriter srcWriter) throws } + /** + * Generate source files using the schemas and write them to specified source file. + */ public Set generate() throws KaaGeneratorException { try { LOG.debug("Processing schemas: [" + join(schemas, ", ") + "]"); @@ -199,9 +236,9 @@ public Set generate() throws KaaGeneratorException { LOG.debug("Sources were successfully generated"); return schemaGenerationQueue.keySet(); - } catch (Exception e) { - LOG.error("Failed to generate C sources: ", e); - throw new KaaGeneratorException("Failed to generate sources: " + e.toString()); + } catch (Exception ex) { + LOG.error("Failed to generate C sources: ", ex); + throw new KaaGeneratorException("Failed to generate sources: " + ex.toString()); } finally { headerWriter.close(); sourceWriter.close();