Skip to content

Commit

Permalink
Fixed Compiler classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill380 committed Sep 28, 2016
1 parent ba30b2a commit 33376e4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 24 deletions.
Expand Up @@ -17,6 +17,7 @@
package org.kaaproject.kaa.avro.avrogen; package org.kaaproject.kaa.avro.avrogen;




import org.kaaproject.kaa.avro.avrogen.compiler.Compiler;
import org.kaaproject.kaa.avro.avrogen.compiler.ObjectiveCCompiler; import org.kaaproject.kaa.avro.avrogen.compiler.ObjectiveCCompiler;


public class Main { public class Main {
Expand All @@ -27,10 +28,10 @@ public static void main(String[] args) {
+ "Need {FULL_PATH_TO_SCHEMA} {OUTPUT_PATH} {SOURCE_NAME}"); + "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(); compiler.generate();
} catch (Exception e) { } catch (Exception ex) {
System.err.println("Compilation failure: " + e.toString()); System.err.println("Compilation failure: " + ex.toString());
} }
} }
} }
Expand Up @@ -30,15 +30,18 @@


public class CCompiler extends Compiler { 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); 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); super(schema, sourceName, hdrS, srcS);
} }


public CCompiler(List<Schema> schemas, String sourceName, OutputStream hdrS, OutputStream srcS) throws KaaGeneratorException { public CCompiler(List<Schema> schemas, String sourceName, OutputStream hdrS, OutputStream srcS)
throws KaaGeneratorException {
super(schemas, sourceName, hdrS, srcS); super(schemas, sourceName, hdrS, srcS);
} }


Expand Down
Expand Up @@ -46,6 +46,9 @@
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;


/**
* The type Compiler.
*/
public abstract class Compiler { public abstract class Compiler {
private static final String DIRECTION_PROP = "direction"; private static final String DIRECTION_PROP = "direction";


Expand All @@ -71,29 +74,59 @@ private Compiler(String sourceName) throws KaaGeneratorException {
initVelocityEngine(); 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(sourceName);
this.schemas.add(schema); this.schemas.add(schema);
this.headerWriter = new PrintWriter(hdrS); this.headerWriter = new PrintWriter(hdrS);
this.sourceWriter = new PrintWriter(srcS); this.sourceWriter = new PrintWriter(srcS);
prepareTemplates(false); prepareTemplates(false);
} }


public Compiler(List<Schema> 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<Schema> schemas, String sourceName, OutputStream hdrS, OutputStream srcS)
throws KaaGeneratorException {
this(sourceName); this(sourceName);
this.schemas.addAll(schemas); this.schemas.addAll(schemas);
this.headerWriter = new PrintWriter(hdrS); this.headerWriter = new PrintWriter(hdrS);
this.sourceWriter = new PrintWriter(srcS); this.sourceWriter = new PrintWriter(srcS);
prepareTemplates(false); prepareTemplates(false);
} }


public Compiler(List<Schema> schemas, String sourceName, OutputStream hdrS, OutputStream srcS, Set<Schema> generatedSchemas) throws KaaGeneratorException {
public Compiler(List<Schema> schemas, String sourceName, OutputStream hdrS, OutputStream srcS,
Set<Schema> generatedSchemas) throws KaaGeneratorException {
this(schemas, sourceName, hdrS, srcS); this(schemas, sourceName, hdrS, srcS);
this.generatedSchemas = new HashSet<>(generatedSchemas); 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); this(sourceName);
try { try {
this.schemas.add(new Schema.Parser().parse(new File(schemaPath))); this.schemas.add(new Schema.Parser().parse(new File(schemaPath)));
Expand All @@ -106,16 +139,16 @@ public Compiler(String schemaPath, String outputPath, String sourceName) throws
String headerPath = outputPath + File.separator + generatedSourceName + ".h"; String headerPath = outputPath + File.separator + generatedSourceName + ".h";
String sourcePath = outputPath + File.separator + generatedSourceName + getSourceExtension(); String sourcePath = outputPath + File.separator + generatedSourceName + getSourceExtension();


Files.move(new File(headerTemplateGen()).toPath() Files.move(new File(headerTemplateGen()).toPath(),
, new File(headerPath).toPath(), StandardCopyOption.REPLACE_EXISTING); new File(headerPath).toPath(), StandardCopyOption.REPLACE_EXISTING);
Files.move(new File(sourceTemplateGen()).toPath() Files.move(new File(sourceTemplateGen()).toPath(),
, new File(sourcePath).toPath(), StandardCopyOption.REPLACE_EXISTING); new File(sourcePath).toPath(), StandardCopyOption.REPLACE_EXISTING);


this.headerWriter = new PrintWriter(new BufferedWriter(new FileWriter(headerPath, true))); this.headerWriter = new PrintWriter(new BufferedWriter(new FileWriter(headerPath, true)));
this.sourceWriter = new PrintWriter(new BufferedWriter(new FileWriter(sourcePath, true))); this.sourceWriter = new PrintWriter(new BufferedWriter(new FileWriter(sourcePath, true)));
} catch (Exception e) { } catch (Exception ex) {
LOG.error("Failed to create ouput path: ", e); LOG.error("Failed to create ouput path: ", ex);
throw new KaaGeneratorException("Failed to create output path: " + e.toString()); throw new KaaGeneratorException("Failed to create output path: " + ex.toString());
} }
} }


Expand All @@ -129,7 +162,8 @@ private void initVelocityEngine() {
"org.apache.velocity.runtime.resource.loader.FileResourceLoader"); "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
engine.addProperty("file.resource.loader.path", "/, ."); engine.addProperty("file.resource.loader.path", "/, .");
engine.setProperty("runtime.references.strict", true); 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(); protected abstract String headerTemplateGen();
Expand Down Expand Up @@ -158,9 +192,9 @@ private void prepareTemplates(boolean toFile) throws KaaGeneratorException {
} else { } else {
writeToStream(hdrWriter, srcWriter); writeToStream(hdrWriter, srcWriter);
} }
} catch (Exception e) { } catch (Exception ex) {
LOG.error("Failed to prepare source templates: ", e); LOG.error("Failed to prepare source templates: ", ex);
throw new KaaGeneratorException("Failed to prepare source templates: " + e.toString()); throw new KaaGeneratorException("Failed to prepare source templates: " + ex.toString());
} }
} }


Expand All @@ -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<Schema> generate() throws KaaGeneratorException { public Set<Schema> generate() throws KaaGeneratorException {
try { try {
LOG.debug("Processing schemas: [" + join(schemas, ", ") + "]"); LOG.debug("Processing schemas: [" + join(schemas, ", ") + "]");
Expand All @@ -199,9 +236,9 @@ public Set<Schema> generate() throws KaaGeneratorException {


LOG.debug("Sources were successfully generated"); LOG.debug("Sources were successfully generated");
return schemaGenerationQueue.keySet(); return schemaGenerationQueue.keySet();
} catch (Exception e) { } catch (Exception ex) {
LOG.error("Failed to generate C sources: ", e); LOG.error("Failed to generate C sources: ", ex);
throw new KaaGeneratorException("Failed to generate sources: " + e.toString()); throw new KaaGeneratorException("Failed to generate sources: " + ex.toString());
} finally { } finally {
headerWriter.close(); headerWriter.close();
sourceWriter.close(); sourceWriter.close();
Expand Down

0 comments on commit 33376e4

Please sign in to comment.