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.commons.io.FileUtils;
11 import org.apache.maven.plugin.AbstractMojo;
12 import org.apache.maven.plugin.MojoExecutionException;
13 import org.apache.maven.plugin.MojoFailureException;
16 import java.io.IOException;
17 import java.util.regex.Pattern;
20 * Add implementation code from stub.txt
22 * @goal process-sources
24 * @phase process-sources
27 public class ProcessSources extends AbstractMojo{
29 * @parameter expression="${project.build.sourceDirectory}"
33 private File directory;
36 public void execute() throws MojoExecutionException, MojoFailureException {
37 if (directory == null || !directory.exists()) {
38 super.getLog().error("Directory does not exists.");
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());
45 File[] sourceFiles = sourceDirectory.listFiles();
46 for (File sourceFile: sourceFiles) {
47 if (sourceFile.getName().endsWith(".java")) {
50 sourceContent = FileUtils.readFileToString(sourceFile);
51 } catch (IOException e) {
52 getLog().error("Cannot read " + sourceFile.getAbsolutePath(), e);
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;
60 stubContent = FileUtils.readFileToString(stubFile);
61 } catch (IOException e) {
62 getLog().error("Cannot read " + stubFile.getAbsolutePath(), e);
64 if (stubContent != null) {
65 sourceContent = rewriteStub(sourceContent, stubContent);
69 // remove copyright headers as they can contain timestamp
70 sourceContent = removeCopyrights(sourceContent);
72 // replace the file content
74 FileUtils.write(sourceFile, sourceContent);
75 } catch (IOException e) {
76 getLog().error("Cannot write " + sourceFile.getAbsolutePath(), e);
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);
90 private static Pattern UNSUPPORTED_OP_PATTERN = Pattern.compile("^.*TODO.*\n.*throw new java.lang.UnsupportedOperationException.*$", Pattern.MULTILINE);
92 private static String rewriteStub(String source, String replaceTODOWith) {
93 String target = UNSUPPORTED_OP_PATTERN.matcher(source).replaceFirst(replaceTODOWith);