001    /**
002     * Copyright (c) 2000-2013 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    
070                            if (i < (s.length() - 1)) {
071                                    nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
072                            }
073    
074                            if ((i > 0) && Character.isUpperCase(c)) {
075                                    if (upperCase && nextUpperCase) {
076                                            c = Character.toLowerCase(c);
077                                    }
078    
079                                    upperCase = true;
080                            }
081                            else {
082                                    upperCase = false;
083                            }
084    
085                            sb.append(c);
086                    }
087    
088                    return sb.toString();
089            }
090    
091            public static String toCamelCase(String s) {
092                    return toCamelCase(s, CharPool.DASH);
093            }
094    
095            public static String toCamelCase(String s, char delimiter) {
096                    StringBuilder sb = new StringBuilder(s.length());
097    
098                    boolean upperCase = false;
099    
100                    for (int i = 0; i < s.length(); i++) {
101                            char c = s.charAt(i);
102    
103                            if (c == delimiter) {
104                                    upperCase = true;
105                            }
106                            else if (upperCase) {
107                                    sb.append(Character.toUpperCase(c));
108    
109                                    upperCase = false;
110                            }
111                            else {
112                                    sb.append(c);
113                            }
114                    }
115    
116                    return sb.toString();
117            }
118    
119    }