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                            if ((i > 0) && Character.isUpperCase(c)) {
036                                    if (!upperCase ||
037                                            ((i < (s.length() - 1)) &&
038                                             !Character.isUpperCase(s.charAt(i + 1)))) {
039    
040                                            sb.append(delimiter);
041                                    }
042    
043                                    c = Character.toLowerCase(c);
044    
045                                    upperCase = true;
046                            }
047                            else {
048                                    upperCase = false;
049                            }
050    
051                            sb.append(c);
052                    }
053    
054                    return sb.toString();
055            }
056    
057            public static String normalizeCamelCase(String s) {
058                    return normalizeCamelCase(s, false);
059            }
060    
061            public static String normalizeCamelCase(
062                    String s, boolean normalizeInnerTerms) {
063    
064                    StringBuilder sb = new StringBuilder(s);
065    
066                    boolean upperCase = false;
067    
068                    for (int i = 0; i < s.length(); i++) {
069                            char c = s.charAt(i);
070    
071                            if (!normalizeInnerTerms && (c == CharPool.PERIOD)) {
072                                    return sb.toString();
073                            }
074    
075                            if ((i > 0) && Character.isUpperCase(c)) {
076                                    boolean nextUpperCase = true;
077    
078                                    if (i < (s.length() - 1)) {
079                                            char nextChar = s.charAt(i + 1);
080    
081                                            if ((nextChar != CharPool.PERIOD) &&
082                                                    !Character.isUpperCase(nextChar)) {
083    
084                                                    nextUpperCase = false;
085                                            }
086                                    }
087    
088                                    if (upperCase && nextUpperCase) {
089                                            sb.setCharAt(i, Character.toLowerCase(c));
090                                    }
091    
092                                    upperCase = true;
093                            }
094                            else {
095                                    upperCase = false;
096                            }
097                    }
098    
099                    return sb.toString();
100            }
101    
102            public static String toCamelCase(String s) {
103                    return toCamelCase(s, CharPool.DASH);
104            }
105    
106            public static String toCamelCase(String s, char delimiter) {
107                    StringBuilder sb = new StringBuilder(s.length());
108    
109                    boolean upperCase = false;
110    
111                    for (int i = 0; i < s.length(); i++) {
112                            char c = s.charAt(i);
113    
114                            if (c == delimiter) {
115                                    upperCase = true;
116                            }
117                            else if (upperCase) {
118                                    sb.append(Character.toUpperCase(c));
119    
120                                    upperCase = false;
121                            }
122                            else {
123                                    sb.append(c);
124                            }
125                    }
126    
127                    return sb.toString();
128            }
129    
130    }