Search in sources :

Example 51 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.

the class ModelHandlerRegistry method getHandlerFor.

/**
 * Finds the registered IModelHandler for a given named file's content
 * type.
 *
 * @param file
 * @param provideDefault should the default extension be used in the absence of other methods
 * @return The IModelHandler registered for the content type of the given
 *         file. If an exact match is not found, the most-specific match
 *         according to IContentType.isKindOf() will be returned. If none
 *         are found, either a default or null will be returned.
 * @throws CoreException
 */
public IModelHandler getHandlerFor(IFile file, boolean provideDefault) throws CoreException {
    IModelHandler modelHandler = null;
    IContentDescription contentDescription = null;
    IContentType contentType = null;
    boolean accessible = file.isAccessible();
    if (accessible) {
        /* Try the optimized method first as the description may be cached */
        contentDescription = file.getContentDescription();
        if (contentDescription != null) {
            // use the provided description
            contentType = contentDescription.getContentType();
        } else {
            /* use the more thorough discovery method to get a description */
            InputStream contents = null;
            try {
                contents = file.getContents(false);
                contentDescription = Platform.getContentTypeManager().getDescriptionFor(contents, file.getName(), IContentDescription.ALL);
                if (contentDescription != null) {
                    contentType = contentDescription.getContentType();
                }
            } catch (IOException e) {
                // nothing further can be done, but will log for debugging
                Logger.logException(e);
            } finally {
                if (contents != null) {
                    try {
                        contents.close();
                    } catch (IOException e1) {
                    // nothing can be done
                    }
                }
            }
        }
    }
    /*
		 * If we couldn't get the content type from a description, try basing
		 * it on just the filename
		 */
    if (contentType == null) {
        contentType = Platform.getContentTypeManager().findContentTypeFor(file.getName());
    }
    if (contentType != null) {
        modelHandler = getHandlerForContentType(contentType);
    } else if (contentType == null && provideDefault) {
        // hard coding for null content type
        // $NON-NLS-1$
        modelHandler = getHandlerExtension(INTERNAL_DEFAULT_EXTENSION);
    }
    return modelHandler;
}
Also used : InputStream(java.io.InputStream) IContentType(org.eclipse.core.runtime.content.IContentType) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) IModelHandler(org.eclipse.wst.sse.core.internal.ltk.modelhandler.IModelHandler)

Example 52 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.

the class JSPModelLoader method initEmbeddedTypePre.

protected void initEmbeddedTypePre(IStructuredModel model, IStructuredDocument structuredDocument) {
    // note: this will currently only work for models backed by files
    EmbeddedTypeHandler embeddedContentType = null;
    IDOMModel domModel = (IDOMModel) model;
    if (embeddedContentType == null) {
        IContentDescription desc = getContentDescription(structuredDocument);
        if (desc != null) {
            Object prop = null;
            prop = desc.getProperty(IContentDescriptionForJSP.CONTENT_FAMILY_ATTRIBUTE);
            if (prop != null) {
                if (ContentTypeFamilyForHTML.HTML_FAMILY.equals(prop)) {
                    embeddedContentType = EmbeddedTypeRegistryImpl.getInstance().getTypeFor("text/html");
                }
            }
            if (embeddedContentType == null) {
                prop = desc.getProperty(IContentDescriptionForJSP.CONTENT_TYPE_ATTRIBUTE);
                if (prop != null) {
                    embeddedContentType = EmbeddedTypeRegistryImpl.getInstance().getTypeFor((String) prop);
                }
            }
        }
    }
    IDOMDocument document = domModel.getDocument();
    PageDirectiveAdapter pageDirectiveAdapter = (PageDirectiveAdapter) document.getAdapterFor(PageDirectiveAdapter.class);
    if (embeddedContentType != null) {
        pageDirectiveAdapter.setEmbeddedType(embeddedContentType);
        embeddedContentType.initializeFactoryRegistry(model.getFactoryRegistry());
    } else {
        // use default embeddedType if it couldn't determine one
        embeddedContentType = getJSPDefaultEmbeddedType(model);
        pageDirectiveAdapter.setEmbeddedType(embeddedContentType);
        embeddedContentType.initializeFactoryRegistry(model.getFactoryRegistry());
    }
}
Also used : EmbeddedTypeHandler(org.eclipse.wst.sse.core.internal.ltk.modelhandler.EmbeddedTypeHandler) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) PageDirectiveAdapter(org.eclipse.jst.jsp.core.internal.document.PageDirectiveAdapter) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 53 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.

the class JSPContentSourceValidator method isFragment.

/**
 * Determines if file is jsp fragment or not (does a deep, indepth check,
 * looking into contents of file)
 *
 * @param file
 *            assumes file is not null and exists
 * @return true if file is jsp fragment, false otherwise
 */
private boolean isFragment(IFile file) {
    // copied from JSPValidator
    boolean isFragment = false;
    InputStream is = null;
    try {
        IContentDescription contentDescription = file.getContentDescription();
        // it can be null
        if (contentDescription == null) {
            is = file.getContents();
            contentDescription = Platform.getContentTypeManager().getDescriptionFor(is, file.getName(), new QualifiedName[] { IContentDescription.CHARSET });
        }
        if (contentDescription != null) {
            String fileCtId = contentDescription.getContentType().getId();
            isFragment = (fileCtId != null && ContentTypeIdForJSP.ContentTypeID_JSPFRAGMENT.equals(fileCtId));
        }
    } catch (IOException e) {
    // ignore, assume it's invalid JSP
    } catch (CoreException e) {
    // ignore, assume it's invalid JSP
    } finally {
        // must close input stream in case others need it
        if (is != null)
            try {
                is.close();
            } catch (Exception e) {
            // not sure how to recover at this point
            }
    }
    return isFragment;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) InputStream(java.io.InputStream) QualifiedName(org.eclipse.core.runtime.QualifiedName) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 54 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.

the class FileBufferModelManager method detectContentType.

IContentType detectContentType(IFileBuffer buffer) {
    IContentType type = null;
    IPath location = buffer.getLocation();
    if (location != null) {
        IResource resource = FileBuffers.getWorkspaceFileAtLocation(location);
        if (resource != null) {
            if (resource.getType() == IResource.FILE && resource.isAccessible()) {
                IContentDescription d = null;
                try {
                    // Optimized description lookup, might not succeed
                    d = ((IFile) resource).getContentDescription();
                    if (d != null) {
                        type = d.getContentType();
                    }
                } catch (CoreException e) {
                /*
						 * Should not be possible given the accessible and
						 * file type check above
						 */
                }
                if (type == null) {
                    type = Platform.getContentTypeManager().findContentTypeFor(resource.getName());
                }
            }
        } else {
            File file = FileBuffers.getSystemFileAtLocation(location);
            if (file != null) {
                InputStream input = null;
                try {
                    input = new FileInputStream(file);
                    type = Platform.getContentTypeManager().findContentTypeFor(input, file.getName());
                } catch (FileNotFoundException e) {
                } catch (IOException e) {
                } finally {
                    if (input != null) {
                        try {
                            input.close();
                        } catch (IOException e1) {
                        }
                    }
                }
                if (type == null) {
                    type = Platform.getContentTypeManager().findContentTypeFor(file.getName());
                }
            }
        }
    } else {
        IFileStore fileStore = buffer.getFileStore();
        if (fileStore != null) {
            InputStream input = null;
            try {
                input = fileStore.openInputStream(EFS.NONE, null);
                if (input != null) {
                    type = Platform.getContentTypeManager().findContentTypeFor(input, fileStore.getName());
                }
            } catch (CoreException e) {
            // failure, assume plain text
            } catch (IOException e) {
            // failure, assume plain text
            } finally {
                if (input != null) {
                    try {
                        input.close();
                    } catch (IOException e1) {
                    }
                }
            }
            if (type == null) {
                type = Platform.getContentTypeManager().findContentTypeFor(fileStore.getName());
            }
        }
    }
    if (type == null) {
        type = Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT);
    }
    return type;
}
Also used : IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IFileStore(org.eclipse.core.filesystem.IFileStore) IContentType(org.eclipse.core.runtime.content.IContentType) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource) FileInputStream(java.io.FileInputStream)

Example 55 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.

the class FormatHandler method format.

protected void format(IProgressMonitor monitor, IFile file) {
    if (monitor == null || monitor.isCanceled())
        return;
    try {
        monitor.beginTask("", 100);
        IContentDescription contentDescription = file.getContentDescription();
        monitor.worked(5);
        if (contentDescription != null) {
            IContentType contentType = contentDescription.getContentType();
            IStructuredFormatProcessor formatProcessor = getFormatProcessor(contentType.getId());
            if (formatProcessor != null && (monitor == null || !monitor.isCanceled())) {
                String message = NLS.bind(SSEUIMessages.FormatActionDelegate_3, new String[] { file.getFullPath().toString().substring(1) });
                monitor.subTask(message);
                formatProcessor.setProgressMonitor(new SubProgressMonitor(monitor, 95));
                formatProcessor.formatFile(file);
            }
        }
        monitor.done();
    } catch (MalformedInputExceptionWithDetail e) {
        String message = NLS.bind(SSEUIMessages.FormatActionDelegate_5, new String[] { file.getFullPath().toString() });
        fErrorStatus.add(new Status(IStatus.ERROR, SSEUIPlugin.ID, IStatus.ERROR, message, e));
    } catch (IOException e) {
        String message = NLS.bind(SSEUIMessages.FormatActionDelegate_4, new String[] { file.getFullPath().toString() });
        fErrorStatus.add(new Status(IStatus.ERROR, SSEUIPlugin.ID, IStatus.ERROR, message, e));
    } catch (CoreException e) {
        String message = NLS.bind(SSEUIMessages.FormatActionDelegate_4, new String[] { file.getFullPath().toString() });
        fErrorStatus.add(new Status(IStatus.ERROR, SSEUIPlugin.ID, IStatus.ERROR, message, e));
    }
}
Also used : IStructuredFormatProcessor(org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor) MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) CoreException(org.eclipse.core.runtime.CoreException) MalformedInputExceptionWithDetail(org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail) IContentType(org.eclipse.core.runtime.content.IContentType) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Aggregations

IContentDescription (org.eclipse.core.runtime.content.IContentDescription)58 CoreException (org.eclipse.core.runtime.CoreException)27 IOException (java.io.IOException)24 IContentType (org.eclipse.core.runtime.content.IContentType)22 InputStream (java.io.InputStream)19 IFile (org.eclipse.core.resources.IFile)14 QualifiedName (org.eclipse.core.runtime.QualifiedName)13 IContentTypeManager (org.eclipse.core.runtime.content.IContentTypeManager)8 Reader (java.io.Reader)6 IStatus (org.eclipse.core.runtime.IStatus)6 Status (org.eclipse.core.runtime.Status)6 BufferedReader (java.io.BufferedReader)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStreamReader (java.io.InputStreamReader)4 IStructuredFormatProcessor (org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor)4 SequenceInputStream (java.io.SequenceInputStream)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 IResource (org.eclipse.core.resources.IResource)3