Merge "BUG-509: add missing copyright headers"
[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.commons.io.FileUtils;
11 import org.apache.maven.plugin.AbstractMojo;
12 import org.apache.maven.plugin.MojoExecutionException;
13 import org.apache.maven.plugin.MojoFailureException;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.regex.Pattern;
18
19 /**
20  * Add implementation code from stub.txt
21  *
22  * @goal process-sources
23  *
24  * @phase process-sources
25  *
26  */
27 public class ProcessSources extends AbstractMojo{
28     /**
29      * @parameter expression="${project.build.sourceDirectory}"
30      * @readOnly
31      * @required
32      */
33     private File directory;
34
35     @Override
36     public void execute() throws MojoExecutionException, MojoFailureException {
37         if (directory == null || !directory.exists()) {
38             super.getLog().error("Directory does not exists.");
39         }
40         File sourceDirectory = new File(directory.getPath() + Util.replaceDots(".org.opendaylight.controller.config.yang.test.impl"));
41         if (!sourceDirectory.exists()) {
42             super.getLog().error("Source directory does not exists " + sourceDirectory.getPath());
43         }
44
45         File[] sourceFiles = sourceDirectory.listFiles();
46         for (File sourceFile: sourceFiles) {
47             if(sourceFile.getName().endsWith("Module.java") || sourceFile.getName().endsWith("ModuleFactory.java")) {
48                 File stubFile = new File(sourceFile.getPath().replace(".java", "Stub.txt"));
49                 if (stubFile.exists()) {
50                     try {
51                         rewrite(sourceFile, FileUtils.readFileToString(stubFile));
52                     } catch (IOException e) {
53                         getLog().error("Error while reading/writing to files.", e);
54                     }
55                 }
56             }
57         }
58     }
59
60     private static void rewrite(File sourceFile, String replaceTODOWith) throws IOException {
61         String source = FileUtils.readFileToString(sourceFile);
62         String target = Pattern.compile("^.*TODO.*\n.*throw new java.lang.UnsupportedOperationException.*$", Pattern.MULTILINE).matcher(source).replaceFirst(replaceTODOWith);
63         FileUtils.write(sourceFile, target);
64     }
65 }