Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the possibility to exclude some keys to write or/and generate (or not) the comment header with the timestamp #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
* under the License.
*/

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.*;
import java.util.Enumeration;
import java.util.Properties;
import java.util.regex.Pattern;

/*
* Licensed to the Apache Software Foundation (ASF) under one
Expand Down Expand Up @@ -63,6 +62,12 @@ public abstract class AbstractWritePropertiesMojo
@Parameter( required = true )
private File outputFile;

@Parameter(required = false)
private String exclusionRegex;

@Parameter(required = false, defaultValue = "true")
private boolean generateCommentHeader;

/**
* @param properties {@link Properties}
* @param file {@link File}
Expand All @@ -71,11 +76,29 @@ public abstract class AbstractWritePropertiesMojo
protected void writeProperties( Properties properties, File file )
throws MojoExecutionException
{
// Filter keys and/or the header comment
final Properties propertiesToWrite = generateCommentHeader ? new Properties() : new PropertiesWithoutHeader();
if (this.exclusionRegex == null) {
propertiesToWrite.putAll(properties);
} else {
final Pattern exclusionPattern = Pattern.compile(exclusionRegex);
final Enumeration<?> enumeration = properties.keys();
while ( enumeration.hasMoreElements() )
{
final String key = (String) enumeration.nextElement();
if (!exclusionPattern.matcher(key).find()) {
propertiesToWrite.put(key, properties.getProperty(key));
}
}
}

// Write the properties
FileOutputStream fos = null;
try
{
fos = new FileOutputStream( file );
properties.store( fos, "Properties" );
propertiesToWrite.store( fos, "Properties" );

}
catch ( FileNotFoundException e )
{
Expand Down Expand Up @@ -132,4 +155,18 @@ public File getOutputFile()
return outputFile;
}

/**
* @return {@link #exclusionRegex}
*/
public String getExclusionRegex() {
return exclusionRegex;
}

/**
* @return {@link #generateCommentHeader}
*/
public boolean getGenerateCommentHeader() {
return generateCommentHeader;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.codehaus.mojo.properties;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
* PropertiesWithoutHeader
*
* Properties without writing header comment and generated timestamp at the beginning of the properties file
*
*/
public class PropertiesWithoutHeader extends Properties {

private static final long serialVersionUID = 8667775349218327383L;

private static class SkipFirstLineStream extends FilterOutputStream {

private boolean firstLineDetected = false;

public SkipFirstLineStream(final OutputStream out) {
super(out);
}

@Override
public void write(final int b) throws IOException {
if (firstLineDetected) {
super.write(b);
} else if (b == '\n') {
firstLineDetected = true;
}
}
}

@Override
public void store(final OutputStream out, final String comments) throws IOException {
super.store(new SkipFirstLineStream(out), null);
}
}