use of org.eclipse.core.runtime.content.IContentDescription in project eclipse.platform.text by eclipse.
the class StorageDocumentProvider method getContentType.
@Override
public IContentType getContentType(Object element) throws CoreException {
if (element instanceof IStorageEditorInput) {
IStorage storage = ((IStorageEditorInput) element).getStorage();
try {
IContentDescription desc;
IDocument document = getDocument(element);
if (document != null) {
try (Reader reader = new DocumentReader(document)) {
desc = Platform.getContentTypeManager().getDescriptionFor(reader, storage.getName(), NO_PROPERTIES);
}
} else {
try (InputStream stream = storage.getContents()) {
desc = Platform.getContentTypeManager().getDescriptionFor(stream, storage.getName(), NO_PROPERTIES);
}
}
if (desc != null && desc.getContentType() != null)
return desc.getContentType();
} catch (IOException x) {
IPath path = storage.getFullPath();
String name;
if (path != null)
name = path.toOSString();
else
name = storage.getName();
String message;
if (name != null)
message = NLSUtility.format(TextEditorMessages.StorageDocumentProvider_getContentDescriptionFor, name);
else
message = TextEditorMessages.StorageDocumentProvider_getContentDescription;
throw new CoreException(new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, message, x));
}
}
return super.getContentType(element);
}
use of org.eclipse.core.runtime.content.IContentDescription in project eclipse.platform.text by eclipse.
the class FileStoreTextFileBuffer method computeEncoding.
private String computeEncoding() {
// Make sure cache is up to date
if (!fIsCacheUpdated)
cacheEncodingState();
// User-defined encoding has first priority
if (fExplicitEncoding != null)
return fExplicitEncoding;
// Probe content
try (Reader reader = new DocumentReader(fDocument)) {
QualifiedName[] options = new QualifiedName[] { IContentDescription.CHARSET, IContentDescription.BYTE_ORDER_MARK };
IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(reader, fFileStore.getName(), options);
if (description != null) {
String encoding = description.getCharset();
if (encoding != null)
return encoding;
}
} catch (IOException ex) {
// Try next strategy
}
// Use file's encoding if the file has a BOM
if (fHasBOM)
return fEncoding;
// Use global default
return fManager.getDefaultEncoding();
}
use of org.eclipse.core.runtime.content.IContentDescription in project eclipse.platform.text by eclipse.
the class ResourceTextFileBuffer method computeEncoding.
private String computeEncoding() {
// User-defined encoding has first priority
if (fExplicitEncoding != null)
return fExplicitEncoding;
try (Reader reader = new DocumentReader(fDocument)) {
QualifiedName[] options = new QualifiedName[] { IContentDescription.CHARSET, IContentDescription.BYTE_ORDER_MARK };
IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(reader, fFile.getName(), options);
if (description != null) {
String encoding = description.getCharset();
if (encoding != null)
return encoding;
}
} catch (IOException ex) {
// try next strategy
}
// Use file's encoding if the file has a BOM
if (fBOM != null)
return fEncoding;
// Use parent chain
try {
return fFile.getParent().getDefaultCharset();
} catch (CoreException ex) {
// Use global default
return fManager.getDefaultEncoding();
}
}
use of org.eclipse.core.runtime.content.IContentDescription in project eclipse.platform.text by eclipse.
the class ResourceTextFileBuffer method cacheBOM.
/**
* Caches the BOM of the underlying file.
*
* @throws CoreException if reading of file's content description fails
*/
protected void cacheBOM() throws CoreException {
fBOM = null;
IContentDescription description = fFile.getContentDescription();
if (description != null)
fBOM = (byte[]) description.getProperty(IContentDescription.BYTE_ORDER_MARK);
}
use of org.eclipse.core.runtime.content.IContentDescription in project webtools.sourceediting by eclipse.
the class JSPContentValidator 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) {
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
* (IFile.getContents() requirement as well)
*/
if (is != null)
try {
is.close();
} catch (Exception e) {
// not sure how to recover at this point
}
}
return isFragment;
}
Aggregations