0

I have a simple GUI that pops up and asks the user to enter a couple of fields. One of the fields is for a configuration path. I take what the user entered in the GUI (a JTextField), save that to a String and use the Apache Commons Configuration library (I'm using PropertiesConfiguration.setProperty() ) to update a .properties file based on what the user entered. The problem is this is not working due to how the characters are escaped. If the user enters in:

\:cust\:authprocessor

Then I want that exact string to be updated in the properties file so that it looks like this:

path = \:cust\:authprocessor

Instead, it looks like this:

path = \\:cust\\:authprocessor

I've tried using String.replace(), but that does not work since they are escaped. Any ideas on how to handle?

Trinculo
  • 1,950
  • 1
  • 17
  • 22
  • 1
    I think in file `\ ` will be displayed as `\\ ` but while reading in (java) program it'll be `\ `. can you please try with `/` ? – A Stranger Jul 11 '14 at 07:05
  • I'll keep that in mind for sure, but the user is used to entering it in this wayalready: "\:cust\:authprocessor" so even if that worked, it would be hard to get a bunch of people to change how they are used to entering when this is this application is supposed to make their lives easier. Is there any workaround where I can easily modify after it is inserted or anything? – Trinculo Jul 11 '14 at 07:28
  • Did you check [this](http://stackoverflow.com/questions/3537706/howto-unescape-a-java-string-literal-in-java) ? – A Stranger Jul 11 '14 at 08:31
  • @A Stranger - That did not work. If I pass in the String "\\:cust\\:authprocessor", it returns "\\:cust\\:authprocessor". Same thing. Using the Apache Commons one in the same thread results in ":cust:authprocessor". I cannot find a way to get a single backslash from the GUI and save it a property. – Trinculo Jul 11 '14 at 14:49

1 Answers1

1

That is not possible. \ is a special character in properties. If you strore these properties they will be escaped.

Here you can see the source code of java.util.properties

private String saveConvert(String theString,
                           boolean escapeSpace,
                           boolean escapeUnicode) {
    int len = theString.length();
    int bufLen = len * 2;
    if (bufLen < 0) {
        bufLen = Integer.MAX_VALUE;
    }
    StringBuffer outBuffer = new StringBuffer(bufLen);

    for(int x=0; x<len; x++) {
        char aChar = theString.charAt(x);
        // Handle common case first, selecting largest block that
        // avoids the specials below
        if ((aChar > 61) && (aChar < 127)) {
            if (aChar == '\\') {
                outBuffer.append('\\'); outBuffer.append('\\');
                continue;
            }
            outBuffer.append(aChar);
            continue;
        }
        switch(aChar) {
            case ' ':
                if (x == 0 || escapeSpace)
                    outBuffer.append('\\');
                outBuffer.append(' ');
                break;
            case '\t':outBuffer.append('\\'); outBuffer.append('t');
                      break;
            case '\n':outBuffer.append('\\'); outBuffer.append('n');
                      break;
            case '\r':outBuffer.append('\\'); outBuffer.append('r');
                      break;
            case '\f':outBuffer.append('\\'); outBuffer.append('f');
                      break;
            case '=': // Fall through
            case ':': // Fall through
            case '#': // Fall through
            case '!':
                outBuffer.append('\\'); outBuffer.append(aChar);
                break;
            default:
                if (((aChar < 0x0020) || (aChar > 0x007e)) & escapeUnicode ) {
                    outBuffer.append('\\');
                    outBuffer.append('u');
                    outBuffer.append(toHex((aChar >> 12) & 0xF));
                    outBuffer.append(toHex((aChar >>  8) & 0xF));
                    outBuffer.append(toHex((aChar >>  4) & 0xF));
                    outBuffer.append(toHex( aChar        & 0xF));
                } else {
                    outBuffer.append(aChar);
                }
        }
    }
    return outBuffer.toString();
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Jens
  • 67,715
  • 15
  • 98
  • 113
  • Are you suggested to extend the class and change how the escaping rules are implemented?...or just showing why it's not possible :-) – Trinculo Jul 11 '14 at 07:29
  • @Trinculo I will show you that it is not possible with the standard implementation of `java.util.Properties`. – Jens Jul 11 '14 at 08:09