langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/file/SymbolArchive.java
changeset 27579 d1a63c99cdd5
parent 27578 d61af14a5cf7
child 27580 ca12cdda0551
equal deleted inserted replaced
27578:d61af14a5cf7 27579:d1a63c99cdd5
     1 /*
       
     2  * Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package com.sun.tools.javac.file;
       
    27 
       
    28 import java.io.File;
       
    29 import java.io.IOException;
       
    30 import java.util.zip.ZipEntry;
       
    31 import java.util.zip.ZipFile;
       
    32 import javax.tools.JavaFileObject;
       
    33 
       
    34 import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
       
    35 import com.sun.tools.javac.file.RelativePath.RelativeFile;
       
    36 import com.sun.tools.javac.util.List;
       
    37 
       
    38 /**
       
    39  * <p><b>This is NOT part of any supported API.
       
    40  * If you write code that depends on this, you do so at your own risk.
       
    41  * This code and its internal interfaces are subject to change or
       
    42  * deletion without notice.</b>
       
    43 */
       
    44 public class SymbolArchive extends ZipArchive {
       
    45 
       
    46     final File origFile;
       
    47     final RelativeDirectory prefix;
       
    48 
       
    49     public SymbolArchive(JavacFileManager fileManager, File orig, ZipFile zdir, RelativeDirectory prefix) throws IOException {
       
    50         super(fileManager, zdir, false);
       
    51         this.origFile = orig;
       
    52         this.prefix = prefix;
       
    53         initMap();
       
    54     }
       
    55 
       
    56     @Override
       
    57     void addZipEntry(ZipEntry entry) {
       
    58         String name = entry.getName();
       
    59         if (!name.startsWith(prefix.path)) {
       
    60             return;
       
    61         }
       
    62         name = name.substring(prefix.path.length());
       
    63         int i = name.lastIndexOf('/');
       
    64         RelativeDirectory dirname = new RelativeDirectory(name.substring(0, i+1));
       
    65         String basename = name.substring(i + 1);
       
    66         if (basename.length() == 0) {
       
    67             return;
       
    68         }
       
    69         List<String> list = map.get(dirname);
       
    70         if (list == null)
       
    71             list = List.nil();
       
    72         list = list.prepend(basename);
       
    73         map.put(dirname, list);
       
    74     }
       
    75 
       
    76     @Override
       
    77     public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
       
    78         RelativeDirectory prefix_subdir = new RelativeDirectory(prefix, subdirectory.path);
       
    79         ZipEntry ze = new RelativeFile(prefix_subdir, file).getZipEntry(zfile);
       
    80         return new SymbolFileObject(this, file, ze);
       
    81     }
       
    82 
       
    83     @Override
       
    84     public String toString() {
       
    85         return "SymbolArchive[" + zfile.getName() + "]";
       
    86     }
       
    87 
       
    88     /**
       
    89      * A subclass of JavaFileObject representing zip entries in a symbol file.
       
    90      */
       
    91     public static class SymbolFileObject extends ZipFileObject {
       
    92         protected SymbolFileObject(SymbolArchive zarch, String name, ZipEntry entry) {
       
    93             super(zarch, name, entry);
       
    94         }
       
    95 
       
    96         @Override
       
    97         protected String inferBinaryName(Iterable<? extends File> path) {
       
    98             String entryName = entry.getName();
       
    99             String prefix = ((SymbolArchive) zarch).prefix.path;
       
   100             if (entryName.startsWith(prefix))
       
   101                 entryName = entryName.substring(prefix.length());
       
   102             return removeExtension(entryName).replace('/', '.');
       
   103         }
       
   104     }
       
   105 
       
   106 
       
   107 }