Java Code Examples for org.eclipse.core.runtime.content.IContentTypeManager#getContentType()

The following examples show how to use org.eclipse.core.runtime.content.IContentTypeManager#getContentType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IsNodeProjectPropertyTester.java    From wildwebdeveloper with Eclipse Public License 2.0 6 votes vote down vote up
@Override public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
	if (IS_NODE_RESOURCE_PROPERTY.equals(property)) {
		IResource resource = Adapters.adapt(receiver, IResource.class);
		if (resource == null) {
			return false;
		}
		if (resource instanceof IFile) {
			IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
			IContentType jsContentType = contentTypeManager.getContentType("org.eclipse.wildwebdeveloper.js");
			IContentType tsContentType = contentTypeManager.getContentType("org.eclipse.wildwebdeveloper.ts");
			try (
				InputStream content = ((IFile) resource).getContents();
			) {
				List<IContentType> contentTypes = Arrays.asList(contentTypeManager.findContentTypesFor(content, resource.getName()));
				return contentTypes.contains(jsContentType) || contentTypes.contains(tsContentType);
			} catch (Exception e) {
				return false;
			}
		}
	}
	return false;
}
 
Example 2
Source File: ProjectTemplate.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns true if the given file can be evaluated for template-variables.<br>
 * There is no good way of detecting what is binary and what is not, so we decide what is supported by checking if
 * the content type is a sub-type of text.
 * 
 * @param file
 * @return true if the file can be processed; false, otherwise.
 */
private static boolean isSupportedFile(IFile file)
{
	IContentTypeManager manager = Platform.getContentTypeManager();
	if (manager == null)
	{
		return false;
	}
	IContentType contentType = manager.findContentTypeFor(file.getName());
	if (contentType == null)
	{
		return false;
	}
	IContentType text = manager.getContentType("org.eclipse.core.runtime.text"); //$NON-NLS-1$
	return contentType.isKindOf(text);
}
 
Example 3
Source File: ParserPoolFactory.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
/**
 * The main use of this class. Pass in a content type and get back an IParserPool to use to "borrow" a parser
 * instance. If the specified content type does not exist in the parser pool, then we work our way up the base
 * content types until we find a parser or fail.
 * 
 * @param contentTypeId
 * @return
 */
public synchronized IParserPool getParserPool(String contentTypeId)
{
	IContentTypeManager ctm = Platform.getContentTypeManager();
	IContentType contentType = ctm.getContentType(contentTypeId);
	IParserPool result = null;

	if (pools == null)
	{
		pools = new HashMap<String, IParserPool>();
	}

	while (result == null && (contentType != null || contentTypeId != null))
	{
		if (contentType != null)
		{
			contentTypeId = contentType.getId(); // $codepro.audit.disable questionableAssignment
		}
		result = pools.get(contentTypeId);

		if (result == null)
		{
			if (parsers == null)
			{
				parsers = getParsers();
			}

			IConfigurationElement parserExtension = parsers.get(contentTypeId);

			if (parserExtension != null)
			{
				result = new ParserPool(parserExtension);
				pools.put(contentTypeId, result);
			}
			else
			{
				contentType = contentType.getBaseType();

				if (contentType == null)
				{
					break;
				}
			}
		}
	}

	return result;
}
 
Example 4
Source File: AbstractBuildParticipant.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
public void setInitializationData(IConfigurationElement config, String propertyName, Object data)
		throws CoreException
{
	String rawPriority = config.getAttribute(ATTR_PRIORITY);
	if (!StringUtil.isEmpty(rawPriority))
	{
		try
		{
			this.fPriority = Integer.parseInt(rawPriority);
		}
		catch (NumberFormatException e)
		{
			IdeLog.logWarning(BuildPathCorePlugin.getDefault(), MessageFormat.format(
					"Unable to parse priority value ({0}) as an integer, defaulting to 50.", rawPriority), e); //$NON-NLS-1$
		}
	}
	this.fId = config.getAttribute(ID);
	this.fName = config.getAttribute(NAME);
	this.contributor = config.getContributor().getName();

	// Read in the content types
	IContentTypeManager manager = getContentTypeManager();
	IConfigurationElement[] rawContentTypes = config.getChildren(CONTENT_TYPE_BINDING);
	this.contentTypes = new HashSet<IContentType>(rawContentTypes.length);
	for (IConfigurationElement contentTypeBinding : rawContentTypes)
	{
		String contentTypeId = contentTypeBinding.getAttribute(CONTENT_TYPE_ID);
		IContentType type = manager.getContentType(contentTypeId);
		if (type != null)
		{
			contentTypes.add(type);
		}
	}

	// Read in the associated project natures
	IConfigurationElement[] rawProjectNatures = config.getChildren(PROJECT_NATURE_BINDING);
	this.projectNatures = new HashSet<String>(rawContentTypes.length);
	for (IConfigurationElement projectNatureBinding : rawProjectNatures)
	{
		String natureId = projectNatureBinding.getAttribute(NATURE_ID);
		if (!StringUtil.isEmpty(natureId))
		{
			projectNatures.add(natureId);
		}
	}
}
 
Example 5
Source File: ReportPlugin.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns all available extension names for report design files.
 * 
 * @return the extension name lisr
 */
public List<String> getReportExtensionNameList( )
{
	if ( reportExtensionNames == null )
	{
		reportExtensionNames = new ArrayList<String>( );

		IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry( );
		IConfigurationElement[] elements = extensionRegistry.getConfigurationElementsFor( "org.eclipse.ui.editors" ); //$NON-NLS-1$
		for ( int i = 0; i < elements.length; i++ )
		{

			String id = elements[i].getAttribute( "id" ); //$NON-NLS-1$
			if ( "org.eclipse.birt.report.designer.ui.editors.ReportEditor".equals( id ) ) //$NON-NLS-1$
			{
				if ( elements[i].getAttribute( "extensions" ) != null ) //$NON-NLS-1$
				{
					String[] extensionNames = elements[i].getAttribute( "extensions" ) //$NON-NLS-1$
							//$NON-NLS-1$
							.split( "," ); //$NON-NLS-1$
					for ( int j = 0; j < extensionNames.length; j++ )
					{
						extensionNames[j] = extensionNames[j].trim( );
						if ( !reportExtensionNames.contains( extensionNames[j] ) )
						{
							reportExtensionNames.add( extensionNames[j] );
						}
					}
				}
			}
		}

		IContentTypeManager contentTypeManager = Platform.getContentTypeManager( );
		IContentType contentType = contentTypeManager.getContentType( "org.eclipse.birt.report.designer.ui.editors.reportdesign" ); //$NON-NLS-1$
		String[] fileSpecs = contentType.getFileSpecs( IContentType.FILE_EXTENSION_SPEC );
		for ( int i = 0; i < fileSpecs.length; i++ )
		{
			reportExtensionNames.add( fileSpecs[i] );
		}
	}
	return reportExtensionNames;
}
 
Example 6
Source File: ContentTypeHelper.java    From tm4e with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Find the content type with the given contentTypeId
 * 
 * @param contentTypeId
 * @return matching content type or null
 */
public static IContentType getContentTypeById(String contentTypeId) {
	IContentTypeManager manager = Platform.getContentTypeManager();
	return manager.getContentType(contentTypeId);
}