2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.config.yang.test.plugin;
10 import org.apache.maven.plugin.AbstractMojo;
11 import org.apache.maven.plugin.MojoExecutionException;
12 import org.apache.maven.plugin.MojoFailureException;
14 import java.io.BufferedReader;
16 import java.io.FileInputStream;
17 import java.io.FileOutputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.InputStreamReader;
21 import java.io.OutputStream;
24 * Add implementation code from stub.txt
26 * @goal process-sources
28 * @phase process-sources
31 public class ProcessSources extends AbstractMojo{
33 * @parameter expression="${project.build.sourceDirectory}"
37 private File directory;
40 public void execute() throws MojoExecutionException, MojoFailureException {
41 if (directory == null || !directory.exists()) {
42 super.getLog().error("Directory does not exists.");
44 File sourceDirectory = new File(directory.getPath() + Util.replaceDots(".org.opendaylight.controller.config.yang.test.impl"));
45 if (!sourceDirectory.exists()) {
46 super.getLog().error("Source directory does not exists " + sourceDirectory.getPath());
50 header = Util.loadHeader();
51 } catch (IOException e) {
52 super.getLog().error("Header.txt not found.");
54 File[] sourceFiles = sourceDirectory.listFiles();
55 for (File sourceFile: sourceFiles) {
56 if(sourceFile.getName().endsWith("Module.java") || sourceFile.getName().endsWith("ModuleFactory.java")) {
57 File stubFile = new File(sourceFile.getPath().replace(".java", "Stub.txt"));
58 String stubLines = null;
60 if (stubFile.exists()) {
61 stubLines = Util.loadStubFile(stubFile.getPath());
64 InputStream javaIn = new FileInputStream(sourceFile.getPath());
65 BufferedReader javaBuf = new BufferedReader(new InputStreamReader(javaIn));
66 StringBuffer output = new StringBuffer();
67 String line = javaBuf.readLine();
68 boolean writeLine = false;
69 while ((line = javaBuf.readLine()) != null) {
70 if(!writeLine && line.contains("*/")) {
74 if (line.contains("TODO")) {
77 if (stubLines != null && line.contains("throw new")) {
78 line = stubLines.toString();
84 output.append(line).append(System.lineSeparator());
89 OutputStream javaOut = new FileOutputStream(sourceFile.getPath());
90 javaOut.write(output.toString().getBytes());
92 } catch (IOException e) {
93 getLog().error("Error while reading/writing to files.", e);