Trinculo
Trinculo

Reputation: 2031

Pass user entered path into a .properties file using Apache Commons Configuration

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?

Upvotes: 0

Views: 197

Answers (1)

Jens
Jens

Reputation: 69470

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();
}

Upvotes: 1

Related Questions