054edffe68a77aa3d4e87ec15a986f79cadaa78c
[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 com.google.common.io.Files;
11 import java.io.File;
12 import java.io.IOException;
13 import java.nio.charset.StandardCharsets;
14 import java.util.regex.Pattern;
15 import org.apache.maven.plugin.AbstractMojo;
16 import org.apache.maven.plugin.MojoExecutionException;
17 import org.apache.maven.plugin.MojoFailureException;
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(String.format("Source directory does not exists %s", sourceDirectory.getPath()));
43         }
44
45         File[] sourceFiles = sourceDirectory.listFiles();
46         for (File sourceFile: sourceFiles) {
47             if (sourceFile.getName().endsWith(".java")) {
48                 String sourceContent;
49                 try {
50                     sourceContent = Files.toString(sourceFile, StandardCharsets.UTF_8);
51                 } catch (IOException e) {
52                     getLog().error(String.format("Cannot read %s", sourceFile.getAbsolutePath()), e);
53                     continue;
54                 }
55                 if (sourceFile.getName().endsWith("Module.java") || sourceFile.getName().endsWith("ModuleFactory.java")) {
56                     File stubFile = new File(sourceFile.getPath().replace(".java", "Stub.txt"));
57                     if (stubFile.exists()) {
58                         String stubContent = null;
59                         try {
60                             stubContent = Files.toString(stubFile, StandardCharsets.UTF_8);
61                         } catch (IOException e) {
62                             getLog().error(String.format("Cannot read %s", stubFile.getAbsolutePath()), e);
63                         }
64                         if (stubContent != null) {
65                             sourceContent = rewriteStub(sourceContent, stubContent);
66                         }
67                     }
68                 }
69                 // remove copyright headers as they can contain timestamp
70                 sourceContent = removeCopyrights(sourceContent);
71
72                 // replace the file content
73                 try {
74                     Files.write(sourceContent, sourceFile, StandardCharsets.UTF_8);
75                 } catch (IOException e) {
76                     getLog().error(String.format("Cannot write %s", sourceFile.getAbsolutePath()), e);
77                 }
78             }
79
80         }
81     }
82
83     private static Pattern MULTILINE_COMMENT_PATTERN = Pattern.compile("/\\*.*\\*/", Pattern.MULTILINE | Pattern.DOTALL);
84     private static String removeCopyrights(String source) {
85         String target = MULTILINE_COMMENT_PATTERN.matcher(source).replaceAll("\n");
86         //FileUtils.write(sourceFile, target);
87         return target;
88     }
89
90     private static Pattern UNSUPPORTED_OP_PATTERN = Pattern.compile("^.*TODO.*\n.*throw new java.lang.UnsupportedOperationException.*$", Pattern.MULTILINE);
91
92     private static String rewriteStub(String source, String replaceTODOWith) {
93         String target = UNSUPPORTED_OP_PATTERN.matcher(source).replaceFirst(replaceTODOWith);
94         return target;
95     }
96 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.