| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
package org.fest.swing.keystroke; |
| 16 |
|
|
| 17 |
|
import static java.lang.Thread.currentThread; |
| 18 |
|
import static org.fest.reflect.core.Reflection.staticField; |
| 19 |
|
import static org.fest.swing.keystroke.KeyStrokeMapping.mapping; |
| 20 |
|
import static org.fest.swing.keystroke.KeyStrokeMappingProvider.NO_MASK; |
| 21 |
|
import static org.fest.util.Closeables.close; |
| 22 |
|
import static org.fest.util.Strings.*; |
| 23 |
|
|
| 24 |
|
import java.awt.event.InputEvent; |
| 25 |
|
import java.awt.event.KeyEvent; |
| 26 |
|
import java.io.*; |
| 27 |
|
import java.util.*; |
| 28 |
|
|
| 29 |
|
import org.fest.reflect.exception.ReflectionError; |
| 30 |
|
import org.fest.swing.exception.ParsingException; |
| 31 |
|
import org.fest.util.VisibleForTesting; |
| 32 |
|
|
| 33 |
|
|
| 34 |
|
@link |
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
@link |
| 62 |
|
@link |
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
@link@link |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
@author |
| 72 |
|
@author |
| 73 |
|
|
| 74 |
|
@since |
| 75 |
|
|
|
|
|
| 96.7% |
Uncovered Elements: 3 (91) |
Complexity: 30 |
Complexity Density: 0.54 |
|
| 76 |
|
public class KeyStrokeMappingsParser { |
| 77 |
|
|
| 78 |
|
private static final Map<String, Character> SPECIAL_MAPPINGS = new HashMap<String, Character>(); |
| 79 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 80 |
3
|
static {... |
| 81 |
3
|
SPECIAL_MAPPINGS.put("COMMA", ','); |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
@link |
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
@link |
| 90 |
|
|
| 91 |
|
@param |
| 92 |
|
@return |
| 93 |
|
@throws |
| 94 |
|
@throws |
| 95 |
|
@throws |
| 96 |
|
@see |
| 97 |
|
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 98 |
4
|
public KeyStrokeMappingProvider parse(String file) {... |
| 99 |
4
|
validate(file); |
| 100 |
2
|
try { |
| 101 |
2
|
return parse(fileAsStream(file)); |
| 102 |
|
} catch (IOException e) { |
| 103 |
0
|
throw new ParsingException(concat("An I/O error ocurred while parsing file ", file), e); |
| 104 |
|
} |
| 105 |
|
} |
| 106 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 3 |
Complexity Density: 0.75 |
|
| 107 |
4
|
private void validate(String file) {... |
| 108 |
4
|
if (file == null) |
| 109 |
1
|
throw new NullPointerException("The name of the file to parse should not be null"); |
| 110 |
3
|
if (isEmpty(file)) |
| 111 |
1
|
throw new IllegalArgumentException("The name of the file to parse should not be an empty string"); |
| 112 |
|
} |
| 113 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 114 |
2
|
private InputStream fileAsStream(String file) {... |
| 115 |
2
|
InputStream stream = currentThread().getContextClassLoader().getResourceAsStream(file); |
| 116 |
1
|
if (stream == null) throw new ParsingException(concat("Unable to open file ", file)); |
| 117 |
1
|
return stream; |
| 118 |
|
} |
| 119 |
|
|
| 120 |
|
|
| 121 |
|
@link |
| 122 |
|
|
| 123 |
|
@param |
| 124 |
|
@return |
| 125 |
|
@throws |
| 126 |
|
@throws |
| 127 |
|
@throws |
| 128 |
|
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 129 |
3
|
public KeyStrokeMappingProvider parse(File file) {... |
| 130 |
3
|
validate(file); |
| 131 |
1
|
try { |
| 132 |
1
|
return parse(fileAsStream(file)); |
| 133 |
|
} catch (IOException e) { |
| 134 |
0
|
throw new ParsingException(concat("An I/O error ocurred while parsing file ", file), e); |
| 135 |
|
} |
| 136 |
|
} |
| 137 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 3 |
Complexity Density: 0.75 |
|
| 138 |
3
|
private void validate(File file) {... |
| 139 |
3
|
if (file == null) |
| 140 |
1
|
throw new NullPointerException("The file to parse should not be null"); |
| 141 |
2
|
if (!file.isFile()) |
| 142 |
1
|
throw new IllegalArgumentException(concat("The file ", file.getPath(), " is not an existing file")); |
| 143 |
|
} |
| 144 |
|
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 145 |
1
|
private InputStream fileAsStream(File file) {... |
| 146 |
1
|
try { |
| 147 |
1
|
return new FileInputStream(file); |
| 148 |
|
} catch (FileNotFoundException e) { |
| 149 |
0
|
throw new ParsingException(concat("The file ", file.getPath(), " was not found"), e); |
| 150 |
|
} |
| 151 |
|
} |
| 152 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
| 153 |
2
|
private KeyStrokeMappingProvider parse(InputStream input) throws IOException {... |
| 154 |
2
|
List<KeyStrokeMapping> mappings = new ArrayList<KeyStrokeMapping>(); |
| 155 |
2
|
BufferedReader reader = new BufferedReader(new InputStreamReader(input)); |
| 156 |
2
|
try { |
| 157 |
2
|
String line = reader.readLine(); |
| 158 |
6
|
while(line != null) { |
| 159 |
4
|
mappings.add(mappingFrom(line)); |
| 160 |
4
|
line = reader.readLine(); |
| 161 |
|
} |
| 162 |
2
|
return new ParsedKeyStrokeMappingProvider(mappings); |
| 163 |
|
} finally { |
| 164 |
2
|
close(reader); |
| 165 |
|
} |
| 166 |
|
} |
| 167 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 168 |
13
|
@VisibleForTesting... |
| 169 |
|
KeyStrokeMapping mappingFrom(String line) { |
| 170 |
13
|
String[] parts = split(line); |
| 171 |
1
|
if (parts.length != 3) throw notConformingWithPatternError(line); |
| 172 |
12
|
char character = characterFrom(parts[0].trim()); |
| 173 |
11
|
int keyCode = keyCodeFrom(parts[1].trim()); |
| 174 |
10
|
int modifiers = modifiersFrom(parts[2].trim()); |
| 175 |
9
|
return mapping(character, keyCode, modifiers); |
| 176 |
|
} |
| 177 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 178 |
13
|
private static String[] split(String line) {... |
| 179 |
13
|
return line.trim().split(","); |
| 180 |
|
} |
| 181 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 182 |
1
|
private static ParsingException notConformingWithPatternError(String line) {... |
| 183 |
1
|
return new ParsingException(concat( |
| 184 |
|
"Line ", quote(line), " does not conform with pattern '{char}, {keycode}, {modifiers}'")); |
| 185 |
|
} |
| 186 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 187 |
12
|
private static char characterFrom(String s) {... |
| 188 |
4
|
if (SPECIAL_MAPPINGS.containsKey(s)) return SPECIAL_MAPPINGS.get(s); |
| 189 |
7
|
if (s.length() == 1) return s.charAt(0); |
| 190 |
1
|
throw new ParsingException(concat("The text ", quote(s) , " should have a single character")); |
| 191 |
|
} |
| 192 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 193 |
11
|
private static int keyCodeFrom(String s) {... |
| 194 |
11
|
try { |
| 195 |
11
|
return staticField(keyCodeNameFrom(s)).ofType(int.class).in(KeyEvent.class).get(); |
| 196 |
|
} catch (ReflectionError e) { |
| 197 |
1
|
throw new ParsingException(concat("Unable to retrieve key code from text ", quote(s)), e.getCause()); |
| 198 |
|
} |
| 199 |
|
} |
| 200 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 201 |
11
|
private static String keyCodeNameFrom(String s) {... |
| 202 |
11
|
return concat("VK_", s); |
| 203 |
|
} |
| 204 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 205 |
10
|
private static int modifiersFrom(String s) {... |
| 206 |
7
|
if ("NO_MASK".equals(s)) return NO_MASK; |
| 207 |
3
|
try { |
| 208 |
3
|
return staticField(s).ofType(int.class).in(InputEvent.class).get(); |
| 209 |
|
} catch (ReflectionError e) { |
| 210 |
1
|
throw new ParsingException(concat("Unable to retrieve modifiers from text ", quote(s)), e.getCause()); |
| 211 |
|
} |
| 212 |
|
} |
| 213 |
|
} |