Java String Abbreviate abbreviate(String fileName, int maxLen)

Here you can find the source of abbreviate(String fileName, int maxLen)

Description

Abbreviates a path name by replacing sub directories by an ellipsis character.

License

Open Source License

Parameter

Parameter Description
fileName the pathName to abbreviate
maxLen maximum length of the Abbreviation. Beware: this method appends sub directories until maxLen is exceeded, therefore the returned String can well exceed maxLen!

Return

the abbreviated path name or the original path name if it was shorter or equal maxLen. It is guaranteed that the file name part of the path is fully included.

Declaration

public static String abbreviate(String fileName, int maxLen) 

Method Source Code


//package com.java2s;
/*//from   w w w  . j a  v  a 2s .c  o m
 *  IOUtil.java
 *  (ScissLib)
 *
 *  Copyright (c) 2004-2013 Hanns Holger Rutz. All rights reserved.
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *   For further information, please contact Hanns Holger Rutz at
 *   contact@sciss.de
 */

import java.io.File;

import java.util.ArrayList;

public class Main {
    /**
     *  Abbreviates a path name by
     *  replacing sub directories 
     *  by an ellipsis character.
     *  This is used inside <code>MenuFactory</code>
     *  to shorten the pathnames attached to
     *  the Open-Recent menu.
     *
     *  @param  fileName   the pathName to abbreviate
     *  @param  maxLen      maximum length of the
     *                  Abbreviation. Beware: this
     *                  method appends sub directories
     *                  until maxLen is exceeded, therefore
     *                  the returned String can well exceed
     *                  maxLen! 
     *  @return            the abbreviated path name or
     *                  the original path name if it was
     *                  shorter or equal maxLen. It is
     *                  guaranteed that the file name part
     *                  of the path is fully included.
     *
     *  @todo   BBEdit 7 has a nicer looking alternative: placing
     *         the filename in the beginning of the menu item
     *         line, followed by an abbreviated path.
     */
    public static String abbreviate(String fileName, int maxLen) {
        if (fileName.length() <= maxLen)
            return fileName;

        File f = new File(fileName);
        ArrayList<String> coll = new ArrayList<String>();
        String name;
        StringBuffer begBuf = new StringBuffer();
        StringBuffer endBuf = new StringBuffer();
        int len;
        boolean b;

        while ((f != null) && (name = f.getName()) != null) {
            coll.add(0, name);
            f = f.getParentFile();
        }
        if (coll.isEmpty())
            return fileName;

        len = coll.size() << 1; // ellipsis character per subdir and filename, separator per subdir
        name = (String) coll.remove(coll.size() - 1);
        endBuf.insert(0, name);
        len += name.length();
        if (!coll.isEmpty()) {
            name = (String) coll.remove(0);
            begBuf.append(name);
            begBuf.append(File.separator);
            len += name.length() - 1;
        }
        if (!coll.isEmpty()) {
            name = (String) coll.remove(0);
            if (name.equals("Volumes")) { // ok dis wan me don want
                begBuf.append('\u2026');
                begBuf.append(File.separator);
            } else {
                begBuf.append(name);
                begBuf.append(File.separator);
                len += name.length() - 1;
            }
        }
        for (b = true; !coll.isEmpty() && len <= maxLen; b = !b) {
            if (b) {
                name = (String) coll.remove(coll.size() - 1);
                endBuf.insert(0, File.separator);
                endBuf.insert(0, name);
            } else {
                name = (String) coll.remove(0);
                begBuf.append(name);
                begBuf.append(File.separator);
            }
            len += name.length() - 1; // full name instead of single character ellipsis
        }

        while (!coll.isEmpty()) {
            coll.remove(0);
            begBuf.append('\u2026');
            begBuf.append(File.separator);
        }

        return (begBuf.append(endBuf).toString());
    }
}

Related

  1. abbreviate(final String str, int lower, int upper, final String appendToEnd)
  2. abbreviate(final String value, final int maxLength)
  3. abbreviate(final String value, int max)
  4. abbreviate(String _str, int _length)
  5. abbreviate(String content, int maxWidth, String enc, String suffix)
  6. abbreviate(String input, int length)
  7. abbreviate(String longName)
  8. abbreviate(String longStr, int maxLength)
  9. abbreviate(String name)