001    /*
002     * Copyright (c) 2005 Henri Sivonen
003     *
004     * Permission is hereby granted, free of charge, to any person obtaining a 
005     * copy of this software and associated documentation files (the "Software"), 
006     * to deal in the Software without restriction, including without limitation 
007     * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
008     * and/or sell copies of the Software, and to permit persons to whom the 
009     * Software is furnished to do so, subject to the following conditions:
010     *
011     * The above copyright notice and this permission notice shall be included in 
012     * all copies or substantial portions of the Software.
013     *
014     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
015     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
016     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
017     * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
018     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
019     * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
020     * DEALINGS IN THE SOFTWARE.
021     */
022    
023    package fi.iki.hsivonen.util;
024    
025    /**
026     * @version $Id: StringLiteralUtil.java,v 1.4 2006/11/18 00:05:24 hsivonen Exp $
027     * @author hsivonen
028     */
029    public class StringLiteralUtil {
030        public static String unquotedCharLiteral(char c) {
031            // http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#101089
032            switch (c) {
033                case '\b':
034                    return "\\b";
035                case '\t':
036                    return "\\t";
037                case '\n':
038                    return "\\n";
039                case '\f':
040                    return "\\f";
041                case '\r':
042                    return "\\r";
043                case '\"':
044                    return "\\\"";
045                case '\'':
046                    return "\\\'";
047                case '\\':
048                    return "\\\\";
049                default:
050                    if (c >= ' ' && c <= '~') {
051                        return "" + c;
052                    } else {
053                        String hex = Integer.toHexString((int) c);
054                        switch (hex.length()) {
055                            case 1:
056                                return "\\u000" + hex;
057                            case 2:
058                                return "\\u00" + hex;
059                            case 3:
060                                return "\\u0" + hex;
061                            default:
062                                return "\\u" + hex;
063                        }
064                    }
065            }
066        }
067    
068        public static String charLiteral(char c) {
069            return "\'" + unquotedCharLiteral(c) + "\'";
070        }
071    
072        public static String stringLiteral(CharSequence cs) {
073            if (cs == null) {
074                return "null";
075            }
076            StringBuilder sb = new StringBuilder();
077            sb.append('\"');
078            int len = cs.length();
079            for (int i = 0; i < len; i++) {
080                sb.append(unquotedCharLiteral(cs.charAt(i)));
081            }
082            sb.append('\"');
083            return sb.toString();
084        }
085    
086        public static String charArrayLiteral(CharSequence cs) {
087            if (cs == null) {
088                return "null";
089            }
090            StringBuilder sb = new StringBuilder();
091            sb.append('{');
092            int len = cs.length();
093            for (int i = 0; i < len; i++) {
094                sb.append(" \'");
095                sb.append(unquotedCharLiteral(cs.charAt(i)));
096                sb.append("\',");
097            }
098            if (sb.length() > 1) {
099                sb.deleteCharAt(sb.length() - 1);
100            }
101            sb.append(" }");
102            return sb.toString();
103        }
104    
105    }