Added yang-test-plugin maven plugin.
[controller.git] / opendaylight / config / yang-test-plugin / src / main / java / org / opendaylight / controller / config / yang / test / plugin / ProcessSources.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.yang.test.plugin;
9
10 import org.apache.maven.plugin.AbstractMojo;
11 import org.apache.maven.plugin.MojoExecutionException;
12 import org.apache.maven.plugin.MojoFailureException;
13
14 import java.io.BufferedReader;
15 import java.io.File;
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;
22
23 /**
24  * Add implementation code from stub.txt
25  *
26  * @goal process-sources
27  *
28  * @phase process-sources
29  *
30  */
31 public class ProcessSources extends AbstractMojo{
32     /**
33      * @parameter expression="${project.build.sourceDirectory}"
34      * @readOnly
35      * @required
36      */
37     private File directory;
38
39     @Override
40     public void execute() throws MojoExecutionException, MojoFailureException {
41         if (directory == null || !directory.exists()) {
42             super.getLog().error("Directory does not exists.");
43         }
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());
47         }
48         String header = "";
49         try {
50             header = Util.loadHeader();
51         } catch (IOException e) {
52            super.getLog().error("Header.txt not found.");
53         }
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;
59                 try {
60                     if (stubFile.exists()) {
61                         stubLines = Util.loadStubFile(stubFile.getPath());
62                     }
63
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("*/")) {
71                             line = header;
72                             writeLine = true;
73                         } else {
74                             if (line.contains("TODO")) {
75                                 writeLine = false;
76                             } else {
77                                 if (stubLines != null && line.contains("throw new")) {
78                                     line = stubLines.toString();
79                                     writeLine = true;
80                                 }
81                             }
82                         }
83                         if(writeLine) {
84                             output.append(line).append(System.lineSeparator());
85                         }
86                     }
87                     javaBuf.close();
88
89                     OutputStream javaOut = new FileOutputStream(sourceFile.getPath());
90                     javaOut.write(output.toString().getBytes());
91                     javaOut.close();
92                 } catch (IOException e) {
93                     getLog().error("Error while reading/writing to files.", e);
94                 }
95
96             }
97         }
98     }
99 }