1 |
iliev |
912 |
/* |
2 |
|
|
* JSampler - a java front-end for LinuxSampler |
3 |
|
|
* |
4 |
iliev |
1143 |
* Copyright (C) 2005-2006 Grigor Iliev <grigor@grigoriliev.com> |
5 |
iliev |
912 |
* |
6 |
|
|
* This file is part of JSampler. |
7 |
|
|
* |
8 |
|
|
* JSampler is free software; you can redistribute it and/or modify |
9 |
|
|
* it under the terms of the GNU General Public License version 2 |
10 |
|
|
* as published by the Free Software Foundation. |
11 |
|
|
* |
12 |
|
|
* JSampler is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
* GNU General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with JSampler; if not, write to the Free Software |
19 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
|
|
* MA 02111-1307 USA |
21 |
|
|
*/ |
22 |
|
|
|
23 |
|
|
package org.jsampler; |
24 |
|
|
|
25 |
|
|
import java.util.StringTokenizer; |
26 |
|
|
import java.util.Vector; |
27 |
|
|
|
28 |
|
|
|
29 |
|
|
/** |
30 |
|
|
* This class provides a collection of utility methods regarding LSCP protocol. |
31 |
|
|
* @author Grigor Iliev |
32 |
|
|
*/ |
33 |
|
|
public class LscpUtils { |
34 |
|
|
|
35 |
|
|
/** Forbits the instantiation of this class. */ |
36 |
|
|
private |
37 |
|
|
LscpUtils() { } |
38 |
|
|
|
39 |
|
|
/** |
40 |
|
|
* Determines whether the specified command (or part of a command) is |
41 |
|
|
* valid LSCP command. |
42 |
|
|
* @param cmd The command to be checked. |
43 |
|
|
* @return <code>true</code> if <code>cmd</code> is <code>null</code>, |
44 |
|
|
* empty string, or valid command (or part of a command). |
45 |
|
|
*/ |
46 |
|
|
public static boolean |
47 |
|
|
spellCheck(String cmd) { |
48 |
|
|
if(cmd == null || cmd.length() == 0) return true; |
49 |
|
|
if(cmd.charAt(0) == '#') return true; |
50 |
|
|
|
51 |
|
|
LscpNode node = LscpTree.getRoot(); |
52 |
|
|
|
53 |
|
|
// Uses negative to prevent the discarding of the trailing empty strings. |
54 |
|
|
String[] kwS = cmd.split(" ", -20); |
55 |
|
|
|
56 |
|
|
for(int i = 0; i < kwS.length; i++) { |
57 |
|
|
if(i < kwS.length - 1) { |
58 |
|
|
node = checkCommand(kwS[i], node); |
59 |
|
|
if(node == null) return false; |
60 |
|
|
if(node.getChildren().length == 0) return node.hasParameters(); |
61 |
|
|
} else { |
62 |
|
|
return checkPartialCommand(kwS[i], node); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
if(node.isEndOfACommand() && node.hasParameters()) return true; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
return true; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
private static LscpNode |
72 |
|
|
checkCommand(String s, LscpNode node) { |
73 |
|
|
for(LscpNode n : node.getChildren()) { |
74 |
|
|
if(n.getName().equals(s)) return n; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
return null; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
private static boolean |
81 |
|
|
checkPartialCommand(String s, LscpNode node) { |
82 |
|
|
for(LscpNode n : node.getChildren()) { |
83 |
|
|
if(n.getName().startsWith(s)) return true; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
return false; |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
/** |
90 |
|
|
* Gets all completion possibilities of the specified incomplete LSCP command. |
91 |
|
|
* @param cmd An incomplete command for which |
92 |
|
|
* all completion possibilities should be returned. |
93 |
|
|
* @return All completion possibilities of the specified incomplete LSCP command. |
94 |
|
|
* @throws IllegalStateException If the specified part of a command is not valid. |
95 |
|
|
*/ |
96 |
|
|
public static String[] |
97 |
|
|
getCompletionPossibilities(String cmd) { |
98 |
|
|
String prefix = ""; |
99 |
|
|
LscpNode node = LscpTree.getRoot(); |
100 |
|
|
|
101 |
|
|
// Uses negative to prevent the discarding of the trailing empty strings. |
102 |
|
|
String[] kwS = cmd.split(" ", -20); |
103 |
|
|
|
104 |
|
|
for(int i = 0; i < kwS.length; i++) { |
105 |
|
|
String s = kwS[i]; |
106 |
|
|
if(i < kwS.length - 1) { |
107 |
|
|
if(prefix.length() > 0) prefix += " "; |
108 |
|
|
prefix += s; |
109 |
|
|
node = checkCommand(s, node); |
110 |
|
|
|
111 |
|
|
if(node == null) |
112 |
|
|
throw new IllegalStateException("Invalid command!"); |
113 |
|
|
|
114 |
|
|
if(node.getChildren().length == 0) { |
115 |
|
|
if(node.isEndOfACommand() && !node.hasParameters()) { |
116 |
|
|
throw new IllegalStateException("Invalid command!"); |
117 |
|
|
} else return new String[0]; |
118 |
|
|
} |
119 |
|
|
} else { |
120 |
|
|
if(!checkPartialCommand(s, node)) |
121 |
|
|
throw new IllegalStateException("Invalid command!"); |
122 |
|
|
|
123 |
|
|
LscpNode n = checkCommand(s, node); |
124 |
|
|
if(n != null) { |
125 |
|
|
node = n; |
126 |
|
|
if(prefix.length() > 0) prefix += " "; |
127 |
|
|
prefix += s; |
128 |
|
|
} else { |
129 |
|
|
// The command ends with incomplete keyword. |
130 |
|
|
String[] cmdS = getKeywords(node, prefix, s); |
131 |
|
|
return cmdS; |
132 |
|
|
} |
133 |
|
|
} |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
/* If we are here this means that the command ends with |
137 |
|
|
* complete keyword, or is empty string. |
138 |
|
|
*/ |
139 |
|
|
|
140 |
|
|
return getKeywords(node, prefix, ""); |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
/** |
144 |
|
|
* Returns an array of commands which last keyword begins with <code>prefix</code>. |
145 |
|
|
*/ |
146 |
|
|
private static String[] |
147 |
|
|
getKeywords(LscpNode node, String cmdPrefix, String prefix) { |
148 |
|
|
Vector<String> v = new Vector<String>(); |
149 |
|
|
|
150 |
|
|
for(LscpNode n : node.getChildren()) { |
151 |
|
|
if(n.getName().startsWith(prefix)) { |
152 |
|
|
String suffix = |
153 |
|
|
n.isEndOfACommand() && !n.hasParameters() ? "" : " "; |
154 |
|
|
|
155 |
|
|
if(cmdPrefix.length() != 0) { |
156 |
|
|
v.add(cmdPrefix + " " + n.getName() + suffix); |
157 |
|
|
} else v.add(n.getName() + suffix); |
158 |
|
|
} |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
return v.toArray(new String[v.size()]); |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
/** |
165 |
|
|
* Gets a list of all LSCP commands. |
166 |
|
|
* @return An array containing all LSCP commands. |
167 |
|
|
*/ |
168 |
|
|
public static String[] |
169 |
|
|
getCommandList() { |
170 |
|
|
Vector<String> v = new Vector<String>(); |
171 |
|
|
addVariants(LscpTree.getRoot(), v); |
172 |
|
|
return v.toArray(new String[v.size()]); |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
private static void |
176 |
|
|
addVariants(LscpNode node, Vector<String> v) { |
177 |
|
|
addVariants(node, v, new String("")); |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
private static void |
181 |
|
|
addVariants(LscpNode node, Vector<String> v, String s) { |
182 |
|
|
if(node.getChildren().length == 0) { |
183 |
|
|
v.add(s + node.getName()); |
184 |
|
|
return; |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
if(node.isEndOfACommand()) v.add(s + node.getName()); |
188 |
|
|
|
189 |
|
|
if(node.getName().length() > 0) s = s + node.getName() + " "; |
190 |
|
|
for(LscpNode n : node.getChildren()) addVariants(n, v, s); |
191 |
|
|
} |
192 |
|
|
} |