NormalizedFileNames.java

    1/*******************************************************************************
    2 * Copyright (c) 2010 Mountainminds GmbH & Co. KG and others
    3 * All rights reserved. This program and the accompanying materials
    4 * are made available under the terms of the Eclipse Public License v1.0
    5 * which accompanies this distribution, and is available at
    6 * http://www.eclipse.org/legal/epl-v10.html
    7 *
    8 * Contributors:
    9 *    Marc R. Hoffmann - initial API and implementation
   10 *    
   11 * $Id: $
   12 *******************************************************************************/
   13package org.jacoco.report;
   14
   15import java.util.BitSet;
   16import java.util.HashMap;
   17import java.util.HashSet;
   18import java.util.Map;
   19import java.util.Set;
   20
   21/**
   22 * Internal utility to create normalized file names from string ids. The file
   23 * names generated by an instance of this class have the following properties:
   24 * 
   25 * <ul>
   26 * <li>The same input id is mapped to the same file name.</li>
   27 * <li>Different ids are mapped to different file names.</li>
   28 * <li>For safe characters the file name corresponds to the input id, other
   29 * characters are replaced by <code>_</code> (underscore).</li>
   30 * <li>File names are case aware, i.e. the same file name but with different
   31 * upper/lower case characters is not possible.</li>
   32 * <li>If unique filenames can't directly created from the ids, additional
   33 * suffixes are appended.</li>
   34 * </ul>
   35 * 
   36 * @author Marc R. Hoffmann
   37 * @version $Revision: $
   38 */
   39class NormalizedFileNames {
   40
   41    private static final BitSet LEGAL_CHARS = new BitSet();
   42
   43    static {
   44        final String legal = "abcdefghijklmnopqrstuvwxyz"
   45                + "ABCDEFGHIJKLMNOPQRSTUVWYXZ0123456789$-._";
   46        for (final char c : legal.toCharArray()) {
   47            LEGAL_CHARS.set(c);
   48        }
   49    }
   50
   51    private final Map<String, String> mapping = new HashMap<String, String>();
   52
   53    private final Set<String> usedNames = new HashSet<String>();
   54
   55    public String getFileName(final String id) {
   56        String name = mapping.get(id);
   57        if (name != null) {
   58            return name;
   59        }
   60        name = replaceIllegalChars(id);
   61        name = ensureUniqueness(name);
   62        mapping.put(id, name);
   63        return name;
   64    }
   65
   66    private String replaceIllegalChars(final String s) {
   67        final StringBuilder sb = new StringBuilder(s.length());
   68        for (int i = 0; i < s.length(); i++) {
   69            final char c = s.charAt(i);
   70            sb.append(LEGAL_CHARS.get(c) ? c : '_');
   71        }
   72        return sb.toString();
   73    }
   74
   75    private String ensureUniqueness(final String s) {
   76        String unique = s;
   77        String lower = unique.toLowerCase();
   78        int idx = 1;
   79        while (usedNames.contains(lower)) {
   80            unique = s + '~' + idx++;
   81            lower = unique.toLowerCase();
   82        }
   83        usedNames.add(lower);
   84        return unique;
   85    }
   86
   87}