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.jsonwebservice;
016    
017    import com.liferay.portal.kernel.util.CharPool;
018    
019    /**
020     * @author Igor Spasic
021     */
022    public class CamelCaseUtil {
023    
024            public static String fromCamelCase(String s) {
025                    StringBuilder sb = new StringBuilder();
026    
027                    boolean upperCase = false;
028    
029                    for (int i = 0; i < s.length(); i++) {
030                            char c = s.charAt(i);
031    
032                            boolean nextUpperCase = true;
033    
034                            if (i < (s.length() - 1)) {
035                                    nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
036                            }
037    
038                            if ((i > 0) && Character.isUpperCase(c)) {
039                                    if (!upperCase || !nextUpperCase) {
040                                            sb.append(CharPool.DASH);
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                    StringBuilder sb = new StringBuilder();
059    
060                    boolean upperCase = false;
061    
062                    for (int i = 0; i < s.length(); i++) {
063                            char c = s.charAt(i);
064    
065                            boolean nextUpperCase = true;
066                            if (i < (s.length() - 1)) {
067                                    nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
068                            }
069    
070                            if ((i > 0) && Character.isUpperCase(c)) {
071                                    if (upperCase && nextUpperCase) {
072                                            c = Character.toLowerCase(c);
073                                    }
074    
075                                    upperCase = true;
076                            }
077                            else {
078                                    upperCase = false;
079                            }
080    
081                            sb.append(c);
082                    }
083    
084                    return sb.toString();
085            }
086    
087            public static String toCamelCase(String s) {
088                    StringBuilder sb = new StringBuilder(s.length());
089    
090                    boolean upperCase = false;
091    
092                    for (int i = 0; i < s.length(); i++) {
093                            char c = s.charAt(i);
094    
095                            if (c == CharPool.DASH) {
096                                    upperCase = true;
097                            }
098                            else if (upperCase) {
099                                    sb.append(Character.toUpperCase(c));
100    
101                                    upperCase = false;
102                            }
103                            else {
104                                    sb.append(c);
105                            }
106                    }
107    
108                    return sb.toString();
109            }
110    
111    }