View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * License.  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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.util.internal;
17  
18  import java.util.Formatter;
19  
20  /**
21   * String utility class.
22   *
23   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
24   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
25   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
26   */
27  public class StringUtil {
28  
29      private StringUtil() {
30          // Unused.
31      }
32  
33      public static final String NEWLINE;
34  
35      static {
36          String newLine = null;
37  
38          try {
39              newLine = new Formatter().format("%n").toString();
40          } catch (Exception e) {
41              newLine = "\n";
42          }
43  
44          NEWLINE = newLine;
45      }
46  
47      /**
48       * Strip an Object of it's ISO control characters.
49       *
50       * @param value
51       *          The Object that should be stripped. This objects toString method will
52       *          called and the result passed to {@link #stripControlCharacters(String)}.
53       * @return {@code String}
54       *          A new String instance with its hexadecimal control characters replaced
55       *          by a space. Or the unmodified String if it does not contain any ISO
56       *          control characters.
57       */
58      public static String stripControlCharacters(Object value) {
59          if (value == null) {
60              return null;
61          }
62  
63          return stripControlCharacters(value.toString());
64      }
65  
66      /**
67       * Strip a String of it's ISO control characters.
68       *
69       * @param value
70       *          The String that should be stripped.
71       * @return {@code String}
72       *          A new String instance with its hexadecimal control characters replaced
73       *          by a space. Or the unmodified String if it does not contain any ISO
74       *          control characters.
75       */
76      public static String stripControlCharacters(String value) {
77          if (value == null) {
78              return null;
79          }
80  
81          boolean hasControlChars = false;
82          for (int i = value.length() - 1; i >= 0; i --) {
83              if (Character.isISOControl(value.charAt(i))) {
84                  hasControlChars = true;
85                  break;
86              }
87          }
88  
89          if (!hasControlChars) {
90              return value;
91          }
92  
93          StringBuilder buf = new StringBuilder(value.length());
94          int i = 0;
95  
96          // Skip initial control characters (i.e. left trim)
97          for (; i < value.length(); i ++) {
98              if (!Character.isISOControl(value.charAt(i))) {
99                  break;
100             }
101         }
102 
103         // Copy non control characters and substitute control characters with
104         // a space.  The last control characters are trimmed.
105         boolean suppressingControlChars = false;
106         for (; i < value.length(); i ++) {
107             if (Character.isISOControl(value.charAt(i))) {
108                 suppressingControlChars = true;
109                 continue;
110             } else {
111                 if (suppressingControlChars) {
112                     suppressingControlChars = false;
113                     buf.append(' ');
114                 }
115                 buf.append(value.charAt(i));
116             }
117         }
118 
119         return buf.toString();
120     }
121 }