View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *******************************************************************************/
16  package it.jnrpe.utils;
17  
18  import org.apache.commons.lang.text.StrMatcher;
19  import org.apache.commons.lang.text.StrTokenizer;
20  
21  /**
22   * A simple string util class.
23   *
24   * @author Massimiliano Ziccardi
25   */
26  public final class StringUtils {
27      /**
28       * Private default constructor to avoid instantiation.
29       */
30      private StringUtils() {
31  
32      }
33  
34      /**
35       * This is a simple utility to split strings. The string is splitted.
36       * following these rules (in the order):
37       * <ul>
38       * <li>If a single quote (') or a double quote (") is found at the start of
39       * the word, the split will occour at the next quote or double quote.
40       * <li>Otherwise, the split occurres as soon as a space is found.
41       * </ul>
42       *
43       * @param string
44       *            The string to split
45       * @param ignoreQuotes
46       *            For future implementation
47       * @return The splitted string
48       *
49       * @since JNRPE Server 1.04
50       */
51      public static String[]
52              split(final String string, final boolean ignoreQuotes) {
53          return split(string, ' ', ignoreQuotes);
54      }
55  
56      /**
57       * Splits the given string using as separator the
58       * <code>separator</code> character.
59       * @param string The string to be splitted
60       * @param separator The separator character
61       * @param ignoreQuotes <code>true</code> if the quotes
62       * must be ignored.
63       * @return The splitted string
64       */
65      public static String[] split(final String string, final char separator,
66              final boolean ignoreQuotes) {
67          StrTokenizer strtok =
68                  new StrTokenizer(string, StrMatcher.charMatcher(separator),
69                          StrMatcher.quoteMatcher());
70          return strtok.getTokenArray();
71      }
72  }