Escape special characters in properties file java - java

I have key, value in properties file like this proj.path=${HOME}/dir. I have both environment variable and directory also with ${HOME}.
In my case I would like to use it as directory path only but when I read this from file it is getting replaced with environment variable value (home/user/dir).
I tried to escape it like proj.path=\\$\\{HOME\\}/dir but in code it is coming like \$\{HOME\}/dir
Required output is ${HOME}/dir.
EDIT:
Prop file:
proj.path=${HOME}/dir,some/dir/dir2
I am accessing in spring like below.
#Value("#{'${proj.path}'.split(',')}")
private List<String> customPaths;

One way of escaping the ${HOME} value is by wrapping the $ character in an expression and changing the type from List<String> to String[].
proj.path=#{'$'}{HOME}/dir,some/dir/dir2
#Value("${proj.path}")
private String[] customPaths;

Related

How to read all properties into an array with spring?

I know you can use spring to read a single property, and to read a single property that has a list of values into a list. But what about reading all the properties from a file into a list?
I.E.
EDIT: The property file we are reading is litterally just a list of values, no key, like the updated example below:
Property File
queueName1
quename2
queName3
...etc (the file is like 100 lines long hence why its not a list of values with one property name)
and then be able to do something like
//Imaginary Code
#Value("${GET ALL THE LINES}")
List<String> eachLineOfPropertyFile;
If you would want to do it using Spring alone, then separate each of the value using "," in the property file and make use of spring EL.
Your properties file will be like:
property.values=queueName1,quename2,queName3
And with Spring Value Annotation
#Value("#{'${property.values}'.split(',')}")
List<String> eachLineOfPropertyFile;
Can you not use the following
List<String> list = Files.readAllLines(new File("propertiesFile").toPath(), Charset.defaultCharset() );
PS: This is part of Java 7

Single Curly Bracket in Properties file

I am trying to read this text which has only a single curly brace
Y8R30j)i{sjmPXfE
from a .properties file using MessageResources.getMessage()
and am getting this exception
java.lang.IllegalArgumentException: Unmatched braces in the pattern.
at java.text.MessageFormat.applyPattern(MessageFormat.java:508)
at java.text.MessageFormat.<init>(MessageFormat.java:363)
I tried to escape by using
Y8R30j)i'{'sjmPXfE
but am getting the same exception.
org.apache.struts.util.MessageResources uses java.text.MessageFormat which interprets things between curly braces as patterns or placeholders to be replaced with strings.
Per the exception it is clear that java is not able to find the closing brace for the opening curly brace you have in your key value, possible workaround (working with struts 1.3) is below. ( in light of unicode escaping or any other escaping not working, can refer to java.text.MessageFormat.applyPattern() method for further escaping possibles)
Specify key as below in message resources file -
key=Y8R30j)i{0}sjmPXfE
Read value of the key with the code below in your action (or any other java) class
MessageResources messages = MessageResources.getMessageResources("MessageResources");
Object[] leftCurlyBrace = { "{" };
String value = messages.getMessage(request.getLocale(), "key", leftCurlyBrace);
Am assuming you are trying to read some encrypted value from a properties file in a struts 1.x J2EE environment

Reading path from ini file, backslash escape character disappearing?

I'm reading in an absolute pathname from an ini file and storing the pathname as a String value in my program. However, when I do this, the value that gets stored somehow seems to be losing the backslash so that the path just comes out one big jumbled mess? For example, the ini file would have key, value of:
key=C:\folder\folder2\filename.extension
and the value that gets stored is coming out as C:folderfolder2filename.extension.
Would anyone know how to escape the keys before it gets read in?
Let's also assume that changing the values of the ini file is not an alternative because it's not a file that I create.
Try setting the escape property to false in Ini4j.
http://ini4j.sourceforge.net/apidocs/org/ini4j/Config.html#setEscape%28boolean%29
You can try:
Config.getGlobal().setEscape(false);
If you read the file and then translate the \ to a / before processing, that would work. So the library you are using has a method Ini#load(InputStream) that takes the INI file contents, call it like this:
byte[] data = Files.readAllBytes(Paths.get("directory", "file.ini");
String contents = new String(data).replaceAll("\\\\", "/");
InputStream stream = new ByteArrayInputStream(contents.getBytes());
ini.load(stream);
The processor must be doing the interpretation of the back-slashes, so this will give it data with forward-slashes instead. Or, you could escape the back-slashes before processing, like this:
String contents = new String(data).replaceAll("\\\\", "\\\\\\\\");

Java properties value ending in backlash

I have a value in a Java .properties file that needs to end in a backlash. The property value should be "\\server\folder\", and I enter the value like so:
name=\\\\server\\folder\\
The trailing backslash is killing whatever property comes on the next line. Am I escaping this incorrectly?
Not sure what the problem is in your case, but this snippet
Properties props = new Properties();
props.load(new FileInputStream("filename.txt"));
System.out.println(props);
Prints
{prop3=val3, prop2=val2\, prop1=val1}
If filename.txt contains
prop1=val1
prop2=val2\\
prop3=val3
Note that a single (or actually, an odd number) of \ in the end of a property line would escape the newline character and things gets messed up.

apache commons configuration loads property until "," character

I want to load configuration (apache commons configuration) from a properties file. My program is:
PropertiesConfiguration pc = new PropertiesConfiguration("my.properties");
System.out.println(pc.getString("myValue"));
In my.properties I have
myValue=value,
with comma
When I run program the output is value, not value, with comma. Looks like value is loaded until , character.
Any ideas?
That behavior is clearly documented, i.e., that PropertiesConfiguration treats a value with a comma as multiple values allowing things like:
fruit=apples,banana,oranges
to be interpreted sensibly. The fix (from the doc) is to add a backslash to escape the comma, e.g.,
myKey=value\, with an escaped comma
Check Javadoc. You have to setDelimiterParsingDisabled(true) to disable parsing list of properties.
Actually propConfig.setDelimiterParsingDisabled(true) is working, but you must load the config file after this setting, for example:
propConfig = new PropertiesConfiguration();
propConfig.setDelimiterParsingDisabled(true);
propConfig.load(propertiesFile);
Settings won't work if your code like is:
propConfig = new PropertiesConfiguration(propertiesFile);
propConfig.setDelimiterParsingDisabled(true);
PropertiesConfiguration interprets ',' as a value separator.
If you put \ before the ,, you escape it, and you can read the value
Example:
myValue=value\, with comma
You read = value, with comma without problems

Categories

Resources