001    /*
002     * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.tools.javac.file;
027    
028    import java.io.File;
029    import java.io.IOException;
030    import java.util.zip.ZipEntry;
031    import java.util.zip.ZipFile;
032    import javax.tools.JavaFileObject;
033    
034    import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
035    import com.sun.tools.javac.file.RelativePath.RelativeFile;
036    import com.sun.tools.javac.util.List;
037    
038    public class SymbolArchive extends ZipArchive {
039    
040        final File origFile;
041        final RelativeDirectory prefix;
042    
043        public SymbolArchive(JavacFileManager fileManager, File orig, ZipFile zdir, RelativeDirectory prefix) throws IOException {
044            super(fileManager, zdir, false);
045            this.origFile = orig;
046            this.prefix = prefix;
047            initMap();
048        }
049    
050        @Override
051        void addZipEntry(ZipEntry entry) {
052            String name = entry.getName();
053            if (!name.startsWith(prefix.path)) {
054                return;
055            }
056            name = name.substring(prefix.path.length());
057            int i = name.lastIndexOf('/');
058            RelativeDirectory dirname = new RelativeDirectory(name.substring(0, i+1));
059            String basename = name.substring(i + 1);
060            if (basename.length() == 0) {
061                return;
062            }
063            List<String> list = map.get(dirname);
064            if (list == null)
065                list = List.nil();
066            list = list.prepend(basename);
067            map.put(dirname, list);
068        }
069    
070        public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
071            RelativeDirectory prefix_subdir = new RelativeDirectory(prefix, subdirectory.path);
072            ZipEntry ze = new RelativeFile(prefix_subdir, file).getZipEntry(zdir);
073            return new SymbolFileObject(this, file, ze);
074        }
075    
076        public String toString() {
077            return "SymbolArchive[" + zdir.getName() + "]";
078        }
079    
080        /**
081         * A subclass of JavaFileObject representing zip entries in a symbol file.
082         */
083        public static class SymbolFileObject extends ZipFileObject {
084            protected SymbolFileObject(SymbolArchive zarch, String name, ZipEntry entry) {
085                super(zarch, name, entry);
086            }
087    
088            @Override
089            protected String inferBinaryName(Iterable<? extends File> path) {
090                String entryName = getZipEntryName();
091                String prefix = ((SymbolArchive) zarch).prefix.path;
092                if (entryName.startsWith(prefix))
093                    entryName = entryName.substring(prefix.length());
094                return removeExtension(entryName).replace('/', '.');
095            }
096        }
097    
098    
099    }