Skip to content

Commit

Permalink
Remove spring dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Mar 30, 2021
1 parent f2ca77a commit 3992cc1
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 19 deletions.
14 changes: 8 additions & 6 deletions license-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,15 @@
<configuration>
<goalPrefix>license</goalPrefix>
<helpPackageName>com.mycila.maven.plugin.license</helpPackageName>
<!-- see https://issues.apache.org/jira/browse/MNG-5346 -->
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
<executions>
<execution>
<id>default-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
<phase>process-classes</phase>
</execution>
<!-- if you want to generate help goal -->
Expand All @@ -59,6 +64,7 @@
<goals>
<goal>helpmojo</goal>
</goals>
<phase>process-classes</phase>
</execution>
</executions>
</plugin>
Expand All @@ -70,13 +76,14 @@
<exclude>src/test/**</exclude>
<exclude>src/it/**</exclude>
<exclude>src/main/resources/**</exclude>
<exclude>**/PropertyPlaceholderResolver.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<additionalparam>-Xdoclint:none</additionalparam>
<!--additionalparam>-Xdoclint:none</additionalparam-->
<sourceFileExcludes combine.children="append">
<sourceFileExclude>**/*Mojo.java</sourceFileExclude>
</sourceFileExcludes>
Expand Down Expand Up @@ -132,11 +139,6 @@
<version>3.3.3</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>

<dependency>
<groupId>com.mycila</groupId>
<artifactId>mycila-xmltool</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.mycila.maven.plugin.license.header.HeaderType;
import com.mycila.maven.plugin.license.util.FileContent;
import com.mycila.maven.plugin.license.util.FileUtils;
import org.springframework.util.PropertyPlaceholderHelper;

import java.io.File;
import java.io.IOException;
Expand All @@ -38,7 +37,7 @@ public final class Document {
private final String encoding;
private final String[] keywords;
private final DocumentPropertiesLoader documentPropertiesLoader;
private final PropertyPlaceholderHelper placeholderHelper = new PropertyPlaceholderHelper("${", "}", ":", true);
private final PropertyPlaceholderResolver placeholderResolver = new PropertyPlaceholderResolver();
private HeaderParser parser;


Expand Down Expand Up @@ -94,7 +93,7 @@ public void updateHeader(Header header) {
}

public String mergeProperties(String str) {
return placeholderHelper.replacePlaceholders(str, documentPropertiesLoader.load(this));
return placeholderResolver.replacePlaceholders(str, documentPropertiesLoader.load(this));
}

public void save() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed 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
*
* https://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.
*/
package com.mycila.maven.plugin.license.document;

import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.function.Function;

/**
* Copy and simplification of Spring's PropertyPlaceholderHelper at
* https://github.com/spring-projects/spring-framework/blob/master/spring-core/src/main/java/org/springframework/util/PropertyPlaceholderHelper.java
* <p>
* Utility class for working with Strings that have placeholder values in them. A placeholder takes the form
* {@code ${name}}. Using {@code PropertyPlaceholderHelper} these placeholders can be substituted for
* user-supplied values. <p> Values for substitution can be supplied using a {@link Properties} instance or
* using a {@link Function<String, String>}.
*
* @author Juergen Hoeller
* @author Rob Harrop
* @since 3.0
*/
class PropertyPlaceholderResolver {

private final String placeholderPrefix = "${";
private final String placeholderSuffix = "}";

public String replacePlaceholders(String value, final Properties properties) {
return replacePlaceholders(value, properties::getProperty);
}

private String replacePlaceholders(String value, Function<String, String> placeholderResolver) {
return parseStringValue(value, placeholderResolver, new HashSet<>());
}

private String parseStringValue(String value, Function<String, String> placeholderResolver, Set<String> visitedPlaceholders) {
StringBuilder result = new StringBuilder(value);
int startIndex = value.indexOf(this.placeholderPrefix);
while (startIndex != -1) {
int endIndex = findPlaceholderEndIndex(result, startIndex);
if (endIndex != -1) {
String placeholder = result.substring(startIndex + this.placeholderPrefix.length(), endIndex);
String originalPlaceholder = placeholder;
if (!visitedPlaceholders.add(originalPlaceholder)) {
throw new IllegalArgumentException("Circular placeholder reference '" + originalPlaceholder + "' in property definitions");
}
// Recursive invocation, parsing placeholders contained in the placeholder key.
placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);
// Now obtain the value for the fully resolved key...
String propVal = placeholderResolver.apply(placeholder);
String valueSeparator = ":";
if (propVal == null) {
int separatorIndex = placeholder.indexOf(valueSeparator);
if (separatorIndex != -1) {
String actualPlaceholder = placeholder.substring(0, separatorIndex);
String defaultValue = placeholder.substring(separatorIndex + valueSeparator.length());
propVal = placeholderResolver.apply(actualPlaceholder);
if (propVal == null) {
propVal = defaultValue;
}
}
}
if (propVal != null) {
// Recursive invocation, parsing placeholders contained in the
// previously resolved placeholder value.
propVal = parseStringValue(propVal, placeholderResolver, visitedPlaceholders);
result.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);
startIndex = result.indexOf(this.placeholderPrefix, startIndex + propVal.length());
} else {
// Proceed with unprocessed value.
startIndex = result.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length());
}
visitedPlaceholders.remove(originalPlaceholder);
} else {
startIndex = -1;
}
}

return result.toString();
}

private int findPlaceholderEndIndex(CharSequence buf, int startIndex) {
int index = startIndex + this.placeholderPrefix.length();
int withinNestedPlaceholder = 0;
while (index < buf.length()) {
String simplePrefix = "{";
if (substringMatch(buf, index, this.placeholderSuffix)) {
if (withinNestedPlaceholder > 0) {
withinNestedPlaceholder--;
index = index + this.placeholderSuffix.length();
} else {
return index;
}
} else if (substringMatch(buf, index, simplePrefix)) {
withinNestedPlaceholder++;
index = index + simplePrefix.length();
} else {
index++;
}
}
return -1;
}

private static boolean substringMatch(CharSequence str, int index, CharSequence substring) {
if (index + substring.length() > str.length()) {
return false;
}
for (int i = 0; i < substring.length(); i++) {
if (str.charAt(index + i) != substring.charAt(i)) {
return false;
}
}
return true;
}
}
18 changes: 8 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
<parent>
<groupId>com.mycila</groupId>
<artifactId>mycila-pom</artifactId>
<version>3</version>
<relativePath />
<version>9</version>
</parent>

<artifactId>license-maven-plugin-parent</artifactId>
Expand Down Expand Up @@ -72,11 +71,6 @@
<artifactId>maven-plugin-annotations</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.19.RELEASE</version>
</dependency>
<dependency>
<groupId>com.mycila</groupId>
<artifactId>mycila-xmltool</artifactId>
Expand All @@ -99,7 +93,6 @@
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>

</dependencies>
</dependencyManagement>

Expand All @@ -108,7 +101,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.2</version>
<version>3.6.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -141,13 +134,18 @@
</plugin>
<plugin>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.4</version>
<version>1.6</version>
<configuration>
<useAgent>false</useAgent>
<keyname>${gpg.keyname}</keyname>
<passphrase>${gpg.passphrase}</passphrase>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.6.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
Expand Down

0 comments on commit 3992cc1

Please sign in to comment.