Escaping a json string inside a properties file - java

I use typical properties file in a java environment. I want one of the properties to be a json array:
#Country list in json by language
countries = [{"Name":"America"},{"Name":"Germany"},...]
This gives a number format exception in java when trying to read the string mapped to countries.
I tried a bunch of escaping sequences, but none seem to work:
countries = [{\"Name\":\"America\"},{\"Name\":\"Germany\"},...]
countries = [{\\"Name\\":\\"America\\"},{\\"Name\\":\\"Germany\\"},...]
countries = [{''Name'':''America''},{''Name'':''Germany''},...]
I'm wondering why a number formatexception is thrown, considering this is a string? Also, what is wrong with a json string that makes the file flip? Is it the [, {, " or : character(s)?
EDIT:
Her's the actual json in my properties file:
countries_json = [{"Name":"Afghanistan","Code":"AF","TelephoneCode":"+93"},{"Name":"Belgium","Code":"BE","TelephoneCode":"+32"}]
Here's the code in my jsp page which gets this value:
<input type="hidden" id="countryListJSON" value='<s:text name="countries_json"/>'/>
And here's the exception that happens on the java backend when the jsp is being rendered:
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.NumberFormatException: For input string: ""Name":"Afghanistan""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.text.MessageFormat.makeFormat(Unknown Source)
at java.text.MessageFormat.applyPattern(Unknown Source)
at java.text.MessageFormat.<init>(Unknown Source)
at com.opensymphony.xwork2.util.LocalizedTextUtil.buildMessageFormat(LocalizedTextUtil.java:704)
at com.opensymphony.xwork2.util.LocalizedTextUtil.getDefaultMessage(LocalizedTextUtil.java:663)
at com.opensymphony.xwork2.util.LocalizedTextUtil.findText(LocalizedTextUtil.java:534)
at com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.java:259)
at com.opensymphony.xwork2.ActionSupport.getText(ActionSupport.java:131)
at org.apache.struts2.util.TextProviderHelper.getText(TextProviderHelper.java:75)
at org.apache.struts2.components.Text.end(Text.java:160)
at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
at org.apache.jsp.shared.jsp.RegisterForm_jsp._jspx_meth_s_005ftext_005f14(RegisterForm_jsp.java:874)
at org.apache.jsp.shared.jsp.RegisterForm_jsp._jspService(RegisterForm_jsp.java:177)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
...

The struts tag might be treating it as a message argument. So its expecting a {0}, {1} etc. Give escape quotes for { in your property file like countries = ['{'"Name":"America"'}','{'"Name":"Germany"'}',...]

Related

Cannot parse "1,000.00" String to Number

I have several labels displaying numeric values and I need to parse these texts to number. The problem is that when the value is greater than 999, the parse method fails throwing the following exception:
Exception in thread "AWT-EventQueue-0"
java.lang.NumberFormatException: For input string: "1,000.00" at
java.lang.NumberFormatException.forInputString(Unknown Source)
I tried several parse methods like Double.valueOf(string), new BigDecimal(string), new BigInteger(string) and so on...but the exception is always thrown.
I guess you are using a French numbers.
You can add a Locale to NumberFormat and parse in the documentation :
NumberFormat.getNumberInstance(Locale.FRANCE).parse("1,000")
Remove the comma before parsing like this:
double d = Double.parseDouble(string.replace(",", ""));

Is it possible to use print Strings and format doubles in the same printf statement? IllegalFormatConversionException

I want to print a sentence with formatted doubles without having to use several print statements. These are a couple of the statement I tried to run:
System.out.printf("( %.2f", real + ", %.2f", imaginary + ")");
System.out.printf("(" + "%.2f",real + ", " + "%.2f",imaginary + ")");
However, this is the error I get
java.util.IllegalFormatConversionException: f != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printFloat(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at ComplexNumber.print(ComplexNumber.java:17)
at ComplexClient.main(ComplexClient.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
Am I just using the printf statement incorrectly? Or is what I'm trying to do not possible? Thanks
You don't mix in the variables within the construction of your format string. You should create your format string as the first parameter, with all variables passed in afterwards. The number of variables passed in must match the number of placeholders in the format string, and they must match the formats specified by the placeholders.
Try:
// <- format ->
System.out.printf("( %.2f, %.2f)", real, imaginary);
// ... variables afterwards here
This matches the printf method signature, which takes in the format String first, followed by any number of Object variable parameters.

jdbc-hsqldb exception for text table

My java code is :
conn = DriverManager.getConnection(url, "user", "password"); // line 1
stm = conn.createStatement(); // line 2
stm.execute("CREATE TEXT TABLE someTableName("NLID" VARCHAR(20),
"Scheduled.Primary.Scripting.Code" VARCHAR(20), "Scheduled.Site" VARCHAR(20),
"Scheduled.Location.Long.Name" VARCHAR(20),
"primary_key_1644" int PRIMARY KEY)"); // line 3
stm.execute("SET TABLE someTableName SOURCE
"/some.csv;ignore_first=true;all_quoted=true;shutdown=true""); // line 4
The CSV which is being linked to the hsqldb is :
NLID,Scheduled.Primary.Scripting.Code,Scheduled.Site,Scheduled.Location.Long.Name,primary_key_1644
100,INMRSB,Shopping,Shopping General-Banner-728x90-INMRSB-I,1
100002,MSVT08,MSN Video,msnbc.com-TODAYshow.com Special Sponsorships 8-Streaming Media-300x60-MSVT08-S,2
100004,MSV10T,MSN Video,msnbc.com-TODAYshow.com Special Sponsorships 10-Streaming Media-300x60-MSV10T-S,3
I get the below exception in line 4 :
java.sql.SQLException: bad TEXT table source file - line number: 1196 java.lang.NumberFormatException: For input string: "Body Connection-Banner-728x90-HEAMBA-S" in statement [SET TABLE someTableName SOURCE "/some.csv;ignore_first=true;all_quoted=true;shutdown=true"]
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
at org.hsqldb.jdbc.JDBCStatement.execute(Unknown Source)
at java.lang.NumberFormatException: For input string: "Body Connection-Banner-728x90-HEAMBA-S"
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.TextTable.connect(Unknown Source)
at org.hsqldb.TextTable.openCache(Unknown Source)
at org.hsqldb.TextTable.setDataSource(Unknown Source)
at org.hsqldb.StatementCommand.getResult(Unknown Source)
at org.hsqldb.StatementCommand.execute(Unknown Source)
at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
at org.hsqldb.Session.executeDirectStatement(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 6 more
Caused by: java.lang.NumberFormatException: For input string: "Body Connection-Banner-728x90-HEAMBA-S"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at org.hsqldb.rowio.RowInputText.readInteger(Unknown Source)
at org.hsqldb.rowio.RowInputBase.readData(Unknown Source)
at org.hsqldb.rowio.RowInputText.readData(Unknown Source)
at org.hsqldb.rowio.RowInputBase.readData(Unknown Source)
at org.hsqldb.rowio.RowInputText.readData(Unknown Source)
at org.hsqldb.RowAVLDiskData.getRowData(Unknown Source)
at org.hsqldb.persist.RowStoreAVLDiskData.get(Unknown Source)
... 14 more
Any help will be appreciable .
EDITED : Hsqldb throws NumberFormatException while reading a column which is defined as VARCHAR !!!
MODIFIED : The exception occurs because one of the column values has a comma in it like "Mind, Body Connection-Banner-728x90-HEAMBA-S" . Since the hsqldb reads the csv as a table so an additional comma is interpreted as an additional column by the hsql . Can anyone guide me how to circumvent this ?
As the OP found out, following these simple steps help find the problem in the CSV file:
Look at the error message: bad TEXT table source file - line number: 1196 The line number is the line number of the CSV file. Using a text editor, go to that line. Lines are numbered from 1.
ava.lang.NumberFormatException: For input string: "Body Connection-Banner-728x90-HEAMBA-S" note NumberFormatException indicates a number was expected, instead the reported string was found.
Luckily, there is only one number (INT) column in the table, so it's easy to see there is an extra comma in the CVS, which terminates the last VARCHAR string and causes the problem.
The TEXT table definition includes all_quoted=true, which means if you use double quotes around the string that contains the comma, the whole string is treated as a field and the problem disappears.

java.util.UnknownFormatConversionException:

System.out.printf("%s%13s%\n", "TarrifType", "AnnualCost");
System.out.printf("%s%d.%n", "String" 243.08);
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '
at java.util.Formatter.checkText(Unknown Source)
at java.util.Formatter.parse(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at ModelComparison.main(ModelComparison.java:12)
Any idea whats wrong?
What's wrong is the %\n in the first line. Note that the % is a special character in a format string that indicates that a format specifier follows. The \n after the % is not a valid format specifier.
If you wanted to print a percent sign, then double it in the format string: %%
If you wanted to print a newline, then use %n, not %\n.
The problem in your format string is that you mixed two ways of doing newline: %n and \n. The former tells the formatter to put a newline in whatever format the platform requires, whereas the latter puts in just a literal newline char. But what you wrote was %\n, which means you're escaping the newline char, and that's what's blowing up.
You also forgot a comma between "String" and 243.08 in the second call. And btw, %d formats an integer, so you probably don't want it if you're trying to print 243.08.
Bugs..
System.out.printf("%s%13s\n", "TarrifType", "AnnualCost");
System.out.printf("%s%f\n", "String", 243.08);
http://ideone.com/USOx1
In my case (Android) there was an error in strings.xml:
<string name="cashback">10% cashback</string>
When I tried to get getString(R.string.cashback), I received this exception. To fix it, replace % with %%.
If a string desired to format in Kotlin, the following code snippet can be used.
In the string.xml:
<string name = "exampleString"><![CDATA[<font color=#3177a3> String_1: <b>%%s</b><br> String_2: <b>%%s</b><br> String_3: <b>%%s</b></font>]]></string>
In the main code:
val format = String.format(getString(R.string.exampleString),
value1,
value2,
value3
)

Velocity - Correct Regex to remove control characters?

I'm trying to remove undesirable characters from a string in Velocity (newlines are ok, but not things like EM and CAN ASCII control characters).
#set($cleanScreen = $cleanScreen.replaceAll("\p{Cc}", ""))
Throws:
org.apache.velocity.exception.ParseErrorException: Lexical error: org.apache.velocity.runtime.parser.TokenMgrError: Lexical error at line 13, column 82. Encountered: "p" (112), after : "\"\\"
at org.apache.velocity.Template.process(Template.java:137)
at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:415)
at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:335)
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1102)
at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:1077)
at org.apache.velocity.runtime.RuntimeSingleton.getTemplate(RuntimeSingleton.java:303)
at org.apache.velocity.app.Velocity.getTemplate(Velocity.java:503)
and
#set($cleanScreen = $cleanScreen.replaceAll("[[:cntrl:]]", ""))
This one doesn't thrown an exception, instead, it matches the characters c,n,t,r,l and removes them from the string.
and...
#set($cleanScreen = $cleanScreen.replaceAll("\\p{Cntrl}", ""))
Throws:
java.util.regex.PatternSyntaxException: Illegal repetition near index 2
\\p{Cntrl}
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.closure(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.replaceAll(Unknown Source)
at sun.reflect.GeneratedMethodAccessor168.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.invoke(UberspectImpl.java:295)
at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:245)
I've tried several regex's (many seem to work in Java, but not VTL)? My key issue seems to be how things differ in their escaping between Java and Velocity?
Can anyone help? I only have access the the VTL, not the Java class.
I can't comment on the actual regexp.
On the velocity side however, I find that...
#set($cleanScreen = $cleanScreen.replaceAll("\p{Cc}", ""))
#set($cleanScreen = $cleanScreen.replaceAll("[[:cntrl:]]", ""))
...these two are correct as they are. I have a little vtl shell into which I just copy pasted your vtl code. Are you really getting these errors with the first two expressions? How about using '\p{Cc}'?
#set($cleanScreen = $cleanScreen.replaceAll("\\p{Cntrl}", ""))
The '\\p' gets you into trouble.
On a side note, you can use http://velocity.apache.org/tools/devel/generic/EscapeTool.html for all your escaping needs.
Those Velocity Parser Exceptions, might come from the double-quotes characters.
I had similar problem in VTL when trying to String.replaceAll regular expression with a capturing group, like so:
#set( $Jira_links = $Jira_tickets.replaceAll("(CT-\d+)", "http://jira.site.com/browse/$1") )
Throws:
org.apache.velocity.exception.ParseErrorException: Lexical error: org.apache.velocity.runtime.parser.TokenMgrError: Lexical error at line 2, column 58. Encountered: "d" (100), after : "\" (CT-\"
Changing it into single quotes worked:
#set( $Jira_links = $Jira_tickets.replaceAll('(CT-\d+)', 'http://jira.site.com/browse/$1') )

Categories