Saving to properties file escapes : - java

Does anyone know why the colons are getting escaped when I store the properties file?
I'm doing this:
Properties prop = new Properties();
// Set the properties value.
prop.setProperty("url","http://localhost:7101/test/home");
And storing using:
prop.store(new FileOutputStream(propFile), null);
It's working but the output has colons escaped for some reason:
url=http\://localhost\:7101/test/home
Anyone know a fix?

In properties files, both of these are legit:
key1 = value
key2: value
So both = and : must be escaped.
Now, if you read the thing back with Properties, it's no problem. Otherwise, you'll have to write custom code

That's what the store() API does:-
Each character of the key and element
strings is examined to see whether it
should be rendered as an escape
sequence. The ASCII characters \, tab,
form feed, newline, and carriage
return are written as \, \t, \f \n,
and \r, respectively. Characters less
than \u0020 and characters greater
than \u007E are written as \uxxxx for
the appropriate hexadecimal value
xxxx. For the key, all space
characters are written with a
preceding \ character. For the
element, leading space characters, but
not embedded or trailing space
characters, are written with a
preceding \ character. The key and
element characters #, !, =, and : are
written with a preceding backslash to
ensure that they are properly loaded.
It shouldn't really matter to you as long as you use Properties to get the values.

Related

How to use Escape character in HL7 message

I am using ca.uhn.hl7v2.util.Terser to create a HL7 message. For one of the HL7 fields I need to set the following value
"\home\one\two".
HL7 message type is MDM_T02(version is 2.3.1). Because "" is an escape character in hl7 messages if I try to use
public void methodOne() {
MDM_T02 mdmt02 = new MDM_T02();
Terser terser = new Terser(mdmt02);
terser.set("OBX-5-1", "\\\\usne-server\\Pathology\\Quantum");
}
In the HL7 message OBX-5-1 is printed as "\E\E\usne-server\E\Pathology\E\Quantum".
Can someone help me to print the proper message?
You may refer to the description of HL7 Escape Sequences here or here.
HL7 defines character sequences to represent ’special’ characters not otherwise permitted in HL7 messages. These sequences begin and end with the message’s Escape character (usually ‘\’), and contain an identifying character, followed by 0 or more characters. The most common use of HL7 escape sequences is to escape the HL7 defined delimiter characters.
Character Description Conversion
\Cxxyy\ Single-byte character set escape sequence with two hexadecimal values not converted
\E\ Escape character converted to escape character (e.g., ‘\’)
\F\ Field separator converted to field separator character (e.g., ‘|’)
\H\ Start highlighting not converted
\Mxxyyzz\ Multi-byte character set escape sequence with two or three hexadecimal values (zz is optional) not converted
\N\ Normal text (end highlighting) not converted
\R\ Repetition separator converted to repetition separator character (e.g., ‘~’)
\S\ Component separator converted to component separator character (e.g., ‘^’)
\T\ Subcomponent separator converted to subcomponent separator character (e.g., ‘&’)
\Xdd…\ Hexadecimal data (dd must be hexadecimal characters) converted to the characters identified by each pair of digits
\Zdd…\ Locally defined escape sequence not converted
If \ is part of your data, you need to escape it with \E\.
So your value:
"\home\one\two"
becomes
"\E\home\E\one\E\two"
About second issue:
In the HL7 message OBX-5-1 is printed as "\E\E\usne-server\E\Pathology\E\Quantum".
While reading the value, you have to reverse the process. That means, you should replace \E\ with \ back to get original value.
As #Amit Joshi mentioned, this has to do with HL7 escaping. You may want to try to change your escape character to one other than a backslash that is unlikely to appear in your message as your client appears to not be following it anyway.
This would be the 3rd character in MSH-2.

Special characters in properties file

In my Java/Spring web application i had problem printing special characters of italian language (ò,à,è etc.) that I retrieve from a properties file.
I found this article http://docs.oracle.com/cd/E26180_01/Platform.94/ATGProgGuide/html/s1816convertingpropertiesfilestoescap01.html.
But something is not clear: after I run the command written in the article, in my console (CMD console of windows) i can read my properties file "translated". After it, what should i do?
Should I copy the texts from the windows console and paste them into my properties file? It doens't seem a "professional" work!
There's no need in copying the output, you may just redirect it to the file:
native2ascii notTranslated.properties > translated.properties
Also, if you're building your project with Ant, you can use native2ascii ant task, for example:
<native2ascii src="srcdir" dest="srcdir" includes="**/*._properties" ext=".properties"/>
I assume here, that the initial non-ASCII properties files are named *._properties in your project.
The key contains all of the characters in the line starting with the first non-white space character and up to, but not including, the first unescaped '=', ':', or white space character other than a line terminator. All of these key termination characters may be included in the key by escaping them with a preceding backslash character; for example,
\:\=
would be the two-character key ":=". Line terminator characters can be included using \r and \n escape sequences. Any white space after the key is skipped; if the first non-white space character after the key is '=' or ':', then it is ignored and any white space characters after it are also skipped. All remaining characters on the line become part of the associated element string; if there are no remaining characters, the element is the empty string "". Once the raw character sequences constituting the key and element are identified, escape processing is performed as described above.
See this link to escape special char and see this link to read by different UTF.

Determine if there is/are escape character(s) in string

Let say I have
String str="hello\" world\\";
when printing str, the output is
hello" world\
even when printing str.length() the output is
13
Is there any way to prove that str value has escape character(s)?
There is no such thing as escape characters at run time.
Escape characters appear only in String literals. For example,
String literal = "Some\nEscape\rSequence\\\"";
At compilation time, the compiler produces a String value with their actual binary representation (UTF-8 iirc). The JVM uses that String value directly.
You wrote
I am thinking that whenever we print a string and the output contains
character such as " and \, then we can conclude that those character,
" and \ was escaped?
This is not true, those characters might have been read from a file or some other InputStream. They were definitely not escaped in a text file.
Yes.
Use the Apache Commons Library, specifically StringEscapeUtils#escapeJava.
jshell> StringEscapeUtils.escapeJava("Newline \n here \u0344 and unicode \f\n\r\t\"\0\13 and more")
$136 ==> "Newline \\n here \\u0344 and unicode \\f\\n\\r\\t\\\"\\u0000\\u000B and more"
This prepends a backslash to each escape sequence and also swaps the variable-width octal sequences for fixed-width Unicode sequences. This means that every escape sequence will consist of "\\" two backslashes, followed by one of {n, b, r, t, f, ", \}, or a 'u' character, plus exactly four hexadecimal [0-F] digits.
If you just want to know whether or not the original String contains escape sequences, search for "\\" in the Apache-fied string. If you want to find the positions of those sequences, it's a bit more involved.
See more at this Gist.

Java - Writing file location to Properties file

I'm writing the FILEPATH parameter into the properties file like below..
String newFilePath = txtFilepath.getText();
Properties prop = new Properties();
java.io.File propFile = new File("src/com/app/tool/properties/settings.properties");
prop.setProperty("FILEPATH", newFilePath);
System.out.println("newFilePath "+newFilePath);
try {
prop.store(new FileOutputStream(propFile), "New File location");
} catch (IOException ex) {
ex.printStackTrace();
}
The Filepath is D:\filelog.txt
While writing, the console prints like..
newFilePath D:\filelog.txt
But when I open the properties file, content is like..
FILEPATH=D\:\\filelog.txt.
Anyone suggest me , what I'm really m(i)essing here ..?
In a properties file, : has a meaning.
key = value
key: value
both are valid. Hence, it has to be escaped while writing to a properties file. Even \ needs escaping. But when you display them on the console, the escaping is identified, and the values are shown properly.
If you see the docs of store() method of the Properties class
Every entry in this Properties table is written out, one per line. For
each entry the key string is written, then an ASCII =, then the
associated element string. Each character of the key and element
strings is examined to see whether it should be rendered as an escape
sequence. The ASCII characters \, tab, form feed, newline, and
carriage return are written as \, \t, \f \n, and \r, respectively.
Characters less than \u0020 and characters greater than \u007E are
written as \uxxxx for the appropriate hexadecimal value xxxx. For the
key, all space characters are written with a preceding \ character.
For the element, leading space characters, but not embedded or
trailing space characters, are written with a preceding \ character.
The key and element characters #, !, =, and : are written with a
preceding backslash to ensure that they are properly loaded.
Note:- It shouldn't be a problem if you use Properties to read the properties file(it will handle the escaping), but if you read the properties file like a normal text file(you should never do it unless you're out of your mind), then it'll be a problem.
Just call prop.get("FILEPATH"). You will see that you get back "D:\filelog.txt", just as you put it in.
Backslash \ is an escape character that is silently dropped in not followed by another \
Colon : is a special character that must by escaped with \.
Check docs at
http://commons.apache.org/proper/commons-configuration/userguide/howto_properties.html

Storing date in .properties file java

I am trying to store a date in my config.properties file however the format is wrong.
try{
prop.setProperty("last_run_time",sdf.format(date));
prop.store(new FileOutputStream("config.properties"),null);
}
catch (Exception e){
e.printStackTrace();
}
The value of sdf.format(date)) is correct e.g. 2013-08-23 02:47 . Issue is that in the properties file 2013-08-23 02\:47 gets stored. Where does the '\' come from?
The \ unmask your :. Normaly the : is used to define a key with a value! You can read more about unmasking and the .properties file here.
This is from the Java Doc:
The key contains all of the characters in the line starting with the
first non-white space character and up to, but not including, the
first unescaped '=', ':', or white space character other than a line
terminator. All of these key termination characters may be included in
the key by escaping them with a preceding backslash character; for
example,
\:\=
would be the two-character key ":=". Line terminator characters can be
included using \r and \n escape sequences. Any white space after the
key is skipped; if the first non-white space character after the key
is '=' or ':', then it is ignored and any white space characters after
it are also skipped. All remaining characters on the line become part
of the associated element string; if there are no remaining
characters, the element is the empty string "". Once the raw character
sequences constituting the key and element are identified, escape
processing is performed as described above.
I think it is fine to save like \:
The Java property file is not a text for you to read. It is for the Java code to read. The escaping \ will ensure that the next time it is read by your Java app, it will be interpreted as a colon, not as a key/value separator.
The colon is one of the possible key/value separation characters. The leading backslash escapes it (this is only necessary when the key contains a colon, but you're more on the save side when always escaping it).
Variants of valid assignments:
key value
key= value
key: value
See Javadoc: Properties.load(Reader)

Categories

Resources