Java Code Examples for org.openide.filesystems.FileObject.getURL()

The following are Jave code examples for showing how to use getURL() of the org.openide.filesystems.FileObject class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: IconEditor.java   View Source Code Vote up 6 votes
private static NbImageIcon iconFromResourceName(String resName, FileObject srcFile) {
    ClassPath cp = ClassPath.getClassPath(srcFile, ClassPath.SOURCE);
    FileObject fo = cp.findResource(resName);
    if (fo == null) {
        cp = ClassPath.getClassPath(srcFile, ClassPath.EXECUTE);
        fo = cp.findResource(resName);
    }
    if (fo != null) {
        try {
            try {
                Image image = ImageIO.read(fo.getURL());
                if (image != null) { // issue 157546
                    return new NbImageIcon(TYPE_CLASSPATH, resName, new ImageIcon(image));
                }
            } catch (IllegalArgumentException iaex) { // Issue 178906
                Logger.getLogger(IconEditor.class.getName()).log(Level.INFO, null, iaex);
                return new NbImageIcon(TYPE_CLASSPATH, resName, new ImageIcon(fo.getURL()));
            }
        } catch (IOException ex) { // should not happen
            Logger.getLogger(IconEditor.class.getName()).log(Level.WARNING, null, ex);
        }
    }
    return null;
}
 
Example 2
Project: incubator-netbeans   File: ParsingSupport.java   View Source Code Vote up 6 votes
/**
 * Converts to InputSource and pass it.
 */    
protected TreeDocumentRoot parse(FileObject fo) throws IOException, TreeException{
    
    try {
        URL url = fo.getURL();
        InputSource in = new InputSource(url.toExternalForm());  //!!! we could try ti get encoding from MIME content type
        in.setByteStream(fo.getInputStream());
        return parse(in);
        
    } catch (IOException ex) {
        ErrorManager emgr = ErrorManager.getDefault();
        emgr.annotate(ex, Util.THIS.getString("MSG_can_not_create_URL"));
        emgr.notify(ex);
    }           
    return null;
}
 
Example 3
Project: incubator-netbeans   File: NbURLMapper.java   View Source Code Vote up 5 votes
protected URL getUrl(JTextComponent comp) {
    FileObject f = null;
    
    if (comp instanceof Lookup.Provider) {
        f = ((Lookup.Provider) comp).getLookup().lookup(FileObject.class);
    }
    
    if (f == null) {
        Container container = comp.getParent();
        while (container != null) {
            if (container instanceof Lookup.Provider) {
                f = ((Lookup.Provider) container).getLookup().lookup(FileObject.class);
                if (f != null) {
                    break;
                }
            }
            
            container = container.getParent();
        }
    }
    
    if (f != null) {
        try {
            return f.getURL();
        } catch (FileStateInvalidException e) {
            LOG.log(Level.WARNING, "Can't get URL for " + f, e); //NOI18N
        }
    }
    
    return null;
}
 
Example 4
Project: incubator-netbeans   File: ElementJavadoc.java   View Source Code Vote up 5 votes
private String noJavadocFound() {
    if (handle != null) {
        final List<ClassPath> cps = new ArrayList<>(2);
        ClassPath cp = cpInfo.getClassPath(ClasspathInfo.PathKind.BOOT);
        if (cp != null) {
            cps.add(cp);
        }
        cp = cpInfo.getClassPath(ClasspathInfo.PathKind.COMPILE);
        if (cp != null) {
            cps.add(cp);
        }
        cp = ClassPathSupport.createProxyClassPath(cps.toArray(new ClassPath[cps.size()]));
        String toSearch = SourceUtils.getJVMSignature(handle)[0].replace('.', '/');
        if (handle.getKind() != ElementKind.PACKAGE) {
            toSearch += ".class"; //NOI18N
        }
        final FileObject resource = cp.findResource(toSearch);
        if (resource != null) {
            final FileObject root = cp.findOwnerRoot(resource);
            try {
                final URL rootURL = root.getURL();
                if (JavadocForBinaryQuery.findJavadoc(rootURL).getRoots().length == 0) {
                    FileObject userRoot = FileUtil.getArchiveFile(root);
                    if (userRoot == null) {
                        userRoot = root;
                    }
                    return NbBundle.getMessage(
                            ElementJavadoc.class,
                            "javadoc_content_not_found_attach",
                            rootURL.toExternalForm(),
                            FileUtil.getFileDisplayName(userRoot));
                }
            } catch (FileStateInvalidException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }
    return NbBundle.getMessage(ElementJavadoc.class, "javadoc_content_not_found"); //NOI18N
}
 
Example 5
Project: incubator-netbeans   File: CustomIconEditor.java   View Source Code Vote up 5 votes
IconFileItem(FileObject file) {
    this.file = file;
    try {
        try {
            Image image = (file.getSize() < SIZE_LIMIT) ? ImageIO.read(file.getURL()) : null;
            icon = (image != null) ? new ImageIcon(image) : null;
        } catch (IllegalArgumentException iaex) { // Issue 178906
            Logger.getLogger(CustomIconEditor.class.getName()).log(Level.INFO, null, iaex);
            icon = new ImageIcon(file.getURL());
        }
    } catch (IOException ex) {
        Logger.getLogger(CustomIconEditor.class.getName()).log(Level.WARNING, null, ex);
    }
}
 
Example 6
Project: incubator-netbeans   File: TreePathHandle.java   View Source Code Vote up 5 votes
public FileObject getFileObject() {
    //source does not exist
    FileObject file = SourceUtils.getFile(el, cpInfo);
    //tzezula: Very strange and probably useless
    if (file == null && source != null) {
        FileObject fo = URLMapper.findFileObject(source);
        if (fo == null) {
            log.log(Level.INFO, "There is no fileobject for source: " +source + ". Was this file removed?");
            return file;
        }
        file = fo;
        if (fo.getNameExt().endsWith(FileObjects.SIG)) {
            //NOI18N
            //conversion sig -> class
            String pkgName = FileObjects.convertPackage2Folder(qualName);
            StringTokenizer tk = new StringTokenizer(pkgName, "/"); //NOI18N
            for (int i = 0; fo != null && i <= tk.countTokens(); i++) {
                fo = fo.getParent();
            }
            if (fo != null) {
                try {
                    URL url = fo.getURL();
                    URL sourceRoot = null;//XXX: Index.getSourceRootForClassFolder(url);
                    if (sourceRoot != null) {
                        FileObject root = URLMapper.findFileObject(sourceRoot);
                        String resourceName = FileUtil.getRelativePath(fo, URLMapper.findFileObject(source));
                        file = root.getFileObject(resourceName.replace('.'+FileObjects.SIG, '.'+FileObjects.CLASS)); //NOI18N
                    } else {
                        Logger.getLogger(TreePathHandle.class.getName()).fine("Index.getSourceRootForClassFolder(url) returned null for url=" + url); //NOI18N
                    }
                } catch (FileStateInvalidException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        }
    }
    
    return file;
}
 
Example 7
Project: incubator-netbeans   File: IndexerTransactionTest.java   View Source Code Vote up 5 votes
public static void register (FileObject binRoot, FileObject sourceRoot) throws IOException {
    URL url = binRoot.getURL();
    map.put (url,sourceRoot);
    Result r = results.get (url);
    if (r != null) {
        r.update (sourceRoot);
    }
}
 
Example 8
Project: incubator-netbeans   File: IndexerTransactionTest.java   View Source Code Vote up 5 votes
public static void unregister (FileObject binRoot) throws IOException {
    URL url = binRoot.getURL();
    map.remove(url);
    Result r = results.get (url);
    if (r != null) {
        r.update (null);
    }
}
 
Example 9
Project: incubator-netbeans   File: ProjectUtilities.java   View Source Code Vote up 5 votes
public static URL copyAppletHTML(Project project, PropertyEvaluator props, FileObject profiledClassFile, String value) {
    try {
        String buildDirProp = props.getProperty("build.dir"); //NOI18N

        FileObject buildFolder = getOrCreateBuildFolder(project, buildDirProp);

        FileObject htmlFile;
        htmlFile = profiledClassFile.getParent().getFileObject(profiledClassFile.getName(), "html"); //NOI18N

        if (htmlFile == null) {
            htmlFile = profiledClassFile.getParent().getFileObject(profiledClassFile.getName(), "HTML"); //NOI18N
        }

        if (htmlFile == null) {
            return null;
        }

        FileObject existingFile = buildFolder.getFileObject(htmlFile.getName(), htmlFile.getExt());

        if (existingFile != null) {
            existingFile.delete();
        }

        htmlFile.copy(buildFolder, profiledClassFile.getName(), value).getURL();

        return htmlFile.getURL();
    } catch (IOException e) {
        ErrorManager.getDefault()
                    .annotate(e, Bundle.ProjectUtilities_FailedCopyAppletFileMsg(e.getMessage()));
        ErrorManager.getDefault().notify(ErrorManager.ERROR, e);

        return null;
    }
}
 
Example 10
Project: incubator-netbeans   File: TestQuery.java   View Source Code Vote up 5 votes
public URL[] findUnitTests(FileObject source) {
    URL[][] data = findSourcesAndTests();
    URL sourceURL;
    try {
        sourceURL = source.getURL();
    } catch (FileStateInvalidException e) {
        return null;
    }
    if (Arrays.asList(data[0]).contains(sourceURL)) {
        return data[1];
    } else {
        return null;
    }
}
 
Example 11
Project: incubator-netbeans   File: TestQuery.java   View Source Code Vote up 5 votes
public URL[] findSources(FileObject unitTest) {
    URL[][] data = findSourcesAndTests();
    URL testURL;
    try {
        testURL = unitTest.getURL();
    } catch (FileStateInvalidException e) {
        return null;
    }
    if (Arrays.asList(data[1]).contains(testURL)) {
        return data[0];
    } else {
        return null;
    }
}
 
Example 12
Project: incubator-netbeans   File: URLMapperTest.java   View Source Code Vote up 5 votes
private void checkFileObjectURLMapping(FileObject fo, URL url, URLMapper mapper) throws Exception {
    log ("Testing " + fo);
    log ("     -> " + url);
    assertNotNull("The file tested is null.", fo);
    assertNotNull("Mapper does not produce a URL for file " + fo, url);
    FileObject newFo[] = mapper.getFileObjects(url);
    assertNotNull("Mapper does not produce file for URL " + url, newFo);
    if (newFo.length != 1) {
        fail("Mapper returned array of size " + newFo.length + " for URL " + url);
    }
    assertEquals("Mapping does not produce the original object: " + fo + " != " + newFo, fo, newFo[0]);
    // compare the streams
    URL u2 = fo.getURL();
    compareStream(url.openStream(), u2.openStream());
}
 
Example 13
Project: incubator-netbeans   File: BiIconEditor.java   View Source Code Vote up 5 votes
/**
 * translates resource path defined in {@link java.beans.BeanInfo}'s subclass
 * that complies with {@link Class#getResource(java.lang.String) Class.getResource} format
 * to format complying with {@link ClassPath#getResourceName(org.openide.filesystems.FileObject) ClassPath.getResourceName}
 * @param resourcePath absolute path or path relative to package of BeanInfo's subclass
 * @param beanInfo BeanInfo's subclass
 * @return path as URL
 * @throws FileStateInvalidException invalid FileObject
 * @throws FileNotFoundException resource cannot be found
 */
private static URL resolveIconPath(String resourcePath, FileObject beanInfo)
        throws FileStateInvalidException, FileNotFoundException {
    ClassPath cp = ClassPath.getClassPath(beanInfo, ClassPath.SOURCE);
    String path = resourcePath.charAt(0) != '/'
            ? '/' + cp.getResourceName(beanInfo.getParent()) + '/' + resourcePath
            : resourcePath;
    FileObject res = cp.findResource(path);
    if (res != null && res.canRead() && res.isData()) {
        return res.getURL();
    } else {
        throw new FileNotFoundException(path);
    }
}
 
Example 14
Project: incubator-netbeans   File: FileEntityResolver.java   View Source Code Vote up 5 votes
/** Tries to find the entity on system file system.
 */
public InputSource resolveEntity(String publicID, String systemID) throws IOException, SAXException {
    if (publicID == null) {
        return null;
    }


    String id = convertPublicId (publicID);
    
    StringBuffer sb = new StringBuffer (200);
    sb.append (ENTITY_PREFIX);
    sb.append (id);
    
    FileObject fo = FileUtil.getConfigFile (sb.toString ());
    if (fo != null) {
        
        // fill in InputSource instance
        
        InputSource in = new InputSource (fo.getInputStream ());
        try {
            Object myPublicID = fo.getAttribute("hint.originalPublicID");  //NOI18N
            if (myPublicID instanceof String) {
                in.setPublicId((String)myPublicID);
            }                
            URL url = fo.getURL();
            in.setSystemId(url.toString());  // we get nasty nbfs: instead nbres: but it is enough                
        } catch (IOException ex) {
            // do no care just no system id
        }
        return in;
    } else {
        return null;
    }
}
 
Example 15
Project: incubator-netbeans   File: PropertiesEncoding.java   View Source Code Vote up 5 votes
private synchronized void updateURL(FileObject file) {
    try {
        fileURL = file.getURL();
    } catch (FileStateInvalidException ex) {
        fileURL = null;
    }
}
 
Example 16
Project: incubator-netbeans   File: PathRegistryTest.java   View Source Code Vote up 5 votes
public void removeResource (FileObject fo) throws IOException {
    URL url = fo.getURL();
    for (Iterator<PathResourceImplementation> it = res.iterator(); it.hasNext(); ) {
        PathResourceImplementation r = it.next();
        if (url.equals(r.getRoots()[0])) {
            it.remove();
            this.support.firePropertyChange(PROP_RESOURCES,null,null);
        }
    }
}
 
Example 17
Project: incubator-netbeans   File: PathRegistryTest.java   View Source Code Vote up 5 votes
public static void register (FileObject binRoot, FileObject sourceRoot) throws IOException {
    URL url = binRoot.getURL();
    map.put (url,sourceRoot);
    Result r = results.get (url);
    if (r != null) {
        r.update (sourceRoot);
    }
}
 
Example 18
Project: incubator-netbeans   File: PathRegistryTest.java   View Source Code Vote up 5 votes
public static void unregister (FileObject binRoot) throws IOException {
    URL url = binRoot.getURL();
    map.remove(url);
    Result r = results.get (url);
    if (r != null) {
        r.update (null);
    }
}
 
Example 19
Project: incubator-netbeans   File: ComputeOverriders.java   View Source Code Vote up 4 votes
private Map<ElementHandle<? extends Element>, List<ElementDescription>> processImpl(CompilationInfo info, TypeElement te, ExecutableElement ee, boolean interactive) {
        FileObject file = info.getFileObject();
        FileObject thisSourceRoot;
        if (te != null ) {
            thisSourceRoot = findSourceRoot(SourceUtils.getFile(te, info.getClasspathInfo()));
        } else {
            thisSourceRoot = findSourceRoot(file);
        }
        
        if (thisSourceRoot == null) {
            return null;
        }


        //XXX: special case "this" source root (no need to create a new JS and load the classes again for it):
//        reverseSourceRoots.add(thisSourceRoot);

//        LOG.log(Level.FINE, "reverseSourceRoots: {0}", reverseSourceRoots); //NOI18N

//                if (LOG.isLoggable(Level.FINE)) {
//                    LOG.log(Level.FINE, "method: {0}", ee.toString()); //NOI18N
//                }


        final Map<ElementHandle<TypeElement>, List<ElementHandle<ExecutableElement>>> methods = new HashMap<ElementHandle<TypeElement>, List<ElementHandle<ExecutableElement>>>();

        if (ee == null) {
            if (te == null) {
                fillInMethods(info.getTopLevelElements(), methods);
            } else {
                methods.put(ElementHandle.create(te), Collections.<ElementHandle<ExecutableElement>>emptyList());
            }
        } else {
            TypeElement owner = (TypeElement) ee.getEnclosingElement();

            methods.put(ElementHandle.create(owner), Collections.singletonList(ElementHandle.create(ee)));
        }

        final Map<ElementHandle<? extends Element>, List<ElementDescription>> overriding = new HashMap<ElementHandle<? extends Element>, List<ElementDescription>>();

        long startTime = System.currentTimeMillis();
        long[] classIndexTime = new long[1];
        final Map<URL, Map<ElementHandle<TypeElement>, Set<ElementHandle<TypeElement>>>> users = computeUsers(info, thisSourceRoot, methods.keySet(), classIndexTime, interactive);
        long endTime = System.currentTimeMillis();

        if (users == null) {
            return null;
        }

        Logger.getLogger("TIMER").log(Level.FINE, "Overridden Candidates - Class Index", //NOI18N
            new Object[] {file, classIndexTime[0]});
        Logger.getLogger("TIMER").log(Level.FINE, "Overridden Candidates - Total", //NOI18N
            new Object[] {file, endTime - startTime});

	FileObject currentFileSourceRoot = findSourceRoot(file);

	if (currentFileSourceRoot != null) {
	    try {
		URL rootURL = currentFileSourceRoot.getURL();
		Map<ElementHandle<TypeElement>, Set<ElementHandle<TypeElement>>> overridingHandles = users.remove(rootURL);

		if (overridingHandles != null) {
		    computeOverridingForRoot(rootURL, overridingHandles, methods, overriding);
		}
	    } catch (FileStateInvalidException ex) {
		LOG.log(Level.INFO, null, ex);
	    }
	}

        for (Map.Entry<URL, Map<ElementHandle<TypeElement>, Set<ElementHandle<TypeElement>>>> data : users.entrySet()) {
	    computeOverridingForRoot(data.getKey(), data.getValue(), methods, overriding);
        }

	if (cancel.get()) return null;

        return overriding;
    }
 
Example 20
Project: incubator-netbeans   File: TaskTest.java   View Source Code Vote up 4 votes
public void testGetters() throws FileStateInvalidException {
    String description = "task description";
    int lineNo = 123;
    FileObject resource = FileUtil.getConfigRoot();
    
    Task t = Task.create(resource, TASK_GROUP_NAME, description, lineNo );
    
    assertEquals( description, t.getDescription() );
    assertEquals( lineNo, t.getLine() );
    assertEquals( resource, t.getFile() );
    assertNull( t.getURL() );
    assertNull( t.getActions() );
    assertEquals( TaskGroupFactory.getDefault().getGroup( TASK_GROUP_NAME), t.getGroup() );
    assertNull( t.getDefaultAction() );
    
    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    };
    
    t = Task.create(resource, TASK_GROUP_NAME, description, al );
    
    assertEquals( description, t.getDescription() );
    assertEquals( -1, t.getLine() );
    assertEquals( resource, t.getFile() );
    assertNull( t.getURL() );
    assertNull( t.getActions() );
    assertEquals( TaskGroupFactory.getDefault().getGroup( TASK_GROUP_NAME), t.getGroup() );
    assertEquals( al, t.getDefaultAction() );

    URL url = resource.getURL();

    t = Task.create(url, TASK_GROUP_NAME, description );

    assertEquals( description, t.getDescription() );
    assertEquals( -1, t.getLine() );
    assertEquals( url, t.getURL() );
    assertNull( t.getFile() );
    assertNull( t.getActions() );
    assertEquals( TaskGroupFactory.getDefault().getGroup( TASK_GROUP_NAME), t.getGroup() );
    assertNull( t.getDefaultAction() );

    Action[] actions = new Action[1];
    t = Task.create(url, TASK_GROUP_NAME, description, al, actions );

    assertEquals( description, t.getDescription() );
    assertEquals( -1, t.getLine() );
    assertEquals( url, t.getURL() );
    assertNull( t.getFile() );
    assertEquals( TaskGroupFactory.getDefault().getGroup( TASK_GROUP_NAME), t.getGroup() );
    assertEquals( al, t.getDefaultAction() );
    assertSame( actions, t.getActions() );
}