Search in sources :

Example 31 with IContentDescription

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) {
        }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 32 with IContentDescription

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();
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) QualifiedName(org.eclipse.core.runtime.QualifiedName) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 33 with IContentDescription

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();
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) QualifiedName(org.eclipse.core.runtime.QualifiedName) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) HashMap(java.util.HashMap) Map(java.util.Map)

Example 34 with IContentDescription

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;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 35 with IContentDescription

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();
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) QualifiedName(org.eclipse.core.runtime.QualifiedName) Reader(java.io.Reader) IOException(java.io.IOException) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

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