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

The following examples show how to use org.openide.filesystems.FileObject#getExt() . 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: FightWithWrongOrderInPathsTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected FileObject findPrimaryFile(FileObject fo)
{
    LOG.info("FormKitDataLoader.findPrimaryFile(): " + fo.getNameExt());
    cnt++;
    
    String ext = fo.getExt();
    if (ext.equals(FORM_EXTENSION))
    {
        return FileUtil.findBrother(fo, JAVA_EXTENSION);
    }
    if (ext.equals(JAVA_EXTENSION) && FileUtil.findBrother(fo, FORM_EXTENSION) != null)
    {
        return fo;
    }
    return null;
}
 
Example 2
Source File: FileObjectInLookupByMatteoTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected FileObject findPrimaryFile(FileObject fo) {

    if (fo.isFolder()) {
        return null;
    }

    // check if it's itself the primary file
    String ext = fo.getExt();
    if (ext.equalsIgnoreCase(PRIMARY_EXTENSION)) {
        return fo;
    }

    // check if it's a secondary entry
    String completeFileName = fo.getNameExt();
    Matcher m = SECONDARY_PATTERN.matcher(completeFileName);
    if (m.find()) {
        String primaryName = m.group(1);
        FileObject primaryFO = fo.getParent().getFileObject(primaryName, PRIMARY_EXTENSION);
        return primaryFO;
    }

    return null;
}
 
Example 3
Source File: FightWithWrongOrderOfLoadersTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected FileObject findPrimaryFile(FileObject fo)
{
    LOG.info("FormKitDataLoader.findPrimaryFile(): " + fo.getNameExt());
    cnt++;

    String ext = fo.getExt();
    if (ext.equals(FORM_EXTENSION))
    {
        return FileUtil.findBrother(fo, JAVA_EXTENSION);
    }
    if (ext.equals(JAVA_EXTENSION) && FileUtil.findBrother(fo, FORM_EXTENSION) != null)
    {
        return fo;
    }
    return null;
}
 
Example 4
Source File: FolderComparator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** for sorting folders first and then by extensions and then by names */
private int compareExtensions(Object o1, Object o2) {
    FileObject obj1 = findFileObject(o1);
    FileObject obj2 = findFileObject(o2);

    boolean folder1 = obj1.isFolder();
    boolean folder2 = obj2.isFolder();

    if (folder1 != folder2) {
        return folder1 ? -1 : 1; // folders first
    } else if (folder1) { // && folder2
        return obj1.getNameExt().compareTo(obj2.getNameExt()); // by nameExt
    } else {
        String ext1 = obj1.getExt();
        String ext2 = obj2.getExt();
        if (ext1.equals(ext2)) { // same extensions
            return obj1.getName().compareTo(obj2.getName()); // by name
        } else { // different extensions
            return ext1.compareTo(ext2); // by extension
        }
    }
}
 
Example 5
Source File: EncodedReaderFactory.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Get the writer to file, suggest the encoding, if known.
 */
public Writer getWriter(FileObject fo, FileLock lock, String encoding) throws IOException {
    if (lock == null) {
        lock = fo.lock();
    }
    if (encoding != null) {
        try {
            return new OutputStreamWriter(fo.getOutputStream(lock), encoding);
        } catch (UnsupportedEncodingException ueex) {
            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ueex);
        }
    }
    Writer w = null;
    String ext = fo.getExt();
    if (!"java".equalsIgnoreCase(ext)) { // We read the encoding for Java files explicitely
                                         // If it's not defined, read with default encoding from stream (because of guarded blocks)
        w = getWriterFromEditorSupport(fo, lock);
        if (w == null) {
            w = getWriterFromKit(null, fo, lock, fo.getMIMEType());
        }
    }
    if (w == null) {
        // Fallback, use current encoding
        w = new OutputStreamWriter(fo.getOutputStream(lock));
    }
    return w;
}
 
Example 6
Source File: FastOpenInfoParser.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean isXMLSyntax(FileObject fo) {
    String ext = fo.getExt();
    if (ext != null && ("jspx".equalsIgnoreCase(ext) || "tagx".equalsIgnoreCase(ext))) { // NOI18N
        return true;
    }
    return false;
}
 
Example 7
Source File: FastDeploy.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void addDescriptorToDeployedDirectory(J2eeModule module, File sunDDFile) {
    FileObject sunDDTemplate = Utils.getSunDDFromProjectsModuleVersion(module, sunDDFile.getName()); //FileUtil.getConfigFile(resource);
    if (sunDDTemplate != null) {
        try {
            FileObject configFolder = FileUtil.createFolder(sunDDFile.getParentFile());
            FileSystem fs = configFolder.getFileSystem();
            XmlFileCreator creator = new XmlFileCreator(sunDDTemplate, configFolder, sunDDTemplate.getName(), sunDDTemplate.getExt());
            fs.runAtomicAction(creator);
        } catch (IOException ioe) {
            Logger.getLogger("payara").log(Level.WARNING, "could not create {0}", sunDDTemplate.getPath());
        }
    }
}
 
Example 8
Source File: CakePHP3ModuleDefault.java    From cakephp3-netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isTemplateFile(FileObject fileObject) {
    String ext = fileObject.getExt();
    if (DEFAULT_CTP_EXT.equals(ext)) {
        return true;
    }
    Category category = getCategory(fileObject);
    if (!ModuleUtils.isTemplate(category)) {
        return false;
    }
    return getCtpExt().equals(ext); // NOI18N
}
 
Example 9
Source File: CompletionTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public SuiteCreator() {
    super();
    FileObjectFilter filter = new FileObjectFilter() {

        public boolean accept(FileObject fo) {
            String ext = fo.getExt();
            String name = fo.getName();
            return (name.startsWith("test") || name.startsWith("Test"))
                    && (XML_EXTS.contains(ext) || JSP_EXTS.contains(ext) || JS_EXTS.contains(ext))
                    && !CompletionTest.ignored_tests.contains(name + "." + ext);
        }
    };
    addTest(RecurrentSuiteFactory.createSuite(CompletionTest.class,
            new CompletionTest().getProjectsDir(), filter));
}
 
Example 10
Source File: JSONMinify.java    From minifierbeans with Apache License 2.0 5 votes vote down vote up
private static void jsonMinify(DataObject context, String content, boolean notify) {
    MinifyProperty minifyProperty = MinifyProperty.getInstance();
    MinifyUtil util = new MinifyUtil();
    try {
        FileObject file = context.getPrimaryFile();
        if (!util.isMinifiedFile(file.getName(), minifyProperty.getPreExtensionJSON())) {
            String inputFilePath = file.getPath();
            String outputFilePath;

            if (minifyProperty.isNewJSONFile() && minifyProperty.getPreExtensionJSON() != null && !minifyProperty.getPreExtensionJSON().trim().isEmpty()) {
                outputFilePath = file.getParent().getPath() + File.separator + file.getName() + minifyProperty.getPreExtensionJSON() + "." + file.getExt();
            } else {
                outputFilePath = inputFilePath;
            }

            MinifyFileResult minifyFileResult;
            if (content != null) {
                minifyFileResult = util.compressContent(inputFilePath, content, "text/x-json", outputFilePath, minifyProperty);
            } else {
                minifyFileResult = util.compress(inputFilePath, "text/x-json", outputFilePath, minifyProperty);
            }
            if (minifyProperty.isEnableOutputLogAlert() && notify) {
                NotificationDisplayer.getDefault().notify("Successful JSON minification", NotificationDisplayer.Priority.NORMAL.getIcon(), String.format("Input JSON Files Size: %s Bytes \n"
                        + "After Minifying JSON Files Size:  %s Bytes \n"
                        + "JSON Space Saved %s%%", minifyFileResult.getInputFileSize(), minifyFileResult.getOutputFileSize(), minifyFileResult.getSavedPercentage()), null);
            }
        }
    } catch (HeadlessException | IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 11
Source File: AutoCompletionTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public SuiteCreator() {
    File datadir = new AutoCompletionTest(null, null).getDataDir();
    File projectsDir = new File(datadir, "AutoCompletionTestProjects");
    FileObjectFilter filter = new FileObjectFilter() {

        public boolean accept(FileObject fo) {
            String ext = fo.getExt();
            String name = fo.getName();
            return (name.startsWith("test") || name.startsWith("Test")) && (XML_EXTS.contains(ext) || JSP_EXTS.contains(ext) || ext.equals("java"));
        }
    };
    addTest(RecurrentSuiteFactory.createSuite(AutoCompletionTest.class, projectsDir, filter));
}
 
Example 12
Source File: JSMinify.java    From minifierbeans with Apache License 2.0 5 votes vote down vote up
private static void jsMinify(DataObject context, String content, boolean notify) {
    MinifyProperty minifyProperty = MinifyProperty.getInstance();
    MinifyUtil util = new MinifyUtil();

    try {
        FileObject file = context.getPrimaryFile();
        if (!util.isMinifiedFile(file.getName(), minifyProperty.getPreExtensionJS())) {
            String inputFilePath = file.getPath();
            String outputFilePath;

            if (minifyProperty.isNewJSFile() && minifyProperty.getPreExtensionJS() != null && !minifyProperty.getPreExtensionJS().trim().isEmpty()) {
                outputFilePath = file.getParent().getPath() + File.separator + file.getName() + minifyProperty.getPreExtensionJS() + "." + file.getExt();
            } else {
                outputFilePath = inputFilePath;
            }

            MinifyFileResult minifyFileResult;

            if (content != null) {
                minifyFileResult = util.compressContent(inputFilePath, content, "text/javascript", outputFilePath, minifyProperty);
            } else {
                minifyFileResult = util.compress(inputFilePath, "text/javascript", outputFilePath, minifyProperty);
            }

            if (minifyProperty.isEnableOutputLogAlert() && notify) {
                NotificationDisplayer.getDefault().notify("Successful JS minification", NotificationDisplayer.Priority.NORMAL.getIcon(), String.format("Input JS Files Size: %s Bytes \n"
                        + "After Minifying JS Files Size: %s Bytes \n"
                        + "JS Space Saved %s%%", minifyFileResult.getInputFileSize(), minifyFileResult.getOutputFileSize(), minifyFileResult.getSavedPercentage()), null);
            }
        }
    } catch (HeadlessException | IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 13
Source File: IndentationTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public SuiteCreator() {
    super();
    // find folder with test projects and define file objects filter
    File datadir = new IndentationTest(null, null).getDataDir();
    File projectsDir = new File(datadir, "IndentationTestProjects");
    FileObjectFilter filter = new FileObjectFilter() {

        public boolean accept(FileObject fObject) {
            String ext = fObject.getExt();
            String name = fObject.getName();
            return name.startsWith("test") && (XML_EXTS.contains(ext) || JSP_EXTS.contains(ext));
        }
    };
    addTest(RecurrentSuiteFactory.createSuite(IndentationTest.class, projectsDir, filter));
}
 
Example 14
Source File: BiNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static Node createNoSourceNode(FileObject bi) {
    String name = bi.getName();
    name = name.substring(0, name.length() - "BeanInfo".length()); // NOI18N
    String ext = bi.getExt();
    if (ext.length() > 0) {
        name += '.' + ext;
    }
    String msg = NbBundle.getMessage(BiNode.class, "CTL_NODE_MissingBeanFile", name);
    return new Error(msg);
}
 
Example 15
Source File: FormDataLoader.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** For a given file finds a primary file.
    * @param fo the file to find primary file for
    *
    * @return the primary file for the file or null if the file is not
    *   recognized by this loader
    */
   @Override
   protected FileObject findPrimaryFile(FileObject fo) {
// never recognize folders.
       if (fo.isFolder()) return null;
       String ext = fo.getExt();
       if (ext.equals(FORM_EXTENSION))
           return FileUtil.findBrother(fo, JAVA_EXTENSION);

       FileObject javaFile = findJavaPrimaryFile(fo);
       return javaFile != null
                   && FileUtil.findBrother(javaFile, FORM_EXTENSION) != null ?
           javaFile : null;
   }
 
Example 16
Source File: CategoryNode.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Converts category name to name that can be used as name of folder
 * for the category (restricted even to package name).
 */ 
static String convertCategoryToFolderName( FileObject paletteFO, 
                                                   String name,
                                                   String currentName)
{
    if (name == null || "".equals(name)) // NOI18N
        return null;

    int i;
    int n = name.length();
    StringBuffer nameBuff = new StringBuffer(n);

    char ch = name.charAt(0);
    if (Character.isJavaIdentifierStart(ch)) {
        nameBuff.append(ch);
        i = 1;
    }
    else {
        nameBuff.append('_');
        i = 0;
    }

    while (i < n) {
        ch = name.charAt(i);
        if (Character.isJavaIdentifierPart(ch))
            nameBuff.append(ch);
        i++;
    }

    String fName = nameBuff.toString();
    if ("_".equals(fName)) // NOI18N
        fName = "Category"; // NOI18N
    if (fName.equals(currentName))
        return fName;

    // having the base name, make sure it is not used yet
    String freeName = null;
    boolean nameOK = false;

    for (i=0; !nameOK; i++) {
        freeName = i > 0 ? fName + "_" + i : fName; // NOI18N

        if (Utilities.isWindows()) {
            nameOK = true;
            java.util.Enumeration en = paletteFO.getChildren(false);
            while (en.hasMoreElements()) {
                FileObject fo = (FileObject)en.nextElement();
                String fn = fo.getName();
                String fe = fo.getExt();

                // case-insensitive on Windows
                if ((fe == null || "".equals(fe)) && fn.equalsIgnoreCase(freeName)) { // NOI18N
                    nameOK = false;
                    break;
                }
            }
        }
        else nameOK = paletteFO.getFileObject(freeName) == null;
    }
    return freeName;
}
 
Example 17
Source File: FileNameMatcher.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public boolean pathMatches(FileObject fileObject) {
    String fileExt = fileObject.getExt();
    return fileExt != null && fileExt.equalsIgnoreCase(ext);
}
 
Example 18
Source File: Util.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 *
 * @param f
 * @param parent Directory where to search
 * @param baseName of the locale (name without locale suffix)
 * @return BundleStructure or null 
 * @throws org.openide.loaders.DataObjectNotFoundException
 */
static BundleStructure findBundleStructure (FileObject f, FileObject parent, String baseName) throws DataObjectNotFoundException{
        String fName;
        PropertiesDataObject dataObject = null;
        BundleStructure structure;
        String extension = PropertiesDataLoader.PROPERTIES_EXTENSION;
        if (!f.hasExt(extension)) {
            if (f.getMIMEType().equalsIgnoreCase(PropertiesDataLoader.PROPERTIES_MIME_TYPE))
                extension = f.getExt();
        }
        for (FileObject file : parent.getChildren()) {
            if (!file.hasExt(extension) || file.equals(f)) {
                continue;
            }
            fName = file.getName();
            if (fName.equals(baseName) && file.isValid()) {
                    dataObject = (PropertiesDataObject) DataObject.find(file);
                    if (dataObject == null) continue;
                    structure = dataObject.getBundleStructureOrNull();
                    if (structure != null)
                         return structure;
                    else
                         continue;
            }
            if (fName.indexOf(baseName) != -1) {
                int index = fName.indexOf(PropertiesDataLoader.PRB_SEPARATOR_CHAR);
                if (baseName.length()!=index)
                    continue;
                while (index != -1) {
                    FileObject candidate = file;
                    if (candidate != null && isValidLocaleSuffix(fName.substring(index)) && file.isValid()) {
                        DataObject defaultDataObject = DataObject.find(candidate);
                        if (defaultDataObject instanceof PropertiesDataObject) {
                            dataObject = (PropertiesDataObject) DataObject.find(candidate);
                        } else {
                            index = -1;
                        }
                        if (dataObject == null) continue;
                        structure = dataObject.getBundleStructureOrNull();
                        if (structure != null) 
                            return structure;
                    }
                    index = fName.indexOf(PropertiesDataLoader.PRB_SEPARATOR_CHAR, index + 1);
                }
            }
        }
        return null;
}
 
Example 19
Source File: CssRenameRefactoringPlugin.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void refactorFile(ModificationResult modificationResult, FileObject context, CssIndex index) {
    LOGGER.log(Level.FINE, "refactor file {0}", context.getPath()); //NOI18N
    String newName = refactoring.getNewName();

    //a. get all importing files
    //b. rename the references
    //c. rename the file itself - done via default rename plugin
    DependenciesGraph deps = index.getDependencies(context);
    Collection<org.netbeans.modules.web.common.api.DependenciesGraph.Node> refering = deps.getSourceNode().getReferingNodes();
    for (org.netbeans.modules.web.common.api.DependenciesGraph.Node ref : refering) {
        FileObject file = ref.getFile();
        try {
            Source source;
            CloneableEditorSupport editor = GsfUtilities.findCloneableEditorSupport(file);
            //prefer using editor
            //XXX this approach doesn't match the dependencies graph
            //which is made strictly upon the index data
            if (editor != null && editor.isModified()) {
                source = Source.create(editor.getDocument());
            } else {
                source = Source.create(file);
            }

            CssFileModel model = CssFileModel.create(source);

            List<Difference> diffs = new ArrayList<>();
            for (Entry entry : model.getImports()) {
                String imp = entry.getName(); //unquoted
                FileObject resolvedFileObject = WebUtils.resolve(file, imp);
                if (resolvedFileObject != null && resolvedFileObject.equals(context)) {
                    //the import refers to me - lets refactor it
                    if (entry.isValidInSourceDocument()) {
                        //new relative path creation
                        String newImport;
                        String extension = context.getExt(); //use the same extension as source file (may not be .css)
                        int slashIndex = imp.lastIndexOf('/'); //NOI18N
                        if (slashIndex != -1) {
                            newImport = imp.substring(0, slashIndex) + "/" + newName + "." + extension; //NOI18N
                        } else {
                            newImport = newName + "." + extension; //NOI18N
                        }

                        diffs.add(new Difference(Difference.Kind.CHANGE,
                                editor.createPositionRef(entry.getDocumentRange().getStart(), Bias.Forward),
                                editor.createPositionRef(entry.getDocumentRange().getEnd(), Bias.Backward),
                                entry.getName(),
                                newImport,
                                NbBundle.getMessage(CssRenameRefactoringPlugin.class, "MSG_Modify_Css_File_Import"))); //NOI18N
                    }
                }
            }

            if (!diffs.isEmpty()) {
                modificationResult.addDifferences(file, diffs);
            }

        } catch (ParseException ex) {
            Exceptions.printStackTrace(ex);
        }

    }
}
 
Example 20
Source File: VirtualSourceProviderQuery.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static boolean hasVirtualSource (final FileObject file) {
    Parameters.notNull("file", file);
    final String ext = file.getExt();
    return getExt2ProvMap().keySet().contains(ext);
}