alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.javacore.parser;

import java.io.Reader;
import org.netbeans.lib.java.parser.ECRequestDesc;
import org.netbeans.lib.java.parser.ErrConsumer;
import org.openide.ErrorManager;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import java.util.*;
import java.net.URL;
import java.net.URI;
import java.io.File;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.api.java.queries.SourceForBinaryQuery;

/**
 *
 * @author  Pavel Flaska
 */
public class ECRequestDescImpl implements ECRequestDesc {
    private final String sourcePath;
    private final String classPath;
    private final String bootPath;
    private final ASTProvider provider;
    private final ErrConsumer consumer;

    /** Creates a new instance of ECRequestDescImpl */
    public ECRequestDescImpl(ASTProvider provider, ErrConsumer consumer) {
        this.provider = provider;
        this.consumer = consumer;

        StringBuffer compileRoots=new StringBuffer(241);
        StringBuffer sourceRoots=new StringBuffer(246);
        FileObject fo=provider.getDataObject().getPrimaryFile();
        ClassPath cp = ClassPath.getClassPath(fo, ClassPath.EXECUTE);

        Set cprootsSet = new HashSet();
        getCompileAndSourcePath(cp,sourceRoots,compileRoots, cprootsSet);
        cp = ClassPath.getClassPath(fo, ClassPath.SOURCE);
        getSourcePath(cp, sourceRoots, cprootsSet);
        sourcePath=sourceRoots.toString();
        classPath=compileRoots.toString();
        bootPath=getClassPathString(ClassPath.getClassPath(fo, ClassPath.BOOT));
    }
    
    public String getBootClassPath() {
        return bootPath;
    }
    
    public String getClassPath() {
        return classPath;
    }
    
    public ErrConsumer getErrConsumer() {
        return consumer;
    }
    
    public String getFileName() {
        return provider.getFileName();
    }
    
    public Reader getReader() {
        try {
            return provider.getFileReader(false);
        } catch (Exception ex) {
            ErrorManager.getDefault().notify(ex);
            return null;
        }
    }
    
    public String getSourceClassPath() {
        return sourcePath;
    }
    
    public String getSourceLevel() {
        return provider.getSourceLevel();
    }
    
    private void getCompileAndSourcePath(ClassPath classPath, StringBuffer sourceRoots, StringBuffer compileRoots, Set cprootsSet) {
        if (classPath != null) {
            for (Iterator it = classPath.entries().iterator(); it.hasNext(); ) {
                ClassPath.Entry entry = (ClassPath.Entry)it.next();
                FileObject root = entry.getRoot();
                if (root != null) {
                    cprootsSet.add(root);
                    getFileName(root,compileRoots);
                }
                FileObject[] sRoots = SourceForBinaryQuery.findSourceRoots(entry.getURL()).getRoots();
                for (int x = 0; x < sRoots.length; x++) {
                    FileObject sRoot=sRoots[x];
                    
                    if (!cprootsSet.contains(sRoot)) {
                        cprootsSet.add(sRoot);
                        getFileName(sRoot,sourceRoots);
                    } // if
                } // for
            } // for
        } // if
    }
    
    private void getSourcePath(ClassPath classPath, StringBuffer sourceRoots, Set cprootsSet) {
        if (classPath != null) {
            for (Iterator it = classPath.entries().iterator(); it.hasNext(); ) {
                ClassPath.Entry entry = (ClassPath.Entry)it.next();
                FileObject root = entry.getRoot();
                if (root != null && cprootsSet.add(root)) {
                    getFileName(root,sourceRoots);
                }
            } // for
        } // if
    }

    void getFileName(FileObject fo,StringBuffer buf) {
        try {
            URL url=fo.getURL();
            if (url.getProtocol().equals("jar")) { // NOI18N
                url=FileUtil.getArchiveFile(url);
            }
            URI uri = new URI(url.toExternalForm());
            File f =  new File(uri);
            
            if (buf.length()>0)
                buf.append(File.pathSeparatorChar);
            buf.append(f.getAbsolutePath());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    String getClassPathString(ClassPath cp) {
        FileObject[] roots = cp.getRoots();
        if (roots.length == 0)
            return "";
        StringBuffer buf = new StringBuffer(237);
        for (int i = 0; i < roots.length; i++) {
            getFileName(roots[i],buf);
        }
        return buf.toString();
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.