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.editor.java;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.spi.java.classpath.ClassPathProvider;
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.LocalFileSystem;
import org.openide.filesystems.Repository;

/**
 * Provide some classpath for the tested Experiment.java file.
 *
 * @autor David Konecny
 */
public class ClassPathProviderImpl implements ClassPathProvider {

    /** Source classpath. */
    private Map cp;
    
    /** Experiment.java has no COMPILE classpath */
    public static final int MODE_ALONE = 0;
    
    /** Experiment.java has on COMPILE classpath junit.jar + nbjunit.jar*/
    public static final int MODE_JUNIT_NBUNIT = 1;
    
    private int mode = 0;
    
    private ClassPath bootcp;
    
    public ClassPathProviderImpl() {
        cp = new HashMap();
        cp.put("src01/ex/Experiment.java", "src01");
        cp.put("src02/exa/Big/BigBig/Foo.java", "src02");
        cp.put("src02/exa/norm/Norm.java", "src02");
        cp.put("src02/exa/small/bar.java", "src02");
        cp.put("src02/exa/Main.java", "src02");
        cp.put("data01/root1/test/unit/src/Empty.java", "data01/root1/test/unit/src");
    }
    
    
    public ClassPath findClassPath(FileObject file, String type) {
        FileSystem fs = TestUtils.getDataFilesystem();
        FileObject root = TestUtils.getDataFolder();
        if (fs == null || root == null) {
            // it happens that background parser asks for classpath 
            // after test was finished. ignore it
            return null;
        }
        if (type.equals(ClassPath.SOURCE)) {
            String path = (String)cp.get(file.getPath());
            if (path != null) {
                FileObject fo = fs.findResource(path);
                return ClassPathSupport.createClassPath(new FileObject[]{fo});
            }
        }
        if (file.getNameExt().equals("Experiment.java") && type.equals(ClassPath.COMPILE) && mode == MODE_JUNIT_NBUNIT) {
            ClassPath cp = ClassPathSupport.createClassPath(new FileObject[]{
                FileUtil.getArchiveRoot(root.getFileObject("junit", "jar")),
                FileUtil.getArchiveRoot(root.getFileObject("nbjunit", "jar"))});
            return cp;
        }
        if (file.getNameExt().equals("App.java") && type.equals(ClassPath.COMPILE)) {
            ClassPath cp = ClassPathSupport.createClassPath(new FileObject[]{
                fs.findResource("data02/lib1"),
                fs.findResource("data02/lib2")});
            return cp;
        }
        if (type.equals(ClassPath.BOOT)) {
            if (bootcp == null) {
                bootcp = createClassPath(System.getProperty("sun.boot.class.path"));
            }
            return bootcp;
        }
        return null;
    }
    
    /**
     * 
     */
    public void setMode(int i) {
        mode = i;
    }
    
    private ClassPath createClassPath(String classpath) {
        StringTokenizer tokenizer = new StringTokenizer(classpath, File.pathSeparator);
        List/**/ list = new ArrayList();
        while (tokenizer.hasMoreTokens()) {
            String item = tokenizer.nextToken();
            try {
                mount(item);
                URL url;
                if (item.endsWith(".jar") || item.endsWith(".zip")) {
                    url = new URL("jar:"+new File(item).toURI().toURL().toExternalForm()+"!/");
                } else {
                    url = new File(item).toURI().toURL();
                }
                list.add(ClassPathSupport.createResource(url));
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        return ClassPathSupport.createClassPath(list);
    }
    
    private void mount(String path) {
        File f = new File(path);
        if (FileUtil.fromFile(f).length == 0) {
            f = f.getParentFile();
            if (!f.isDirectory()) {
                throw new RuntimeException("Must be folder: "+f);
            }
            LocalFileSystem lfs = new LocalFileSystem();
            try {
                lfs.setRootDirectory(f);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            Repository.getDefault().addFileSystem(lfs);
        }
    }
    
}
... 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.