Juan Hernandez has uploaded a new change for review. Change subject: codegen: Use commons-io instead of custom code ......................................................................
codegen: Use commons-io instead of custom code This patch replaces the custom FileUtils class with commons-io, which provides many functionalities that will be used by future patches. Change-Id: I310b20d54e64e814301ff19c8d1f90242bc581f6 Signed-off-by: Juan Hernandez <juan.hernan...@redhat.com> --- M generator/pom.xml M generator/src/main/java/org/ovirt/engine/sdk/codegen/rsdl/RsdlCodegen.java M generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/ExtraMethodParameterTemplate.java M generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/HeaderBuilderParameterTemplate.java M generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/MethodParameterTemplate.java M generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/UrlBuilderParameterTemplate.java M generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/ClassUtils.java D generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/FileUtils.java M generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/StringTemplateWrapper.java M generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/StringUtils.java M generator/src/main/java/org/ovirt/engine/sdk/codegen/xsd/XsdCodegen.java 11 files changed, 94 insertions(+), 350 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-sdk-java refs/changes/91/38191/1 diff --git a/generator/pom.xml b/generator/pom.xml index 7ca4e33..da6f65b 100644 --- a/generator/pom.xml +++ b/generator/pom.xml @@ -38,6 +38,12 @@ <dependencies> <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>2.4</version> + </dependency> + + <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-xjc</artifactId> <version>${jaxb.version}</version> diff --git a/generator/src/main/java/org/ovirt/engine/sdk/codegen/rsdl/RsdlCodegen.java b/generator/src/main/java/org/ovirt/engine/sdk/codegen/rsdl/RsdlCodegen.java index a63fed8..1f759c1 100644 --- a/generator/src/main/java/org/ovirt/engine/sdk/codegen/rsdl/RsdlCodegen.java +++ b/generator/src/main/java/org/ovirt/engine/sdk/codegen/rsdl/RsdlCodegen.java @@ -35,6 +35,7 @@ import com.sun.xml.xsom.XSComplexType; import com.sun.xml.xsom.XSSchemaSet; import com.sun.xml.xsom.parser.XSOMParser; +import org.apache.commons.io.FileUtils; import org.ovirt.engine.sdk.codegen.documentation.DocsGen; import org.ovirt.engine.sdk.codegen.holders.CollectionHolder; import org.ovirt.engine.sdk.codegen.holders.ResourceHolder; @@ -54,7 +55,6 @@ import org.ovirt.engine.sdk.codegen.templates.UpdateMethodTemplate; import org.ovirt.engine.sdk.codegen.templates.VariableTemplate; import org.ovirt.engine.sdk.codegen.utils.ClassUtils; -import org.ovirt.engine.sdk.codegen.utils.FileUtils; import org.ovirt.engine.sdk.codegen.xsd.XsdCodegen; import org.ovirt.engine.sdk.entities.Capabilities; import org.ovirt.engine.sdk.entities.DetailedLink; @@ -291,8 +291,10 @@ public void generate(String distPath) throws IOException, JAXBException { // Remove all the previously generate classes, so that classes corresponding to types that have been // removed from the XML schema will be later removed from the source code repository: - String packagePath = distPath + File.separatorChar + DECORATORS_PACKAGE.replace('.', File.separatorChar); - FileUtils.deleteAllFiles(packagePath); + File packageDir = new File(distPath, DECORATORS_PACKAGE.replace('.', File.separatorChar)); + if (packageDir.exists()) { + FileUtils.cleanDirectory(packageDir); + } String url, rel, requestBodyType, responseBodyType, parent, collectionName, actualReturnType; HttpMethod requestMethod; @@ -974,7 +976,7 @@ * @param distPath * directory to write the files to */ - private void persistContent(String distPath) { + private void persistContent(String distPath) throws IOException { for (CollectionHolder collection : this.collectionsHolder.values()) { ClassUtils.saveClass(distPath, DECORATORS_PACKAGE + "." +collection.getName(), collection.produce()); } diff --git a/generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/ExtraMethodParameterTemplate.java b/generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/ExtraMethodParameterTemplate.java index cce745a..7d2166a 100644 --- a/generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/ExtraMethodParameterTemplate.java +++ b/generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/ExtraMethodParameterTemplate.java @@ -33,7 +33,7 @@ String param = paramTemplate.getTemplate(type, name); // Add the separation from the previous parameter: - StringTemplateWrapper templateWrapper = new StringTemplateWrapper(getTemplate(), true); + StringTemplateWrapper templateWrapper = new StringTemplateWrapper(getTemplate()); templateWrapper.set("param", param); return templateWrapper.toString(); } diff --git a/generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/HeaderBuilderParameterTemplate.java b/generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/HeaderBuilderParameterTemplate.java index a2cdf0e..65f5998 100644 --- a/generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/HeaderBuilderParameterTemplate.java +++ b/generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/HeaderBuilderParameterTemplate.java @@ -38,7 +38,7 @@ public String getTemplate(String realUrlParamName, String headerParamName) { StringTemplateWrapper templateWrapper = - new StringTemplateWrapper(getTemplate(), true); + new StringTemplateWrapper(getTemplate()); templateWrapper.set("realUrlParamName", realUrlParamName); templateWrapper.set("headerParamName", headerParamName); diff --git a/generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/MethodParameterTemplate.java b/generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/MethodParameterTemplate.java index 20f04f0..28cdb63 100644 --- a/generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/MethodParameterTemplate.java +++ b/generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/MethodParameterTemplate.java @@ -28,7 +28,7 @@ } public String getTemplate(String type, String name) { - StringTemplateWrapper templateWrapper = new StringTemplateWrapper(getTemplate(), true); + StringTemplateWrapper templateWrapper = new StringTemplateWrapper(getTemplate()); templateWrapper.set("type", type); templateWrapper.set("name", name); return templateWrapper.toString(); diff --git a/generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/UrlBuilderParameterTemplate.java b/generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/UrlBuilderParameterTemplate.java index 99911bd..c2fd9d4 100644 --- a/generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/UrlBuilderParameterTemplate.java +++ b/generator/src/main/java/org/ovirt/engine/sdk/codegen/templates/UrlBuilderParameterTemplate.java @@ -39,7 +39,7 @@ public String getTemplate(String realUrlParamName, String urlParamName, String urlParamType) { StringTemplateWrapper templateWrapper = - new StringTemplateWrapper(getTemplate(), true); + new StringTemplateWrapper(getTemplate()); templateWrapper.set("realUrlParamName", realUrlParamName); templateWrapper.set("urlParamName", urlParamName); diff --git a/generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/ClassUtils.java b/generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/ClassUtils.java index 57ae57c..c403ae6 100644 --- a/generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/ClassUtils.java +++ b/generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/ClassUtils.java @@ -16,13 +16,12 @@ package org.ovirt.engine.sdk.codegen.utils; +import org.apache.commons.io.FileUtils; + import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.PrintWriter; -import java.util.ArrayList; +import java.io.IOException; +import java.util.Arrays; import java.util.List; -import java.util.Scanner; /** * Utilities for handling class files. @@ -35,8 +34,10 @@ * @param className the fully qualified name of the class * @param classCode the source code of the class */ - public static void saveClass(String distPath, String className, String classCode) { - String classPath = distPath + File.separator + className.replace('.', File.separatorChar) + ".java"; - FileUtils.saveFile(classPath, classCode); + public static void saveClass(String distPath, String className, String classCode) throws IOException { + File classFile = new File(distPath, className.replace('.', File.separatorChar) + ".java"); + List<String> classLines = Arrays.asList(classCode.split("\n")); + classLines = StringUtils.removeTrailingWhitespace(classLines); + FileUtils.writeLines(classFile, classLines); } } diff --git a/generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/FileUtils.java b/generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/FileUtils.java deleted file mode 100644 index a6f2162..0000000 --- a/generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/FileUtils.java +++ /dev/null @@ -1,242 +0,0 @@ -// -// Copyright (c) 2012 Red Hat, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -package org.ovirt.engine.sdk.codegen.utils; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; - -/** - * Provides File/Directory related services - */ -public class FileUtils { - - public static final String LINE_SEPARATOR = "line.separator"; - public static final String ENCODING = "UTF-8"; - public static final String NEW_LINE = System.getProperty(LINE_SEPARATOR); - - /** - * Deletes all files in given directory - * - * @param directory - * directory to clean - * - * @return sucess/failure - */ - public static boolean deleteAllFiles(String directory) { - boolean res = true; - for (File f : list(directory)) { - res = res & f.delete(); - } - return res; - } - - /** - * Deletes given file - * - * @param fileName - * file to delete - * - * @return boolean - */ - public static boolean delete(String fileName) { - return new File(fileName).delete(); - } - - /** - * Deletes given file - * - * @param file - * file to delete - * - * @return boolean - */ - public static boolean delete(File file) { - return file.delete(); - } - - /** - * Renames file - * - * @param file - * File to rename - * @param name - * new file name - * - * @return boolean - */ - public static boolean rename(File file, String name) { - return file.renameTo(new File(name)); - } - - /** - * List all files in given directory - * - * @param directory - * - * @return File[] - */ - public static File[] list(String directory) { - return new File(directory).listFiles(); - } - - /** - * Reads file content - * - * @param path - * file path to read from - * - * @return file content - * - * @throws FileNotFoundException - */ - public static String getFileContent(String path) throws FileNotFoundException { - return getFileContent(path, true, true); - } - - /** - * Reads file content - * - * @param path - * file path to read from - * @param appendNewLine - * append new line to the end of the file - * @param removeTrailingWhitespace - * removes trailing whitespaces - * - * @return file content - * - * @throws FileNotFoundException - */ - public static String getFileContent(String path, boolean appendNewLine, - boolean removeTrailingWhitespace) throws FileNotFoundException { - StringBuilder text = new StringBuilder(); - Scanner scanner = new Scanner(new FileInputStream(path), ENCODING); - - try { - while (scanner.hasNextLine()) { - if (scanner.hasNextLine()) { - text.append( - removeTrailingWhitespaces( - scanner.nextLine(), - removeTrailingWhitespace) + NEW_LINE); - } else { - if (appendNewLine) { - text.append( - removeTrailingWhitespaces( - scanner.nextLine(), - removeTrailingWhitespace) + NEW_LINE); - } else { - text.append( - removeTrailingWhitespaces( - scanner.nextLine(), - removeTrailingWhitespace)); - } - } - } - } finally { - if (scanner != null) { - scanner.close(); - } - } - return text.toString(); - } - - /** - * removes trailing whitespaces - * - * @param string - * the string to process - * - * @return string with no trailing whitespaces - */ - private static String removeTrailingWhitespaces(String string, boolean remove) { - return remove && string != null ? string.replaceAll("\\s+$", "") : string; - } - - /** - * Reads file content - * - * @return file content as list of strings - * - * @throws FileNotFoundException - */ - public static List<String> getFileContentAsList(String path) - throws FileNotFoundException { - return getFileContentAsList(path, true); - } - - /** - * Reads file content - * - * @return file content as list of strings - * @param removeTrailingWhitespace - * removes trailing whitespaces - * - * @throws FileNotFoundException - */ - public static List<String> getFileContentAsList(String path, boolean removeTrailingWhitespace) - throws FileNotFoundException { - List<String> strings = new ArrayList<String>(); - String NL = System.getProperty(LINE_SEPARATOR); - Scanner scanner = new Scanner(new FileInputStream(path), ENCODING); - - try { - while (scanner.hasNextLine()) { - strings.add( - removeTrailingWhitespaces( - scanner.nextLine(), - removeTrailingWhitespace) + NL); - } - } finally { - if (scanner != null) { - scanner.close(); - } - } - return strings; - } - - /** - * Stores file - * - * @param filePath - * file path - * @param content - * file content - */ - public static void saveFile(String filePath, String content) { - PrintWriter out = null; - if (filePath != null && content != null) { - try { - out = new PrintWriter(filePath); - out.println(content); - } catch (FileNotFoundException e) { - // TODO: Log error - e.printStackTrace(); - throw new RuntimeException("File \"" + filePath + "\" write failed."); - } finally { - if (out != null) { - out.close(); - } - } - } - } -} diff --git a/generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/StringTemplateWrapper.java b/generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/StringTemplateWrapper.java index 7128e3f..c3cbcc1 100644 --- a/generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/StringTemplateWrapper.java +++ b/generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/StringTemplateWrapper.java @@ -35,20 +35,6 @@ /** * @param content * template content - * - * @param noTrallingNewLine - * removes trailing new line - */ - public StringTemplateWrapper(String content, boolean noTrailingNewLine) { - this.content = content; - if (noTrailingNewLine) { - this.content = StringUtils.removeTrailingNewLine(this.content); - } - } - - /** - * @param content - * template content * @param keyWrapper * key wrapping string */ diff --git a/generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/StringUtils.java b/generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/StringUtils.java index 816d252..ec11f33 100644 --- a/generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/StringUtils.java +++ b/generator/src/main/java/org/ovirt/engine/sdk/codegen/utils/StringUtils.java @@ -76,21 +76,6 @@ } /** - * Removes trailing NewLine - * - * @param string - * - * @return string with no trailing NewLine - */ - public static String removeTrailingNewLine(String string) { - if (string.endsWith(FileUtils.NEW_LINE)) { - return string.substring(0, - string.length() - FileUtils.NEW_LINE.length()); - } - return string; - } - - /** * Removes leading string * * @param string @@ -263,4 +248,26 @@ } return buffer.toString(); } + + /** + * Remove trailing whitespace from the given lines. The result will be a new list, the one passed as parameter + * won't be modified. + * + * @param lines the lines to be processed + * @return a new list containing the result of removing trailing whitespace from the given lines + */ + public static List<String> removeTrailingWhitespace(List<String> lines) { + if (lines == null) { + return null; + } + List<String> result = new ArrayList<>(lines); + for (int i = 0; i < result.size(); i++) { + String line = result.get(i); + if (line != null) { + line = line.replaceAll("\\s+$", ""); + result.set(i, line); + } + } + return result; + } } diff --git a/generator/src/main/java/org/ovirt/engine/sdk/codegen/xsd/XsdCodegen.java b/generator/src/main/java/org/ovirt/engine/sdk/codegen/xsd/XsdCodegen.java index 9ea3f04..0146611 100644 --- a/generator/src/main/java/org/ovirt/engine/sdk/codegen/xsd/XsdCodegen.java +++ b/generator/src/main/java/org/ovirt/engine/sdk/codegen/xsd/XsdCodegen.java @@ -17,14 +17,15 @@ package org.ovirt.engine.sdk.codegen.xsd; import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; +import org.apache.commons.io.FileUtils; import org.ovirt.engine.sdk.codegen.templates.CopyrightTemplate; -import org.ovirt.engine.sdk.codegen.utils.FileUtils; +import org.ovirt.engine.sdk.codegen.utils.StringUtils; /** * Provides XSD schema related services @@ -32,7 +33,7 @@ public class XsdCodegen { private static final String ENTITIES_PACKAGE = "org.ovirt.engine.sdk.entities"; - private static final String copyrightTemplate = new CopyrightTemplate().getTemplate(); + private static final List<String> copyrightLines = Arrays.asList(new CopyrightTemplate().getTemplate().split("\n")); /** * The location of the XSD file. @@ -57,8 +58,10 @@ public void generate(String distPath) throws IOException { // Remove all the previously generate classes, so that classes corresponding to types that have been // removed from the XML schema will be later removed from the source code repository: - String packagePath = distPath + File.separatorChar + ENTITIES_PACKAGE.replace('.', File.separatorChar); - FileUtils.deleteAllFiles(packagePath); + File packageDir = new File(distPath, ENTITIES_PACKAGE.replace('.', File.separatorChar)); + if (packageDir.exists()) { + FileUtils.cleanDirectory(packageDir); + } // Run the XJC compiler to generate all the classes: System.setProperty("javax.xml.accessExternalSchema", "all"); @@ -89,69 +92,55 @@ * @param distPath the top level directory of the source code * @param accessors a list of possible getter types */ - public static void modifyGetters(String distPath, Map<String, List<String>> accessors) { - StringBuffer finalContent, tempContent = new StringBuffer(); - List<String> accessorsToCheck; - String templateOriginal = " public $accessor$ get$accessor$() {" + "\n"; - String templateReplace = " public Object get$accessor$() {" + "\n"; + public static void modifyGetters(String distPath, Map<String, List<String>> accessors) throws IOException { + String templateOriginal = " public $accessor$ get$accessor$() {"; + String templateReplace = " public Object get$accessor$() {"; String placeHolder = "$accessor$"; boolean isInAccessor = false; // change shadowed getters - String entitiesPath = distPath + File.separator + ENTITIES_PACKAGE.replace('.', File.separatorChar); - for (File file : FileUtils.list(entitiesPath)) { - finalContent = new StringBuffer(); - finalContent.append(copyrightTemplate); - tempContent = new StringBuffer(); - accessorsToCheck = new ArrayList<String>(); + File entitiesDir = new File(distPath, ENTITIES_PACKAGE.replace('.', File.separatorChar)); + for (File file : entitiesDir.listFiles()) { + List<String> finalLines = new ArrayList<>(copyrightLines); + List<String> tempLines = new ArrayList<>(); - List<String> accessorsContent = accessors.get(file.getName().replace(".java", "")); - if (accessorsContent != null) { - accessorsToCheck.addAll(accessorsContent); - } else { + List<String> accessorsToCheck = accessors.get(file.getName().replace(".java", "")); + if (accessorsToCheck == null) { injectCopyrightHeader(file); continue; } - try { - List<String> strings = FileUtils.getFileContentAsList(file.getAbsolutePath()); - for (int i = 1; i < strings.size(); i++) { - String str = strings.get(i); - if (str.equals("\n")) { - isInAccessor = false; - finalContent.append("\n"); - finalContent.append(tempContent.toString()); - tempContent = new StringBuffer(); - } else if (str.equals("}\n")) { - isInAccessor = false; - finalContent.append("\n}"); - } else { + for (String line : FileUtils.readLines(file)) { + if (line.isEmpty()) { + isInAccessor = false; + finalLines.addAll(tempLines); + finalLines.add(line); + tempLines.clear(); + } else if (line.equals("}")) { + isInAccessor = false; + finalLines.add("}"); + } else { + if (!isInAccessor) { + for (String accessor : accessorsToCheck) { + if (line.toLowerCase().equals(templateOriginal.replace(placeHolder, accessor) + .toLowerCase())) { + isInAccessor = true; + tempLines.add(templateReplace.replace(placeHolder, accessor)); + break; + } + } if (!isInAccessor) { - for (String accessor : accessorsToCheck) { - if (str.toLowerCase().equals(templateOriginal.replace(placeHolder, accessor) - .toLowerCase())) { - isInAccessor = true; - tempContent.append(templateReplace.replace(placeHolder, accessor)); - break; - } - } - if (!isInAccessor) { - tempContent.append(str); - } else { - isInAccessor = false; - } + tempLines.add(line); + } else { + isInAccessor = false; } } } - - } catch (FileNotFoundException e) { - // TODO: Log error - e.printStackTrace(); - continue; } + // save new content - FileUtils.saveFile(file.getAbsolutePath(), - finalContent.toString()); + finalLines = StringUtils.removeTrailingWhitespace(finalLines); + FileUtils.writeLines(file, finalLines); } } @@ -161,15 +150,10 @@ * @param file * file to inject to */ - private static void injectCopyrightHeader(File file) { - StringBuffer content = new StringBuffer(); - try { - content.append(copyrightTemplate); - content.append(FileUtils.getFileContent(file.getAbsolutePath())); - FileUtils.saveFile(file.getAbsolutePath(), content.toString()); - } catch (FileNotFoundException e) { - // TODO: Log error - e.printStackTrace(); - } + private static void injectCopyrightHeader(File file) throws IOException { + List<String> lines = new ArrayList<>(copyrightLines); + lines.addAll(FileUtils.readLines(file)); + lines = StringUtils.removeTrailingWhitespace(lines); + FileUtils.writeLines(file, lines); } } -- To view, visit https://gerrit.ovirt.org/38191 To unsubscribe, visit https://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I310b20d54e64e814301ff19c8d1f90242bc581f6 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine-sdk-java Gerrit-Branch: master Gerrit-Owner: Juan Hernandez <juan.hernan...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches