alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.properties;

/**
 * Contains conversion utilities which allow reading and storing a properties file 
 * while preserving formatting and comments that user may have entered.
 * 

* Huge portions of this class marked by // prj40 trunk compatability * are there only for being able to run unmodified i18n module code in * prj40_prototype branch and trunk. Properties module is not * prj40 branched and it is updated by cvs up -f -rprj40_prototype. *
* 28th March 2003 Petr Kuzel * * @author Petr Jiricka * @author Petr Kuzel - simplification */ public class UtilConvert { private UtilConvert() { } /** * These characters play role as key-value sepsrators */ public static final String keyValueSeparators = "=: \t\r\n\f"; public static final String strictKeyValueSeparators = "=:"; /** Differs from JDK's implementation in that it does not save ' ' as '\ '. */ private static final String specialSaveChars = "=:\t\r\n\f#!"; public static final String whiteSpaceChars = " \t\r\n\f"; /** * Escape key value. Converts string to one with escaped ' ','=',':', and last '\\' * in case they are not escaped already. Used for formating user input. * //!!! wrong semantics what does it mean if not escaped already? */ public static String escapePropertiesSpecialChars (String source) { return source; // prj40 trunk compatability // if(source == null) return null; // StringBuffer result = new StringBuffer(); // for(int i=0; i0 && source.charAt(i-1) != '\\')) // result.append('\\'); // } // // last char == '\\' // if(i==source.length()-1 && x == '\\') { // if( i>0 && source.charAt(i-1)!='\\') // result.append('\\'); // } // result.append(x); // } // return result.toString(); } /** Checks whether the string contains only spaces */ private static boolean onlySpaces(String s){ for (int i = 0; i1 && source.charAt(source.length()-2)!='\\') // return new String(new StringBuffer(source).append('\\')); // } // return source; } /** * Converts these java special chars ('\t', '\n', '\b', '\r', '\f') to encoded escapes. * Note there are not converted unicode chars. */ public static String escapeJavaSpecialChars(String source) { return source; // prj40 trunk compatability // if(source == null) return null; // StringBuffer result = new StringBuffer(); // for (int i=0; i interpret as a normal char // } // } else // outBuffer.append(aChar); // } // return outBuffer.toString(); } /** * Convert chars to encoded '\\uxxxx' using comment escaping rules. * @param commentString * @return escaped comment */ public static String escapeComment(String commentString) { return charsToUnicodes(commentString, true); } /** * Converts chars to encoded '\\uxxxx'. * Note there are not converted '\\"', '\\'', '\\ ', '\\\\' and java special chars escapes. */ public static String charsToUnicodes(String s){ return charsToUnicodes(s, false); } /** * Converts chars to encoded '\\uxxxx'. If skipWhiteSpaces is true, then white spaces won't be converted * Note there are not converted '\\"', '\\'', '\\ ', '\\\\' and java special chars escapes. */ public static String charsToUnicodes(String theString, boolean skipWhiteSpaces) { return theString; // prj40 trunk compatability // if(theString == null) return null; // char aChar; // int len = theString.length(); // StringBuffer outBuffer = new StringBuffer(len*2); // // for(int x=0; x 127) ) { // // if (skipWhiteSpaces && Character.isWhitespace(aChar)){ // // do not convert white spaces // outBuffer.append(aChar); // continue; // } // // 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 >> 0) & 0xF)); // } else { // outBuffer.append(aChar); // } // } // return outBuffer.toString(); } /** * Converts encoded \\uxxxx to unicode chars * and changes special saved chars to their original forms */ public static String loadConvert (String theString) { char aChar; final int len = theString.length(); StringBuffer outBuffer = new StringBuffer(len); main: for(int x=0; x len - 4) { outBuffer.append('\\').append('u'); continue main; } // Read the xxxx int value=0; for (int i=0; i<4; i++) { aChar = theString.charAt(x++); switch (aChar) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': value = (value << 4) + aChar - '0'; break; case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': value = (value << 4) + 10 + aChar - 'a'; break; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': value = (value << 4) + 10 + aChar - 'A'; break; default: /* * Handle a malformed \\uxxxx encoding: * * We want to print "\\u" plus all the hexadecimal * digits that passed the above switch. * To achieve it, print ("\\u")..., */ outBuffer.append('\\').append('u'); /* ... move 'x' back to character after "u"... */ x -= i + 1; /* ... and continue with the main loop. */ continue main; } } outBuffer.append((char)value); } else { if (aChar == 't') aChar = '\t'; else if (aChar == 'r') aChar = '\r'; else if (aChar == 'n') aChar = '\n'; else if (aChar == 'f') aChar = '\f'; outBuffer.append(aChar); } } else outBuffer.append(aChar); } return outBuffer.toString(); } /** * Converts unicodes to encoded \\uxxxx * and writes out any of the characters in specialSaveChars * with a preceding slash. * Differs from Sun's implementation in that it does not save ' ' as '\ '. */ public static String saveConvert(String theString) { return saveConvert(theString, false); } public static String saveConvert(String theString, boolean escapeSpace) { char aChar; int len = theString.length(); StringBuffer outBuffer = new StringBuffer(len*2); for(int x=0; x 127)) { 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 >> 0) & 0xF)); } else if (escapeSpace && (aChar == ' ')) { outBuffer.append('\\').append(' '); } else { if (specialSaveChars.indexOf(aChar) != -1) outBuffer.append('\\'); outBuffer.append(aChar); } } } return outBuffer.toString(); } /** * Convert a nibble to a hex character * @param nibble the nibble to convert. */ private static char toHex(int nibble) { return hexDigit[(nibble & 0xF)]; } /** A table of hex digits */ private static final char[] hexDigit = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }; }

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.