Example usage for org.eclipse.jdt.internal.core.util Util toLocalFile

List of usage examples for org.eclipse.jdt.internal.core.util Util toLocalFile

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core.util Util toLocalFile.

Prototype

public static File toLocalFile(URI uri, IProgressMonitor monitor) throws CoreException 

Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.builder.ClasspathJar.java

License:Open Source License

ClasspathJar(IFile resource, AccessRuleSet accessRuleSet) {
    this.resource = resource;
    try {//from   w  ww.j  ava  2 s. c om
        java.net.URI location = resource.getLocationURI();
        if (location == null) {
            this.zipFilename = ""; //$NON-NLS-1$
        } else {
            File localFile = Util.toLocalFile(location, null);
            this.zipFilename = localFile.getPath();
        }
    } catch (CoreException e) {
        // ignore
    }
    this.zipFile = null;
    this.knownPackageNames = null;
    packageNames = null;
    this.accessRuleSet = accessRuleSet;
}

From source file:io.sarl.eclipse.util.JavaClasspathParser.java

License:Apache License

/**
 * Reads entry of a .classpath file./*from w w w . j  a v a  2s.  c om*/
 *
 * @param projectName
 *            - the name of project containing the .classpath file
 * @param projectRootAbsoluteFullPath
 *            - the path to project containing the .classpath file
 * @param unknownElements
 *            - map of unknow elements
 * @return the set of CLasspath Entries extracted from the .classpath
 * @throws CoreException
 *             - exception during parsing of .classpath
 * @throws IOException
 *             - exception during parsing of .classpath
 * @throws ClasspathEntry.AssertionFailedException
 *             - exception during parsing of .classpath
 * @throws URISyntaxException
 *             - exception during parsing of .classpath
 */
@SuppressWarnings("checkstyle:innerassignment")
public static IClasspathEntry[][] readFileEntriesWithException(String projectName,
        URL projectRootAbsoluteFullPath, Map<IPath, UnknownXmlElements> unknownElements)
        throws CoreException, IOException, ClasspathEntry.AssertionFailedException, URISyntaxException {

    final URL rscFile = new URL(
            projectRootAbsoluteFullPath.toExternalForm().concat(JavaProject.CLASSPATH_FILENAME));
    byte[] bytes;

    // when a project is imported, we get a first delta for the addition of the .project, but the .classpath is not accessible
    // so default to using java.io.File
    // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=96258
    final URI location;
    try {
        location = rscFile.toURI();
    } catch (URISyntaxException e) {
        throw e;
    }
    if (location == null) {
        throw new IOException("Cannot obtain a location URI for " + rscFile); //$NON-NLS-1$
    }
    final File file = Util.toLocalFile(location, null/* no progress monitor available */);
    if (file == null) {
        throw new IOException("Unable to fetch file from " + location); //$NON-NLS-1$
    }

    try {
        bytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(file);
    } catch (IOException e) {
        throw e;
    }

    if (hasUTF8BOM(bytes)) {
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=240034
        final int length = bytes.length - IContentDescription.BOM_UTF_8.length;
        System.arraycopy(bytes, IContentDescription.BOM_UTF_8.length, bytes = new byte[length], 0, length);
    }
    String xmlClasspath;
    try {
        // .classpath always encoded with UTF-8
        xmlClasspath = new String(bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8);
    } catch (UnsupportedEncodingException e) {
        Util.log(e, "Could not read .classpath with UTF-8 encoding"); //$NON-NLS-1$
        // fallback to default
        xmlClasspath = new String(bytes);
    }
    return decodeClasspath(projectName, Path.fromPortableString(projectRootAbsoluteFullPath.getPath()),
            xmlClasspath, unknownElements);
}

From source file:net.sf.j2s.core.builder.ClasspathJar.java

License:Open Source License

ClasspathJar(IFile resource, AccessRuleSet accessRuleSet) {
    this.resource = resource;
    try {/*from  w w w  . j  a va 2 s .  co m*/
        java.net.URI location = resource.getLocationURI();
        if (location == null) {
            this.zipFilename = ""; //$NON-NLS-1$
        } else {
            File localFile = Util.toLocalFile(location, null);
            this.zipFilename = localFile.getPath();
        }
    } catch (CoreException e) {
        // ignore
    }
    this.zipFile = null;
    this.knownPackageNames = null;
    this.accessRuleSet = accessRuleSet;
}

From source file:org.eclipse.jdt.internal.core.JavaModelManager.java

License:Open Source License

/**
 * Returns the open ZipFile at the given path. If the ZipFile
 * does not yet exist, it is created, opened, and added to the cache
 * of open ZipFiles.//from w w w  . jav a2 s.c  om
 *
 * The path must be a file system path if representing an external
 * zip/jar, or it must be an absolute workspace relative path if
 * representing a zip/jar inside the workspace.
 *
 * @exception CoreException If unable to create/open the ZipFile
 */
public ZipFile getZipFile(IPath path) throws CoreException {

    if (isInvalidArchive(path))
        throw new CoreException(new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.status_IOException,
                new ZipException()));

    ZipCache zipCache;
    ZipFile zipFile;
    if ((zipCache = (ZipCache) this.zipFiles.get()) != null && (zipFile = zipCache.getCache(path)) != null) {
        return zipFile;
    }
    File localFile = null;
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IResource file = root.findMember(path);
    if (file != null) {
        // internal resource
        URI location;
        if (file.getType() != IResource.FILE || (location = file.getLocationURI()) == null) {
            throw new CoreException(new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1,
                    Messages.bind(Messages.file_notFound, path.toString()), null));
        }
        localFile = Util.toLocalFile(location, null/*no progress availaible*/);
        if (localFile == null)
            throw new CoreException(new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1,
                    Messages.bind(Messages.file_notFound, path.toString()), null));
    } else {
        // external resource -> it is ok to use toFile()
        localFile = path.toFile();
    }

    try {
        if (ZIP_ACCESS_VERBOSE) {
            System.out.println("(" + Thread.currentThread() //$NON-NLS-1$
                    + ") [JavaModelManager.getZipFile(IPath)] Creating ZipFile on " + localFile); //$NON-NLS-1$
        }
        zipFile = new ZipFile(localFile);
        if (zipCache != null) {
            zipCache.setCache(path, zipFile);
        }
        return zipFile;
    } catch (IOException e) {
        addInvalidArchive(path);
        throw new CoreException(
                new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.status_IOException, e));
    }
}

From source file:org.eclipse.jdt.internal.core.JavaProject.java

License:Open Source License

/**
 * Retrieve a shared property on a project. If the property is not defined, answers null.
 * Note that it is orthogonal to IResource persistent properties, and client code has to decide
 * which form of storage to use appropriately. Shared properties produce real resource files which
 * can be shared through a VCM onto a server. Persistent properties are not shareable.
 *
 * @param key String//from w  ww  . j  a  va2  s  . c  o  m
 * @see JavaProject#setSharedProperty(String, String)
 * @return String
 * @throws CoreException
 */
public String getSharedProperty(String key) throws CoreException {

    String property = null;
    IFile rscFile = this.project.getFile(key);
    if (rscFile.exists()) {
        byte[] bytes = Util.getResourceContentsAsByteArray(rscFile);
        try {
            property = new String(bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8); // .classpath always encoded with UTF-8
        } catch (UnsupportedEncodingException e) {
            Util.log(e, "Could not read .classpath with UTF-8 encoding"); //$NON-NLS-1$
            // fallback to default
            property = new String(bytes);
        }
    } else {
        // when a project is imported, we get a first delta for the addition of the .project, but the .classpath is not accessible
        // so default to using java.io.File
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=96258
        URI location = rscFile.getLocationURI();
        if (location != null) {
            File file = Util.toLocalFile(location, null/*no progress monitor available*/);
            if (file != null && file.exists()) {
                byte[] bytes;
                try {
                    bytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(file);
                } catch (IOException e) {
                    return null;
                }
                try {
                    property = new String(bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8); // .classpath always encoded with UTF-8
                } catch (UnsupportedEncodingException e) {
                    Util.log(e, "Could not read .classpath with UTF-8 encoding"); //$NON-NLS-1$
                    // fallback to default
                    property = new String(bytes);
                }
            }
        }
    }
    return property;
}

From source file:org.eclipse.jdt.internal.core.JavaProject.java

License:Open Source License

/**
 * Reads the classpath file entries of this project's .classpath file.
 * Returns a two-dimensional array, where the number of elements in the row is fixed to 2.
 * The first element is an array of raw classpath entries, which includes the output entry,
 * and the second element is an array of referenced entries that may have been stored 
 * by the client earlier. /*from   w  w w . j  av  a 2s  .  c  o  m*/
 * See {@link IJavaProject#getReferencedClasspathEntries()} for more details.
 * As a side effect, unknown elements are stored in the given map (if not null)
 * Throws exceptions if the file cannot be accessed or is malformed.
 */
public IClasspathEntry[][] readFileEntriesWithException(Map unknownElements)
        throws CoreException, IOException, ClasspathEntry.AssertionFailedException {
    IFile rscFile = this.project.getFile(JavaProject.CLASSPATH_FILENAME);
    byte[] bytes;
    if (rscFile.exists()) {
        bytes = Util.getResourceContentsAsByteArray(rscFile);
    } else {
        // when a project is imported, we get a first delta for the addition of the .project, but the .classpath is not accessible
        // so default to using java.io.File
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=96258
        URI location = rscFile.getLocationURI();
        if (location == null)
            throw new IOException("Cannot obtain a location URI for " + rscFile); //$NON-NLS-1$
        File file = Util.toLocalFile(location, null/*no progress monitor available*/);
        if (file == null)
            throw new IOException("Unable to fetch file from " + location); //$NON-NLS-1$
        try {
            bytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(file);
        } catch (IOException e) {
            if (!file.exists())
                return new IClasspathEntry[][] { defaultClasspath(), ClasspathEntry.NO_ENTRIES };
            throw e;
        }
    }
    if (hasUTF8BOM(bytes)) { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=240034
        int length = bytes.length - IContentDescription.BOM_UTF_8.length;
        System.arraycopy(bytes, IContentDescription.BOM_UTF_8.length, bytes = new byte[length], 0, length);
    }
    String xmlClasspath;
    try {
        xmlClasspath = new String(bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8); // .classpath always encoded with UTF-8
    } catch (UnsupportedEncodingException e) {
        Util.log(e, "Could not read .classpath with UTF-8 encoding"); //$NON-NLS-1$
        // fallback to default
        xmlClasspath = new String(bytes);
    }
    return decodeClasspath(xmlClasspath, unknownElements);
}

From source file:org.jboss.tools.maven.conversion.ui.dialog.xpl.ConversionUtils.java

License:Open Source License

public static File getFile(IPath path) throws CoreException {
    Assert.isNotNull(path, "path can not be null");
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IResource file = root.findMember(path);
    File localFile = null;/*from ww w  .  j  a va2  s .c o  m*/
    if (file != null) {
        // internal resource
        URI location;
        if (file.getType() != IResource.FILE || (location = file.getLocationURI()) == null) {
            throw new CoreException(new Status(IStatus.ERROR, MavenDependencyConversionActivator.PLUGIN_ID, -1,
                    Messages.bind(Messages.file_notFound, path.toString()), null));
        }
        localFile = Util.toLocalFile(location, null/*no progress availaible*/);
        if (localFile == null)
            throw new CoreException(new Status(IStatus.ERROR, MavenDependencyConversionActivator.PLUGIN_ID, -1,
                    Messages.bind(Messages.file_notFound, path.toString()), null));
    } else {
        // external resource -> it is ok to use toFile()
        localFile = path.toFile();
    }
    return localFile;
}