| 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 |
|
|---|
| 15 | package de.ugoe.cs.util;
|
|---|
| 16 |
|
|---|
| 17 | import java.util.ArrayList;
|
|---|
| 18 | import java.util.List;
|
|---|
| 19 |
|
|---|
| 20 | /**
|
|---|
| 21 | * <p>
|
|---|
| 22 | * Helper class that provides methods to simplify working with {@link String}s.
|
|---|
| 23 | * </p>
|
|---|
| 24 | *
|
|---|
| 25 | * @author Steffen Herbold
|
|---|
| 26 | * @version 1.0
|
|---|
| 27 | */
|
|---|
| 28 | final public class StringTools {
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * <p>
|
|---|
| 32 | * Private constructor to prevent initializing of the class.
|
|---|
| 33 | * </p>
|
|---|
| 34 | */
|
|---|
| 35 | private StringTools() {
|
|---|
| 36 |
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * <p>
|
|---|
| 41 | * Simplifies use of operation system specific line separators.
|
|---|
| 42 | * </p>
|
|---|
| 43 | */
|
|---|
| 44 | public final static String ENDLINE = System.getProperty("line.separator");
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * <p>
|
|---|
| 48 | * Replaces all occurrences of {@literal &, <, >, ', and "} with their respective XML entities
|
|---|
| 49 | * {@literal &, <, >, ', and "} without destroying already existing
|
|---|
| 50 | * entities.
|
|---|
| 51 | * </p>
|
|---|
| 52 | *
|
|---|
| 53 | * @param str
|
|---|
| 54 | * String where the XML entities are to be replaced
|
|---|
| 55 | * @return new String, where the XML entities are used instead of the literals
|
|---|
| 56 | */
|
|---|
| 57 | public static String xmlEntityReplacement(String str) {
|
|---|
| 58 | String result = str;
|
|---|
| 59 | if (result != null && !"".equals(result)) {
|
|---|
| 60 | result = result.replaceAll("&(?!(?:lt|gt|apos|quot|amp);)", "&");
|
|---|
| 61 | result = result.replaceAll("<", "<");
|
|---|
| 62 | result = result.replaceAll(">", ">");
|
|---|
| 63 | result = result.replaceAll("'", "'");
|
|---|
| 64 | result = result.replaceAll("\"", """);
|
|---|
| 65 | }
|
|---|
| 66 | return result;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * <p>
|
|---|
| 71 | * Performs an auto completion of the provided prefix using the given potential completions.
|
|---|
| 72 | * The method searches for all completions that start with the given prefix and stores them in
|
|---|
| 73 | * a subset. It then extends the prefix with characters of the subset of completions as long as
|
|---|
| 74 | * the prefix still prefixes all completions in the subset. The result is then returned. If
|
|---|
| 75 | * there is no matching completion or if there are several matching completions differing
|
|---|
| 76 | * in the succeeding letter, the prefix is returned as is. If there is only one matching
|
|---|
| 77 | * completion this completion is returned.
|
|---|
| 78 | * </p>
|
|---|
| 79 | *
|
|---|
| 80 | * @param prefix the prefix to be further completed
|
|---|
| 81 | * @param completions the potential completions of the prefix
|
|---|
| 82 | *
|
|---|
| 83 | * @return as described
|
|---|
| 84 | */
|
|---|
| 85 | public static String autocomplete(String prefix, String[] completions) {
|
|---|
| 86 | List<String> matchingCompletions = new ArrayList<String>();
|
|---|
| 87 | for (String completion : completions) {
|
|---|
| 88 | if (completion.startsWith(prefix)) {
|
|---|
| 89 | matchingCompletions.add(completion);
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | StringBuffer completedPrefix = new StringBuffer(prefix);
|
|---|
| 94 |
|
|---|
| 95 | boolean foundCompletion = false;
|
|---|
| 96 |
|
|---|
| 97 | while (!foundCompletion) {
|
|---|
| 98 | char nextCompletionChar = 0;
|
|---|
| 99 | for (String completion : matchingCompletions) {
|
|---|
| 100 | if (completion.length() > completedPrefix.length()) {
|
|---|
| 101 | if (nextCompletionChar == 0) {
|
|---|
| 102 | nextCompletionChar = completion.charAt(completedPrefix.length());
|
|---|
| 103 | }
|
|---|
| 104 | else if (nextCompletionChar != completion.charAt(completedPrefix.length())) {
|
|---|
| 105 | foundCompletion = true;
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 | else {
|
|---|
| 109 | foundCompletion = true;
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | if (!foundCompletion && (nextCompletionChar != 0)) {
|
|---|
| 114 | completedPrefix.append(nextCompletionChar);
|
|---|
| 115 | }
|
|---|
| 116 | else {
|
|---|
| 117 | foundCompletion = true;
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | return completedPrefix.toString();
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|