1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2009 Eric Lafortune (eric@graphics.cornell.edu)
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21package proguard.io;
22
23import proguard.classfile.ClassConstants;
24
25import java.io.IOException;
26import java.util.Map;
27
28/**
29 * This DataEntryReader delegates to another DataEntryReader, renaming the
30 * data entries based on the given map. Entries whose name does not appear
31 * in the map may be passed to an alternative DataEntryReader.
32 *
33 * @author Eric Lafortune
34 */
35public class DataEntryRenamer implements DataEntryReader
36{
37    private final Map             nameMap;
38    private final DataEntryReader renamedDataEntryReader;
39    private final DataEntryReader missingDataEntryReader;
40
41
42    /**
43     * Creates a new DataEntryRenamer.
44     * @param nameMap                the map from old names to new names.
45     * @param renamedDataEntryReader the DataEntryReader to which renamed data
46     *                               entries will be passed.
47     */
48    public DataEntryRenamer(Map             nameMap,
49                            DataEntryReader renamedDataEntryReader)
50    {
51        this(nameMap, renamedDataEntryReader, null);
52    }
53
54
55    /**
56     * Creates a new DataEntryRenamer.
57     * @param nameMap                the map from old names to new names.
58     * @param renamedDataEntryReader the DataEntryReader to which renamed data
59     *                               entries will be passed.
60     * @param missingDataEntryReader the optional DataEntryReader to which data
61     *                               entries that can't be renamed will be
62     *                               passed.
63     */
64    public DataEntryRenamer(Map             nameMap,
65                            DataEntryReader renamedDataEntryReader,
66                            DataEntryReader missingDataEntryReader)
67    {
68        this.nameMap                = nameMap;
69        this.renamedDataEntryReader = renamedDataEntryReader;
70        this.missingDataEntryReader = missingDataEntryReader;
71    }
72
73
74    // Implementations for DataEntryReader.
75
76    public void read(DataEntry dataEntry) throws IOException
77    {
78        String name = dataEntry.getName();
79
80        // Add a directory separator if necessary.
81        if (dataEntry.isDirectory() &&
82            name.length() > 0)
83        {
84            name += ClassConstants.INTERNAL_PACKAGE_SEPARATOR;
85        }
86
87        String newName = (String)nameMap.get(name);
88        if (newName != null)
89        {
90            // Add remove the directory separator if necessary.
91            if (dataEntry.isDirectory() &&
92                newName.length() > 0)
93            {
94                newName = newName.substring(0, newName.length() -  1);
95            }
96
97            renamedDataEntryReader.read(new RenamedDataEntry(dataEntry, newName));
98        }
99        else if (missingDataEntryReader != null)
100        {
101            missingDataEntryReader.read(dataEntry);
102        }
103    }
104}