Java Code Examples for org.netbeans.spi.project.support.ant.PropertyUtils#relativizeFile()

The following examples show how to use org.netbeans.spi.project.support.ant.PropertyUtils#relativizeFile() . 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: CustomizerSources.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void configFilesFolderBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configFilesFolderBrowseActionPerformed
    JFileChooser chooser = new JFileChooser();
    FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    File fileName = new File(jTextFieldConfigFilesFolder.getText());
    File configFiles = fileName.isAbsolute() ? fileName : new File(projectFld, fileName.getPath());
    if (configFiles.isAbsolute()) {
        chooser.setSelectedFile(configFiles);
    } else {
        chooser.setSelectedFile(projectFld);
    }
    if ( JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(this)) {
        File selected = FileUtil.normalizeFile(chooser.getSelectedFile());
        String newConfigFiles;
        if (CollocationQuery.areCollocated(projectFld, selected)) {
            newConfigFiles = PropertyUtils.relativizeFile(projectFld, selected);
        } else {
            newConfigFiles = selected.getPath();
        }
        jTextFieldConfigFilesFolder.setText(newConfigFiles);
    }
}
 
Example 2
Source File: SelectionPanel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Shows the dialog for the selection of the library folder.
 */
@NbBundle.Messages({"SelectionPanel.browseDialog.title=Select directory for JS libraries"})
private void showBrowseDialog() {
    File libraryFolder = PropertyUtils.resolveFile(webRoot, getLibraryFolder());
    File selectedDir = new FileChooserBuilder(SelectionPanel.class)
            .setDirectoriesOnly(true)
            .setTitle(Bundle.SelectionPanel_browseDialog_title())
            .setDefaultWorkingDirectory(libraryFolder)
            .forceUseOfDefaultWorkingDirectory(true)
            .showOpenDialog();
    if (selectedDir != null) {
        String relativePath = PropertyUtils.relativizeFile(webRoot, selectedDir);
        String path;
        if (relativePath == null) {
            path = selectedDir.getAbsolutePath();
        } else {
            path = relativePath;
        }
        folderField.setText(path);
    }
}
 
Example 3
Source File: ProjectOperations.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void fixLibraryLocation(Operations original) throws IllegalArgumentException {
    String libPath = original.libraryPath;
    if (libPath != null) {
        if (!new File(libPath).isAbsolute()) {
            //relative path to libraries
            if (!original.libraryWithinProject) {
                File file = original.libraryFile;
                if (file == null) {
                    // could happen in some rare cases, but in that case the original project was already broken, don't fix.
                    return;
                }
                String relativized = PropertyUtils.relativizeFile(FileUtil.toFile(project.getProjectDirectory()), file);
                if (relativized != null) {
                    helper.getAntProjectHelper().setLibrariesLocation(relativized);
                } else {
                    //cannot relativize, use absolute path
                    helper.getAntProjectHelper().setLibrariesLocation(file.getAbsolutePath());
                }
            } else {
                //got copied over to new location.. the relative path is the same..
            }
        } else {

            //absolute path to libraries..
            if (original.libraryWithinProject) {
                if (original.absolutesRelPath != null) {
                    helper.getAntProjectHelper().setLibrariesLocation(PropertyUtils.resolveFile(FileUtil.toFile(project.getProjectDirectory()), original.absolutesRelPath).getAbsolutePath());
                }
            } else {
                // absolute path to an external folder stays the same.
            }
        }
    }
}
 
Example 4
Source File: CodeceptionPreferences.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static String relativizePath(PhpModule phpModule, String filePath) {
    if (!StringUtils.hasText(filePath)) {
        return ""; // NOI18N
    }
    File file = new File(filePath);
    String path = PropertyUtils.relativizeFile(FileUtil.toFile(phpModule.getProjectDirectory()), file);
    if (path == null) {
        // sorry, cannot be relativized
        path = file.getAbsolutePath();
    }
    return path;
}
 
Example 5
Source File: PhpProjectGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void configureSources(AntProjectHelper helper, ProjectProperties projectProperties,
        EditableProperties sharedProperties, EditableProperties privateProperties) {
    File srcDir = projectProperties.getSourcesDirectory();
    File projectDirectory = FileUtil.toFile(helper.getProjectDirectory());
    String srcPath = PropertyUtils.relativizeFile(projectDirectory, srcDir);
    if (srcPath == null) {
        // path cannot be relativized => use absolute path (any VCS can be hardly use, of course)
        srcPath = srcDir.getAbsolutePath();
    }
    sharedProperties.setProperty(PhpProjectProperties.SRC_DIR, srcPath);
    sharedProperties.setProperty(PhpProjectProperties.WEB_ROOT, "."); // NOI18N
    sharedProperties.setProperty(PhpProjectProperties.PHP_VERSION, projectProperties.getPhpVersion().name()); // NOI18N
}
 
Example 6
Source File: ProjectPropertiesSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Produce a machine-independent relativized version of a filename from a dir.
 * If path cannot be relative, the full path of the given file is returned.
 * @param dir base directory
 * @param file file to be relativized
 * @return relativized version of a filename from a dir or full path of the given file if the path cannot be relativized
 * @see PropertyUtils#relativizeFile(File, File)
 */
public static String relativizeFile(File dir, File file) {
    String relativePath = PropertyUtils.relativizeFile(dir, file);
    if (relativePath == null) {
        // path cannot be relativized => use absolute path (any VCS can be hardly use, of course)
        relativePath = file.getAbsolutePath();
    }
    return relativePath;
}
 
Example 7
Source File: SuiteInstallerProjectProperties.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void store() throws IOException {
    Preferences prefs = prefs(suiteProject);
    prefs.putBoolean(GENERATE_FOR_WINDOWS, windowsModel.isSelected());
    prefs.putBoolean(GENERATE_FOR_LINUX, linuxModel.isSelected());
    prefs.putBoolean(GENERATE_FOR_SOLARIS, solarisModel.isSelected());
    prefs.putBoolean(GENERATE_FOR_MAC, macModel.isSelected());
    String licenseName = (String) licenseModel.getSelectedItem();
    if (licenseName != null) {
        int index = licenseModel.getNames().indexOf(licenseName);
        if (index != -1) {
            String type = licenseModel.getTypes().get(index);
            if (type.equals(LICENSE_TYPE_FILE)) {
                File suiteLocation = FileUtil.toFile(suiteProject.getProjectDirectory());
                File f = PropertyUtils.resolveFile(suiteLocation, licenseName);
                String rel = PropertyUtils.relativizeFile(suiteLocation, f);
                if (rel != null) {
                    prefs.put(LICENSE_FILE, rel);
                } else {
                    prefs.put(LICENSE_FILE, f.getAbsolutePath());
                }
                prefs.remove(LICENSE_TYPE);
            } else {
                prefs.put(LICENSE_TYPE, type);
                prefs.remove(LICENSE_FILE);
            }
        }
    }
}
 
Example 8
Source File: AntArtifactProviderImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public URI[] getArtifactLocations() {
    String jarloc = eval.evaluate("${cluster}/${module.jar}"); // NOI18N
    File jar = helper.resolveFile(jarloc); // probably absolute anyway, now
    String reldir = PropertyUtils.relativizeFile(project.getProjectDirectoryFile(), jar);
    if (reldir != null) {
        try {
            return new URI[] {new URI(null, null, reldir, null)};
        } catch (URISyntaxException e) {
            throw new AssertionError(e);
        }
    } else {
        return new URI[] {Utilities.toURI(jar)};
    }
    // XXX should it add in class path extensions?
}
 
Example 9
Source File: PhpUnitPreferences.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static String relativizePath(PhpModule phpModule, String filePath) {
    if (!StringUtils.hasText(filePath)) {
        return ""; // NOI18N
    }
    File file = new File(filePath);
    String path = PropertyUtils.relativizeFile(FileUtil.toFile(phpModule.getProjectDirectory()), file);
    if (path == null) {
        // sorry, cannot be relativized
        path = file.getAbsolutePath();
    }
    return path;
}
 
Example 10
Source File: PhpUnit.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static String getRelPath(File testFile, File sourceFile, String absolutePrefix, String relativePrefix, String suffix, boolean forceAbsolute) {
    File parentFile = testFile.getParentFile();
    String relPath = PropertyUtils.relativizeFile(parentFile, sourceFile);
    if (relPath == null || forceAbsolute) {
        // cannot be versioned...
        relPath = absolutePrefix + sourceFile.getAbsolutePath() + suffix;
    } else {
        relPath = relativePrefix + relPath + suffix;
    }
    return relPath.replace(File.separatorChar, DIRECTORY_SEPARATOR);
}
 
Example 11
Source File: KarmaPreferences.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static String relativizePath(Project project, String filePath) {
    if (!StringUtils.hasText(filePath)) {
        return ""; // NOI18N
    }
    File file = new File(filePath);
    String path = PropertyUtils.relativizeFile(FileUtil.toFile(project.getProjectDirectory()), file);
    if (path == null
            || path.startsWith("../")) { // NOI18N
        // cannot be relativized or outside project
        path = file.getAbsolutePath();
    }
    return path;
}
 
Example 12
Source File: FileChooserAccessory.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private List<String> getRelativeFiles(List<File> files) {
    List<String> fs = new ArrayList<String>();
    for (File file : files) {
        String s = PropertyUtils.relativizeFile(baseFolder, file);
        if (s != null) {
            fs.add(s);
        }
    }
    return fs;
}
 
Example 13
Source File: ProtractorPreferences.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static String relativizePath(Project project, String filePath) {
    if (filePath == null
            || filePath.trim().isEmpty()) {
        return ""; // NOI18N
    }
    File file = new File(filePath);
    String path = PropertyUtils.relativizeFile(FileUtil.toFile(project.getProjectDirectory()), file);
    if (path == null
            || path.startsWith("../")) { // NOI18N
        // cannot be relativized or outside project
        path = file.getAbsolutePath();
    }
    return path;
}
 
Example 14
Source File: AppClientProjectOperations.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void fixLibraryLocation(AppClientProjectOperations original) throws IllegalArgumentException {
    String libPath = original.libraryPath;
    if (libPath != null) {
        if (!new File(libPath).isAbsolute()) {
            //relative path to libraries
            if (!original.libraryWithinProject) {
                File file = original.libraryFile;
                if (file == null) {
                    // could happen in some rare cases, but in that case the original project was already broken, don't fix.
                    return;
                }
                String relativized = PropertyUtils.relativizeFile(FileUtil.toFile(project.getProjectDirectory()), file);
                if (relativized != null) {
                    project.getAntProjectHelper().setLibrariesLocation(relativized);
                } else {
                    //cannot relativize, use absolute path
                    project.getAntProjectHelper().setLibrariesLocation(file.getAbsolutePath());
                }
            } else {
                //got copied over to new location.. the relative path is the same..
            }
        } else {

            //absolute path to libraries..
            if (original.libraryWithinProject) {
                if (original.absolutesRelPath != null) {
                    project.getAntProjectHelper().setLibrariesLocation(PropertyUtils.resolveFile(FileUtil.toFile(project.getProjectDirectory()), original.absolutesRelPath).getAbsolutePath());
                }
            } else {
                // absolute path to an external folder stays the same.
            }
        }
    }
}
 
Example 15
Source File: TesterPreferences.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static String relativizePath(PhpModule phpModule, String filePath) {
    if (!StringUtils.hasText(filePath)) {
        return ""; // NOI18N
    }
    File file = new File(filePath);
    String path = PropertyUtils.relativizeFile(FileUtil.toFile(phpModule.getProjectDirectory()), file);
    if (path == null) {
        // sorry, cannot be relativized
        path = file.getAbsolutePath();
    }
    return path;
}
 
Example 16
Source File: ProjectFactorySupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static String makeRelative(AntProjectHelper helper, String path) {
    File f = new File(path);
    if (!f.isAbsolute()) {
        return path;
    }
    File proj = FileUtil.toFile(helper.getProjectDirectory());
    if (CollocationQuery.areCollocated(f, proj)) {
        String relativePath = PropertyUtils.relativizeFile(proj, f);
        if (relativePath != null) {
            return relativePath;
        }
    }
    return path;
}
 
Example 17
Source File: Util.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** 
 * Relativize given file against the original project and if needed use 
 * ${project.dir} property as base. If file cannot be relativized
 * the absolute filepath is returned.
 * @param projectBase original project base folder
 * @param freeformBase Freeform project base folder
 * @param location location to relativize
 * @return text suitable for storage in project.xml representing given location
 */
public static String relativizeLocation(File projectBase, File freeformBase, File location) {
    if (CollocationQuery.areCollocated(projectBase, location)) {
        if (projectBase.equals(freeformBase)) {
            return PropertyUtils.relativizeFile(projectBase, location);
        } else if (projectBase.equals(location) && ProjectConstants.PROJECT_LOCATION_PREFIX.endsWith("/")) { // NOI18N
            return ProjectConstants.PROJECT_LOCATION_PREFIX.substring(0, ProjectConstants.PROJECT_LOCATION_PREFIX.length() - 1);
        } else {
            return ProjectConstants.PROJECT_LOCATION_PREFIX + PropertyUtils.relativizeFile(projectBase, location);
        }
    } else {
        return location.getAbsolutePath();
    }
}
 
Example 18
Source File: AntServices.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public String relativizeFile(File base, File relative) {
    return PropertyUtils.relativizeFile(base, relative);
}
 
Example 19
Source File: ModuleList.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Find cluster location of a netbeans module (standalone, suite components and NB.org).
 * @param basedir project basedir
 * @param nbroot location of netbeans.org source root; not used for standalone modules and suite components
 */
public static String findClusterLocation(File basedir, File nbroot, NbModuleType type) throws IOException {
    String cluster;
    switch (type) {
        case SUITE_COMPONENT:
            cluster = "${suite.build.dir}/cluster"; // NOI18N
            break;
        case STANDALONE:
            cluster = "${build.dir}/cluster"; // NOI18N
            break;
        case NETBEANS_ORG:
        default:
            String path = PropertyUtils.relativizeFile(nbroot, basedir);
    // #163744: can happen with symlinks       assert path.indexOf("..") == -1 : path;
            Map<String,String> clusterLocationsHere = clusterLocations.get(nbroot);
            if (clusterLocationsHere == null) {
                clusterLocationsHere = new HashMap<String,String>();
                Map<String,String> clusterDefs = getClusterProperties(nbroot);
                for (Map.Entry<String,String> entry : clusterDefs.entrySet()) {
                    String key = entry.getKey();
                    String clusterDir = clusterDefs.get(key + ".dir"); // NOI18N
                    if (clusterDir == null) {
                        // Not a list of modules.
                        // XXX could also just read clusters.list
                        continue;
                    }
                    String val = entry.getValue();
                    StringTokenizer tok = new StringTokenizer(val, ", "); // NOI18N
                    while (tok.hasMoreTokens()) {
                        String p = tok.nextToken();
                        clusterLocationsHere.put(p, clusterDir);
                    }
                }
                clusterLocations.put(nbroot, clusterLocationsHere);
            }
            cluster = clusterLocationsHere.get(path);
            if (cluster == null && path != null) {
                int clusterSep = path.lastIndexOf('/');
                if (clusterSep >= 0) {
                    String id = path.substring(clusterSep + 1);
                    String expCluster = path.substring(0, clusterSep);

                    cluster = clusterLocationsHere.get(id);
                    if (!expCluster.equals(cluster)) {
                        cluster = null;
                    }
                }
            }
            if (cluster == null) {
                cluster = "extra"; // NOI18N
            }
            cluster = "${netbeans.dest.dir}/" + cluster;
    }
    return cluster;
}
 
Example 20
Source File: NbModuleProjectGenerator.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Appends currently created project in the <code>projectDir<code> to a
 * suite project contained in the <code>suiteDir</code>. Also intelligently
 * decides whether an added project is relative to a destination suite or
 * absolute and uses either <em>nbproject/project.properties</em> or
 * <em>nbproject/private/private.properties</em> appropriately.
 */
private static void appendToSuite(String cnb, FileObject projectDir, File suiteDir) throws IOException {
    File projectDirF = FileUtil.toFile(projectDir);
    File suiteGlobalPropsFile = new File(suiteDir, "nbproject/project.properties"); // NOI18N
    FileObject suiteGlobalPropFO;
    if (suiteGlobalPropsFile.exists()) {
        suiteGlobalPropFO = FileUtil.toFileObject(suiteGlobalPropsFile);
    } else {
        suiteGlobalPropFO = createFileObject(suiteGlobalPropsFile);
    }
    EditableProperties globalProps = Util.loadProperties(suiteGlobalPropFO);
    String projectPropKey = "project." + cnb; // NOI18N
    String rel = PropertyUtils.relativizeFile(suiteDir, projectDirF);
    //mkleint: removed CollocationQuery.areCollocated() reference
    // when AlwaysRelativeCQI gets removed the condition resolves to false more frequently.
    // that might not be desirable.
    if (rel != null) {
        globalProps.setProperty(projectPropKey,
                rel);
    } else {
        File suitePrivPropsFile = new File(suiteDir, "nbproject/private/private.properties"); // NOI18N
        FileObject suitePrivPropFO;
        if (suitePrivPropsFile.exists()) {
            suitePrivPropFO = FileUtil.toFileObject(suitePrivPropsFile);
        } else {
            suitePrivPropFO = createFileObject(suitePrivPropsFile);
        }
        EditableProperties privProps= Util.loadProperties(suitePrivPropFO);
        privProps.setProperty(projectPropKey, projectDirF.getAbsolutePath());
        Util.storeProperties(suitePrivPropFO, privProps);
    }
    String modulesProp = globalProps.getProperty("modules"); // NOI18N
    if (modulesProp == null) {
        modulesProp = "";
    }
    if (modulesProp.length() > 0) {
        modulesProp += ":"; // NOI18N
    }
    modulesProp += "${" + projectPropKey + "}"; // NOI18N
    globalProps.setProperty("modules", modulesProp.split("(?<=:)", -1)); // NOI18N
    Util.storeProperties(suiteGlobalPropFO, globalProps);
}