use of org.eclipse.core.runtime.content.IContentDescription in project che by eclipse.
the class FileStoreTextFileBuffer method getContentType.
/*
* @see org.eclipse.core.filebuffers.IFileBuffer#getContentType()
* @since 3.1
*/
public IContentType getContentType() throws CoreException {
InputStream stream = null;
try {
if (isDirty()) {
Reader reader = new DocumentReader(getDocument());
try {
IContentDescription desc = Platform.getContentTypeManager().getDescriptionFor(reader, fFileStore.getName(), NO_PROPERTIES);
if (desc != null && desc.getContentType() != null)
return desc.getContentType();
} finally {
try {
reader.close();
} catch (IOException ex) {
}
}
}
stream = fFileStore.openInputStream(EFS.NONE, null);
IContentDescription desc = Platform.getContentTypeManager().getDescriptionFor(stream, fFileStore.getName(), NO_PROPERTIES);
if (desc != null && desc.getContentType() != null)
return desc.getContentType();
return null;
} catch (IOException x) {
throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, NLSUtility.format(FileBuffersMessages.FileBuffer_error_queryContentDescription, fFileStore.toString()), x));
} finally {
try {
if (stream != null)
stream.close();
} catch (IOException x) {
}
}
}
use of org.eclipse.core.runtime.content.IContentDescription in project che by eclipse.
the class FileStoreTextFileBuffer method cacheEncodingState.
protected void cacheEncodingState() {
fEncoding = fExplicitEncoding;
fHasBOM = false;
fIsCacheUpdated = true;
InputStream stream = null;
try {
stream = getFileContents(fFileStore);
if (stream == null)
return;
QualifiedName[] options = new QualifiedName[] { IContentDescription.CHARSET, IContentDescription.BYTE_ORDER_MARK };
//Platform.getContentTypeManager().getDescriptionFor(stream, fFileStore.getName(), options);
IContentDescription description = null;
if (description != null) {
fHasBOM = description.getProperty(IContentDescription.BYTE_ORDER_MARK) != null;
if (fEncoding == null)
fEncoding = description.getCharset();
}
} catch (CoreException e) {
// do nothing
} finally /*catch (IOException e) {
// do nothing
}*/
{
try {
if (stream != null)
stream.close();
} catch (IOException ex) {
FileBuffersPlugin.getDefault().log(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, FileBuffersMessages.JavaTextFileBuffer_error_closeStream, ex));
}
}
// Use global default
if (fEncoding == null)
fEncoding = fManager.getDefaultEncoding();
}
use of org.eclipse.core.runtime.content.IContentDescription in project dbeaver by dbeaver.
the class ProjectExportWizard method saveResourceProperties.
private void saveResourceProperties(IResource resource, XMLBuilder xml) throws CoreException, IOException {
if (resource instanceof IFile) {
final IContentDescription contentDescription = ((IFile) resource).getContentDescription();
if (contentDescription != null && contentDescription.getCharset() != null) {
xml.addAttribute(ExportConstants.ATTR_CHARSET, contentDescription.getCharset());
// xml.addAttribute(ExportConstants.ATTR_CHARSET, contentDescription.getContentType());
}
} else if (resource instanceof IFolder) {
xml.addAttribute(ExportConstants.ATTR_DIRECTORY, true);
}
for (Object entry : resource.getPersistentProperties().entrySet()) {
Map.Entry<?, ?> propEntry = (Map.Entry<?, ?>) entry;
xml.startElement(ExportConstants.TAG_ATTRIBUTE);
final QualifiedName attrName = (QualifiedName) propEntry.getKey();
xml.addAttribute(ExportConstants.ATTR_QUALIFIER, attrName.getQualifier());
xml.addAttribute(ExportConstants.ATTR_NAME, attrName.getLocalName());
xml.addAttribute(ExportConstants.ATTR_VALUE, (String) propEntry.getValue());
xml.endElement();
}
}
use of org.eclipse.core.runtime.content.IContentDescription in project eclipse.platform.text by eclipse.
the class LastSaveReferenceProvider method isUTF8BOM.
/**
* Returns <code>true</code> if the <code>encoding</code> is UTF-8 and
* the file contains a BOM. Taken from ResourceTextFileBuffer.java.
*
* <p>
* XXX:
* This is a workaround for a corresponding bug in Java readers and writer,
* see http://developer.java.sun.com/developer/bugParade/bugs/4508058.html
* </p>
* @param encoding the encoding
* @param storage the storage
* @return <code>true</code> if <code>storage</code> is a UTF-8-encoded file
* that has an UTF-8 BOM
* @throws CoreException if
* - reading of file's content description fails
* - byte order mark is not valid for UTF-8
*/
private static boolean isUTF8BOM(String encoding, IStorage storage) throws CoreException {
if (storage instanceof IFile && "UTF-8".equals(encoding)) {
// $NON-NLS-1$
IFile file = (IFile) storage;
IContentDescription description = file.getContentDescription();
if (description != null) {
byte[] bom = (byte[]) description.getProperty(IContentDescription.BYTE_ORDER_MARK);
if (bom != null) {
if (bom != IContentDescription.BOM_UTF_8)
// $NON-NLS-1$
throw new CoreException(new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, QuickDiffMessages.getString("LastSaveReferenceProvider.LastSaveReferenceProvider.error.wrongByteOrderMark"), null));
return true;
}
}
}
return false;
}
use of org.eclipse.core.runtime.content.IContentDescription in project eclipse.platform.text by eclipse.
the class FileDocumentProvider method getCharsetForNewFile.
/*
* @since 3.0
*/
private String getCharsetForNewFile(IFile targetFile, IDocument document, FileInfo info) {
// User-defined encoding has first priority
String encoding;
try {
encoding = targetFile.getCharset(false);
} catch (CoreException ex) {
encoding = null;
}
if (encoding != null)
return encoding;
// Probe content
try (Reader reader = new DocumentReader(document)) {
QualifiedName[] options = new QualifiedName[] { IContentDescription.CHARSET, IContentDescription.BYTE_ORDER_MARK };
IContentDescription description = Platform.getContentTypeManager().getDescriptionFor(reader, targetFile.getName(), options);
if (description != null) {
encoding = description.getCharset();
if (encoding != null)
return encoding;
}
} catch (IOException ex) {
// continue with next strategy
}
// Use file's encoding if the file has a BOM
if (info != null && info.fBOM != null)
return info.fEncoding;
// Use parent chain
try {
return targetFile.getParent().getDefaultCharset();
} catch (CoreException ex) {
// Use global default
return ResourcesPlugin.getEncoding();
}
}
Aggregations