All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.core.internal.propertytester.FilePropertyTester Maven / Gradle / Ivy

Go to download

AspectJ tools most notably contains the AspectJ compiler (AJC). AJC applies aspects to Java classes during compilation, fully replacing Javac for plain Java classes and also compiling native AspectJ or annotation-based @AspectJ syntax. Furthermore, AJC can weave aspects into existing class files in a post-compile binary weaving step. This library is a superset of AspectJ weaver and hence also of AspectJ runtime.

There is a newer version: 1.9.22.1
Show newest version
/*******************************************************************************
 * Copyright (c) 2005, 2014 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.core.internal.propertytester;

import org.eclipse.core.internal.utils.Policy;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.content.*;

/**
 * A property tester for various properties of files.
 *
 * @since 3.2
 */
public class FilePropertyTester extends ResourcePropertyTester {

	/**
	 * A property indicating a content type on the selected file (value "contentTypeId").
	 * "kindOf" indicates that the file content type should be the kind of the one given as the expected value.
	 * If "kindOf" is not specified, the file content type identifier should equals the expected value.
	 * @see IContentType#isKindOf(IContentType)
	 */
	private static final String CONTENT_TYPE_ID = "contentTypeId"; //$NON-NLS-1$

	/**
	 * An argument for "contentTypeId".
	 * "kindOf" indicates that the file content type should be the kind of the one given as the expected value.
	 * If "kindOf" is not specified, the file content type identifier should equals the expected value.
	 * @see IContentType#isKindOf(IContentType)
	 */
	private static final String IS_KIND_OF = "kindOf"; //$NON-NLS-1$

	/**
	 * An argument for "contentTypeId".
	 * Setting "useFilenameOnly" indicates that the file content type should be determined by the file name only.
	 * If "useFilenameOnly" is not specified, the file content type is determined by both, the file name and content.
	 * @see IContentTypeMatcher#findContentTypeFor(String)
	 */
	private static final String USE_FILENAME_ONLY = "useFilenameOnly"; //$NON-NLS-1$

	@Override
	public boolean test(Object receiver, String method, Object[] args, Object expectedValue) {
		if ((receiver instanceof IFile) && method.equals(CONTENT_TYPE_ID))
			return testContentType((IFile) receiver, toString(expectedValue), isArgumentUsed(args, IS_KIND_OF), isArgumentUsed(args, USE_FILENAME_ONLY));
		return false;
	}

	private boolean isArgumentUsed(Object[] args, String value) {
		for (Object arg : args)
			if (value.equals(arg))
				return true;
		return false;
	}

	/**
	 * 

* Tests whether the content type for file matches * or is a kind of contentTypeId. *

*

* 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 to test. Must not be null. * @param contentTypeId * The content type to test. Must not be null. * @param isKindOfUsed * Indicates whether the file content type should match contentTypeId * or should be a kind of contentTypeId. * @param useFilenameOnly * Indicates to determine the file content type based on the file name only. * @return true, if the best matching content type for file *
    *
  • has an identifier that matches contentTypeId * and isKindOfUsed is false, or
  • *
  • is a kind of contentTypeId * and isKindOfUsed is true.
  • *
* Otherwise it returns false. */ private boolean testContentType(final IFile file, String contentTypeId, boolean isKindOfUsed, boolean useFilenameOnly) { final String expectedValue = contentTypeId.trim(); IContentType actualContentType = null; if (!useFilenameOnly) { if (!file.exists()) return false; IContentDescription contentDescription = null; try { contentDescription = file.getContentDescription(); } catch (CoreException e) { Policy.log(IStatus.ERROR, "Core exception while retrieving the content description", e);//$NON-NLS-1$ } if (contentDescription != null) actualContentType = contentDescription.getContentType(); } else { actualContentType = Platform.getContentTypeManager().findContentTypeFor(file.getName()); } if (actualContentType != null) { if (isKindOfUsed) return actualContentType.isKindOf(Platform.getContentTypeManager().getContentType(expectedValue)); return expectedValue.equals(actualContentType.getId()); } return false; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy