X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fconfig%2Fyang-test-plugin%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyang%2Ftest%2Fplugin%2FProcessSources.java;h=f2a56f2b1bfbe2a1e1066a90f3ef16ba6afef660;hb=1745f92957146128e8a4a111adb7ed830f737e0a;hp=dbb9ddb363fb168ed820d06c30c1444f4db3b719;hpb=3cc482ed8acee2c0d3d6ff5e65288fef75ee1ba4;p=controller.git diff --git a/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/ProcessSources.java b/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/ProcessSources.java index dbb9ddb363..f2a56f2b1b 100644 --- a/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/ProcessSources.java +++ b/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/ProcessSources.java @@ -7,18 +7,14 @@ */ package org.opendaylight.controller.config.yang.test.plugin; +import org.apache.commons.io.FileUtils; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; -import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; +import java.util.regex.Pattern; /** * Add implementation code from stub.txt @@ -45,55 +41,25 @@ public class ProcessSources extends AbstractMojo{ if (!sourceDirectory.exists()) { super.getLog().error("Source directory does not exists " + sourceDirectory.getPath()); } - String header = ""; - try { - header = Util.loadHeader(); - } catch (IOException e) { - super.getLog().error("Header.txt not found."); - } + File[] sourceFiles = sourceDirectory.listFiles(); for (File sourceFile: sourceFiles) { if(sourceFile.getName().endsWith("Module.java") || sourceFile.getName().endsWith("ModuleFactory.java")) { File stubFile = new File(sourceFile.getPath().replace(".java", "Stub.txt")); - String stubLines = null; - try { - if (stubFile.exists()) { - stubLines = Util.loadStubFile(stubFile.getPath()); - } - - InputStream javaIn = new FileInputStream(sourceFile.getPath()); - BufferedReader javaBuf = new BufferedReader(new InputStreamReader(javaIn)); - StringBuffer output = new StringBuffer(); - String line = javaBuf.readLine(); - boolean writeLine = false; - while ((line = javaBuf.readLine()) != null) { - if(!writeLine && line.contains("*/")) { - line = header; - writeLine = true; - } else { - if (line.contains("TODO")) { - writeLine = false; - } else { - if (stubLines != null && line.contains("throw new")) { - line = stubLines.toString(); - writeLine = true; - } - } - } - if(writeLine) { - output.append(line).append(System.lineSeparator()); - } + if (stubFile.exists()) { + try { + rewrite(sourceFile, FileUtils.readFileToString(stubFile)); + } catch (IOException e) { + getLog().error("Error while reading/writing to files.", e); } - javaBuf.close(); - - OutputStream javaOut = new FileOutputStream(sourceFile.getPath()); - javaOut.write(output.toString().getBytes()); - javaOut.close(); - } catch (IOException e) { - getLog().error("Error while reading/writing to files.", e); } - } } } + + private static void rewrite(File sourceFile, String replaceTODOWith) throws IOException { + String source = FileUtils.readFileToString(sourceFile); + String target = Pattern.compile("^.*TODO.*\n.*throw new java.lang.UnsupportedOperationException.*$", Pattern.MULTILINE).matcher(source).replaceFirst(replaceTODOWith); + FileUtils.write(sourceFile, target); + } }