Skip to content
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
217 changes: 133 additions & 84 deletions maven-config-processor-plugin/pom.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,38 @@
*/
package com.google.code.configprocessor;

import static com.google.code.configprocessor.util.IOUtils.*;
import static org.apache.commons.lang.StringUtils.*;

import java.io.*;
import java.util.*;

import org.apache.commons.lang.*;
import org.apache.tools.ant.*;

import com.google.code.configprocessor.expression.*;
import com.google.code.configprocessor.io.*;
import com.google.code.configprocessor.log.*;
import com.google.code.configprocessor.maven.*;
import com.google.code.configprocessor.parsing.*;
import com.google.code.configprocessor.processing.*;
import com.google.code.configprocessor.processing.properties.*;
import com.google.code.configprocessor.processing.xml.*;
import static com.google.code.configprocessor.util.IOUtils.close;
import static com.google.code.configprocessor.util.IOUtils.forceMkdirs;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.tools.ant.DirectoryScanner;

import com.google.code.configprocessor.expression.ExpressionResolver;
import com.google.code.configprocessor.io.FileResolver;
import com.google.code.configprocessor.log.LogAdapter;
import com.google.code.configprocessor.maven.AntrunXmlPlexusConfigurationWriter;
import com.google.code.configprocessor.parsing.ProcessingConfigurationParser;
import com.google.code.configprocessor.processing.Action;
import com.google.code.configprocessor.processing.ActionProcessor;
import com.google.code.configprocessor.processing.properties.PropertiesActionProcessor;
import com.google.code.configprocessor.processing.xml.XmlActionProcessor;
import com.google.code.configprocessor.processing.xml.XmlHelper;

public class ConfigProcessor {

Expand Down Expand Up @@ -146,9 +161,9 @@ protected Action getAction(Transformation transformation) throws ConfigProcessor
StringWriter writer = new StringWriter();
AntrunXmlPlexusConfigurationWriter xmlWriter = new AntrunXmlPlexusConfigurationWriter();
xmlWriter.write(transformation.getRules(), writer);
String configContent = trimToNull(writer.toString());
configContent = removeStart(configContent, "<rules>");
configContent = removeEnd(configContent, "</rules>");
String configContent = StringUtils.trimToNull(writer.toString());
configContent = StringUtils.removeStart(configContent, "<rules>");
configContent = StringUtils.removeEnd(configContent, "</rules>");
StringBuilder sb = new StringBuilder(configContent.length() + XmlHelper.ROOT_PROCESSOR_START.length() + XmlHelper.ROOT_PROCESSOR_END.length());
sb.append(XmlHelper.ROOT_PROCESSOR_START);
sb.append(configContent);
Expand Down Expand Up @@ -186,7 +201,8 @@ protected String getConfigIdentifier(Transformation transformation) throws Confi
} catch (IOException e) {
throw new ConfigProcessorException("Shouldn't happen because there is no I/O here", e);
}
return abbreviate(trimToNull(removeStart(writer.toString(), "<rules>")), 100);
return StringUtils.abbreviate(StringUtils.trimToNull(StringUtils.removeStart(writer.toString(), "<rules>")),
100);
}
return transformation.getConfig();
}
Expand Down Expand Up @@ -236,7 +252,7 @@ protected List<File> getMatchingFiles(String pattern) throws ConfigProcessorExce
scanner.setCaseSensitive(false);
scanner.scan();
String[] fileNames = scanner.getIncludedFiles();
List<File> files = new ArrayList<File>();
List<File> files = new ArrayList<>();
for (String fileName : fileNames) {
files.add(new File(baseDir, fileName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
*/
package com.google.code.configprocessor.ant;

import java.util.*;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;

import org.apache.tools.ant.*;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.PropertyHelper;

import com.google.code.configprocessor.expression.*;
import com.google.code.configprocessor.expression.ExpressionResolver;

public class AntExpressionResolver implements ExpressionResolver {

Expand All @@ -44,7 +48,8 @@ public AntExpressionResolver(Project project, Properties specificProperties, boo
this.replacePlaceholders = replacePlaceholders;
}

public String resolve(String value, boolean isPropertiesValue) {
@Override
public String resolve(String value, boolean isPropertiesValue) {
String resolvedValue;

if (replacePlaceholders) {
Expand All @@ -55,11 +60,22 @@ public String resolve(String value, boolean isPropertiesValue) {
String name = (String) names.nextElement();
ph.setUserProperty(null, name, specificProperties.getProperty(name));
}
resolvedValue = ph.replaceProperties(null, value, specificProperties);
resolvedValue = ph.replaceProperties(null, value, loopConvert(specificProperties));
} else {
resolvedValue = value;
}

return resolvedValue;
}

private Hashtable<String, Object> loopConvert(Properties prop)
{
Hashtable<String, Object> retMap = new Hashtable<>();
for (Map.Entry<Object, Object> entry : prop.entrySet())
{
retMap.put(String.valueOf(entry.getKey()), entry.getValue());
}
return retMap;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
*/
package com.google.code.configprocessor.maven;

import org.apache.commons.lang.*;
import org.codehaus.plexus.component.configurator.expression.*;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;

import com.google.code.configprocessor.expression.*;
import com.google.code.configprocessor.util.*;
import com.google.code.configprocessor.expression.ExpressionResolver;
import com.google.code.configprocessor.util.PropertiesUtils;

/**
* Resolver of placeholders.
*
*
* @author Leandro Aparecido
* @see org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator
*/
Expand All @@ -50,12 +51,13 @@ public MavenExpressionResolver(ExpressionEvaluator evaluator, boolean replacePla

/**
* Resolves the given text replacing any placeholders if necessary.
*
*
* @param value Value to resolve.
* @param isPropertiesValue True is the value will be used in .properties files and should be escaped.
* @return Resolved value with values replaced as necessary.
*/
public String resolve(String value, boolean isPropertiesValue) {
@Override
public String resolve(String value, boolean isPropertiesValue) {
String resolvedValue;

if (replacePlaceholders) {
Expand All @@ -65,7 +67,7 @@ public String resolve(String value, boolean isPropertiesValue) {
throw new IllegalArgumentException("Expression [" + value + "] did not resolve to String");
}
resolvedValue = (String) aux;

if (isPropertiesValue && !StringUtils.equals(value, resolvedValue)) {
resolvedValue = PropertiesUtils.escapePropertyValue(resolvedValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@
*/
package com.google.code.configprocessor.parsing;

import java.io.*;
import java.nio.charset.*;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;

import com.google.code.configprocessor.*;
import com.google.code.configprocessor.processing.*;
import com.thoughtworks.xstream.*;
import com.thoughtworks.xstream.converters.reflection.*;
import com.google.code.configprocessor.ParsingException;
import com.google.code.configprocessor.processing.AddAction;
import com.google.code.configprocessor.processing.CommentAction;
import com.google.code.configprocessor.processing.ModifyAction;
import com.google.code.configprocessor.processing.NestedAction;
import com.google.code.configprocessor.processing.RemoveAction;
import com.google.code.configprocessor.processing.UncommentAction;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
import com.thoughtworks.xstream.security.NoTypePermission;
import com.thoughtworks.xstream.security.NullPermission;
import com.thoughtworks.xstream.security.PrimitiveTypePermission;

public class ProcessingConfigurationParser {

Expand All @@ -44,6 +54,15 @@ public NestedAction parse(Reader is) throws ParsingException {
protected XStream getXStream() {
XStream xstream = new XStream(new PureJavaReflectionProvider());

// clear out existing permissions and start a whitelist
xstream.addPermission(NoTypePermission.NONE);
// allow some basics
xstream.addPermission(NullPermission.NULL);
xstream.addPermission(PrimitiveTypePermission.PRIMITIVES);
// allow any type from the same package
xstream.allowTypesByWildcard(new String[] { "com.google.code.configprocessor.processing**"
});

xstream.alias("processor", NestedAction.class);
xstream.alias("add", AddAction.class);
xstream.alias("modify", ModifyAction.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
*/
package com.google.code.configprocessor.processing;

import org.apache.commons.lang.*;
import org.apache.commons.lang3.StringUtils;

public abstract class AbstractAction implements Action {

private static final long serialVersionUID = 2344519551554135862L;

private String name;
private String value;
private boolean strict;

public AbstractAction(String name, String value) {
this.name = name;
this.value = value;
this.strict = true;
strict = true;
}

public String getName() {
Expand All @@ -46,11 +46,12 @@ public String getValue() {
public void setValue(String value) {
this.value = value;
}

public boolean isStrict() {

@Override
public boolean isStrict() {
return strict;
}

public void setStrict(boolean strict) {
this.strict = strict;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
package com.google.code.configprocessor.processing;

import org.apache.commons.lang.*;
import org.apache.commons.lang3.StringUtils;

public class AddAction extends AbstractAction {

private static final long serialVersionUID = -5444028483023476286L;

private static final boolean DEFAULT_IGNORE_ROOT = true;

private boolean first;
Expand All @@ -42,22 +42,23 @@ public AddAction(String name, String value) {

public AddAction(String file, String after, String before) {
super(null, null);

this.file = file;
this.after = after;
this.before = before;
this.ignoreRoot = DEFAULT_IGNORE_ROOT;
ignoreRoot = DEFAULT_IGNORE_ROOT;
}

public AddAction(String name, String value, String after, String before) {
super(name, value);

this.after = after;
this.before = before;
this.ignoreRoot = DEFAULT_IGNORE_ROOT;
ignoreRoot = DEFAULT_IGNORE_ROOT;
}

public void validate() throws ActionValidationException {

@Override
public void validate() throws ActionValidationException {
if (getFile() == null && getValue() == null) {
throw new ActionValidationException("File or value are required", this);
}
Expand Down Expand Up @@ -99,47 +100,47 @@ public void setLast(boolean last) {
public String getAfter() {
return StringUtils.trimToNull(after);
}

public void setAfter(String after) {
this.after = after;
}

public String getBefore() {
return StringUtils.trimToNull(before);
}

public void setBefore(String before) {
this.before = before;
}

public String getInside() {
return StringUtils.trimToNull(inside);
}

public void setInside(String inside) {
this.inside = inside;
}

public String getFile() {
return StringUtils.trimToNull(file);
}

public void setFile(String file) {
this.file = file;
}

public boolean isIgnoreRoot() {
return ignoreRoot;
}

public void setIgnoreRoot(boolean ignoreRoot) {
this.ignoreRoot = ignoreRoot;
}

public NestedAction getNestedAction() {
return nestedAction;
}

public void setNestedAction(NestedAction nestedAction) {
this.nestedAction = nestedAction;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package com.google.code.configprocessor.processing;

import org.apache.commons.lang.StringUtils;

import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class ModifyAction extends AbstractAction {

private static final long serialVersionUID = 3614101885803457281L;
Expand All @@ -45,7 +45,8 @@ public ModifyAction(String name, String value, NodeSetPolicy nodeSetPolicy) {
this.nodeSetPolicy = nodeSetPolicy.toString();
}

public void validate() throws ActionValidationException {
@Override
public void validate() throws ActionValidationException {
if (getName() == null) {
if (getValue() == null) {
if (getFind() == null || getReplace() == null) {
Expand Down
Loading