[Title]java to make candidate string for cli autocompletion 23/10623/1
authorhyunsik.noh <hyunsik.noh@samsung.com>
Tue, 8 Oct 2013 05:19:23 +0000 (14:19 +0900)
committerhyunsik.noh <hyunsik.noh@samsung.com>
Tue, 8 Oct 2013 05:19:23 +0000 (14:19 +0900)
[Module]cli

org.tizen.ncli.ide/src/org/tizen/ncli/ide/autocomplete/TizenAutoComplete.java [new file with mode: 0644]

diff --git a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/autocomplete/TizenAutoComplete.java b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/autocomplete/TizenAutoComplete.java
new file mode 100644 (file)
index 0000000..5f0865c
--- /dev/null
@@ -0,0 +1,226 @@
+/*
+ * IDE
+ *
+ * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Kangho Kim <kh5325.kim@samsung.com>
+ * Hyunsik Noh <hyunsik.noh@samsung.com>
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ */
+package org.tizen.ncli.ide.autocomplete;
+import java.util.ArrayList;
+import java.util.List;
+
+
+public class TizenAutoComplete {
+
+    public static String commands = "build-native build-web cli-config create debug help install list package run sign uninstall";
+    
+    public static String[][] subCommands = {
+        {"list", "device"},
+        {"create", "project security-profile security-profile-item certificate"},
+        {"help", commands}
+    };
+    
+    public static String[][] options = {
+        {"cli-config", "--list --global"},
+        {"build-native", "--predefine-option --platform --toolchain --arch"},
+        {"build-web", "--output -opt --optimize -euf --exclude-uifw -efum --exclude-uifw-min"},
+        {"sign", "--profile"},
+        {"package", "-t --type --sign -ref --ref-project"},
+        {"install", "--target"},
+        {"uninstall", "--target"},
+        {"run", "--target"},
+        {"debug", "--target"}
+    };
+    
+    public static String[][] optionsForSub = {
+        {"project", "-t --type -n --name"},
+        {"security-profile", "--kind --active"},
+        {"security-profile-item", "--kind --cert --passwd --ca --rootca"},
+        {"certificate", "--password --file-name --country-code --state-name --city-name --user-name --organization-name --department-name --email"},
+        {"device", "--detail"}
+    };
+    
+    public static String[] mainCommandsHaveDuplicatedOptions = {
+        "create", "build-web", "package"
+    };
+    
+    public static String[][] duplicatedOptions = {
+        {"-opt", "--optimize"},
+        {"-euf", "--exclude-uifw"},
+        {"-eufm", "--exclude-uifw-min"},
+        {"-e", "--exclude"},
+        {"-t", "--type"},
+        {"-n", "--name"},
+        {"-ref", "--ref-project"}
+        
+    };
+    /**
+     * @param args
+     */
+    public static void main(String[] args) {
+        String[] inputs = args[0].trim().split(" ");
+        
+        int count = inputs.length;
+        String mainCmd = null;
+        String subCmd = null;
+        boolean needSubCmd = false;
+        
+        if(count > 1) {
+            mainCmd = inputs[1];
+            if((needSubCmd = hasSub(mainCmd))) {
+                if(count > 2) {
+                    subCmd = inputs[2];
+                }
+            }
+        }
+        System.out.println(getNext(inputs, count, needSubCmd, mainCmd, subCmd));
+    }
+    
+    private static boolean hasSub(String mainCmd) {
+        for(int i = 0; i < subCommands.length; i++) {
+            if(subCommands[i][0].equals(mainCmd)) {
+                return true;
+            }
+        }
+        return false;
+    }
+    
+    private static String getNext(String[] args, int count, boolean needSubCmd, String mainCmd, String subCmd) {
+        String result = null;
+        
+        if(count == 1) {
+            return getCommand();
+        } else {
+            String [][] commands = null;
+            String criteria = mainCmd;
+            
+            if(needSubCmd) {
+                if(count == 2) {
+                    commands = subCommands;
+                    return getCommands(commands, criteria);
+                } else {
+                    commands = optionsForSub;
+                    criteria = subCmd;
+                }
+            } else {
+                commands = options;
+            }
+            result = getCommands(commands, criteria);
+        }
+        return removeExistedOptions(result, args, mainCmd, needSubCmd);
+    }
+    
+    private static String getCommand() {
+        return commands;
+    }
+    
+    private static String getCommands(String[][] commands, String criteria) {
+        String result = null;
+        for(int i = 0; i < commands.length ; i++) {
+            if( commands[i][0].equals(criteria)) {
+                result = commands[i][1];
+            }
+        }
+        //case of "help" specially, it returns main commands except "help"
+        if(subCommands[2][0].equals(criteria)) {
+            result = result.replace(criteria, "");
+        }
+        return result;
+    }
+    
+    
+    //remove candidate if it is already included option
+    private static String removeExistedOptions(String candidateString, String[] existedCommands, String mainCmd, boolean hasSubCmd) {
+        if(candidateString == null) {
+            return "";
+        }
+        
+        String result = "";
+        boolean needDuplicatedCheck = false;
+        
+        for(String mainCommand : mainCommandsHaveDuplicatedOptions) {
+            if(mainCommand.equals(mainCmd)) {
+                needDuplicatedCheck = true;
+                break;
+            }
+        }
+        List<String> duplicatedOptionList = new ArrayList<String>();
+        
+        String[] candidates = candidateString.split(" ");
+        
+        String candidate = null;
+        //no need to check 1st and 2nd command
+        for(int i = 2; i < existedCommands.length ; i++) {
+            for(int J = 0 ; J < candidates.length; J++) {
+                candidate = candidates[J];
+                //if not options, skip.
+                if('-' != existedCommands[i].charAt(0)) {
+                    continue;
+                } else
+                {
+                    if("".equals(candidate)){
+                        continue;
+                    }
+                    if(existedCommands[i].equals(candidate)) {
+                        if(needDuplicatedCheck) {
+                            String duplicatedOption = findDuplicatedOption(candidate);
+                            if( duplicatedOption != null) {
+                                duplicatedOptionList.add(duplicatedOption);
+                            }
+                        }
+                        candidates[J] = "";
+                        break;
+                    }
+                }
+            }
+        }
+        //make candidate strinng except duplicated options
+        boolean isFind = false;
+        for(int i = 0 ; i < candidates.length; i++) {
+            candidate = candidates[i];
+            isFind = false;
+            for(String duplicatedOption : duplicatedOptionList) {
+                if(candidate.equals("") || candidate.equals(duplicatedOption)) {
+                    isFind = true;
+                    break;
+                }
+            }
+            if(!isFind) {
+                result = result + " " + candidate;
+            }
+        }
+        return result.trim();
+    }
+        
+    private static String findDuplicatedOption(String option) {
+        String duplicatedOption = null;
+        int j = 0;
+        if('-'==(option.charAt(1))) {
+            j = 1;
+        }
+        for(int i = 0; i < duplicatedOptions.length; i++) {
+            if(duplicatedOptions[i][j].equals(option)) {
+                duplicatedOption = duplicatedOptions[i][(1^j)];
+                break;
+            }
+        }
+        return duplicatedOption;
+    }
+}