Java Code Examples for org.openide.filesystems.FileUtil#getMIMEType()

The following examples show how to use org.openide.filesystems.FileUtil#getMIMEType() . 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 check out the related API usage on the sidebar.
Example 1
Source File: TestLocatorImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public LocationResult findOpposite(FileObject fo, int caretOffset) {
    String mimeType = FileUtil.getMIMEType(fo);

    if ("text/x-javahints".equals(mimeType)) {
        FileObject test = findOpposite(fo, true);

        if (test != null) {
            return new LocationResult(test, -1);
        } else {
            return new LocationResult("Test not found.");
        }
    }

    if ("text/x-javahintstest".equals(mimeType)) {
        FileObject rule = findOpposite(fo, false);

        if (rule != null) {
            return new LocationResult(rule, -1);
        } else {
            return new LocationResult("Rule file not found.");
        }
    }

    throw new IllegalArgumentException();
}
 
Example 2
Source File: NewFileWizardIterator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private SourceGroup[] getSourceGroups(Project project, FileObject file) {
    // #239027
    if (FileUtil.getMIMEType(file, JSON_MIME_TYPE, null) != null) {
        return ProjectUtils.getSources(project).getSourceGroups(Sources.TYPE_GENERIC);
    }
    ClientSideProject clientSideProject = getClientSideProject(project);
    if (clientSideProject == null) {
        // #231347
        return ProjectUtils.getSources(project).getSourceGroups(Sources.TYPE_GENERIC);
    }
    SourceGroup[] allGroups = ClientSideProjectUtilities.getSourceGroups(project);
    if (!FileUtilities.isHtmlFile(file)
            && !FileUtilities.isCssFile(file)) {
        // not html or css -> return all source groups
        return allGroups;
    }
    // html or css file -> return only sources or site root
    List<SourceGroup> groups = new ArrayList<>();
    groups.addAll(Arrays.asList(ClientSideProjectUtilities.getSourceGroups(project, WebClientProjectConstants.SOURCES_TYPE_HTML5_SITE_ROOT)));
    groups.addAll(Arrays.asList(ClientSideProjectUtilities.getSourceGroups(project, WebClientProjectConstants.SOURCES_TYPE_HTML5)));
    if (!groups.isEmpty()) {
        return groups.toArray(new SourceGroup[groups.size()]);
    }
    return allGroups;
}
 
Example 3
Source File: FileObjectIndexable.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isTypeOf(String mimeType) {
    Parameters.notNull("mimeType", mimeType); //NOI18N
    if (this.mimeType == null) {
        FileObject f = getFileObject();
        if (f != null) {
            String mt = FileUtil.getMIMEType(f, mimeType);
            if (mt != null && !mt.equals("content/unknown")) {
                this.mimeType = mt;
            }
        }
    }
    return this.mimeType == null ? false : this.mimeType.equals(mimeType);
}
 
Example 4
Source File: TestLocatorImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public boolean appliesTo(FileObject fo) {
    String mimeType = FileUtil.getMIMEType(fo);

    if ("text/x-javahints".equals(mimeType)) {
        return true;
    }
    
    if ("text/x-javahintstest".equals(mimeType)) {
        return true;
    }

    return false;
}
 
Example 5
Source File: RunSQLAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public boolean isActionEnabled(String command, Lookup context) throws IllegalArgumentException {
    Collection<? extends DataObject> files = context.lookupAll(DataObject.class);
    if (files.isEmpty()) {
        return false;
    }
    for (DataObject d : files) {
        if (d.getLookup().lookup(SQLExecuteCookie.class) != null
                || (FileUtil.getMIMEType(d.getPrimaryFile()) != null
                && FileUtil.getMIMEType(d.getPrimaryFile()).equals("text/x-sql"))) { // NOI18N
            return true;
        }
    }
    return false;
}
 
Example 6
Source File: LocalTask.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void addAttachment (NbTaskDataModel model, TaskAttribute parentTA,
        File file, String desc, String contentType, boolean isPatch,
        boolean copyToCentralStorage) {
    if (desc == null) {
        desc = "";
    }
    if (contentType == null) {
        file = FileUtil.normalizeFile(file);
        String ct = FileUtil.getMIMEType(FileUtil.toFileObject(file));
        if ((ct != null) && (!"content/unknown".equals(ct))) { // NOI18N
            contentType = ct;
        } else {
            contentType = FileTaskAttachmentSource.getContentTypeFromFilename(file.getName());
        }
    }
    int attachmentIndex = parentTA.getAttributes().size();
    TaskAttribute attachment = parentTA.createAttribute(NB_ATTACHMENT + attachmentIndex);
    TaskAttachmentMapper mapper = new TaskAttachmentMapper();
    mapper.setAttachmentId(String.valueOf(attachmentIndex));
    mapper.setDescription(desc);
    mapper.setFileName(file.getName());
    mapper.setPatch(isPatch);
    mapper.setCreationDate(new Date());
    mapper.setContentType(contentType);
    File realFile = copyToCentralStorage ? copyFileToCentral(file) : file;
    mapper.setUrl(Utilities.toURI(realFile).toString());
    // abuse this attribute and mark the patch residing in the central storage
    mapper.setReplaceExisting(copyToCentralStorage);
    mapper.applyTo(attachment);
    model.attributeChanged(parentTA);
}
 
Example 7
Source File: GenericDataObject.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public GenericDataObject(FileObject pf, MultiFileLoader loader) throws DataObjectExistsException, IOException {
    super(pf, loader);
    this.mimeType = FileUtil.getMIMEType(pf);
    registerEditor(mimeType, false);
    synchronized (REGISTRY) {
        REGISTRY.add(new WeakReference<>(this));
    }
}
 
Example 8
Source File: FSCompletion.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public boolean accept(FileObject file) {
    if (file.equals(currentFile) || isNbProjectMetadata(file)) {
        return false; //do not include self in the cc result
    }

    if (file.isFolder()) {
        return true;
    }

    String mimeType = FileUtil.getMIMEType(file);

    return mimeType != null && mimeType.startsWith("text/");
}
 
Example 9
Source File: JsTestLocator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public boolean appliesTo(FileObject fo) {
    Project project = findProject(fo);
    if (project == null) {
        LOGGER.log(Level.INFO, "Project was not found for file {0}", fo);
        return false;
    }
    if (getSourceGroupForSeleniumTests(project, fo) != null) {
        return false; // disabled for files under Selenium Tests Folder
    }
    return FileUtil.getMIMEType(fo, JS_MIME_TYPE, null) != null;
}
 
Example 10
Source File: FSCompletionUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public boolean accept(FileObject file) {
    if (file.equals(currentFile) || isNbProjectMetadata(file)) {
        return false; //do not include self in the cc result
    }

    if (file.isFolder()) {
        return true;
    }

    String mimeType = FileUtil.getMIMEType(file);

    return mimeType != null && mimeType.startsWith("text/");    //NOI18N
}
 
Example 11
Source File: TestLocatorImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public FileType getFileType(FileObject fo) {
    String mimeType = FileUtil.getMIMEType(fo);

    if ("text/x-javahints".equals(mimeType)) {
        return FileType.TESTED;
    }

    if ("text/x-javahintstest".equals(mimeType)) {
        return FileType.TEST;
    }

    return FileType.NEITHER;
}
 
Example 12
Source File: TestMethodUtil.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static boolean isGroovyFile(FileObject fileObj) {
    final String ext = fileObj.getExt();
    final String mtype = FileUtil.getMIMEType(fileObj);
    return "groovy".equals(ext) || "text/x-groovy".equals(mtype); //NOI18N
}
 
Example 13
Source File: SymfonyUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static boolean isView(FileObject file) {
    return FileUtil.getMIMEType(file, null, TWIG_MIME_TYPE) != null;
}
 
Example 14
Source File: FileElement.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
private static String getMIMEType(String extension) {
    return FileUtil.getMIMEType(extension);
}
 
Example 15
Source File: OpenedEditors.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**Filter unsupported files from the <code>files</code> parameter. A supported file
 * <code>f</code> is defined as follows:
 * <ul>
 *     <li><code>JavaSource.forFileObject(f) != null</code></li>
 *     <li>If the <code>type</code> is annotated with the {@link SupportedMimeTypes} annotation,
 *         the file is supported if <code>type.getAnnotation(SupportedMimeTypes.class).value()</code>
 *         contains <code>FileUtil.getMIMEType(f)</code>.
 *     </li>
 *     <li>If the <code>type</code> is not annotated with the {@link SupportedMimeTypes} annotation,
 *         the file is supported if <code>FileUtil.getMIMEType(f) == "text/x-java"</code>.
 * </ul>
 *
 * @param files the list of files to filter
 * @param type the type to check for the {@link SupportedMimeTypes} annotation
 * @return list of files that are supported (see above).
 * @throws NullPointerException if <code>files == null</code> or <code>type == null</code>
 */
public static List<FileObject> filterSupportedMIMETypes(Collection<FileObject> files, String... mimeTypes) throws NullPointerException {
    Parameters.notNull("files", files);
    
    boolean            allowJavaExtension = false;
    
    if (mimeTypes == null) {
        mimeTypes = new String[] {"text/x-java"};
        allowJavaExtension = true;
    }
    
    List<String>       mimeTypesList = Arrays.asList(mimeTypes);
    boolean            allowAll  = mimeTypesList.contains("*");
    List<FileObject>   result    = new LinkedList<FileObject>();
    
    Logger.getLogger(OpenedEditors.class.getName()).log(Level.FINER, "mimeTypesList={0}", mimeTypesList);
    
    for (FileObject f : files) {
        Logger.getLogger(OpenedEditors.class.getName()).log(Level.FINER, "analyzing={0}", f);
        
        if (JavaSource.forFileObject(f) == null)
            continue;
        
        if (allowAll) {
            result.add(f);
            continue;
        }
        
        if (allowJavaExtension && "java".equals(f.getExt())) {
            result.add(f);
            continue;
        }
        
        String fileMimeType = FileUtil.getMIMEType(f);
        
        Logger.getLogger(OpenedEditors.class.getName()).log(Level.FINER, "fileMimeType={0}", fileMimeType);

        if (fileMimeType == null) {
            continue;
        }
        
        if (mimeTypesList.contains(fileMimeType)) {
            result.add(f);
            continue;
        }
        
        String shorterMimeType = fileMimeType;
        
        while (true) {
            int slash = shorterMimeType.indexOf('/');
            
            if (slash == (-1))
                break;
            
            int plus  = shorterMimeType.indexOf('+', slash);
            
            if (plus == (-1))
                break;
            
            shorterMimeType = shorterMimeType.substring(0, slash + 1) + shorterMimeType.substring(plus + 1);
            
            if (mimeTypesList.contains(shorterMimeType)) {
                result.add(f);
                break;
            }
        }
    }
    
    Logger.getLogger(OpenedEditors.class.getName()).log(Level.FINE, "filter({0}, {1})={2}", new Object[] {files, mimeTypesList, result});
    
    return result;
}
 
Example 16
Source File: FileUtilities.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/**
 * Check whether the given file is an (X)HTML file.
 * @param file file to be checked
 * @return {@code true} if the given file is an (X)HTML file, {@code false} otherwise
 */
public static boolean isHtmlFile(FileObject file) {
    return FileUtil.getMIMEType(file, HTML_MIME_TYPE, XHTML_MIME_TYPE, null) != null;
}
 
Example 17
Source File: FileUtilities.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/**
 * Check whether the given file is a CSS file.
 * @param file file to be checked
 * @return {@code true} if the given file is a CSS file, {@code false} otherwise
 */
public static boolean isCssFile(FileObject file) {
    return FileUtil.getMIMEType(file, CSS_MIME_TYPE, null) != null;
}
 
Example 18
Source File: FileUtilities.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/**
 * Check whether the given file is a JavaScript file.
 * @param file file to be checked
 * @return {@code true} if the given file is a JavaScript file, {@code false} otherwise
 */
public static boolean isJavaScriptFile(FileObject file) {
    return FileUtil.getMIMEType(file, JAVASCRIPT_MIME_TYPE, null) != null;
}
 
Example 19
Source File: SmartyPhpFrameworkProvider.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/**
 * Checks if the given {@code FileObject} has extension registered in the
 * IDE as Smarty template extension (text/x-tpl).
 *
 * @param fo investigated file
 * @return {@code true} if the file has extension registered as Smarty template
 * extension, {@code false} otherwise
 */
public static boolean hasSmartyTemplateExtension(FileObject fo) {
    return FileUtil.getMIMEType(fo, TplDataLoader.MIME_TYPE, null) != null;
}
 
Example 20
Source File: FileUtils.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/**
 * Returns <code>true</code> if the file is a PHP file.
 * @param file file to check
 * @return <code>true</code> if the file is a PHP file
 * @see #PHP_MIME_TYPE
 */
public static boolean isPhpFile(FileObject file) {
    Parameters.notNull("file", file); // NOI18N
    return FileUtil.getMIMEType(file, PHP_MIME_TYPE, null) != null;
}