Home | History | Annotate | Download | only in helper
      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, char folderSeparator) {
     35         this.invariantPrefixLength = invariantPrefixLength;
     36         this.desiredLength = desiredLength;
     37         this.folderSeparator = folderSeparator;
     38     }
     39 
     40     public String abbreviate(String filename) {
     41         if (filename.length() <= desiredLength) {
     42             return filename;
     43         } else {
     44 
     45             int firstIndex = filename.indexOf(folderSeparator, invariantPrefixLength);
     46             if (firstIndex == -1) {
     47                 // we cant't process this string
     48                 return filename;
     49             }
     50             StringBuilder buf = new StringBuilder(desiredLength);
     51             buf.append(filename.substring(0, firstIndex + 1));
     52             buf.append(FILLER);
     53             int nextIndex = computeNextIndex(filename, firstIndex);
     54             if (nextIndex != -1) {
     55                 buf.append(filename.substring(nextIndex));
     56             } else {
     57                 // better long than wrong
     58                 return filename;
     59             }
     60 
     61             if (buf.length() < filename.length()) {
     62                 return buf.toString();
     63             } else {
     64                 // we tried our best but we are still could not shorten the input
     65                 return filename;
     66             }
     67         }
     68     }
     69 
     70     int computeNextIndex(String filename, int firstIndex) {
     71         int nextIndex = firstIndex + 1;
     72         int hitCount = 0;
     73         int minToRemove = filename.length() - desiredLength + FILLER.length();
     74         while (nextIndex < firstIndex + minToRemove) {
     75             int tmpIndex = filename.indexOf(folderSeparator, nextIndex + 1);
     76             if (tmpIndex == -1) {
     77                 if (hitCount == 0) {
     78                     return -1;
     79                 } else {
     80                     return nextIndex;
     81                 }
     82             } else {
     83                 hitCount++;
     84                 nextIndex = tmpIndex;
     85             }
     86         }
     87         return nextIndex;
     88     }
     89 }
     90