001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.intergral.fusiondebug.server.util;
020
021
022
023
024
025
026public class FDUtil {
027        
028        /**
029         * replace the last occurrence of from with to
030         * @param str
031         * @param from
032         * @param to
033         * @return changed string
034         */
035        private static String replaceLast(String str, char from, char to) {
036                int index = str.lastIndexOf(from);
037                if(index==-1)return str;
038                return str.substring(0,index)+to+str.substring(index+1);
039        }
040        
041        /**
042         * if given string is a keyword it will be replaced with none keyword
043         * @param str
044         * @return corrected word
045         */
046        private static String correctReservedWord(String str) {
047                char first=str.charAt(0);
048                
049                switch(first) {
050                case 'a':
051                        if(str.equals("abstract")) return "_"+str;
052                break;
053                case 'b':
054                        if(str.equals("boolean")) return "_"+str;
055                        else if(str.equals("break")) return "_"+str;
056                        else if(str.equals("byte")) return "_"+str;
057                break;
058                case 'c':
059                        if(str.equals("case")) return "_"+str;
060                        else if(str.equals("catch")) return "_"+str;
061                        else if(str.equals("char")) return "_"+str;
062                        else if(str.equals("const")) return "_"+str;
063                        else if(str.equals("class")) return "_"+str;
064                        else if(str.equals("continue")) return "_"+str;
065                break;
066                case 'd':
067                        if(str.equals("default")) return "_"+str;
068                        else if(str.equals("do")) return "_"+str;
069                        else if(str.equals("double")) return "_"+str;
070                break;
071                case 'e':
072                        if(str.equals("else")) return "_"+str;
073                        else if(str.equals("extends")) return "_"+str;
074                        else if(str.equals("enum")) return "_"+str;
075                break;
076                case 'f':
077                        if(str.equals("false")) return "_"+str;
078                        else if(str.equals("final")) return "_"+str;
079                        else if(str.equals("finally")) return "_"+str;
080                        else if(str.equals("float")) return "_"+str;
081                        else if(str.equals("for")) return "_"+str;
082                break;
083                case 'g':
084                        if(str.equals("goto")) return "_"+str;
085                break;
086                case 'i':
087                        if(str.equals("if")) return "_"+str;
088                        else if(str.equals("implements")) return "_"+str;
089                        else if(str.equals("import")) return "_"+str;
090                        else if(str.equals("instanceof")) return "_"+str;
091                        else if(str.equals("int")) return "_"+str;
092                        else if(str.equals("interface")) return "_"+str;
093                break;
094                case 'n':
095                        if(str.equals("native")) return "_"+str;
096                        else if(str.equals("new")) return "_"+str;
097                        else if(str.equals("null")) return "_"+str;
098                break;
099                case 'p':
100                        if(str.equals("package")) return "_"+str;
101                        else if(str.equals("private")) return "_"+str;
102                        else if(str.equals("protected")) return "_"+str;
103                        else if(str.equals("public")) return "_"+str;
104                break;
105                case 'r':
106                        if(str.equals("return")) return "_"+str;
107                break;
108                case 's':
109                        if(str.equals("short")) return "_"+str;
110                        else if(str.equals("static")) return "_"+str;
111                        else if(str.equals("strictfp")) return "_"+str;
112                        else if(str.equals("super")) return "_"+str;
113                        else if(str.equals("switch")) return "_"+str;
114                        else if(str.equals("synchronized")) return "_"+str;
115                break;
116                case 't':
117                        if(str.equals("this")) return "_"+str;
118                        else if(str.equals("throw")) return "_"+str;
119                        else if(str.equals("throws")) return "_"+str;
120                        else if(str.equals("transient")) return "_"+str;
121                        else if(str.equals("true")) return "_"+str;
122                        else if(str.equals("try")) return "_"+str;
123                break;
124                case 'v':
125                        if(str.equals("void")) return "_"+str;
126                        else if(str.equals("volatile")) return "_"+str;
127                break;
128                case 'w':
129                        if(str.equals("while")) return "_"+str;
130                break;
131                }
132                return str;
133                
134        }
135        
136        /**
137         * translate a string to a valid variable string
138         * @param str string to translate
139         * @return translated String
140         */
141        private static String toVariableName(String str) {
142                
143                StringBuffer rtn=new StringBuffer();
144                char[] chars=str.toCharArray();
145                long changes=0;
146                boolean doCorrect=true;
147                for(int i=0;i<chars.length;i++) {
148                        char c=chars[i];
149                        if(i==0 && (c>='0' && c<='9'))rtn.append("_"+c);
150                        else if((c>='a' && c<='z') ||(c>='A' && c<='Z') ||(c>='0' && c<='9') || c=='_' || c=='$')
151                                rtn.append(c);
152                        else {  
153                            doCorrect=false;
154                                rtn.append('_');
155                                changes+=(c*(i+1));
156                        }
157                }
158                
159                if(changes>0)rtn.append(changes);
160                
161                if(doCorrect)return correctReservedWord(rtn.toString());
162                return rtn.toString();
163        }
164        
165        /**
166         * creates a classbane from give source path
167         * @param str
168         * @return
169         */
170        public static String toClassName(String str) {
171                StringBuffer javaName=new StringBuffer();
172                String[] arr=lucee.runtime.type.util.ListUtil.listToStringArray(str, '/');
173                
174                for(int i=0;i<arr.length;i++) {
175                        if(i==(arr.length-1)) arr[i]=replaceLast(arr[i],'.','$');
176                        if(i!=0)javaName.append('.');
177                        javaName.append(toVariableName(arr[i]));
178                }
179                return javaName.toString().toLowerCase();
180        }
181}