View Javadoc

1   /**
2    * Copyright (c) 2004-2011 QOS.ch
3    * All rights reserved.
4    *
5    * Permission is hereby granted, free  of charge, to any person obtaining
6    * a  copy  of this  software  and  associated  documentation files  (the
7    * "Software"), to  deal in  the Software without  restriction, including
8    * without limitation  the rights to  use, copy, modify,  merge, publish,
9    * distribute,  sublicense, and/or sell  copies of  the Software,  and to
10   * permit persons to whom the Software  is furnished to do so, subject to
11   * the following conditions:
12   *
13   * The  above  copyright  notice  and  this permission  notice  shall  be
14   * included in all copies or substantial portions of the Software.
15   *
16   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
17   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
18   * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20   * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21   * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
22   * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23   *
24   */
25  package org.slf4j.migrator.helper;
26  
27  public class Abbreviator {
28    static final String FILLER = "...";
29  
30    final char folderSeparator;
31    final int invariantPrefixLength;
32    final int desiredLength;
33  
34    public Abbreviator(int invariantPrefixLength, int desiredLength,
35        char folderSeparator) {
36      this.invariantPrefixLength = invariantPrefixLength;
37      this.desiredLength = desiredLength;
38      this.folderSeparator = folderSeparator;
39    }
40  
41    public String abbreviate(String filename) {
42      if (filename.length() <= desiredLength) {
43        return filename;
44      } else {
45  
46        int firstIndex = filename.indexOf(folderSeparator, invariantPrefixLength);
47        if (firstIndex == -1) {
48          // we cant't process this string
49          return filename;
50        }
51        StringBuffer buf = new StringBuffer(desiredLength);
52        buf.append(filename.substring(0, firstIndex + 1));
53        buf.append(FILLER);
54        int nextIndex = computeNextIndex(filename, firstIndex);
55        if (nextIndex != -1) {
56          buf.append(filename.substring(nextIndex));
57        } else {
58          // better long than wrong
59          return filename;
60        }
61  
62        if (buf.length() < filename.length()) {
63          return buf.toString();
64        } else {
65          // we tried our best but we are still could not shorten the input
66          return filename;
67        }
68      }
69    }
70  
71    int computeNextIndex(String filename, int firstIndex) {
72      int nextIndex = firstIndex + 1;
73      int hitCount = 0;
74      int minToRemove = filename.length() - desiredLength + FILLER.length();
75      while (nextIndex < firstIndex + minToRemove) {
76        int tmpIndex = filename.indexOf(folderSeparator, nextIndex + 1);
77        if (tmpIndex == -1) {
78          if (hitCount == 0) {
79            return -1;
80          } else {
81            return nextIndex;
82          }
83        } else {
84          hitCount++;
85          nextIndex = tmpIndex;
86        }
87      }
88      return nextIndex;
89    }
90  }