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

The following examples show how to use org.openide.filesystems.FileObject#getURL() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
private URL getNormalizedURL (URL url) throws MalformedURLException {
    //URL is already nornalized, return it
    if (isNormalizedURL(url)) {
        return url;
    }
    //Todo: Should listen on the LibrariesManager and cleanup cache        
    // in this case the search can use the cache onle and can be faster 
    // from O(n) to O(ln(n))
    URL normalizedURL = normalizedURLCache.get(url);
    if (normalizedURL == null) {
        FileObject fo = URLMapper.findFileObject(url);
        if (fo != null) {
            try {
                normalizedURL = fo.getURL();
                this.normalizedURLCache.put (url, normalizedURL);
            } catch (FileStateInvalidException e) {
                Exceptions.printStackTrace(e);
            }
        }
    }
    return normalizedURL;
}
 
Example 2
Source Project: netbeans   File: IconEditor.java    License: Apache License 2.0 6 votes vote down vote up
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 3
private URL getNormalizedURL (URL url) {
    //URL is already nornalized, return it
    if (isNormalizedURL(url)) {
        return url;
    }
    //Todo: Should listen on the LibrariesManager and cleanup cache        
    // in this case the search can use the cache onle and can be faster 
    // from O(n) to O(ln(n))
    URL normalizedURL = (URL) this.normalizedURLCache.get (url);
    if (normalizedURL == null) {
        FileObject fo = URLMapper.findFileObject(url);
        if (fo != null) {
            try {
                normalizedURL = fo.getURL();
                this.normalizedURLCache.put (url, normalizedURL);
            } catch (FileStateInvalidException e) {
                Exceptions.printStackTrace(e);
            }
        }
    }
    return normalizedURL;
}
 
Example 4
Source Project: netbeans   File: ParsingSupport.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 5
Source Project: netbeans   File: ElementJavadoc.java    License: Apache License 2.0 5 votes vote down vote up
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 6
Source Project: netbeans   File: IndexerTransactionTest.java    License: Apache License 2.0 5 votes vote down vote up
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 7
Source Project: netbeans   File: IndexerTransactionTest.java    License: Apache License 2.0 5 votes vote down vote up
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 8
Source Project: netbeans   File: BiIconEditor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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 9
Source Project: netbeans   File: ProjectUtilities.java    License: Apache License 2.0 5 votes vote down vote up
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
Source Project: netbeans   File: ProjectJAXWSSupport.java    License: Apache License 2.0 5 votes vote down vote up
public URL getCatalog() {
    try {
        FileObject catalog = getCatalogFileObject();
        return catalog==null?null:catalog.getURL();
    } catch (FileStateInvalidException ex) {
        return null;
    }
    
}
 
Example 11
Source Project: netbeans   File: URLMapperTest.java    License: Apache License 2.0 5 votes vote down vote up
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 12
Source Project: netbeans   File: PropertiesEncoding.java    License: Apache License 2.0 5 votes vote down vote up
private synchronized void updateURL(FileObject file) {
    try {
        fileURL = file.getURL();
    } catch (FileStateInvalidException ex) {
        fileURL = null;
    }
}
 
Example 13
Source Project: netbeans   File: PathRegistryTest.java    License: Apache License 2.0 5 votes vote down vote up
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 14
Source Project: netbeans   File: PathRegistryTest.java    License: Apache License 2.0 5 votes vote down vote up
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 15
Source Project: netbeans   File: PathRegistryTest.java    License: Apache License 2.0 5 votes vote down vote up
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 16
Source Project: netbeans   File: NbURLMapper.java    License: Apache License 2.0 5 votes vote down vote up
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 17
Source Project: netbeans   File: TaskTest.java    License: Apache License 2.0 4 votes vote down vote up
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() );
}