Java Code Examples for org.netbeans.modules.php.api.util.FileUtils

The following examples show how to use org.netbeans.modules.php.api.util.FileUtils. 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 check out the related API usage on the sidebar.
Example 1
Source Project: netbeans   Source File: PHPTokenListProvider.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public TokenList findTokenList(Document doc) {
    String mimeType = NbEditorUtilities.getMimeType(doc);
    if (FileUtils.PHP_MIME_TYPE.equals(mimeType)
            && doc instanceof BaseDocument) {
        for (TokenListProvider p : MimeLookup.getLookup(MimePath.get("text/html")).lookupAll(TokenListProvider.class)) { // NOI18N
            TokenList l = p.findTokenList(doc);
            if (l != null) {
                List<TokenList> tokens = new ArrayList<>(2);
                tokens.add(new PHPTokenList(doc));
                tokens.add(l);
                return MultiTokenList.create(tokens);
            }
        }
    }
    return null;
}
 
Example 2
Source Project: netbeans   Source File: RemoteClient.java    License: Apache License 2.0 6 votes vote down vote up
private FileLock lockFile(FileObject fo) throws IOException, DownloadSkipException {
    try {
        return fo.lock();
    } catch (FileAlreadyLockedException lockedException) {
        if (warnChangedFile(fo)) {
            FileUtils.saveFile(fo);
            // XXX remove once #213141 is fixed
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
            }
            return fo.lock();
        } else {
            throw new DownloadSkipException();
        }
    }
}
 
Example 3
Source Project: netbeans   Source File: WindowsPhpEnvironment.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected List<DocumentRoot> getDocumentRoots(String projectName) {
    File htDocs = null;
    for (File root : getFsRoots()) {
        // standard apache installation
        File programFiles = new File(root, "Program Files"); // NOI18N
        htDocs = findHtDocsDirectory(programFiles, APACHE_FILENAME_FILTER);
        if (htDocs != null) {
            // one htdocs is enough
            break;
        }
    }
    if (htDocs != null) {
        String documentRoot = getFolderName(htDocs, projectName);
        String url = getDefaultUrl(projectName);
        String hint = NbBundle.getMessage(WindowsPhpEnvironment.class, "TXT_HtDocs");
        return Collections.singletonList(new DocumentRoot(documentRoot, url, hint, FileUtils.isDirectoryWritable(htDocs)));
    }
    // xampp
    return XAMPP.getDocumentRoots(projectName);
}
 
Example 4
Source Project: netbeans   Source File: GoToActionOrViewAction.java    License: Apache License 2.0 6 votes vote down vote up
private int getOffset(Lookup context) {
    EditorCookie editorCookie = context.lookup(EditorCookie.class);
    if (editorCookie != null) {
        return getOffset(editorCookie);
    }
    FileObject fo = FileUtils.getFileObject(context);
    if (fo == null) {
        return DEFAULT_OFFSET;
    }
    try {
        editorCookie = DataObject.find(fo).getLookup().lookup(EditorCookie.class);
        return getOffset(editorCookie);
    } catch (DataObjectNotFoundException ex) {
        Exceptions.printStackTrace(ex);
    }
    return DEFAULT_OFFSET;
}
 
Example 5
Source Project: netbeans   Source File: ConfigurationFiles.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Collection<FileInfo> getFiles() {
    FileObject sourceDir = getSourceDirectory();
    if (sourceDir == null) {
        // broken project
        return Collections.emptyList();
    }
    List<FileInfo> files = new ArrayList<>();
    FileObject configDir = sourceDir.getFileObject(CONFIG_DIRECTORY);
    if (configDir != null
            && configDir.isFolder()
            && configDir.isValid()) {
        Enumeration<? extends FileObject> children = configDir.getChildren(true);
        while (children.hasMoreElements()) {
            FileObject child = children.nextElement();
            if (child.isData()
                    && child.isValid()
                    && FileUtils.isPhpFile(child)) {
                files.add(new FileInfo(child));
            }
        }
        Collections.sort(files, FileInfo.COMPARATOR);
    }
    return files;
}
 
Example 6
Source Project: netbeans   Source File: ConfigActionTest.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean isRunMethodEnabled(Lookup context) {
    SingleMethod singleMethod = CommandUtils.singleMethodForContext(context);
    if (singleMethod == null) {
        return false;
    }
    FileObject file = singleMethod.getFile();
    if (file == null) {
        return false;
    }
    if (!FileUtils.isPhpFile(file)) {
        return false;
    }
    PhpModule phpModule = project.getPhpModule();
    for (PhpTestingProvider testingProvider : project.getTestingProviders()) {
        if (testingProvider.isTestFile(phpModule, file)) {
            return true;
        }
    }
    return false;
}
 
Example 7
Source Project: netbeans   Source File: RunTestsCommand.java    License: Apache License 2.0 6 votes vote down vote up
private FileObject findFolderWithTest(Lookup context) {
    FileObject[] files = CommandUtils.filesForContextOrSelectedNodes(context);
    if (files.length != 1) {
        return null;
    }
    FileObject file = files[0];
    if (!file.isFolder()) {
        return null;
    }
    Enumeration<? extends FileObject> children = file.getChildren(true);
    while (children.hasMoreElements()) {
        FileObject child = children.nextElement();
        if (child.isData()
                && isTestFile(child)
                && FileUtils.isPhpFile(child)) {
            return file;
        }
    }
    return null;
}
 
Example 8
Source Project: netbeans   Source File: NewFileWizardIterator.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Set<FileObject> instantiate() throws IOException {
    FileObject dir = Templates.getTargetFolder(wizard);
    FileObject template = Templates.getTemplate(wizard);

    DataFolder dataFolder = DataFolder.findFolder(dir);
    DataObject dataTemplate = DataObject.find(template);
    DataObject createdFile = dataTemplate.createFromTemplate(dataFolder, Templates.getTargetName(wizard), getTemplateParams());

    // #187374
    try {
        FileUtils.reformatFile(createdFile);
    } catch (IOException exc) {
        LOGGER.log(Level.WARNING, exc.getMessage(), exc);
    }

    return Collections.singleton(createdFile.getPrimaryFile());
}
 
Example 9
Source Project: netbeans   Source File: Utils.java    License: Apache License 2.0 6 votes vote down vote up
public static String validateTestSources(PhpProject project, String testDirPath) {
    if (!StringUtils.hasText(testDirPath)) {
        return NbBundle.getMessage(Utils.class, "MSG_FolderEmpty");
    }

    File testSourcesFile = new File(testDirPath);
    if (!testSourcesFile.isAbsolute()) {
        return NbBundle.getMessage(Utils.class, "MSG_TestNotAbsolute");
    } else if (!testSourcesFile.isDirectory()) {
        return NbBundle.getMessage(Utils.class, "MSG_TestNotDirectory");
    }
    FileObject nbproject = project.getProjectDirectory().getFileObject("nbproject"); // NOI18N
    FileObject testSourcesFo = FileUtil.toFileObject(testSourcesFile);
    if (testSourcesFile.equals(FileUtil.toFile(ProjectPropertiesSupport.getSourcesDirectory(project)))) {
        return NbBundle.getMessage(Utils.class, "MSG_TestEqualsSources");
    } else if (FileUtil.isParentOf(nbproject, testSourcesFo)
            || nbproject.equals(testSourcesFo)) {
        return NbBundle.getMessage(Utils.class, "MSG_TestUnderneathNBMetadata");
    } else if (!FileUtils.isDirectoryWritable(testSourcesFile)) {
        return NbBundle.getMessage(Utils.class, "MSG_TestNotWritable");
    }
    return null;
}
 
Example 10
Source Project: netbeans   Source File: SmartyPhpFrameworkProvider.java    License: Apache License 2.0 6 votes vote down vote up
@NbBundle.Messages({
    "SmartyPhpFrameworkProvider.tit.smarty.template.autodetection=Smarty autodetection",
})
@Override
public void phpModuleOpened(final PhpModule phpModule) {
    if (getSmartyPropertyEnabled(phpModule) == null) {
        try {
            ParserManager.parseWhenScanFinished(FileUtils.PHP_MIME_TYPE, new UserTask() {
                @Override
                public void run(ResultIterator resultIterator) throws Exception {
                    RP.post(new SmartyAutodetectionJob(phpModule));
                }
            });
        } catch (ParseException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
}
 
Example 11
Source Project: cakephp3-netbeans   Source File: CakePHP3GoToStatus.java    License: Apache License 2.0 6 votes vote down vote up
protected List<GoToItem> createAllItems(Category category) {
    if (fileObject == null) {
        return Collections.emptyList();
    }
    List<GoToItem> items = new ArrayList<>();
    CakePHP3Module cakeModule = CakePHP3Module.forFileObject(fileObject);
    ModuleInfo info = cakeModule.createModuleInfo(fileObject);
    List<FileObject> directories = cakeModule.getDirectories(info.getBase(), category, info.getPluginName());
    for (FileObject directory : directories) {
        for (FileObject child : directory.getChildren()) {
            if (child.isFolder() || !FileUtils.PHP_MIME_TYPE.equals(child.getMIMEType(FileUtils.PHP_MIME_TYPE))) {
                continue;
            }
            items.add(GoToItemFactory.create(category, child, 0));
        }
    }
    return items;
}
 
Example 12
Source Project: netbeans   Source File: CreateTestsSupport.java    License: Apache License 2.0 6 votes vote down vote up
private void sanitizeFiles(List<FileObject> sanitizedFiles, List<FileObject> files, PhpModule phpModule, PhpVisibilityQuery phpVisibilityQuery) {
    for (FileObject fo : files) {
        if (fo.isData()
                && FileUtils.isPhpFile(fo)
                && !isUnderTests(phpModule, fo)
                // XXX no way to test selenium here
                //&& !isUnderSelenium(phpModule, fo)
                && phpVisibilityQuery.isVisible(fo)) {
            sanitizedFiles.add(fo);
        }
        FileObject[] children = fo.getChildren();
        if (children.length > 0) {
            sanitizeFiles(sanitizedFiles, Arrays.asList(children), phpModule, phpVisibilityQuery);
        }
    }
}
 
Example 13
Source Project: netbeans   Source File: PHPSQLCompletionTest.java    License: Apache License 2.0 6 votes vote down vote up
private void checkPHPSQLStatement(String testString, String resultString) {
    int caretOffset = testString.indexOf('|');
    testString = testString.replace("|", "");

    Document document = getDocument(testString, FileUtils.PHP_MIME_TYPE, PHPTokenId.language());

    PHPSQLStatement stmt = PHPSQLStatement.computeSQLStatement(document, caretOffset);
    if (resultString == null) {
        assertNull(stmt);
        return;
    } else {
        assertNotNull(stmt);
    }

    assertEquals(resultString, stmt.getStatement());

    int virtualOffset = stmt.sourceToGeneratedPos(caretOffset);
    assertFalse(virtualOffset == -1);
    int sourceOffset = stmt.generatedToSourcePos(virtualOffset);
    assertEquals(caretOffset, sourceOffset);
}
 
Example 14
Source Project: netbeans   Source File: CodeSniffer.java    License: Apache License 2.0 6 votes vote down vote up
private List<String> getParameters(String standard, FileObject file, boolean noRecursion) {
    Charset encoding = FileEncodingQuery.getEncoding(file);
    List<String> params = new ArrayList<>();
    // NETBEANS-3243 the path of Code Sniffer may have --standard parameter
    if (StringUtils.hasText(standard)
            && !codeSnifferPath.contains(STANDARD_PARAM + "=") // NOI18N
            && !codeSnifferPath.contains(STANDARD_PARAM + " ")) { // NOI18N
        // #270987 use --standard
        params.add(String.format(STANDARD_PARAM_FORMAT, standard));
    }
    params.add(REPORT_PARAM);
    params.add(String.format(EXTENSIONS_PARAM, StringUtils.implode(FileUtil.getMIMETypeExtensions(FileUtils.PHP_MIME_TYPE), ","))); // NOI18N
    params.add(String.format(ENCODING_PARAM, encoding.name()));
    addIgnoredFiles(params, file);
    if (noRecursion) {
        params.add(NO_RECURSION_PARAM);
    }
    params.add(FileUtil.toFile(file).getAbsolutePath());
    return params;
}
 
Example 15
Source Project: netbeans   Source File: PhpProjectConvertor.java    License: Apache License 2.0 6 votes vote down vote up
private FileObject detectSourceRoot() {
    // first check if there is any *.php file right in project dir
    for (FileObject child : projectDirectory.getChildren()) {
        if (FileUtils.isPhpFile(child)) {
            return projectDirectory;
        }
    }
    // now, check well known sources
    for (String dir : KNOWN_SOURCE_ROOTS) {
        FileObject srcDir = projectDirectory.getFileObject(dir);
        if (srcDir != null)  {
            return srcDir;
        }
    }
    return projectDirectory;
}
 
Example 16
Source Project: cakephp3-netbeans   Source File: CakePHP3GoToStatus.java    License: Apache License 2.0 6 votes vote down vote up
public List<GoToItem> getTestCases() {
    if (fileObject == null || !FileUtils.isPhpFile(fileObject)) {
        return Collections.emptyList();
    }
    CakePHP3Module cakeModule = CakePHP3Module.forFileObject(fileObject);
    ModuleInfo info = cakeModule.createModuleInfo(fileObject);
    if (ModuleUtils.isTemplate(info.getCategory())) {
        return Collections.emptyList();
    }

    List<GoToItem> items = new ArrayList<>();
    List<FileObject> directories = cakeModule.getDirectories(info.getBase(), null, info.getPluginName());
    for (FileObject directory : directories) {
        Set<ClassElement> classElements = getClassElements(directory, fileObject.getName() + "Test"); // NOI18N
        for (ClassElement classElement : classElements) {
            FileObject testcase = classElement.getFileObject();
            items.add(GoToItemFactory.create(Category.TEST_CASE, testcase, DEFAULT_OFFSET));
        }
    }
    return items;
}
 
Example 17
Source Project: netbeans   Source File: PhpExecutableValidator.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Validate the given command and return error if it is not valid, {@code null} otherwise.
 * @param command command to be validated, can be {@code null}
 * @param executableName the name of the executable (e.g. "Doctrine script"), can be {@code null} (in such case, "File" is used)
 * @return error if it is not valid, {@code null} otherwise
 */
public static String validateCommand(@NullAllowed String command, @NullAllowed String executableName) {
    String executable = null;
    if (command != null) {
        executable = PhpExecutable.parseCommand(command).first();
    }
    if (executableName == null) {
        return FileUtils.validateFile(executable, false);
    }
    return FileUtils.validateFile(executableName, executable, false);
}
 
Example 18
Source Project: netbeans   Source File: TesterOptions.java    License: Apache License 2.0 5 votes vote down vote up
@CheckForNull
public String getTesterPath() {
    String path = getPreferences().get(TESTER_PATH, null);
    if (path == null && !testerSearched) {
        testerSearched = true;
        List<String> scripts = FileUtils.findFileOnUsersPath(Tester.TESTER_FILE_NAME);
        if (!scripts.isEmpty()) {
            path = scripts.get(0);
            setTesterPath(path);
        }
    }
    return path;
}
 
Example 19
Source Project: netbeans   Source File: PhpDocOptions.java    License: Apache License 2.0 5 votes vote down vote up
public synchronized String getPhpDoc() {
    String phpDoc = getPreferences().get(PHPDOC, null);
    if (phpDoc == null && !phpDocSearched) {
        phpDocSearched = true;
        List<String> scripts = FileUtils.findFileOnUsersPath(PhpDocScript.SCRIPT_NAME, PhpDocScript.SCRIPT_NAME_LONG, PhpDocScript.SCRIPT_NAME_PHAR);
        if (!scripts.isEmpty()) {
            phpDoc = scripts.get(0);
            setPhpDoc(phpDoc);
        }
    }
    return phpDoc;
}
 
Example 20
Source Project: netbeans   Source File: CodeceptionOptions.java    License: Apache License 2.0 5 votes vote down vote up
@CheckForNull
public String getCodeceptionPath() {
    String codeceptionPath = getPreferences().get(CODECEPTION_PATH, null);
    if (codeceptionPath == null && !codeceptionSearched) {
        List<String> scripts = FileUtils.findFileOnUsersPath(Codecept.SCRIPT_NAME, Codecept.SCRIPT_NAME_LONG, Codecept.SCRIPT_NAME_PHAR);
        if (!scripts.isEmpty()) {
            codeceptionPath = scripts.get(0);
            setCodeceptionPath(codeceptionPath);
        }
    }
    return codeceptionPath;
}
 
Example 21
Source Project: netbeans   Source File: SymfonyOptions.java    License: Apache License 2.0 5 votes vote down vote up
@CheckForNull
public String getInstaller() {
    String path = getPreferences().get(INSTALLER, null);
    if (path == null
            && !installerSearched) {
        installerSearched = true;
        List<String> files = FileUtils.findFileOnUsersPath(InstallerExecutable.NAME);
        if (!files.isEmpty()) {
            path = files.get(0);
            setInstaller(path);
        }
    }
    return path;
}
 
Example 22
Source Project: netbeans   Source File: AtoumTestLocator.java    License: Apache License 2.0 5 votes vote down vote up
private List<Locations.Offset> filterPhpFiles(FileObject sourceRoot, Collection<Pair<FileObject, Integer>> files) {
    List<Locations.Offset> results = new ArrayList<>(files.size());
    for (Pair<FileObject, Integer> pair : files) {
        FileObject fileObject = pair.first();
        if (FileUtils.isPhpFile(fileObject)
                && FileUtil.isParentOf(sourceRoot, fileObject)) {
            results.add(new Locations.Offset(fileObject, pair.second()));
        }
    }
    return results;
}
 
Example 23
Source Project: netbeans   Source File: AtoumOptions.java    License: Apache License 2.0 5 votes vote down vote up
@CheckForNull
public String getAtoumPath() {
    String path = getPreferences().get(ATOUM_PATH, null);
    if (path == null && !atoumSearched) {
        atoumSearched = true;
        List<String> scripts = FileUtils.findFileOnUsersPath(Atoum.PHAR_FILE_NAME, Atoum.ATOUM_FILE_NAME);
        if (!scripts.isEmpty()) {
            path = scripts.get(0);
            setAtoumPath(path);
        }
    }
    return path;
}
 
Example 24
Source Project: netbeans   Source File: AtoumTestingProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean isTestFile(PhpModule phpModule, FileObject fileObj) {
    if (!FileUtils.isPhpFile(fileObj)) {
        return false;
    }
    for (FileObject testDirectory : phpModule.getTestDirectories()) {
        if (FileUtil.isParentOf(testDirectory, fileObj)) {
            return true;
        }
    }
    return false;
}
 
Example 25
Source Project: netbeans   Source File: AtoumOptionsPanel.java    License: Apache License 2.0 5 votes vote down vote up
@NbBundle.Messages({
    "AtoumOptionsPanel.atoum.search.title=atoum files",
    "AtoumOptionsPanel.atoum.search.files=&atoum files:",
    "AtoumOptionsPanel.atoum.search.pleaseWaitPart=atoum files",
    "AtoumOptionsPanel.atoum.search.notFound=No atoum files found."
})
private void searchAtoumButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_searchAtoumButtonActionPerformed
    String atoum = UiUtils.SearchWindow.search(new UiUtils.SearchWindow.SearchWindowSupport() {
        @Override
        public List<String> detect() {
            return FileUtils.findFileOnUsersPath(Atoum.PHAR_FILE_NAME, Atoum.ATOUM_FILE_NAME);
        }
        @Override
        public String getWindowTitle() {
            return Bundle.AtoumOptionsPanel_atoum_search_title();
        }
        @Override
        public String getListTitle() {
            return Bundle.AtoumOptionsPanel_atoum_search_files();
        }
        @Override
        public String getPleaseWaitPart() {
            return Bundle.AtoumOptionsPanel_atoum_search_pleaseWaitPart();
        }
        @Override
        public String getNoItemsFound() {
            return Bundle.AtoumOptionsPanel_atoum_search_notFound();
        }
    });
    if (atoum != null) {
        atoumPathTextField.setText(atoum);
    }
    // TODO add your handling code here:
}
 
Example 26
Source Project: netbeans   Source File: ComposerOptions.java    License: Apache License 2.0 5 votes vote down vote up
public String getComposerPath() {
    String composerPath = preferences.get(COMPOSER_PATH, null);
    if (composerPath == null && !composerSearched) {
        composerSearched = true;
        List<String> paths = FileUtils.findFileOnUsersPath(Composer.COMPOSER_FILENAMES.toArray(new String[0]));
        if (!paths.isEmpty()) {
            composerPath = paths.get(0);
            setComposerPath(composerPath);
        }
    }
    return composerPath;
}
 
Example 27
Source Project: netbeans   Source File: AtoumPreferencesValidator.java    License: Apache License 2.0 5 votes vote down vote up
private void validatePath(boolean pathEnabled, String path, String label, String source) {
    if (!pathEnabled) {
        return;
    }
    String warning = FileUtils.validateFile(label, path, false);
    if (warning != null) {
        result.addWarning(new ValidationResult.Message(source, warning));
    }
}
 
Example 28
Source Project: netbeans   Source File: RunConfigScriptValidator.java    License: Apache License 2.0 5 votes vote down vote up
@NbBundle.Messages("RunConfigScriptValidator.workDir.label=Working directory")
static String validateWorkDir(String workDir, boolean allowEmptyString) {
    boolean hasText = StringUtils.hasText(workDir);
    if (allowEmptyString && !hasText) {
        return null;
    }
    return FileUtils.validateDirectory(Bundle.RunConfigScriptValidator_workDir_label(), workDir, false);
}
 
Example 29
Source Project: netbeans   Source File: TesterUtils.java    License: Apache License 2.0 5 votes vote down vote up
@NbBundle.Messages("TesterUtils.coverage.source.path.error=Absolute path to directory must be set for coverage source path.")
@CheckForNull
public static String validateCoverageSourcePath(@NullAllowed String sourcePath) {
    if (FileUtils.validateDirectory(sourcePath, false) != null) {
        return Bundle.TesterUtils_coverage_source_path_error();
    }
    return null;
}
 
Example 30
Source Project: netbeans   Source File: CompositePanelProviderImpl.java    License: Apache License 2.0 5 votes vote down vote up
@ProjectCustomizer.CompositeCategoryProvider.Registration(
    projectType = UiUtils.CUSTOMIZER_PATH,
    position = 1000
)
public static ProjectCustomizer.CompositeCategoryProvider createFormatting() {
    return Customizers.createFormattingCategoryProvider(Collections.singletonMap(
            "allowedMimeTypes", FileUtils.PHP_MIME_TYPE + ",text/html,text/css,text/javascript,text/x-json")); // NOI18N
}