testmeclasspathprovider.java

来自「cqME :java framework for TCK test.」· Java 代码 · 共 255 行

JAVA
255
字号
/*
 * $Id$
 *
 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 only, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License version 2 for more details (a copy is
 * included at /legal/license.txt).
 *
 * You should have received a copy of the GNU General Public License
 * version 2 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 * Clara, CA 95054 or visit www.sun.com if you need additional
 * information or have any questions.
 *
 */

package com.sun.testme.project;

import com.sun.testme.data.TestConfiguration;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.api.java.classpath.GlobalPathRegistry;
import org.netbeans.spi.java.classpath.ClassPathFactory;
import org.netbeans.spi.java.classpath.ClassPathImplementation;
import org.netbeans.spi.java.classpath.ClassPathProvider;
import org.netbeans.spi.java.classpath.PathResourceImplementation;
import org.netbeans.spi.java.classpath.support.ClassPathSupport;
import org.openide.filesystems.FileAttributeEvent;
import org.openide.filesystems.FileChangeListener;
import org.openide.filesystems.FileEvent;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileRenameEvent;
import org.openide.filesystems.FileUtil;

/**
 *
 * @author rz155578
 */
public class TestMEClassPathProvider implements ClassPathProvider {
    
    public TestMEClassPathProvider(TestMEProject project) {
        this.project = project;
    }
    
    private HashMap sourceCps = new HashMap();
    private HashMap cbeCps = new HashMap();
    
    public ClassPath findClassPath(FileObject fileObject, String string) {
        File file = FileUtil.toFile(fileObject);
        if (file == null) {
            return null;
        }
        String path = file.getPath();
        if (string.equals(ClassPath.SOURCE)) {
            if (sourceCps.containsKey(path)) {
                ClassPath cp = (ClassPath)sourceCps.get(path);
                return cp;
            }
            FileObject cpFob = fileObject;
            if (!fileObject.isFolder()) {
                cpFob = fileObject.getParent();
            }
            FileClassPathImplementation impl = new FileClassPathImplementation(FileUtil.toFile(cpFob));
            ClassPath cp = ClassPathFactory.createClassPath(impl);
            GlobalPathRegistry.getDefault().register(string, (ClassPath[])new ClassPath[]{cp});
            sourceCps.put(path, cp);
            return cp;
        } else {
            ClassPath cp = (ClassPath)cbeCps.get(path);
            if (cp != null) {
                return cp;
            }
            TestConfiguration conf = (TestConfiguration)confs.get(path);
            if (conf == null) {
                conf = TestConfiguration.tryToLoad(fileObject);
                if (conf == null) {
                    return null;
                } else {
                    confs.put(path, conf);
                }
            }
            TestMEClassPathImplementation impl = new TestMEClassPathImplementation(conf);
            cp = ClassPathFactory.createClassPath(impl);
            GlobalPathRegistry.getDefault().register(string, (ClassPath[])new ClassPath[]{cp});
            cbeCps.put(path, cp);
            return cp;
        }
    }
        
    private class TestMEClassPathImplementation implements ClassPathImplementation, FileChangeListener {
        
        public TestMEClassPathImplementation(TestConfiguration conf) {
            this.conf = conf;
            FileObject fob = FileUtil.toFileObject(new File(conf.getConfFileName()));
            fob.addFileChangeListener(this);
        }
       
        public List getResources() {
            List res = new ArrayList();
            try {
                Vector platformCp = conf.getPlatformCp();
                for (int i = 0; i < platformCp.size(); i++) {
                    String curCp = (String)platformCp.get(i);
                    File file = new File(curCp);
                    file = FileUtil.normalizeFile(file);
                    PathResourceImplementation cp = null;
                    if (file.isFile()) {
                        URL url = file.toURI().toURL();
                        String sURL = url.toString();
                        sURL = "jar:" + sURL + "!/";
                        URL url2 = new URL(sURL);
                        cp = ClassPathSupport.createResource(url2);
                    } else {
                        cp = ClassPathSupport.createResource(file.toURI().toURL());
                    }
                    res.add(cp);
                }
                Vector appCp = conf.getAppCp();
                for (int i = 0; i < appCp.size(); i++) {
                    String curCp = (String)appCp.get(i);
                    File file = new File(curCp);
                    file = FileUtil.normalizeFile(file);
                    PathResourceImplementation cp = null;
                    if (file.isFile()) {
                        URL url = file.toURI().toURL();
                        String sURL = url.toString();
                        sURL = "jar:" + sURL + "!/";
                        URL url2 = new URL(sURL);
                        cp = ClassPathSupport.createResource(url2);
                    } else {
                        cp = ClassPathSupport.createResource(file.toURI().toURL());
                    }
                    res.add(cp);
                }
                
            } catch (Exception e) {
                e.printStackTrace();
            }
            return res;
        }

        public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
            pcs.addPropertyChangeListener(propertyChangeListener);
        }

        public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
            pcs.removePropertyChangeListener(propertyChangeListener);
        }

        public void fileFolderCreated(FileEvent fileEvent) {
        }

        public void fileDataCreated(FileEvent fileEvent) {
        }

        public void fileChanged(FileEvent fileEvent) {
            conf.update();
            pcs.firePropertyChange(ClassPathImplementation.PROP_RESOURCES, null, null);
        }

        public void fileDeleted(FileEvent fileEvent) {
        }

        public void fileRenamed(FileRenameEvent fileRenameEvent) {
        }

        public void fileAttributeChanged(FileAttributeEvent fileAttributeEvent) {
        }
        
        private TestConfiguration conf;
        private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
    }
    
    private class FileClassPathImplementation implements ClassPathImplementation, FileChangeListener  {
        
        public FileClassPathImplementation(FileObject f, TestConfiguration conf) {
            this.f = f;
            this.conf = conf;
            FileObject fob = FileUtil.toFileObject(new File(conf.getConfFileName()));
            fob.addFileChangeListener(this);
        }

        public FileClassPathImplementation(File file) {
            this.f = FileUtil.toFileObject(file);
        }
        
        public List getResources() {
            List res = new ArrayList();
            try {
                File file = FileUtil.toFile(f);
                file = FileUtil.normalizeFile(file);
                res.add(ClassPathSupport.createResource(file.toURI().toURL()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            return res;
        }

        public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
            pcs.addPropertyChangeListener(propertyChangeListener);
        }

        public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
            pcs.removePropertyChangeListener(propertyChangeListener);
        }

        public void fileFolderCreated(FileEvent fileEvent) {
        }

        public void fileDataCreated(FileEvent fileEvent) {
        }

        public void fileChanged(FileEvent fileEvent) {
            conf.update();
            pcs.firePropertyChange(ClassPathImplementation.PROP_RESOURCES, null, null);
        }

        public void fileDeleted(FileEvent fileEvent) {
        }

        public void fileRenamed(FileRenameEvent fileRenameEvent) {
        }

        public void fileAttributeChanged(FileAttributeEvent fileAttributeEvent) {
        }
        
        private FileObject f;
        private TestConfiguration conf;
        private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
    }
    
    private HashMap confs = new HashMap();
    private TestMEProject project;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?