langtools/src/share/classes/com/sun/tools/javadoc/JavadocClassReader.java
changeset 10 06bc494ca11e
child 731 1dd22bdb9ca5
equal deleted inserted replaced
0:fd16c54261b3 10:06bc494ca11e
       
     1 /*
       
     2  * Copyright 2001-2006 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package com.sun.tools.javadoc;
       
    27 
       
    28 import com.sun.tools.javac.code.Symbol.PackageSymbol;
       
    29 import com.sun.tools.javac.jvm.ClassReader;
       
    30 import com.sun.tools.javac.util.Context;
       
    31 import com.sun.tools.javac.util.JavacFileManager;
       
    32 import com.sun.tools.javac.util.Old199;
       
    33 
       
    34 import java.io.File;
       
    35 import java.util.EnumSet;
       
    36 import javax.tools.JavaFileObject;
       
    37 
       
    38 /** Javadoc uses an extended class reader that records package.html entries
       
    39  *  @author Neal Gafter
       
    40  */
       
    41 class JavadocClassReader extends ClassReader {
       
    42 
       
    43     public static JavadocClassReader instance0(Context context) {
       
    44         ClassReader instance = context.get(classReaderKey);
       
    45         if (instance == null)
       
    46             instance = new JavadocClassReader(context);
       
    47         return (JavadocClassReader)instance;
       
    48     }
       
    49 
       
    50     public static void preRegister(final Context context) {
       
    51         context.put(classReaderKey, new Context.Factory<ClassReader>() {
       
    52             public ClassReader make() {
       
    53                 return new JavadocClassReader(context);
       
    54             }
       
    55         });
       
    56     }
       
    57 
       
    58     private DocEnv docenv;
       
    59     private EnumSet<JavaFileObject.Kind> all = EnumSet.of(JavaFileObject.Kind.CLASS,
       
    60                                                           JavaFileObject.Kind.SOURCE,
       
    61                                                           JavaFileObject.Kind.HTML);
       
    62     private EnumSet<JavaFileObject.Kind> noSource = EnumSet.of(JavaFileObject.Kind.CLASS,
       
    63                                                                JavaFileObject.Kind.HTML);
       
    64 
       
    65     private JavadocClassReader(Context context) {
       
    66         super(context, true);
       
    67         docenv = DocEnv.instance(context);
       
    68     }
       
    69 
       
    70     /**
       
    71      * Override getPackageFileKinds to include search for package.html
       
    72      */
       
    73     @Override
       
    74     protected EnumSet<JavaFileObject.Kind> getPackageFileKinds() {
       
    75         return docenv.docClasses ? noSource : all;
       
    76     }
       
    77 
       
    78     /**
       
    79      * Override extraFileActions to check for package documentation
       
    80      */
       
    81     @Override
       
    82     protected void extraFileActions(PackageSymbol pack, JavaFileObject fo) {
       
    83         CharSequence fileName = Old199.getName(fo);
       
    84         if (docenv != null && fileName.equals("package.html")) {
       
    85             if (fo instanceof JavacFileManager.ZipFileObject) {
       
    86                 JavacFileManager.ZipFileObject zfo = (JavacFileManager.ZipFileObject) fo;
       
    87                 String zipName = zfo.getZipName();
       
    88                 String entryName = zfo.getZipEntryName();
       
    89                 int lastSep = entryName.lastIndexOf("/");
       
    90                 String classPathName = entryName.substring(0, lastSep + 1);
       
    91                 docenv.getPackageDoc(pack).setDocPath(zipName, classPathName);
       
    92             }
       
    93             else if (fo instanceof JavacFileManager.ZipFileIndexFileObject) {
       
    94                 JavacFileManager.ZipFileIndexFileObject zfo = (JavacFileManager.ZipFileIndexFileObject) fo;
       
    95                 String zipName = zfo.getZipName();
       
    96                 String entryName = zfo.getZipEntryName();
       
    97                 if (File.separatorChar != '/') {
       
    98                     entryName = entryName.replace(File.separatorChar, '/');
       
    99                 }
       
   100 
       
   101                 int lastSep = entryName.lastIndexOf("/");
       
   102                 String classPathName = entryName.substring(0, lastSep + 1);
       
   103                 docenv.getPackageDoc(pack).setDocPath(zipName, classPathName);
       
   104             }
       
   105             else {
       
   106                 File fileDir = new File(Old199.getPath(fo)).getParentFile();
       
   107                 docenv.getPackageDoc(pack).setDocPath(fileDir.getAbsolutePath());
       
   108             }
       
   109         }
       
   110     }
       
   111 }