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;
}
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());
}
}
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;
}
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;
}
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));
}
}
Aggregations