Search in sources :

Example 26 with IContentDescription

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

the class TestContentTypeDetection method doTestForParent.

protected void doTestForParent(String expectedContentType, String filePath, Class expectedException) throws CoreException, IOException {
    IFile file = (IFile) fTestProject.findMember(filePath);
    assertNotNull("Error in test case: file not found: " + filePath, file);
    IContentDescription contentDescription = file.getContentDescription();
    if (contentDescription == null) {
        InputStream inputStream = null;
        try {
            inputStream = file.getContents();
            contentDescription = Platform.getContentTypeManager().getDescriptionFor(inputStream, file.getName(), IContentDescription.ALL);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }
    assertNotNull("content description was null", contentDescription);
    IContentType contentType = contentDescription.getContentType();
    assertNotNull("content type was null", contentType);
    IContentType parentContentType = contentType;
    boolean found = false;
    while (parentContentType != null && !found) {
        found = expectedContentType.equals(parentContentType.getId());
        parentContentType = parentContentType.getBaseType();
    }
    assertTrue("did not find '" + expectedContentType + "' in parent chain of base types", found);
}
Also used : IFile(org.eclipse.core.resources.IFile) InputStream(java.io.InputStream) IContentType(org.eclipse.core.runtime.content.IContentType) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 27 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project polymap4-core by Polymap4.

the class FileState method getCharset.

/* (non-Javadoc)
	 * @see org.eclipse.core.resources.IEncodedStorage#getCharset()
	 */
public String getCharset() throws CoreException {
    // if there is an existing file at this state's path, use the encoding of that file
    IResource file = workspace.getRoot().findMember(fullPath);
    if (file != null && file.getType() == IResource.FILE)
        return ((IFile) file).getCharset();
    // tries to obtain a description for the file contents
    IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
    InputStream contents = new BufferedInputStream(getContents());
    boolean failed = false;
    try {
        IContentDescription description = contentTypeManager.getDescriptionFor(contents, getName(), new QualifiedName[] { IContentDescription.CHARSET });
        return description == null ? null : description.getCharset();
    } catch (IOException e) {
        failed = true;
        String message = NLS.bind(Messages.history_errorContentDescription, getFullPath());
        throw new ResourceException(IResourceStatus.FAILED_DESCRIBING_CONTENTS, getFullPath(), message, e);
    } finally {
        try {
            contents.close();
        } catch (IOException e) {
            if (!failed) {
                String message = NLS.bind(Messages.history_errorContentDescription, getFullPath());
                throw new ResourceException(IResourceStatus.FAILED_DESCRIBING_CONTENTS, getFullPath(), message, e);
            }
        }
    }
}
Also used : IContentTypeManager(org.eclipse.core.runtime.content.IContentTypeManager) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 28 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project polymap4-core by Polymap4.

the class File method internalGetCharset.

private String internalGetCharset(boolean checkImplicit, ResourceInfo info) throws CoreException {
    // if there is a file-specific user setting, use it
    String charset = workspace.getCharsetManager().getCharsetFor(getFullPath(), false);
    if (charset != null || !checkImplicit)
        return charset;
    // tries to obtain a description for the file contents
    IContentDescription description = workspace.getContentDescriptionManager().getDescriptionFor(this, info);
    if (description != null) {
        String contentCharset = description.getCharset();
        if (contentCharset != null)
            return contentCharset;
    }
    // could not find out the encoding based on the contents... default to parent's
    return workspace.getCharsetManager().getCharsetFor(getFullPath().removeLastSegments(1), true);
}
Also used : IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 29 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project polymap4-core by Polymap4.

the class File method getCharsetFor.

/* (non-Javadoc)
	 * @see IFile#getCharsetFor(Reader)
	 */
public String getCharsetFor(Reader contents) throws CoreException {
    String charset;
    ResourceInfo info = getResourceInfo(false, false);
    int flags = getFlags(info);
    if (exists(flags, true))
        // the file exists, look for user setting
        if ((charset = workspace.getCharsetManager().getCharsetFor(getFullPath(), false)) != null)
            // if there is a file-specific user setting, use it
            return charset;
    // tries to obtain a description from the contents provided
    IContentDescription description;
    try {
        // TODO need to take project specific settings into account
        IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
        description = contentTypeManager.getDescriptionFor(contents, getName(), new QualifiedName[] { IContentDescription.CHARSET });
    } catch (IOException e) {
        String message = NLS.bind(Messages.resources_errorContentDescription, getFullPath());
        throw new ResourceException(IResourceStatus.FAILED_DESCRIBING_CONTENTS, getFullPath(), message, e);
    }
    if (description != null)
        if ((charset = description.getCharset()) != null)
            // the description contained charset info, we are done
            return charset;
    // could not find out the encoding based on the contents... default to parent's
    return workspace.getCharsetManager().getCharsetFor(getFullPath().removeLastSegments(1), true);
}
Also used : IContentTypeManager(org.eclipse.core.runtime.content.IContentTypeManager) IContentDescription(org.eclipse.core.runtime.content.IContentDescription)

Example 30 with IContentDescription

use of org.eclipse.core.runtime.content.IContentDescription in project polymap4-core by Polymap4.

the class FilePropertyTester method testContentType.

/**
 * Tests whether the content type for <code>file</code> matches the
 * <code>contentTypeId</code>. It is possible that this method call could
 * cause the file to be read. It is also possible (through poor plug-in
 * design) for this method to load plug-ins.
 *
 * @param file
 *            The file for which the content type should be determined; must
 *            not be <code>null</code>.
 * @param contentTypeId
 *            The expected content type; must not be <code>null</code>.
 * @return <code>true</code> iff the best matching content type has an
 *         identifier that matches <code>contentTypeId</code>;
 *         <code>false</code> otherwise.
 */
private boolean testContentType(final IFile file, String contentTypeId) {
    final String expectedValue = contentTypeId.trim();
    String actualValue = null;
    try {
        IContentDescription contentDescription = file.getContentDescription();
        if (contentDescription != null) {
            IContentType contentType = contentDescription.getContentType();
            actualValue = contentType.getId();
        }
    } catch (CoreException e) {
        // $NON-NLS-1$
        Policy.log(IStatus.ERROR, "Core exception while retrieving the content description", e);
    }
    return expectedValue.equals(actualValue);
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) IContentType(org.eclipse.core.runtime.content.IContentType) 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