[1040] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
| 2 | //
|
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 4 | // you may not use this file except in compliance with the License.
|
---|
| 5 | // You may obtain a copy of the License at
|
---|
| 6 | //
|
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 8 | //
|
---|
| 9 | // Unless required by applicable law or agreed to in writing, software
|
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 12 | // See the License for the specific language governing permissions and
|
---|
| 13 | // limitations under the License.
|
---|
| 14 |
|
---|
[1030] | 15 | package de.ugoe.cs.autoquest.usability.util;
|
---|
| 16 |
|
---|
| 17 | import java.util.List;
|
---|
| 18 |
|
---|
| 19 | import com.google.common.base.CharMatcher;
|
---|
| 20 | import com.google.common.base.Predicate;
|
---|
| 21 | import com.google.common.base.Splitter;
|
---|
| 22 | import com.google.common.collect.HashMultiset;
|
---|
| 23 | import com.google.common.collect.Iterables;
|
---|
| 24 | import com.google.common.collect.Lists;
|
---|
| 25 | import com.google.common.collect.Multiset;
|
---|
| 26 |
|
---|
| 27 | import de.ugoe.cs.autoquest.eventcore.gui.TextInput;
|
---|
| 28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
|
---|
| 29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
|
---|
| 30 |
|
---|
[1040] | 31 | /**
|
---|
| 32 | * <p>
|
---|
| 33 | * TODO comment
|
---|
| 34 | * </p>
|
---|
| 35 | *
|
---|
| 36 | * @author Alexander Deicke
|
---|
| 37 | */
|
---|
[1030] | 38 | public class TextInputUtil {
|
---|
[1040] | 39 |
|
---|
| 40 | private TextInputUtil() {
|
---|
| 41 | // util class
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public static Multiset<String> aggregateEnteredTextFromTextInputs(List<ITaskTreeNode> nodesWithTextInputEvents)
|
---|
| 45 | {
|
---|
[1030] | 46 | List<Iterable<String>> allTextInputs = Lists.newArrayList();
|
---|
[1040] | 47 | for (ITaskTreeNode nodeWithTextInput : nodesWithTextInputEvents) {
|
---|
[1030] | 48 | TextInput textInput = (TextInput) ((IEventTask) nodeWithTextInput).getEventType();
|
---|
[1031] | 49 | allTextInputs.add(splitTextIntoWordsAndSigns(textInput.getEnteredText()));
|
---|
[1030] | 50 | }
|
---|
| 51 | return HashMultiset.create(Iterables.concat(allTextInputs));
|
---|
| 52 | }
|
---|
[1040] | 53 |
|
---|
[1031] | 54 | public static Iterable<String> splitTextIntoWordsAndSigns(String enteredText) {
|
---|
[1040] | 55 | CharMatcher onlyWords =
|
---|
| 56 | CharMatcher.WHITESPACE.or(CharMatcher
|
---|
| 57 | .forPredicate(characterIsJavaIdentifierPartPredicate()));
|
---|
| 58 | CharMatcher onlySigns =
|
---|
| 59 | CharMatcher.WHITESPACE.or(CharMatcher
|
---|
| 60 | .forPredicate(characterIsJavaIdentifierPartPredicate()).negate());
|
---|
| 61 | Iterable<String> words =
|
---|
| 62 | Splitter.on(onlyWords).omitEmptyStrings().trimResults().split(enteredText);
|
---|
| 63 | Iterable<String> signs =
|
---|
| 64 | Splitter.on(onlySigns).omitEmptyStrings().trimResults().split(enteredText);
|
---|
[1030] | 65 | return Iterables.concat(words, signs);
|
---|
| 66 | }
|
---|
[1040] | 67 |
|
---|
[1030] | 68 | public static Predicate<Character> characterIsJavaIdentifierPartPredicate() {
|
---|
| 69 | return new Predicate<Character>() {
|
---|
[1040] | 70 |
|
---|
[1030] | 71 | @Override
|
---|
| 72 | public boolean apply(Character character) {
|
---|
[1040] | 73 | return !Character.isJavaIdentifierPart(character);
|
---|
[1030] | 74 | }
|
---|
[1040] | 75 |
|
---|
[1030] | 76 | };
|
---|
| 77 | }
|
---|
[1040] | 78 |
|
---|
[1030] | 79 | public static Predicate<Character> characterIsLetterOrDigitPredicate() {
|
---|
| 80 | return new Predicate<Character>() {
|
---|
[1040] | 81 |
|
---|
[1030] | 82 | @Override
|
---|
| 83 | public boolean apply(Character character) {
|
---|
| 84 | return !Character.isLetterOrDigit(character);
|
---|
| 85 | }
|
---|
[1040] | 86 |
|
---|
[1030] | 87 | };
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | }
|
---|