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;
11 import java.io.IOException;
12 import java.util.regex.Pattern;
13 import org.apache.commons.io.FileUtils;
14 import org.apache.maven.plugin.AbstractMojo;
15 import org.apache.maven.plugin.MojoExecutionException;
16 import org.apache.maven.plugin.MojoFailureException;
19 * Add implementation code from stub.txt
21 * @goal process-sources
23 * @phase process-sources
26 public class ProcessSources extends AbstractMojo{
28 * @parameter expression="${project.build.sourceDirectory}"
32 private File directory;
35 public void execute() throws MojoExecutionException, MojoFailureException {
36 if (directory == null || !directory.exists()) {
37 super.getLog().error("Directory does not exists.");
39 File sourceDirectory = new File(directory.getPath() + Util.replaceDots(".org.opendaylight.controller.config.yang.test.impl"));
40 if (!sourceDirectory.exists()) {
41 super.getLog().error(String.format("Source directory does not exists %s", sourceDirectory.getPath()));
44 File[] sourceFiles = sourceDirectory.listFiles();
45 for (File sourceFile: sourceFiles) {
46 if (sourceFile.getName().endsWith(".java")) {
49 sourceContent = FileUtils.readFileToString(sourceFile);
50 } catch (IOException e) {
51 getLog().error(String.format("Cannot read %s", sourceFile.getAbsolutePath()), e);
54 if (sourceFile.getName().endsWith("Module.java") || sourceFile.getName().endsWith("ModuleFactory.java")) {
55 File stubFile = new File(sourceFile.getPath().replace(".java", "Stub.txt"));
56 if (stubFile.exists()) {
57 String stubContent = null;
59 stubContent = FileUtils.readFileToString(stubFile);
60 } catch (IOException e) {
61 getLog().error(String.format("Cannot read %s", stubFile.getAbsolutePath()), e);
63 if (stubContent != null) {
64 sourceContent = rewriteStub(sourceContent, stubContent);
68 // remove copyright headers as they can contain timestamp
69 sourceContent = removeCopyrights(sourceContent);
71 // replace the file content
73 FileUtils.write(sourceFile, sourceContent);
74 } catch (IOException e) {
75 getLog().error(String.format("Cannot write %s", sourceFile.getAbsolutePath()), e);
82 private static Pattern MULTILINE_COMMENT_PATTERN = Pattern.compile("/\\*.*\\*/", Pattern.MULTILINE | Pattern.DOTALL);
83 private static String removeCopyrights(String source) {
84 String target = MULTILINE_COMMENT_PATTERN.matcher(source).replaceAll("\n");
85 //FileUtils.write(sourceFile, target);
89 private static Pattern UNSUPPORTED_OP_PATTERN = Pattern.compile("^.*TODO.*\n.*throw new java.lang.UnsupportedOperationException.*$", Pattern.MULTILINE);
91 private static String rewriteStub(String source, String replaceTODOWith) {
92 String target = UNSUPPORTED_OP_PATTERN.matcher(source).replaceFirst(replaceTODOWith);