/** * Updates a library with a new path. * * <p>This method acts both as a check and an action. If the project does not depend on the given * <var>oldRelativePath</var> then no action is done and <code>null</code> is returned. * * <p>If the project depends on the library, then the project is updated with the new path, and * the {@link LibraryState} for the library is returned. * * <p>Updating the project does two things: * * <ul> * <li>Update LibraryState with new relative path and new {@link IProject} object. * <li>Update the main project's <code>project.properties</code> with the new relative path for * the changed library. * </ul> * * @param oldRelativePath the old library path relative to this project * @param newRelativePath the new library path relative to this project * @param newLibraryState the new {@link ProjectState} object. * @return a non null object if the project depends on the library. * @see LibraryState#getProjectState() */ public LibraryState updateLibrary( String oldRelativePath, String newRelativePath, ProjectState newLibraryState) { // compute current location File projectFile = mProject.getLocation().toFile(); // loop on all libraries and check if the path matches synchronized (mLibraries) { for (LibraryState state : mLibraries) { if (state.getProjectState() == null) { try { // oldRelativePath may not be the same exact string as the // one in the project properties (trailing separator could be different // for instance). // Use java.io.File to deal with this and also do a platform-dependent // path comparison File library1 = new File(projectFile, oldRelativePath); File library2 = new File(projectFile, state.getRelativePath()); if (library1.getCanonicalPath().equals(library2.getCanonicalPath())) { // save the exact property string to replace. String oldProperty = state.getRelativePath(); // then update the LibraryPath. state.setRelativePath(newRelativePath); state.setProject(newLibraryState); // update the project.properties file IStatus status = replaceLibraryProperty(oldProperty, newRelativePath); if (status != null) { if (status.getSeverity() != IStatus.OK) { // log the error somehow. } } else { // This should not happen since the library wouldn't be here in the // first place } // return the LibraryState object. return state; } } catch (IOException e) { // ignore this library } } } } return null; }
/** * Returns whether a given library project is needed by the receiver. * * <p>If the library is needed, this finds the matching {@link LibraryState}, initializes it so * that it contains the library's {@link IProject} object (so that {@link * LibraryState#getProjectState()} does not return null) and then returns it. * * @param libraryProject the library project to check. * @return a non null object if the project is a library dependency, <code>null</code> otherwise. * @see LibraryState#getProjectState() */ public LibraryState needs(ProjectState libraryProject) { // compute current location File projectFile = mProject.getLocation().toFile(); // get the location of the library. File libraryFile = libraryProject.getProject().getLocation().toFile(); // loop on all libraries and check if the path match synchronized (mLibraries) { for (LibraryState state : mLibraries) { if (state.getProjectState() == null) { File library = new File(projectFile, state.getRelativePath()); try { File absPath = library.getCanonicalFile(); if (absPath.equals(libraryFile)) { state.setProject(libraryProject); return state; } } catch (IOException e) { // ignore this library } } } } return null; }