Search in sources :

Example 16 with IContentTypeManager

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

the class TestContentTypeHandlers method testCreation.

public void testCreation() {
    IContentTypeManager registry = getContentTypeRegistry();
    assertTrue("content type identifer registry must exist", registry != null);
    if (DEBUG) {
        IContentType[] allTypes = registry.getAllContentTypes();
        for (int i = 0; i < allTypes.length; i++) {
            System.out.println(allTypes[i]);
        }
    }
}
Also used : IContentTypeManager(org.eclipse.core.runtime.content.IContentTypeManager) IContentType(org.eclipse.core.runtime.content.IContentType)

Example 17 with IContentTypeManager

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

the class FileUtil method isXMLFile.

/**
 * Determines if a file is one of the valid XML content types.
 * @param file The input IFile to check
 * @return True if it is a XML file, false otherwise.
 */
public static boolean isXMLFile(IFile file) {
    IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
    IContentType[] types = contentTypeManager.findContentTypesFor(file.getName());
    for (IContentType contentType : types) {
        if (// $NON-NLS-1$ //$NON-NLS-2$
        contentType.isKindOf(contentTypeManager.getContentType("org.eclipse.core.runtime.xml")) || contentType.isKindOf(contentTypeManager.getContentType("org.eclipse.wst.xml.core.xmlsource"))) {
            return true;
        }
    }
    return false;
}
Also used : IContentTypeManager(org.eclipse.core.runtime.content.IContentTypeManager) IContentType(org.eclipse.core.runtime.content.IContentType)

Example 18 with IContentTypeManager

use of org.eclipse.core.runtime.content.IContentTypeManager 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 19 with IContentTypeManager

use of org.eclipse.core.runtime.content.IContentTypeManager 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 20 with IContentTypeManager

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

the class ValidatorStrategy method calculateParentContentTypeIds.

/**
 * The content type passed in should be the most specific one. TODO: This
 * exact method is also in ValidatorMetaData. Should be in a common place.
 *
 * @param contentType
 * @return
 */
private String[] calculateParentContentTypeIds(String contentTypeId) {
    Set parentTypes = new HashSet();
    IContentTypeManager ctManager = Platform.getContentTypeManager();
    IContentType ct = ctManager.getContentType(contentTypeId);
    String id = contentTypeId;
    while (ct != null && id != null) {
        parentTypes.add(id);
        ct = ctManager.getContentType(id);
        if (ct != null) {
            IContentType baseType = ct.getBaseType();
            id = (baseType != null) ? baseType.getId() : null;
        }
    }
    return (String[]) parentTypes.toArray(new String[parentTypes.size()]);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) IContentTypeManager(org.eclipse.core.runtime.content.IContentTypeManager) IContentType(org.eclipse.core.runtime.content.IContentType) HashSet(java.util.HashSet)

Aggregations

IContentTypeManager (org.eclipse.core.runtime.content.IContentTypeManager)33 IContentType (org.eclipse.core.runtime.content.IContentType)27 IContentDescription (org.eclipse.core.runtime.content.IContentDescription)8 CoreException (org.eclipse.core.runtime.CoreException)5 InputStream (java.io.InputStream)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)3 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 BufferedInputStream (java.io.BufferedInputStream)1 Iterator (java.util.Iterator)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 ExecutionException (org.eclipse.core.commands.ExecutionException)1 IFileInfo (org.eclipse.core.filesystem.IFileInfo)1 IFile (org.eclipse.core.resources.IFile)1 IProject (org.eclipse.core.resources.IProject)1 ProjectScope (org.eclipse.core.resources.ProjectScope)1 IExtensionPoint (org.eclipse.core.runtime.IExtensionPoint)1