001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    /**
018     * @author Igor Spasic
019     * @author Eduardo Lundgren
020     */
021    public class CamelCaseUtil {
022    
023            public static String fromCamelCase(String s) {
024                    return fromCamelCase(s, CharPool.DASH);
025            }
026    
027            public static String fromCamelCase(String s, char delimiter) {
028                    StringBuilder sb = new StringBuilder();
029    
030                    boolean upperCase = false;
031    
032                    for (int i = 0; i < s.length(); i++) {
033                            char c = s.charAt(i);
034    
035                            boolean nextUpperCase = true;
036    
037                            if (i < (s.length() - 1)) {
038                                    nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
039                            }
040    
041                            if ((i > 0) && Character.isUpperCase(c)) {
042                                    if (!upperCase || !nextUpperCase) {
043                                            sb.append(delimiter);
044                                    }
045    
046                                    c = Character.toLowerCase(c);
047    
048                                    upperCase = true;
049                            }
050                            else {
051                                    upperCase = false;
052                            }
053    
054                            sb.append(c);
055                    }
056    
057                    return sb.toString();
058            }
059    
060            public static String normalizeCamelCase(String s) {
061                    StringBuilder sb = new StringBuilder();
062    
063                    boolean upperCase = false;
064    
065                    for (int i = 0; i < s.length(); i++) {
066                            char c = s.charAt(i);
067    
068                            boolean nextUpperCase = true;
069                            if (i < (s.length() - 1)) {
070                                    nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
071                            }
072    
073                            if ((i > 0) && Character.isUpperCase(c)) {
074                                    if (upperCase && nextUpperCase) {
075                                            c = Character.toLowerCase(c);
076                                    }
077    
078                                    upperCase = true;
079                            }
080                            else {
081                                    upperCase = false;
082                            }
083    
084                            sb.append(c);
085                    }
086    
087                    return sb.toString();
088            }
089    
090            public static String toCamelCase(String s) {
091                    return toCamelCase(s, CharPool.DASH);
092            }
093    
094            public static String toCamelCase(String s, char delimiter) {
095                    StringBuilder sb = new StringBuilder(s.length());
096    
097                    boolean upperCase = false;
098    
099                    for (int i = 0; i < s.length(); i++) {
100                            char c = s.charAt(i);
101    
102                            if (c == delimiter) {
103                                    upperCase = true;
104                            }
105                            else if (upperCase) {
106                                    sb.append(Character.toUpperCase(c));
107    
108                                    upperCase = false;
109                            }
110                            else {
111                                    sb.append(c);
112                            }
113                    }
114    
115                    return sb.toString();
116            }
117    
118    }