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

Accept default value in placeholders for unresolved property values #1

Merged
merged 5 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.codehaus.mojo.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.
*/

/**
* ExpansionBuffer implementation that allows processing of default values within property placeholders
*
* @author tdiamantis
*/
public class DefaultValuesAwareExpansionBufferImpl extends ExpansionBuffer {
public DefaultValuesAwareExpansionBufferImpl(String unresolved) {
super(unresolved);
}

public KeyAndDefaultValue extractPropertyKeyAndDefaultValue()
{
advanceToNextPrefix();

discardPrefix();

String key = beforeNextSuffix();

String defaultValue = defaultValue();

discardToAfterNextSuffix();

return new KeyAndDefaultValue(key, defaultValue);
}

protected String beforeNextSuffix()
{
int defValuePos = unresolved.indexOf(":");
int propertySuffixPos = unresolved.indexOf("}");

//check default value separator only if before next suffix
if (defValuePos != -1 && propertySuffixPos != -1 && defValuePos < propertySuffixPos)
{
return unresolved.substring( 0, defValuePos );
}

return unresolved.substring( 0, propertySuffixPos );
}

private String defaultValue()
{
int defValuePos = unresolved.indexOf( ":" );
int propertySuffixPos = unresolved.indexOf( "}" );
if (defValuePos != -1 && propertySuffixPos != -1 && (defValuePos+1) < propertySuffixPos)
{
return unresolved.substring( defValuePos+1, propertySuffixPos );
}

return null;
}
}
27 changes: 6 additions & 21 deletions src/main/java/org/codehaus/mojo/properties/ExpansionBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* under the License.
*/

class ExpansionBuffer
abstract class ExpansionBuffer
{
public final StringBuilder resolved = new StringBuilder();
public String unresolved;
Expand All @@ -35,18 +35,7 @@ public boolean hasMoreLegalPlaceholders()
return prefixPos >= 0 && suffixPos >= 0;
}

public String extractPropertyKey()
{
advanceToNextPrefix();

discardPrefix();

String key = beforeNextSuffix();

discardToAfterNextSuffix();

return key;
}
public abstract KeyAndDefaultValue extractPropertyKeyAndDefaultValue();

public String toString() {
return resolved.append(unresolved).toString();
Expand Down Expand Up @@ -79,16 +68,16 @@ private void skipUnresolvedPlaceholder(String newKey)
resolved.append( "${" ).append(newKey).append( "}" );
}

private void discardToAfterNextSuffix() {
protected void discardToAfterNextSuffix() {
int propertySuffixPos = unresolved.indexOf( "}" );
unresolved = unresolved.substring(propertySuffixPos + 1);
}

private void advanceToNextPrefix() {
protected void advanceToNextPrefix() {
resolved.append( beforePrefix() );
}

private void discardPrefix() {
protected void discardPrefix() {
int propertyPrefixPos = unresolved.indexOf( "${" );
unresolved = unresolved.substring(propertyPrefixPos + 2);
}
Expand All @@ -99,9 +88,5 @@ private String beforePrefix()
return unresolved.substring( 0, propertyPrefixPos );
}

private String beforeNextSuffix()
{
int propertySuffixPos = unresolved.indexOf( "}" );
return unresolved.substring( 0, propertySuffixPos );
}
protected abstract String beforeNextSuffix();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.codehaus.mojo.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.
*/

/**
* Default implementation of the ExpansionBuffer class
*
* @author tdiamantis
*/
public class ExpansionBufferImpl extends ExpansionBuffer {
public ExpansionBufferImpl(String unresolved) {
super(unresolved);
}

public KeyAndDefaultValue extractPropertyKeyAndDefaultValue()
{
advanceToNextPrefix();

discardPrefix();

String key = beforeNextSuffix();

discardToAfterNextSuffix();

return new KeyAndDefaultValue(key, null);
}

protected String beforeNextSuffix()
{
int propertySuffixPos = unresolved.indexOf( "}" );
return unresolved.substring( 0, propertySuffixPos );
}
}
49 changes: 49 additions & 0 deletions src/main/java/org/codehaus/mojo/properties/KeyAndDefaultValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.codehaus.mojo.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.
*/

/**
* @author tdiamantis
*/
public class KeyAndDefaultValue {
private String key;
private String defaultValue;

public KeyAndDefaultValue(String key, String defaultValue) {
this.key = key;
this.defaultValue = defaultValue;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getDefaultValue() {
return defaultValue;
}

public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}
}
37 changes: 32 additions & 5 deletions src/main/java/org/codehaus/mojo/properties/PropertyResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,40 @@ class PropertyResolver {
* @return resolved property value
* @throws IllegalArgumentException when properties are circularly defined
*/
public String getPropertyValue( String key, Properties properties, Properties environment )
public String getPropertyValue( String key, Properties properties, Properties environment ) {
return getPropertyValue(key, properties, environment, false);
}

/**
* Same as the previous method. Accepts an extra flag to indicate whether default values should be
* processed within property placeholders or not.
*
* @param key property key
* @param properties project properties
* @param environment environment variables
* @param useDefaultValues process default values flag
* @return resolved property value
* @throws IllegalArgumentException when properties are circularly defined
*/
public String getPropertyValue( String key, Properties properties, Properties environment, boolean useDefaultValues )
{
String value = properties.getProperty(key);

ExpansionBuffer buffer = new ExpansionBuffer(value);
ExpansionBuffer buffer;
if ( useDefaultValues ) {
buffer = new DefaultValuesAwareExpansionBufferImpl(value);
} else {
buffer = new ExpansionBufferImpl(value);
}

CircularDefinitionPreventer circularDefinitionPreventer =
new CircularDefinitionPreventer().visited( key, value);

while ( buffer.hasMoreLegalPlaceholders() )
{
String newKey = buffer.extractPropertyKey();
String newValue = fromPropertiesThenSystemThenEnvironment( newKey, properties, environment );
KeyAndDefaultValue kv = buffer.extractPropertyKeyAndDefaultValue();
String newKey = kv.getKey();
String newValue = fromPropertiesThenSystemThenEnvironment( newKey, kv.getDefaultValue(), properties, environment );

circularDefinitionPreventer.visited(newKey, newValue);

Expand All @@ -62,7 +83,7 @@ public String getPropertyValue( String key, Properties properties, Properties en
return buffer.toString();
}

private String fromPropertiesThenSystemThenEnvironment(String key, Properties properties, Properties environment)
private String fromPropertiesThenSystemThenEnvironment(String key, String defaultValue, Properties properties, Properties environment)
{
String value = properties.getProperty( key );

Expand All @@ -78,6 +99,12 @@ private String fromPropertiesThenSystemThenEnvironment(String key, Properties pr
value = environment.getProperty( key.substring( 4 ) );
}

// try default value
if ( value == null )
{
value = defaultValue;
}

return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ public void setUrls(String[] urls)
*/
private boolean quiet;

/**
* If the plugin should process default values within property placeholders
*
* @parameter default-value="false"
*/
private boolean useDefaultValues;

/**
* Used for resolving property placeholders.
*/
Expand Down Expand Up @@ -246,7 +253,7 @@ private Properties loadSystemEnvironmentPropertiesWhenDefined() throws MojoExecu
private String getPropertyValue( String k, Properties p, Properties environment ) throws MojoFailureException
{
try {
return resolver.getPropertyValue(k, p, environment);
return resolver.getPropertyValue(k, p, environment, useDefaultValues);
} catch (IllegalArgumentException e) {
throw new MojoFailureException(e.getMessage());
}
Expand All @@ -270,6 +277,14 @@ void setQuiet(boolean quiet)
this.quiet = quiet;
}

/**
* @param useDefaultValues set to <code>true</code> if default values need to be processed within property placeholders
*/
public void setUseDefaultValues(boolean useDefaultValues)
{
this.useDefaultValues = useDefaultValues;
}

/**
* Default scope for test access.
* @param project The test project.
Expand All @@ -279,6 +294,14 @@ void setProject(MavenProject project)
this.project = project;
}

/**
* For test access.
* @return The test project
*/
public MavenProject getProject() {
return project;
}

private static abstract class Resource
{
private InputStream stream;
Expand Down