The specified encoding is only used to load and save project files that are updated with the license header. The header file itself is, however, loaded without using the encoding. In our case, this results in German umlauts not being displayed correctly.
Index: src/main/java/com/google/code/mojo/license/AbstractLicenseMojo.java
===================================================================
--- src/main/java/com/google/code/mojo/license/AbstractLicenseMojo.java (revision 470)
+++ src/main/java/com/google/code/mojo/license/AbstractLicenseMojo.java (working copy)
@@ -31,8 +31,10 @@
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
+import org.xml.sax.InputSource;
import java.io.File;
+import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
@@ -225,13 +227,13 @@
}
finder.setPluginClassPath(getClass().getClassLoader());
- final Header h = new Header(finder.findResource(this.header), mergeProperties(), headerSections);
+ final Header h = new Header(finder.findResource(this.header), encoding, mergeProperties(), headerSections);
debug("Header %s:\n%s", h.getLocation(), h);
if (this.validHeaders == null) this.validHeaders = new String[0];
final List<Header> validHeaders = new ArrayList<Header>(this.validHeaders.length);
for (String validHeader : this.validHeaders)
- validHeaders.add(new Header(finder.findResource(validHeader), mergeProperties(), headerSections));
+ validHeaders.add(new Header(finder.findResource(validHeader), encoding, mergeProperties(), headerSections));
final DocumentFactory documentFactory = new DocumentFactory(basedir, buildMapping(), buildHeaderDefinitions(), encoding, keywords);
@@ -359,10 +361,16 @@
final Map<String, HeaderDefinition> headers = new HashMap<String, HeaderDefinition>(HeaderType.defaultDefinitions());
// and then override them with those provided in properties file
for (String resource : headerDefinitions) {
- final AdditionalHeaderDefinition fileDefinitions = new AdditionalHeaderDefinition(XMLDoc.from(finder.findResource(resource), true));
- final Map<String, HeaderDefinition> map = fileDefinitions.getDefinitions();
- debug("%d header definitions loaded from '%s'", map.size(), resource);
- headers.putAll(map);
+ try {
+ InputSource source = new InputSource(finder.findResource(resource).openStream());
+ source.setEncoding(encoding);
+ final AdditionalHeaderDefinition fileDefinitions = new AdditionalHeaderDefinition(XMLDoc.from(source, true));
+ final Map<String, HeaderDefinition> map = fileDefinitions.getDefinitions();
+ debug("%d header definitions loaded from '%s'", map.size(), resource);
+ headers.putAll(map);
+ } catch (IOException ex) {
+ throw new MojoFailureException("Error reading header definition: " + resource, ex);
+ }
}
// force inclusion of unknow item to manage unknown files
headers.put(HeaderType.UNKNOWN.getDefinition().getType(), HeaderType.UNKNOWN.getDefinition());
Index: src/main/java/com/google/code/mojo/license/header/Header.java
===================================================================
--- src/main/java/com/google/code/mojo/license/header/Header.java (revision 470)
+++ src/main/java/com/google/code/mojo/license/header/Header.java (working copy)
@@ -55,7 +55,7 @@
* @throws IllegalArgumentException If the header file location is null or if an error occurred while reading the
* file content.
*/
- public Header(URL location, Map<String, String> properties, HeaderSection[] sections) {
+ public Header(URL location, String encoding, Map<String, String> properties, HeaderSection[] sections) {
if (location == null) {
throw new IllegalArgumentException("Cannot read license template header file with a null location");
}
@@ -65,7 +65,7 @@
this.location = location;
this.sections = sections;
try {
- this.headerContent = read(location, properties);
+ this.headerContent = read(location, encoding, properties);
lines = headerContent.replace("\r", "").split("\n");
headerContentOneLine = remove(headerContent, " ", "\t", "\r", "\n");
}
Index: src/main/java/com/google/code/mojo/license/util/FileUtils.java
===================================================================
--- src/main/java/com/google/code/mojo/license/util/FileUtils.java (revision 470)
+++ src/main/java/com/google/code/mojo/license/util/FileUtils.java (working copy)
@@ -51,8 +51,8 @@
}
}
- public static String read(URL location, Map<String, String> properties) throws IOException {
- Reader reader = new InterpolationFilterReader(new BufferedReader(new InputStreamReader(location.openStream())), properties);
+ public static String read(URL location, String encoding, Map<String, String> properties) throws IOException {
+ Reader reader = new InterpolationFilterReader(new BufferedReader(new InputStreamReader(location.openStream(), encoding)), properties);
try {
return IOUtil.toString(reader);
} finally {
Index: src/test/java/com/google/code/mojo/license/document/DocumentTest.java
===================================================================
--- src/test/java/com/google/code/mojo/license/document/DocumentTest.java (revision 470)
+++ src/test/java/com/google/code/mojo/license/document/DocumentTest.java (working copy)
@@ -39,7 +39,7 @@
public void setup() throws MalformedURLException {
Map<String, String> props = new HashMap<String, String>();
props.put("year", "2008");
- header = new Header(new File("src/test/resources/test-header1.txt").toURI().toURL(), props, null);
+ header = new Header(new File("src/test/resources/test-header1.txt").toURI().toURL(), "UTF-8", props, null);
}
@Test
Index: src/test/java/com/google/code/mojo/license/header/AdditionalHeaderDefinitionTest.java
===================================================================
--- src/test/java/com/google/code/mojo/license/header/AdditionalHeaderDefinitionTest.java (revision 470)
+++ src/test/java/com/google/code/mojo/license/header/AdditionalHeaderDefinitionTest.java (working copy)
@@ -54,7 +54,7 @@
Map<String, String> props = new HashMap<String, String>();
props.put("year", "2008");
- Header header = new Header(getClass().getResource("/test-header1.txt"), props, null);
+ Header header = new Header(getClass().getResource("/test-header1.txt"), "UTF-8", props, null);
//FileUtils.write(new File("src/test/resources/test-header3.txt"), header.buildForDefinition(loader.getDefinitions().get("xquery")));
@@ -81,7 +81,7 @@
Map<String, String> props = new HashMap<String, String>();
props.put("year", "2008");
- Header header = new Header(getClass().getResource("/check/header.txt"), props, null);
+ Header header = new Header(getClass().getResource("/check/header.txt"), "UTF-8", props, null);
System.out.println(header.buildForDefinition(loader.getDefinitions().get("text"), false));
}
@@ -102,7 +102,7 @@
Map<String, String> props = new HashMap<String, String>();
props.put("year", "2008");
- Header header = new Header(getClass().getResource("/test-header1.txt"), props, null);
+ Header header = new Header(getClass().getResource("/test-header1.txt"), "UTF-8", props, null);
//FileUtils.write(new File("src/test/resources/test-header4.txt"), header.buildForDefinition(loader.getDefinitions().get("csregion"), false), System.getProperty("file.encoding"));
Index: src/test/java/com/google/code/mojo/license/header/DefaultHeaderDefinitionTest.java
===================================================================
--- src/test/java/com/google/code/mojo/license/header/DefaultHeaderDefinitionTest.java (revision 470)
+++ src/test/java/com/google/code/mojo/license/header/DefaultHeaderDefinitionTest.java (working copy)
@@ -34,7 +34,7 @@
public void test_styles() throws Exception {
Map<String, String> props = new HashMap<String, String>();
props.put("year", "2008");
- Header header = new Header(getClass().getResource("/test-header1.txt"), props, null);
+ Header header = new Header(getClass().getResource("/test-header1.txt"), "UTF-8", props, null);
for (HeaderDefinition definition : HeaderType.defaultDefinitions().values()) {
final String content = FileUtils.read(new File(format("src/test/resources/styles/%s.txt", definition.getType())), System.getProperty("file.encoding"));
assertEquals(content, header.buildForDefinition(definition, content.indexOf("\n") == -1),
Index: src/test/java/com/google/code/mojo/license/header/HeaderTest.java
===================================================================
--- src/test/java/com/google/code/mojo/license/header/HeaderTest.java (revision 470)
+++ src/test/java/com/google/code/mojo/license/header/HeaderTest.java (working copy)
@@ -39,7 +39,7 @@
public void test() throws Exception {
Map<String, String> props = new HashMap<String, String>();
props.put("year", "2008");
- Header header = new Header(getClass().getResource("/test-header1.txt"), props, null);
+ Header header = new Header(getClass().getResource("/test-header1.txt"), "UTF-8", props, null);
assertEquals(header.getLineCount(), 13);
assertTrue(header.asOneLineString().contains("2008"));
assertEquals(header.getLocation(), getClass().getResource("/test-header1.txt"));
@@ -62,7 +62,7 @@
HeaderSection[] sections = { section };
Map<String, String> props = Collections.emptyMap();
- Header header = new Header(getClass().getResource("/test-header5.txt"), props, sections);
+ Header header = new Header(getClass().getResource("/test-header5.txt"), "UTF-8", props, sections);
HeaderDefinition headerDefinition = HeaderType.JAVADOC_STYLE.getDefinition();
@@ -125,7 +125,7 @@
HeaderSection[] sections = { sectionA, sectionB };
Map<String, String> props = Collections.emptyMap();
- Header header = new Header(getClass().getResource("/test-header6.txt"), props, sections);
+ Header header = new Header(getClass().getResource("/test-header6.txt"), "UTF-8", props, sections);
HeaderDefinition headerDefinition = HeaderType.JAVADOC_STYLE.getDefinition();
@@ -181,7 +181,7 @@
HeaderSection[] sections = { section };
Map<String, String> props = Collections.emptyMap();
- Header header = new Header(getClass().getResource("/test-header5.txt"), props, sections);
+ Header header = new Header(getClass().getResource("/test-header5.txt"), "UTF-8", props, sections);
HeaderDefinition headerDefinition = HeaderType.JAVADOC_STYLE.getDefinition();