[Jukebox-cvs] CVS update: J4/src/java/gnu/j4/config PropertiesReader.java
Brought to you by:
vtt
From: CVS B. <vt...@fr...> - 2000-09-14 22:40:49
|
User: vt Date: 00/09/14 15:40:27 Modified: src/java/gnu/j4/config PropertiesReader.java Log: Now the keys and values may contain the double quotes, and the double quotes will be handled properly (Why? Think of value consisting of 8 spaces with a dot in the middle) Revision Changes Path 1.6 +79 -2 J4/src/java/gnu/j4/config/PropertiesReader.java CVSWEB Options: ------------------- CVSWeb: Annotate this file: http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/J4/src/java/gnu/j4/config/PropertiesReader.java?annotate=1.6&cvsroot=jukebox4 CVSWeb: View this file: http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/J4/src/java/gnu/j4/config/PropertiesReader.java?rev=1.6&content-type=text/x-cvsweb-markup&cvsroot=jukebox4 CVSWeb: Diff to previous version: http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/J4/src/java/gnu/j4/config/PropertiesReader.java.diff?r1=1.6&r2=1.5&cvsroot=jukebox4 ----------------------------------- Index: PropertiesReader.java =================================================================== RCS file: /usr/local/cvs/J4/src/java/gnu/j4/config/PropertiesReader.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- PropertiesReader.java 1999/12/08 03:25:58 1.5 +++ PropertiesReader.java 2000/09/14 22:40:27 1.6 @@ -14,7 +14,7 @@ * Project</a> in general and code written by <a * href="mailto:maz...@sy...">Stefano Mazzocchi</a> in particular. * - * @version $Id: PropertiesReader.java,v 1.5 1999/12/08 03:25:58 vt Exp $ + * @version $Id: PropertiesReader.java,v 1.6 2000/09/14 22:40:27 vt Exp $ */ public class PropertiesReader extends ConfigReader { @@ -83,6 +83,83 @@ String key = line.substring( 0,idx ).trim(), value = line.substring( idx+1 ).trim(); - target.put( key,value ); + target.put(unescape(key), unescape(value)); + } + + /** + * Get rid of the quotes, unless they're escaped. + * + * @param source Original string, possibly escaped. + * + * @return The original string stripped from the quotes. + * + * @exception IOException if the double quote is not balanced. + */ + protected String unescape(String source) throws IOException { + + System.err.println("Source: '" + source + "'"); + + StringBuffer target = new StringBuffer(); + + // True if the current character is within the double quote + + boolean inQuote = false; + + // True if the previous character is a backslash (escape) + + boolean escaped = false; + + for ( int idx = 0; idx < source.length(); idx++ ) { + + char ch = source.charAt(idx); + + if ( ch == '"' ) { + + if ( escaped ) { + + // This double quote was escaped + + target.append(ch); + escaped = false; + + } else { + + // This double quote ends the escape + + escaped = false; + } + + } else { + + // Escape is good for one symbol only. If some other character + // except a double quote was escaped, we don't care and pass the + // escape on. In any case, escape flag is cleared. + + if ( escaped ) { + + target.append('\\'); + target.append(ch); + escaped = false; + + } else if ( ch == '\\' ) { + + escaped = true; + + } else { + + target.append(ch); + escaped = false; + } + } + } + + if ( escaped ) { + + throw new IOException("Unbalanced double quote: '" + source + "'"); + } + + String result = target.toString(); + System.out.println("Target: '" + result + "'"); + return result; } } |