Skip to content

Commit

Permalink
Merge pull request #93 from mamh-java/add-encoding-to-uft-8-when-read…
Browse files Browse the repository at this point in the history
…-Properties-file

add one param to set charset when read Properties file
  • Loading branch information
rsandell authored Jun 24, 2024
2 parents bd32f56 + de2145a commit 7fe33bc
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
public class ReadPropertiesStep extends AbstractFileOrTextStep {
private Map<Object, Object> defaults;
private boolean interpolate;
private String charset;

@DataBoundConstructor
public ReadPropertiesStep() {
Expand Down Expand Up @@ -93,6 +94,19 @@ public void setInterpolate(Boolean interpolate) {
this.interpolate = interpolate;
}

public String getCharset() {
return charset;
}

/**
* The charset encoding to use when read the properties file. Defaults to ISO 8859-1 .
* @param charset the charset
*/
@DataBoundSetter
public void setCharset(String charset) {
this.charset = charset;
}

@Extension
public static class DescriptorImpl extends AbstractFileOrTextStepDescriptorImpl {
public DescriptorImpl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import java.io.InputStream;
import java.io.PrintStream;
import java.io.StringReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -84,7 +86,13 @@ protected Map<String, Object> doRun() throws Exception {
FilePath f = ws.child(step.getFile());
if (f.exists() && !f.isDirectory()) {
try(InputStream is = f.read()){
properties.load(is);
if (StringUtils.isEmpty(step.getCharset())) {
properties.load(is);
} else {
try (InputStreamReader isr = new InputStreamReader(is, step.getCharset()); BufferedReader br = new BufferedReader(isr)) {
properties.load(br);
}
}
}
} else if (f.isDirectory()) {
logger.print("warning: ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:block>
<p>This is a special step. No snippet generation available. See inline help for more information.</p>
</f:block>
<f:entry title="${%file.title}" field="file" description="${%file.description}">
<f:textbox />
</f:entry>
<f:entry title="${%text.title}" field="text" description="${%text.description}">
<f:textbox />
</f:entry>
<f:entry title="${%interpolate.title}" field="interpolate" description="${%interpolate.description}">
<f:checkbox />
</f:entry>
<f:entry title="${%charset.title}" field="charset" description="${%charset.description}">
<f:textbox />
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
file.title=File
file.description=(Optional) path to a file in the workspace to read the properties from
text.title=Text
text.description=An Optional String containing properties formatted data
interpolate.title=Interpolate
interpolate.description=Flag to indicate if the properties should be interpolated or not.
charset.title=Charset
charset.description=(Optional) What charset to use when read the properties file. Default is empty, then will use jvm ISO 8859-1.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@

In case of error or cyclic dependencies, the original properties will be returned.
</li>
<li>
<code>charset</code>:
What charset to use when read properties file. Default is empty, then will use jvm ISO 8859-1.
</li>
</ul>
<p>
<strong>Example:</strong><br/>
Expand All @@ -82,4 +86,10 @@
assert props.fullUrl = 'http://localhost/README.txt'
</pre>
</code>
<strong>Example with charset:</strong>
<code>
<pre>
def props = readProperties charset: 'UTF-8', file: 'test.properties'
</pre>
</code>
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,30 @@ public void readFileWithDefaults() throws Exception {
j.assertBuildStatusSuccess(p.scheduleBuild2(0));
}

@Test
public void readFileWithCharset() throws Exception {
Properties props = new Properties();
props.setProperty("test", "One");
props.setProperty("another", "Two");
props.setProperty("zh", "中文");
File file = temp.newFile();
try (FileWriter f = new FileWriter(file)) {
props.store(f, "Pipeline test");
}

WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node('slaves') {\n" +
" def props = readProperties charset: 'utf-8', file: '" + separatorsToSystemEscaped(file.getAbsolutePath()) + "'\n" +
" assert props['test'] == 'One'\n" +
" assert props['another'] == 'Two'\n" +
" assert props.test == 'One'\n" +
" assert props.another == 'Two'\n" +
" assert props.zh == '中文' \n" +
"}", true));
j.assertBuildStatusSuccess(p.scheduleBuild2(0));
}

@Test
public void readText() throws Exception {
Properties props = new Properties();
Expand Down

0 comments on commit 7fe33bc

Please sign in to comment.