Search in sources :

Example 11 with URIResolver

use of org.eclipse.wst.sse.core.internal.util.URIResolver 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)

Example 12 with URIResolver

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

the class TestFormatProcessorHTML method getModelForEdit.

/**
 * must release model (from edit) after
 *
 * @param filename
 *            relative to this class (TestStructuredPartitioner)
 */
private IStructuredModel getModelForEdit(final String filename) {
    IStructuredModel model = null;
    try {
        IModelManager modelManager = StructuredModelManager.getModelManager();
        InputStream inStream = getClass().getResourceAsStream(filename);
        if (inStream == null)
            throw new FileNotFoundException("Can't file resource stream " + filename);
        final String baseFile = getClass().getResource(filename).toString();
        model = modelManager.getModelForEdit(baseFile, inStream, new URIResolver() {

            String fBase = baseFile;

            public String getFileBaseLocation() {
                return fBase;
            }

            public String getLocationByURI(String uri) {
                return getLocationByURI(uri, fBase);
            }

            public String getLocationByURI(String uri, boolean resolveCrossProjectLinks) {
                return getLocationByURI(uri);
            }

            public String getLocationByURI(String uri, String baseReference) {
                int lastSlash = baseReference.lastIndexOf("/");
                if (lastSlash > 0)
                    return baseReference.substring(0, lastSlash + 1) + uri;
                return baseReference;
            }

            public String getLocationByURI(String uri, String baseReference, boolean resolveCrossProjectLinks) {
                return getLocationByURI(uri, baseReference);
            }

            public IProject getProject() {
                return null;
            }

            public IContainer getRootLocation() {
                return null;
            }

            public InputStream getURIStream(String uri) {
                return getClass().getResourceAsStream(getLocationByURI(uri));
            }

            public void setFileBaseLocation(String newLocation) {
                this.fBase = newLocation;
            }

            public void setProject(IProject newProject) {
            }
        });
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return model;
}
Also used : InputStream(java.io.InputStream) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) FileNotFoundException(java.io.FileNotFoundException) URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IOException(java.io.IOException) IProject(org.eclipse.core.resources.IProject)

Example 13 with URIResolver

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

the class ModelManagerImpl method createUnManagedEmptyModelFor.

/**
 * @param iFile
 * @param result
 * @return
 * @throws CoreException
 */
private IStructuredModel createUnManagedEmptyModelFor(IFile iFile) throws CoreException {
    IStructuredModel result = null;
    IModelHandler handler = calculateType(iFile);
    String id = calculateId(iFile);
    URIResolver resolver = calculateURIResolver(iFile);
    try {
        result = _commonCreateModel(id, handler, resolver);
    } catch (ResourceInUse e) {
        // this may need to be re-examined.
        if (Logger.DEBUG_MODELMANAGER)
            // $NON-NLS-1$ //$NON-NLS-2$
            Logger.log(Logger.INFO, "ModelMangerImpl::createUnManagedStructuredModelFor. Model unexpectedly in use.");
    }
    return result;
}
Also used : URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IModelHandler(org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler) ResourceInUse(org.eclipse.wst.sse.core.internal.provisional.exceptions.ResourceInUse)

Example 14 with URIResolver

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

the class ModelManagerImpl method _commonGetModel.

private IStructuredModel _commonGetModel(IFile iFile, ReadEditType rwType, EncodingRule encodingRule) throws UnsupportedEncodingException, IOException, CoreException {
    IStructuredModel model = null;
    if (iFile != null && iFile.exists()) {
        String id = calculateId(iFile);
        IModelHandler handler = calculateType(iFile);
        URIResolver resolver = calculateURIResolver(iFile);
        model = _commonCreateModel(iFile, id, handler, resolver, rwType, encodingRule);
    }
    return model;
}
Also used : URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IModelHandler(org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler)

Example 15 with URIResolver

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

the class ModelManagerImpl method createNewInstance.

/**
 * Similar to clone, except the new instance has no content. Note: this
 * produces an unmanaged model, for temporary use. If a true shared model
 * is desired, use "copy".
 */
public IStructuredModel createNewInstance(IStructuredModel oldModel) throws IOException {
    IModelHandler handler = oldModel.getModelHandler();
    IModelLoader loader = handler.getModelLoader();
    IStructuredModel newModel = loader.createModel(oldModel);
    newModel.setModelHandler(handler);
    if (newModel instanceof AbstractStructuredModel) {
        ((AbstractStructuredModel) newModel).setContentTypeIdentifier(oldModel.getContentTypeIdentifier());
    }
    URIResolver oldResolver = oldModel.getResolver();
    if (oldResolver instanceof URIResolverExtension) {
        oldResolver = ((URIResolverExtension) oldResolver).newInstance();
    }
    newModel.setResolver(oldResolver);
    try {
        newModel.setId(DUPLICATED_MODEL);
    } catch (ResourceInUse e) {
    // impossible, since this is an unmanaged model
    }
    // base location should be null, but we'll set to
    // null to be sure.
    newModel.setBaseLocation(null);
    return newModel;
}
Also used : IModelLoader(org.eclipse.wst.sse.core.internal.provisional.IModelLoader) URIResolver(org.eclipse.wst.sse.core.internal.util.URIResolver) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IModelHandler(org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler) URIResolverExtension(org.eclipse.wst.sse.core.internal.util.URIResolverExtension) ResourceInUse(org.eclipse.wst.sse.core.internal.provisional.exceptions.ResourceInUse)

Aggregations

URIResolver (org.eclipse.wst.sse.core.internal.util.URIResolver)17 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)12 IProject (org.eclipse.core.resources.IProject)10 FileNotFoundException (java.io.FileNotFoundException)7 IOException (java.io.IOException)7 InputStream (java.io.InputStream)7 IPath (org.eclipse.core.runtime.IPath)5 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)5 IContainer (org.eclipse.core.resources.IContainer)4 IFile (org.eclipse.core.resources.IFile)4 Path (org.eclipse.core.runtime.Path)4 IModelHandler (org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler)3 ProjectResolver (org.eclipse.wst.sse.core.internal.util.ProjectResolver)3 FileInputStream (java.io.FileInputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)2 ResourceInUse (org.eclipse.wst.sse.core.internal.provisional.exceptions.ResourceInUse)2 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1