View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.websocket.common.util;
20  
21  /**
22   * Collection of utility methods for Text content
23   */
24  public final class TextUtil
25  {
26      /**
27       * Create a hint of what the text is like.
28       * <p>
29       * Used by logging and error messages to get a hint of what the text is like.
30       * 
31       * @param text
32       *            the text to abbreviate, quote, and generally give you a hint of what the value is.
33       * @return the abbreviated text
34       */
35      public static String hint(String text)
36      {
37          if (text == null)
38          {
39              return "<null>";
40          }
41          return '"' + maxStringLength(30,text) + '"';
42      }
43  
44      /**
45       * Smash a long string to fit within the max string length, by taking the middle section of the string and replacing them with an ellipsis "..."
46       * 
47       * <pre>
48       * Examples:
49       * .maxStringLength( 9, "Eatagramovabits") == "Eat...its"
50       * .maxStringLength(10, "Eatagramovabits") == "Eat...bits"
51       * .maxStringLength(11, "Eatagramovabits") == "Eata...bits"
52       * </pre>
53       * 
54       * @param max
55       *            the maximum size of the string (minimum size supported is 9)
56       * @param raw
57       *            the raw string to smash
58       * @return the ellipsis'd version of the string.
59       */
60      public static String maxStringLength(int max, String raw)
61      {
62          int length = raw.length();
63          if (length <= max)
64          {
65              // already short enough
66              return raw;
67          }
68  
69          if (max < 9)
70          {
71              // minimum supported
72              return raw.substring(0,max);
73          }
74  
75          StringBuilder ret = new StringBuilder();
76          int startLen = (int)Math.round((double)max / (double)3);
77          ret.append(raw.substring(0,startLen));
78          ret.append("...");
79          ret.append(raw.substring(length - (max - startLen - 3)));
80  
81          return ret.toString();
82      }
83  }