Solved

Urgent: Need to escape curly braces in property file.

Posted on 2004-04-20
9
5,701 Views
Last Modified: 2009-07-29
Prolly really easy, but it's urgent, hence the 500 points
I'm storing regular expressions in property files based on language/country pairs. I'm using the struts internationalisation and message-bundles, reading my property-files and exporting it into a javascript for use of validation.
I need to know how to escape the curly braces in such a way that when I use <bean:message ... /> to output it, the curly braces are used as output. A simple example:
inputpattern=^(\\w\\s?){2,25}$

Should output to: ^(\w\s?){2,25}$

Right now struts won't be able to output this because it expects arguments in the {0} style and finds unexpected characters inbetween the curly braces...
0
Comment
Question by:mreuring
  • 4
  • 4
9 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 10867894
Shouldn't it be:

inputpattern = "^(\\w\\s?){2,25}$" ;

?  I'm confused...  can you give a fuller example?
0
 
LVL 17

Author Comment

by:mreuring
ID: 10867902
My, current, inbetween workaround:
inputpattern=^(\\w\\s?){0}2,25{1}$

<bean:message arg0="{" arg1="}" key="inputpattern"/>

But I'm pretty damn sure there's an easier way to go about getting curly braces printed from a properties file...
0
 
LVL 7

Expert Comment

by:searlas
ID: 10867911
I'd be surprised if it was anything more complicated than:
inputpattern=^(\\w\\s?)\\{2,25}$
0
 
LVL 17

Author Comment

by:mreuring
ID: 10867936
In struts you can define a message bundle and use the associated .properties files in combination with the bean taglib to print internationalised messages. Since I'm building an internationalised form that has different validation rules per country/language I'm storing regular expressions in the property files and print them out in my internationalised javascript file (based on the current locale of the user).
Basic rules for a properties file:
property=value

And backslash being the escape character.
Struts adds to this that in the value of a property you can define arguments:
property=value{n}

Where n is an integer value for the argument. Problem is that a regular expression also uses curly braces and I now have to find a way to make struts ignore the curly braces entered into the property pair. And the escape char isn't enough, I tried:
inputpattern=^(\\w\\s?)\{2,25\}$
inputpattern=^(\\w\\s?)\\{2,25\\}$

Neither solution succesfully escapes the curly brace for use with the bean taglib.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:searlas
ID: 10867943
But, I am surprised...

From the MessageFormat docs:
Thus, a string that should result in the formatted message "'{0}'" can be written as "'''{'0}''" or "'''{0}'''".

So, you want:
inputpattern=^(\\w\\s?)'{'2,25}$
0
 
LVL 17

Author Comment

by:mreuring
ID: 10868038
Thanx for the quick suggestions!

I was getting my hopes up, but this only changes the error message:
java.lang.IllegalArgumentException: can't parse argument number ''1

Although I did combine your earlier suggestion with my current workaround, making it:
inputpattern=^(\\w\\s?){0}2,25}$

<bean:message arg0="{" key="inputpattern"/>

Still not verry elegant :(
0
 
LVL 7

Accepted Solution

by:
searlas earned 500 total points
ID: 10868828
mreuring,

Sadly, you seem to have found the best option IF you have to use the Struts tags.  Maybe you should consider a custom tag, or some other way of retrieving your regular expressions.

The problem is due to org.apache.struts.util.MessageResources.   The property values are passed to the MessageFormat constructor, so you (I) would expect to be able to quote the curlies by surrounding them in quotes.  However, just before it calls the MessageFormat constructor it preprocesses the property value and doubles up all the quotes:

    public String getMessage(Locale locale, String key, Object args[]) {
....
            format = new MessageFormat(escape(formatString));
....


    /**
     * Escape any single quote characters that are included in the specified
     * message string.
     *
     * @param string The string to be escaped
     */
    protected String escape(String string) {

        if ((string == null) || (string.indexOf('\'') < 0))
            return (string);
        int n = string.length();
        StringBuffer sb = new StringBuffer(n);
        for (int i = 0; i < n; i++) {
            char ch = string.charAt(i);
            if (ch == '\'')
                sb.append('\'');
            sb.append(ch);
        }
        return (sb.toString());

    }


So, basically, you cannot escape the {} processing, and what you have done is about as good as it gets...

0
 
LVL 17

Author Comment

by:mreuring
ID: 10869012
What the hell were they thinking?
Well, on basis of 'enlightening beyond the direct question' I most certainly find this worth a grade A :)
0
 
LVL 7

Expert Comment

by:searlas
ID: 10869343
Thanks!

I've found an entry in the struts/apache bug database, so you can vote for it or give feedback etc if you wish:
http://issues.apache.org/bugzilla/show_bug.cgi?id=21890
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
logging jar 1 106
nested if has else if 13 92
Getting NoSuchMethodError on accessing a spring mvc project 3 158
How to Post an If Statement in JSP 3 51
Scenario: Your operations manager has discovered an anomaly in your security system. The business will start to suffer within 15 minutes if it is a major IT incident. What should she do? We have 6 recommendations for managing major incidents (https:…
Note: This is the second blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   Every month t…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now