Search in sources :

Example 1 with ProjectResolver

use of org.eclipse.wst.sse.core.internal.util.ProjectResolver in project webtools.sourceediting by eclipse.

the class URLModelProvider method getCommonModelFor.

/**
 * <code>baseModel</code>: the model containing the link
 * <code>ref</code>: the link URL string
 */
private IStructuredModel getCommonModelFor(final IStructuredModel baseModel, final String ref, final int which) throws IOException {
    // first, create absolute url
    String absURL = resolveURI(baseModel, ref, true);
    if ((absURL == null) || (absURL.length() == 0)) {
        return null;
    }
    // need to remove file:// scheme if necessary
    try {
        final java.net.URL aURL = new java.net.URL(absURL);
        // resolve it by finding the file it points to
        if (!aURL.getProtocol().equals("platform")) {
            // $NON-NLS-1$
            if (aURL.getProtocol().equals("file") && (aURL.getHost().equals("localhost") || aURL.getHost().length() == 0)) {
                // $NON-NLS-2$//$NON-NLS-1$
                absURL = aURL.getFile();
                final IPath ipath = new Path(absURL);
                // if path has a device, and if it begins with
                // IPath.SEPARATOR, remove it
                final String device = ipath.getDevice();
                if ((device != null) && (device.length() > 0)) {
                    if (device.charAt(0) == IPath.SEPARATOR) {
                        final String newDevice = device.substring(1);
                        absURL = ipath.setDevice(newDevice).toString();
                    }
                }
            }
        }
    } catch (java.net.MalformedURLException mfuExc) {
    }
    // next, decide project
    IProject project = null;
    final IPath fullIPath = new Path(absURL);
    IWorkspaceRoot workspace = ResourcesPlugin.getWorkspace().getRoot();
    IContainer container = workspace.getContainerForLocation(fullIPath);
    if (container != null) {
        // fullIPath doesn't exist in workspace
        project = container.getProject();
    }
    // now, get absURL's IFile
    if ((project != null) && (project.getLocation().isPrefixOf(fullIPath) == false)) {
        // it's at outside of Project
        return null;
    }
    IStructuredModel model = null;
    if (project != null) {
        IPath filePath = fullIPath.removeFirstSegments(project.getLocation().segmentCount());
        IFile file = (filePath != null && !filePath.isEmpty()) ? project.getFile(filePath) : null;
        if (file == null) {
            return null;
        }
        // obtain model
        if (which == GET_MODEL_FOR_EDIT) {
            model = getModelForEdit(file);
        } else if (which == GET_MODEL_FOR_READ) {
            model = getModelForRead(file);
        }
        // responsibility
        if (model != null && model.getSynchronizationStamp() == IResource.NULL_STAMP)
            model.resetSynchronizationStamp(file);
    } else {
        String id = null;
        InputStream inStream = null;
        // obtain resolver
        URIResolver resolver = (project != null) ? (URIResolver) project.getAdapter(URIResolver.class) : null;
        if (resolver == null) {
            // ProjectResolver can take care of the case if project is
            // null.
            resolver = new ProjectResolver(project);
        }
        if (resolver == null) {
            return null;
        }
        // there is no project. we can't expect IProject help to create
        // id/inputStream
        java.io.File file = fullIPath.toFile();
        // obatin id
        id = calculateId(fullIPath);
        // obtain InputStream
        try {
            inStream = new FileInputStream(file);
        } catch (FileNotFoundException fnfe) {
            // the file does not exist, or we don't have read permission
            return null;
        }
        // obtain model
        try {
            if (which == GET_MODEL_FOR_EDIT) {
                model = getModelManager().getModelForEdit(id, inStream, resolver);
            } else if (which == GET_MODEL_FOR_READ) {
                model = getModelManager().getModelForRead(id, inStream, resolver);
            }
        } catch (UnsupportedEncodingException ue) {
        } catch (IOException ioe) {
        } finally {
            // close now !
            if (inStream != null) {
                inStream.close();
            }
        }
    }
    // set locationid
    if (model != null && model.getBaseLocation() == null) {
        model.setBaseLocation(fullIPath.toString());
    }
    return model;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ProjectResolver(org.eclipse.wst.sse.core.internal.util.ProjectResolver) FileNotFoundException(java.io.FileNotFoundException) URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) IProject(org.eclipse.core.resources.IProject) FileInputStream(java.io.FileInputStream) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IContainer(org.eclipse.core.resources.IContainer)

Example 2 with ProjectResolver

use of org.eclipse.wst.sse.core.internal.util.ProjectResolver in project webtools.sourceediting by eclipse.

the class ModelManagerImpl method calculateURIResolver.

/**
 */
private URIResolver calculateURIResolver(IFile file) {
    // Note: see comment in plugin.xml for potentially
    // breaking change in behavior.
    IProject project = file.getProject();
    URIResolver resolver = project.getAdapter(URIResolver.class);
    if (resolver == null)
        resolver = new ProjectResolver(project);
    Object location = file.getLocation();
    if (location == null)
        location = file.getLocationURI();
    if (location != null)
        resolver.setFileBaseLocation(location.toString());
    return resolver;
}
Also used : ProjectResolver(org.eclipse.wst.sse.core.internal.util.ProjectResolver) URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) IProject(org.eclipse.core.resources.IProject)

Example 3 with ProjectResolver

use of org.eclipse.wst.sse.core.internal.util.ProjectResolver in project webtools.sourceediting by eclipse.

the class URLModelProviderCSS method getCommonModelFor.

/**
 * <code>baseModel</code>: the model containing the link
 * <code>ref</code>: the link URL string
 */
private IStructuredModel getCommonModelFor(final IStructuredModel baseModel, final String ref, final int which) throws IOException {
    // first, create absolute url
    String absURL = resolveURI(baseModel, ref, true);
    if ((absURL == null) || (absURL.length() == 0)) {
        return null;
    }
    // need to remove file:// scheme if necessary
    try {
        final URL aURL = new URL(absURL);
        // resolve it by finding the file it points to
        if (!aURL.getProtocol().equals("platform")) {
            // $NON-NLS-1$
            if (aURL.getProtocol().equals("file") && (aURL.getHost().equals("localhost") || aURL.getHost().length() == 0)) {
                // $NON-NLS-2$//$NON-NLS-1$
                absURL = aURL.getFile();
                final IPath ipath = new Path(absURL);
                // if path has a device, and if it begins with
                // IPath.SEPARATOR, remove it
                final String device = ipath.getDevice();
                if ((device != null) && (device.length() > 0)) {
                    if (device.charAt(0) == IPath.SEPARATOR) {
                        final String newDevice = device.substring(1);
                        absURL = ipath.setDevice(newDevice).toString();
                    }
                }
            }
        }
    } catch (MalformedURLException mfuExc) {
    }
    // next, decide project
    IProject project = null;
    final IPath fullIPath = new Path(absURL);
    IWorkspaceRoot workspace = ResourcesPlugin.getWorkspace().getRoot();
    IContainer container = workspace.getContainerForLocation(fullIPath);
    if (container != null) {
        // fullIPath doesn't exist in workspace
        project = container.getProject();
    }
    // now, get absURL's IFile
    if ((project != null) && (project.getLocation().isPrefixOf(fullIPath) == false)) {
        // it's at outside of Project
        return null;
    }
    IStructuredModel model = null;
    if (project != null) {
        IPath filePath = fullIPath.removeFirstSegments(project.getLocation().segmentCount());
        IFile file = (filePath != null && !filePath.isEmpty()) ? project.getFile(filePath) : null;
        if (file == null) {
            return null;
        }
        // obtain model
        if (which == GET_MODEL_FOR_EDIT) {
            model = getModelForEdit(file);
        } else if (which == GET_MODEL_FOR_READ) {
            model = getModelForRead(file);
        }
        // responsibility
        if (model != null && model.getSynchronizationStamp() == IResource.NULL_STAMP)
            model.resetSynchronizationStamp(file);
    } else {
        String id = null;
        InputStream inStream = null;
        // obtain resolver
        URIResolver resolver = (project != null) ? (URIResolver) project.getAdapter(URIResolver.class) : null;
        if (resolver == null) {
            // ProjectResolver can take care of the case if project is
            // null.
            resolver = new ProjectResolver(project);
        }
        if (resolver == null) {
            return null;
        }
        // there is no project. we can't expect IProject help to create
        // id/inputStream
        File file = fullIPath.toFile();
        // obatin id
        id = calculateId(fullIPath);
        // obtain InputStream
        try {
            inStream = new FileInputStream(file);
        } catch (FileNotFoundException fnfe) {
            // the file does not exist, or we don't have read permission
            return null;
        }
        // obtain model
        try {
            if (which == GET_MODEL_FOR_EDIT) {
                model = getModelManager().getModelForEdit(id, inStream, resolver);
            } else if (which == GET_MODEL_FOR_READ) {
                model = getModelManager().getModelForRead(id, inStream, resolver);
            }
        } catch (UnsupportedEncodingException ue) {
        } catch (IOException ioe) {
        } finally {
            // close now !
            if (inStream != null) {
                inStream.close();
            }
        }
    }
    // set locationid
    if (model != null && model.getBaseLocation() == null) {
        model.setBaseLocation(fullIPath.toString());
    }
    return model;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) MalformedURLException(java.net.MalformedURLException) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ProjectResolver(org.eclipse.wst.sse.core.internal.util.ProjectResolver) FileNotFoundException(java.io.FileNotFoundException) URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) URL(java.net.URL) IProject(org.eclipse.core.resources.IProject) FileInputStream(java.io.FileInputStream) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IContainer(org.eclipse.core.resources.IContainer) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Aggregations

IProject (org.eclipse.core.resources.IProject)3 ProjectResolver (org.eclipse.wst.sse.core.internal.util.ProjectResolver)3 URIResolver (org.eclipse.wst.sse.core.internal.util.URIResolver)3 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 IContainer (org.eclipse.core.resources.IContainer)2 IFile (org.eclipse.core.resources.IFile)2 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)2 IPath (org.eclipse.core.runtime.IPath)2 Path (org.eclipse.core.runtime.Path)2 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)2 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1