X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fsal%2Fyang-prototype%2Fcode-generator%2Fbinding-java-api-generator%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fjava%2Fapi%2Fgenerator%2FGeneratorJavaFile.java;h=32b3fa03eb0814e8a3902a63e7c4e370652818da;hp=f2388fef19a04c044eaa8f541bd120ca9193f95c;hb=4221068644c7e8d08880b4d54e2a099a646796b9;hpb=2f3199b3a037b532cffe917451205e948be1697b diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorJavaFile.java b/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorJavaFile.java index f2388fef19..32b3fa03eb 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorJavaFile.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorJavaFile.java @@ -1,153 +1,153 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.controller.sal.java.api.generator; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.Writer; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import org.opendaylight.controller.sal.binding.model.api.CodeGenerator; -import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject; -import org.opendaylight.controller.sal.binding.model.api.GeneratedType; - -public class GeneratorJavaFile { - - private final CodeGenerator interfaceGenerator; - private final CompositeKeyGenerator classGenerator; - private final Set types; - private final Set genTransferObjects; - - public GeneratorJavaFile(final CodeGenerator codeGenerator, - final Set types) { - this.interfaceGenerator = codeGenerator; - this.types = types; - this.genTransferObjects = new HashSet(); - classGenerator = new CompositeKeyGenerator(); - } - - public GeneratorJavaFile(final Set types, - final Set genTransferObjects) { - this.interfaceGenerator = new InterfaceGenerator(); - this.classGenerator = new CompositeKeyGenerator(); - this.types = types; - this.genTransferObjects = genTransferObjects; - } - - public boolean generateToFile() { - return generateToFile(null); - } - - public boolean generateToFile(String path) { - try { - for (GeneratedType type : types) { - String parentPath = generateParentPath(path, - type.getPackageName()); - - File file = new File(parentPath, type.getName() + ".java"); - File parent = file.getParentFile(); - if (!parent.exists()) { - parent.mkdirs(); - } - - if (!file.exists()) { - FileWriter fw = null; - BufferedWriter bw = null; - - file.createNewFile(); - fw = new FileWriter(file); - bw = new BufferedWriter(fw); - Writer writer = interfaceGenerator.generate(type); - bw.write(writer.toString()); - - if (bw != null) { - try { - bw.close(); - } catch (IOException e) { - // TODO: log? - } - } - } - } - for (GeneratedTransferObject transferObject : genTransferObjects) { - String parentPath = generateParentPath(path, - transferObject.getPackageName()); - - File file = new File(parentPath, transferObject.getName() + ".java"); - File parent = file.getParentFile(); - if (!parent.exists()) { - parent.mkdirs(); - } - - if (!file.exists()) { - FileWriter fw = null; - BufferedWriter bw = null; - - file.createNewFile(); - fw = new FileWriter(file); - bw = new BufferedWriter(fw); - Writer writer = classGenerator.generate(transferObject); - bw.write(writer.toString()); - - if (bw != null) { - try { - bw.close(); - } catch (IOException e) { - // TODO: log? - } - } - } - } - return true; - } catch (IOException e) { - // TODO: log? - return false; - } - } - - private String generateParentPath(String path, String pkg) { - List dirs = new ArrayList(); - String pkgPath = ""; - if (pkg != null) { - if (pkg.length() > 0) { - if (pkg.contains(".")) { - String[] split = pkg.split("\\."); - for (String dir : split) { - dirs.add(dir); - } - } else { - dirs.add(pkg); - } - for (int i = 0; i < dirs.size(); i++) { - if (i == 0) { - pkgPath += dirs.get(i); - } else { - pkgPath += File.separator + dirs.get(i); - } - } - } - } - String fullPath = ""; - if (path != null) { - if (path.endsWith(File.separator)) { - fullPath = path + pkgPath; - } else { - fullPath = path + File.separator + pkgPath; - } - } else { - fullPath = pkgPath; - } - return fullPath; - } - -} +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.sal.java.api.generator; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.opendaylight.controller.sal.binding.model.api.CodeGenerator; +import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject; +import org.opendaylight.controller.sal.binding.model.api.GeneratedType; + +public class GeneratorJavaFile { + + private final CodeGenerator interfaceGenerator; + private final ClassCodeGenerator classGenerator; + private final Set types; + private final Set genTransferObjects; + + public GeneratorJavaFile(final CodeGenerator codeGenerator, + final Set types) { + this.interfaceGenerator = codeGenerator; + this.types = types; + this.genTransferObjects = new HashSet(); + classGenerator = new ClassCodeGenerator(); + } + + public GeneratorJavaFile(final Set types, + final Set genTransferObjects) { + this.interfaceGenerator = new InterfaceGenerator(); + this.classGenerator = new ClassCodeGenerator(); + this.types = types; + this.genTransferObjects = genTransferObjects; + } + + public boolean generateToFile() { + return generateToFile(null); + } + + public boolean generateToFile(String path) { + try { + for (GeneratedType type : types) { + String parentPath = generateParentPath(path, + type.getPackageName()); + + File file = new File(parentPath, type.getName() + ".java"); + File parent = file.getParentFile(); + if (!parent.exists()) { + parent.mkdirs(); + } + + if (!file.exists()) { + FileWriter fw = null; + BufferedWriter bw = null; + + file.createNewFile(); + fw = new FileWriter(file); + bw = new BufferedWriter(fw); + Writer writer = interfaceGenerator.generate(type); + bw.write(writer.toString()); + + if (bw != null) { + try { + bw.close(); + } catch (IOException e) { + // TODO: log? + } + } + } + } + for (GeneratedTransferObject transferObject : genTransferObjects) { + String parentPath = generateParentPath(path, + transferObject.getPackageName()); + + File file = new File(parentPath, transferObject.getName() + ".java"); + File parent = file.getParentFile(); + if (!parent.exists()) { + parent.mkdirs(); + } + + if (!file.exists()) { + FileWriter fw = null; + BufferedWriter bw = null; + + file.createNewFile(); + fw = new FileWriter(file); + bw = new BufferedWriter(fw); + Writer writer = classGenerator.generate(transferObject); + bw.write(writer.toString()); + + if (bw != null) { + try { + bw.close(); + } catch (IOException e) { + // TODO: log? + } + } + } + } + return true; + } catch (IOException e) { + // TODO: log? + return false; + } + } + + private String generateParentPath(String path, String pkg) { + List dirs = new ArrayList(); + String pkgPath = ""; + if (pkg != null) { + if (pkg.length() > 0) { + if (pkg.contains(".")) { + String[] split = pkg.split("\\."); + for (String dir : split) { + dirs.add(dir); + } + } else { + dirs.add(pkg); + } + for (int i = 0; i < dirs.size(); i++) { + if (i == 0) { + pkgPath += dirs.get(i); + } else { + pkgPath += File.separator + dirs.get(i); + } + } + } + } + String fullPath = ""; + if (path != null) { + if (path.endsWith(File.separator)) { + fullPath = path + pkgPath; + } else { + fullPath = path + File.separator + pkgPath; + } + } else { + fullPath = pkgPath; + } + return fullPath; + } + +}