1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-30 21:55:31 +02:00

2004-05-19 Alain Magloire

Contribution from Sam Robb, for PR 52864.
	File discovery.
This commit is contained in:
Alain Magloire 2004-05-20 03:01:12 +00:00
parent 1329b3aedf
commit 5cc077060d
17 changed files with 797 additions and 70 deletions

View file

@ -1,3 +1,6 @@
2004-05-19 Alain Magloire
New test provided by Sam Rob added for the resolver.
2004-05-03 Bogdan Gheorghe
Changed search and indexer tests to work with new index enablement

View file

@ -0,0 +1,508 @@
/**********************************************************************
* Copyright (c) 2004 TimeSys Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* TimeSys Corporation - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.core.filetype.tests;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.filetype.ICFileType;
import org.eclipse.cdt.core.filetype.ICFileTypeAssociation;
import org.eclipse.cdt.core.filetype.ICFileTypeConstants;
import org.eclipse.cdt.core.filetype.ICFileTypeResolver;
import org.eclipse.cdt.core.filetype.ICLanguage;
import org.eclipse.cdt.core.internal.filetype.CFileType;
import org.eclipse.cdt.core.internal.filetype.CFileTypeAssociation;
import org.eclipse.cdt.core.internal.filetype.CLanguage;
public class ResolverTests extends TestCase {
private ICFileTypeResolver resolver;
private static final String PLUGIN_ID = "org.eclipse.cdt.core.filetype.tests";
private static final String LANG_TEST = PLUGIN_ID + ".test";
private static final String FT_TEST_HEADER = LANG_TEST + ".header";
private static final String FT_TEST_SOURCE = LANG_TEST + ".source";
private static final String FT_TEST_WHASAT = LANG_TEST + ".unknown";
public static Test suite() {
TestSuite suite = new TestSuite(ResolverTests.class.getName());
suite.addTest(new ResolverTests("testInternalCtors"));
suite.addTest(new ResolverTests("testFileTypeResolution"));
suite.addTest(new ResolverTests("testGetLanguages"));
suite.addTest(new ResolverTests("testGetTypes"));
suite.addTest(new ResolverTests("testGetFileTypeAssociations"));
suite.addTest(new ResolverTests("testAdd"));
suite.addTest(new ResolverTests("testRemove"));
return suite;
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
resolver = CCorePlugin.getDefault().getFileTypeResolver();
super.setUp();
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
resolver = null;
super.tearDown();
}
/**
* Constructor for ResolverTests.
* @param name
*/
public ResolverTests(String name) {
super(name);
}
public final void testInternalCtors() {
ICLanguage lang = null;
ICFileType type = null;
ICFileTypeAssociation assoc = null;
// Language
try {
lang = new CLanguage(LANG_TEST, null);
} catch (IllegalArgumentException e) {
}
assertNull(lang);
try {
lang = new CLanguage(LANG_TEST, "");
} catch (IllegalArgumentException e) {
}
assertNull(lang);
try {
lang = new CLanguage(null, "L");
} catch (IllegalArgumentException e) {
}
assertNull(lang);
try {
lang = new CLanguage("", "L");
} catch (IllegalArgumentException e) {
}
assertNull(lang);
lang = new CLanguage(LANG_TEST, "Test Language");
assertNotNull(lang);
// File type
// str id, cls lang, str name, int type
try {
type = new CFileType(FT_TEST_HEADER, lang, "T", -1);
} catch (IllegalArgumentException e) {
}
assertNull(type);
try {
type = new CFileType(FT_TEST_HEADER, lang, "T", 0x04091998);
} catch (IllegalArgumentException e) {
}
assertNull(type);
try {
type = new CFileType(FT_TEST_HEADER, lang, null, ICFileType.TYPE_HEADER);
} catch (IllegalArgumentException e) {
}
assertNull(type);
try {
type = new CFileType(FT_TEST_HEADER, lang, "", ICFileType.TYPE_HEADER);
} catch (IllegalArgumentException e) {
}
assertNull(type);
try {
type = new CFileType(FT_TEST_HEADER, null, "T", ICFileType.TYPE_HEADER);
} catch (IllegalArgumentException e) {
}
assertNull(type);
try {
type = new CFileType(null, lang, "T", ICFileType.TYPE_HEADER);
} catch (IllegalArgumentException e) {
}
assertNull(type);
try {
type = new CFileType("", lang, "T", ICFileType.TYPE_HEADER);
} catch (IllegalArgumentException e) {
}
assertNull(type);
type = new CFileType(FT_TEST_HEADER, lang, "T", ICFileType.TYPE_HEADER);
assertNotNull(type);
// Association
try {
assoc = new CFileTypeAssociation("*.xyz", null);
} catch (IllegalArgumentException e) {
}
assertNull(assoc);
try {
assoc = new CFileTypeAssociation(null, type);
} catch (IllegalArgumentException e) {
}
assertNull(assoc);
try {
assoc = new CFileTypeAssociation("", type);
} catch (IllegalArgumentException e) {
}
assertNull(assoc);
assoc = new CFileTypeAssociation("*.xyz", type);
assertNotNull(assoc);
}
private void doTestFileTypeResolution(String fileName, String expectedTypeId) {
ICFileType typeByName = resolver.getFileType(fileName);
assertNotNull(typeByName);
assertEquals(expectedTypeId, typeByName.getId());
ICFileType typeById = resolver.getFileTypeById(typeByName.getId());
assertNotNull(typeById);
assertEquals(typeByName, typeById);
ICLanguage languageById = resolver.getLanguageById(typeByName.getLanguage().getId());
assertNotNull(languageById);
assertEquals(typeByName.getLanguage().getId(), languageById.getId());
}
public final void testFileTypeResolution() {
// - Null string, Empty string, Strings w/spaces
doTestFileTypeResolution(null, ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution("", ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution(" ", ICFileTypeConstants.FT_UNKNOWN);
// Odd filenames
doTestFileTypeResolution(".", ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution(".c.", ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution(".cpp.", ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution("file.c.", ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution("file.cpp.", ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution("file.c.input", ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution("file.cpp.input", ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution("c", ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution("cpp", ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution("numerical", ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution("some/path/file.c", ICFileTypeConstants.FT_C_SOURCE);
doTestFileTypeResolution("some/path/file.cpp", ICFileTypeConstants.FT_CXX_SOURCE);
// C source/header
doTestFileTypeResolution("file.c", ICFileTypeConstants.FT_C_SOURCE);
doTestFileTypeResolution("file.h", ICFileTypeConstants.FT_C_HEADER);
doTestFileTypeResolution("some.file.c", ICFileTypeConstants.FT_C_SOURCE);
doTestFileTypeResolution("some.file.h", ICFileTypeConstants.FT_C_HEADER);
// C++ source/header
doTestFileTypeResolution("file.cpp", ICFileTypeConstants.FT_CXX_SOURCE);
doTestFileTypeResolution("file.cxx", ICFileTypeConstants.FT_CXX_SOURCE);
doTestFileTypeResolution("file.cc", ICFileTypeConstants.FT_CXX_SOURCE);
doTestFileTypeResolution("file.C", ICFileTypeConstants.FT_CXX_SOURCE);
doTestFileTypeResolution("file.hpp", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("file.hxx", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("file.hh", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("file.H", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("some.file.cpp", ICFileTypeConstants.FT_CXX_SOURCE);
doTestFileTypeResolution("some.file.hxx", ICFileTypeConstants.FT_CXX_HEADER);
// Assembly
doTestFileTypeResolution("file.asm", ICFileTypeConstants.FT_ASM_SOURCE);
doTestFileTypeResolution("file.s", ICFileTypeConstants.FT_ASM_SOURCE);
doTestFileTypeResolution("file.S", ICFileTypeConstants.FT_ASM_SOURCE);
// Std C++ library
doTestFileTypeResolution("algorithm", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("bitset", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("deque", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("exception", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("fstream", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("functional", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("iomanip", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("ios", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("iosfwd", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("iostream", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("istream", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("iterator", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("limits", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("list", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("locale", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("map", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("memory", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("new", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("numeric", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("ostream", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("queue", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("set", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("sstream", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("stack", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("stdexcept", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("streambuf", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("string", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("typeinfo", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("utility", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("valarray", ICFileTypeConstants.FT_CXX_HEADER);
doTestFileTypeResolution("vector", ICFileTypeConstants.FT_CXX_HEADER);
// Failure cases
doTestFileTypeResolution("file.txt", ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution("file.doc", ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution("files", ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution("FILES", ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution("stream", ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution("streambu", ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution("streambuff", ICFileTypeConstants.FT_UNKNOWN);
doTestFileTypeResolution("sstreams", ICFileTypeConstants.FT_UNKNOWN);
}
public final void testGetLanguages() {
ICLanguage[] languages = resolver.getLanguages();
assertNotNull(languages);
for (int i = 0; i < languages.length; i++) {
ICLanguage lang = resolver.getLanguageById(languages[i].getId());
assertNotNull(lang);
assertEquals(languages[i], lang);
}
}
public final void testGetTypes() {
ICFileType[] types = resolver.getFileTypes();
assertNotNull(types);
for (int i = 0; i < types.length; i++) {
ICFileType type = resolver.getFileTypeById(types[i].getId());
assertNotNull(type);
assertEquals(types[i], type);
}
}
public final void testGetFileTypeAssociations() {
ICFileTypeAssociation[] assocs = resolver.getFileTypeAssociations();
assertNotNull(assocs);
for (int i = 0; i < assocs.length; i++) {
// Check the pattern
String pattern = assocs[i].getPattern();
assertNotNull(pattern);
assertTrue(pattern.length() > 0);
// Check the file type
ICFileType type = assocs[i].getType();
assertNotNull(type);
ICFileType typeById = resolver.getFileTypeById(type.getId());
assertNotNull(typeById);
assertEquals(type, typeById);
// Check the type's language
ICLanguage langIn = type.getLanguage();
assertNotNull(langIn);
String langId = langIn.getId();
assertNotNull(langId);
assertTrue(langId.length() > 0);
ICLanguage langOut = resolver.getLanguageById(langId);
assertNotNull(langOut);
assertEquals(langIn, langOut);
}
}
public final void testAdd() {
boolean result = false;
// Languages
ICLanguage langIn = new CLanguage(LANG_TEST, "Test Language");
result = resolver.removeLanguage(langIn);
assertFalse(result);
result = resolver.addLanguage(langIn);
assertTrue(result);
ICLanguage langOut = resolver.getLanguageById(LANG_TEST);
assertNotNull(langOut);
assertEquals(langIn, langOut);
// File types
ICFileType th = new CFileType(FT_TEST_HEADER, langIn, "Test Language Header", ICFileType.TYPE_HEADER);
ICFileType ts = new CFileType(FT_TEST_SOURCE, langIn, "Test Language Source", ICFileType.TYPE_SOURCE);
ICFileType tu = new CFileType(FT_TEST_WHASAT, langIn, "Test Language Unknown", ICFileType.TYPE_UNKNOWN);
// -- header
result = resolver.removeFileType(th);
assertFalse(result);
result = resolver.addFileType(th);
assertTrue(result);
ICFileType thOut = resolver.getFileTypeById(FT_TEST_HEADER);
assertNotNull(thOut);
assertEquals(th, thOut);
// -- source
result = resolver.removeFileType(ts);
assertFalse(result);
result = resolver.addFileType(ts);
assertTrue(result);
ICFileType tsOut = resolver.getFileTypeById(FT_TEST_SOURCE);
assertNotNull(tsOut);
assertEquals(ts, tsOut);
// -- unknown
result = resolver.removeFileType(tu);
assertFalse(result);
result = resolver.addFileType(tu);
assertTrue(result);
ICFileType tuOut = resolver.getFileTypeById(FT_TEST_WHASAT);
assertNotNull(tuOut);
assertEquals(tu, tuOut);
// File type associations
ICFileTypeAssociation tha = new CFileTypeAssociation("*.aest", th);
ICFileTypeAssociation tsa = new CFileTypeAssociation("*.test", th);
ICFileTypeAssociation tua = new CFileTypeAssociation("*.zest", th);
// -- header
result = resolver.removeFileTypeAssociation(tha);
assertFalse(result);
result = resolver.addFileTypeAssociation(tha);
assertTrue(result);
ICFileType thaOut = resolver.getFileType("file.aest");
assertNotNull(thaOut);
assertEquals(tha.getType(), thaOut);
// -- source
result = resolver.removeFileTypeAssociation(tsa);
assertFalse(result);
result = resolver.addFileTypeAssociation(tsa);
assertTrue(result);
ICFileType tsaOut = resolver.getFileType("file.test");
assertNotNull(tsaOut);
assertEquals(tsa.getType(), tsaOut);
// -- unknown
result = resolver.removeFileTypeAssociation(tua);
assertFalse(result);
result = resolver.addFileTypeAssociation(tua);
assertTrue(result);
ICFileType tuaOut = resolver.getFileType("file.zest");
assertNotNull(tuaOut);
assertEquals(tua.getType(), tuaOut);
}
public final void testRemove() {
boolean result = false;
// Languages
ICLanguage lang = resolver.getLanguageById(LANG_TEST);
assertNotNull(lang);
assertEquals(LANG_TEST, lang.getId());
result = resolver.removeLanguage(lang);
assertTrue(result);
result = resolver.removeLanguage(lang);
assertFalse(result);
// File types
ICFileType ft = resolver.getFileTypeById(FT_TEST_HEADER);
result = resolver.removeFileType(ft);
assertTrue(result);
result = resolver.removeFileType(ft);
assertFalse(result);
ft = resolver.getFileTypeById(FT_TEST_SOURCE);
result = resolver.removeFileType(ft);
assertTrue(result);
result = resolver.removeFileType(ft);
assertFalse(result);
ft = resolver.getFileTypeById(FT_TEST_WHASAT);
result = resolver.removeFileType(ft);
assertTrue(result);
result = resolver.removeFileType(ft);
assertFalse(result);
// File type associations
ICFileTypeAssociation[] assocs = resolver.getFileTypeAssociations();
assertNotNull(assocs);
assertTrue(assocs.length > 3);
for (int i = 0; i < assocs.length; i++) {
if (assocs[i].getType().getLanguage().getId().equals(LANG_TEST)) {
resolver.removeFileTypeAssociation(assocs[i]);
}
}
}
}

View file

@ -11,8 +11,11 @@ import junit.framework.TestSuite;
import org.eclipse.cdt.core.build.managed.tests.StandardBuildTests;
import org.eclipse.cdt.core.cdescriptor.tests.CDescriptorTests;
import org.eclipse.cdt.core.filetype.tests.ResolverTests;
import org.eclipse.cdt.core.indexer.tests.DependencyTests;
import org.eclipse.cdt.core.indexer.tests.IndexManagerTests;
import org.eclipse.cdt.core.internal.errorparsers.tests.GCCErrorParserTests;
import org.eclipse.cdt.core.internal.errorparsers.tests.GenericErrorParserTests;
import org.eclipse.cdt.core.model.tests.AllCoreTests;
import org.eclipse.cdt.core.model.tests.BinaryTests;
import org.eclipse.cdt.core.model.tests.ElementDeltaTests;
@ -51,6 +54,8 @@ public class AutomatedIntegrationSuite extends TestSuite {
// Add all success tests
suite.addTest(CDescriptorTests.suite());
//suite.addTest(GCCErrorParserTests.suite());
suite.addTest(ResolverTests.suite());
suite.addTest(StandardBuildTests.suite());
suite.addTest(ParserTestSuite.suite());
suite.addTest(AllCoreTests.suite());

View file

@ -23,3 +23,9 @@ org.eclipse.cdt.core/debug/matchlocator=false
# Reports delta processor tree activity
org.eclipse.cdt.core/debug/deltaprocessor=false
# Reports file type resolver activity
org.eclipse.cdt.core/debug/typeresolver=false
# Reports file type resolver activity
org.eclipse.cdt.core/debug/typeresolver=false

View file

@ -1,3 +1,7 @@
2004-05-19 Alain Magloire
Contribution from Sam Robb, for PR 52864.
2004-05-19 Alain Magloire
Patch contributed by James Langley.
Let Elf.java recognize Altera?s Nios and Nios II magic numbers.

View file

@ -537,7 +537,7 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
public boolean isCLanguage() {
IProject project = getCProject().getProject();
ICFileType type = CCorePlugin.getDefault().getFileType(project, getPath().lastSegment());
String lid = type.getLanguageId();
String lid = type.getLanguage().getId();
return lid != null && lid.equals(ICFileTypeConstants.LANG_C);
}
@ -547,7 +547,7 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
public boolean isCXXLanguage() {
IProject project = getCProject().getProject();
ICFileType type = CCorePlugin.getDefault().getFileType(project, getPath().lastSegment());
String lid = type.getLanguageId();
String lid = type.getLanguage().getId();
return lid != null && lid.equals(ICFileTypeConstants.LANG_CXX);
}
@ -557,7 +557,7 @@ public class TranslationUnit extends Openable implements ITranslationUnit {
public boolean isASMLanguage() {
IProject project = getCProject().getProject();
ICFileType type = CCorePlugin.getDefault().getFileType(project, getPath().lastSegment());
String lid = type.getLanguageId();
String lid = type.getLanguage().getId();
return lid != null && lid.equals(ICFileTypeConstants.LANG_ASM);
}

View file

@ -920,6 +920,7 @@ public class CCorePlugin extends Plugin {
private static final String PARSER = CCorePlugin.PLUGIN_ID + "/debug/parser" ; //$NON-NLS-1$
private static final String SCANNER = CCorePlugin.PLUGIN_ID + "/debug/scanner"; //$NON-NLS-1$
private static final String DELTA = CCorePlugin.PLUGIN_ID + "/debug/deltaprocessor" ; //$NON-NLS-1$
private static final String RESOLVER = CCorePlugin.PLUGIN_ID + "/debug/typeresolver" ; //$NON-NLS-1$
//private static final String CONTENTASSIST = CCorePlugin.PLUGIN_ID + "/debug/contentassist" ; //$NON-NLS-1$
/**
@ -955,7 +956,10 @@ public class CCorePlugin extends Plugin {
option = Platform.getDebugOption(MATCH_LOCATOR);
if(option != null) MatchLocator.VERBOSE = option.equalsIgnoreCase("true") ; //$NON-NLS-1$
option = Platform.getDebugOption(RESOLVER);
if(option != null) CFileTypeResolver.VERBOSE = option.equalsIgnoreCase("true") ; //$NON-NLS-1$
if (indexFlag == true){
JobManager.VERBOSE = true;
}

View file

@ -30,9 +30,9 @@ public interface ICFileType {
public String getId();
/**
* @return Language id associated with this file type.
* @return Language associated with this file type.
*/
public String getLanguageId();
public ICLanguage getLanguage();
/**
* @return Name of this file type.

View file

@ -39,9 +39,9 @@ public interface ICFileTypeConstants {
public static String FT_C_HEADER = FT_PREFIX + "c_header"; //$NON-NLS-1$
public static String FT_CXX_HEADER = FT_PREFIX + "cxx_source"; //$NON-NLS-1$
public static String FT_CXX_SOURCE = FT_PREFIX + "cxx_header"; //$NON-NLS-1$
public static String FT_CXX_SOURCE = FT_PREFIX + "cxx_source"; //$NON-NLS-1$
public static String FT_CXX_HEADER = FT_PREFIX + "cxx_header"; //$NON-NLS-1$
public static String FT_ASM_SOURCE = FT_PREFIX + "asm_source"; //$NON-NLS-1$
}

View file

@ -15,22 +15,12 @@ package org.eclipse.cdt.core.filetype;
* associated file type.
*
* Accessed by ICFileTypeResolver and file type management UI.
*
* The implementation checks for a match for the provided file name
* - By looking for an exact filename match ("iostream" == "iostream")
* - By looking for an extension match ("foo.c" == "*.c")
* - By looking for a pattern match ("libfoo.so.1.0" == "*.so*")
*/
public interface ICFileTypeResolver {
/**
* Determine which file type corresponds to the given
* file name.
*
* @param fileName file name to check.
*
* @return file type for the provided file name
* @return array containing all known languages.
*/
public ICFileType getFileType(String fileName);
public ICLanguage[] getLanguages();
/**
* @return array containing all known file types.
@ -42,6 +32,34 @@ public interface ICFileTypeResolver {
*/
public ICFileTypeAssociation[] getFileTypeAssociations();
/**
* Add a new language to the resolver's list.
*
* @param language language to add.
*
* @return true if the language was added.
*/
public boolean addLanguage(ICLanguage language);
/**
* Remove a language from the resolver's list.
*
* @param language language to remove.
*
* @return true if the language was removed.
*/
public boolean removeLanguage(ICLanguage language);
/**
* Get the language that has the specified id.
* Returns null if no language has that id.
*
* @param languageId language id
*
* @return language with the specified id, or null
*/
public ICLanguage getLanguageById(String languageId);
/**
* Add a new file type to the resolver's list.
*
@ -61,6 +79,26 @@ public interface ICFileTypeResolver {
*/
public boolean removeFileType(ICFileType type);
/**
* Determine which file type corresponds to the given
* file name.
*
* @param fileName file name to check.
*
* @return file type for the provided file name
*/
public ICFileType getFileType(String fileName);
/**
* Get the file type that has the specified id.
* Returns null if no file type has that id.
*
* @param typeId file type id
*
* @return file type with the specified id, or null
*/
public ICFileType getFileTypeById(String typeId);
/**
* Add a new file type association to the resolver's list.
*
@ -79,5 +117,4 @@ public interface ICFileTypeResolver {
* @return true if the file type association was removed.
*/
public boolean removeFileTypeAssociation(ICFileTypeAssociation assoc);
}

View file

@ -0,0 +1,80 @@
/**********************************************************************
* Copyright (c) 2004 TimeSys Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* TimeSys Corporation - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.core.internal.filetype;
/**
* Utility class used to check arguments and generate an
* IllegalArgumentException if they fail to meet some
* minimum requirements (null ref, empty string, etc.)
*/
public class Argument {
/**
* Throw an exception if a string argument is null or empty.
*
* @param arg String to check
*
* @throws IllegalArgumentException
*/
static public void check(String arg) throws IllegalArgumentException {
check(arg, false);
}
/**
* Throw an exception if a string argument is null (and optionally,
* if the string is empty).
*
* @param arg String to check
* @param allowEmpty True to allow an empty string.
*
* @throws IllegalArgumentException
*/
static public void check(String arg, boolean allowEmpty) throws IllegalArgumentException {
if (null == arg) {
throw new IllegalArgumentException("Null string argument"); //$NON-NLS-1$
} else if (0 == arg.length()) {
throw new IllegalArgumentException("Empty string argument"); //$NON-NLS-1$
}
}
/**
* Throws an exception if an object argument is null.
*
* @param arg Object to check
*
* @throws IllegalArgumentException
*/
static public void check(Object arg) throws IllegalArgumentException {
if (null == arg) {
throw new IllegalArgumentException("Null reference argument"); //$NON-NLS-1$
}
}
/**
* Throws an exception if an integer argument lies outside the specified
* range.
*
* @param arg Integer to check
* @param min Minimum allowed value for the argument.
* @param max Maximum allowed value for the argument.
*
* @throws IllegalArgumentException
*/
static public void check(int arg, int min, int max) throws IllegalArgumentException {
if (arg < min) {
throw new IllegalArgumentException("Integer argument out of range (low)"); //$NON-NLS-1$
} else if (arg > max) {
throw new IllegalArgumentException("Integer argument out of range (high)"); //$NON-NLS-1$
}
}
}

View file

@ -11,20 +11,26 @@
package org.eclipse.cdt.core.internal.filetype;
import org.eclipse.cdt.core.filetype.ICFileType;
import org.eclipse.cdt.core.filetype.ICLanguage;
/**
* Representation of a declared file type.
*/
public class CFileType implements ICFileType {
private String fId;
private String fLangId;
private String fName;
private int fType;
private ICLanguage fLang;
private String fId;
private String fName;
private int fType;
public CFileType(String id, String languageId, String name, int type) {
public CFileType(String id, ICLanguage language, String name, int type) {
Argument.check(id);
Argument.check(language);
Argument.check(name);
Argument.check(type, ICFileType.TYPE_UNKNOWN, ICFileType.TYPE_HEADER);
fId = id;
fLangId = languageId;
fLang = language;
fName = name;
fType = type;
}
@ -33,8 +39,8 @@ public class CFileType implements ICFileType {
return fId;
}
public String getLanguageId() {
return fLangId;
public ICLanguage getLanguage() {
return fLang;
}
public String getName() {
@ -56,4 +62,19 @@ public class CFileType implements ICFileType {
public boolean isTranslationUnit() {
return (isSource() || isHeader());
}
public boolean equals(Object object) {
if (!(object instanceof ICFileType)) {
return false;
}
ICFileType rhs = (ICFileType) object;
boolean eq = (fType == rhs.getType());
if (eq) eq = fId.equals(rhs.getId());
if (eq) eq = fLang.equals(rhs.getLanguage());
if (eq) eq = fName.equals(rhs.getName());
return eq;
}
}

View file

@ -24,6 +24,9 @@ public class CFileTypeAssociation implements ICFileTypeAssociation {
private StringMatcher fMatcher;
public CFileTypeAssociation(String pattern, ICFileType type) {
Argument.check(pattern);
Argument.check(type);
fPattern = pattern;
fType = type;
fMatcher = new StringMatcher(pattern, false, false);
@ -38,6 +41,22 @@ public class CFileTypeAssociation implements ICFileTypeAssociation {
}
public boolean matches(String fileName) {
if (null == fileName) {
return (null == fPattern);
}
return fMatcher.match(fileName);
}
public boolean equals(Object object) {
if (!(object instanceof ICFileTypeAssociation)) {
return false;
}
ICFileTypeAssociation rhs = (ICFileTypeAssociation) object;
boolean eq = fPattern.equals(rhs.getPattern());
if (eq) eq = fType.equals(rhs.getType());
return eq;
}
}

View file

@ -33,13 +33,12 @@ import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
/**
* @author sam.robb
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
* Implementation of the file type resolver interface.
*/
public class CFileTypeResolver implements ICFileTypeResolver {
public static boolean VERBOSE = false;
private static final String EXTENSION_LANG = "CLanguage"; //$NON-NLS-1$
private static final String EXTENSION_TYPE = "CFileType"; //$NON-NLS-1$
private static final String EXTENSION_ASSOC = "CFileTypeAssociation"; //$NON-NLS-1$
@ -52,40 +51,37 @@ public class CFileTypeResolver implements ICFileTypeResolver {
private static final String ATTR_VAL_SOURCE = "source"; //$NON-NLS-1$
private static final String ATTR_VAL_HEADER = "header"; //$NON-NLS-1$
private static final String NAME_UNKNOWN = "Unknown";
/**
* Default language, returned when no other language matches a language id.
*/
public static final ICLanguage DEFAULT_LANG_TYPE =
new CLanguage(ICFileTypeConstants.LANG_UNKNOWN, ""); //$NON-NLS-1$
new CLanguage(ICFileTypeConstants.LANG_UNKNOWN, NAME_UNKNOWN);
/**
* Default file type, returned when no other file type matches a file name.
*/
public static final ICFileType DEFAULT_FILE_TYPE =
new CFileType(ICFileTypeConstants.FT_UNKNOWN, ICFileTypeConstants.LANG_UNKNOWN, "", ICFileType.TYPE_UNKNOWN); //$NON-NLS-1$
new CFileType(ICFileTypeConstants.FT_UNKNOWN, DEFAULT_LANG_TYPE, NAME_UNKNOWN, ICFileType.TYPE_UNKNOWN);
// Singleton
private static ICFileTypeResolver instance = new CFileTypeResolver();
private static ICFileTypeResolver instance = null;
// Private ctor to preserve singleton status
private CFileTypeResolver() {
loadLanguages();
loadTypes();
loadAssociations();
if (CCorePlugin.getDefault().isDebugging()) {
String[] test = { "foo.c", "bar.h", "baz.s", "numeric" };
for (int i = 0; i < test.length; i++) {
ICFileType ft = getFileType(test[i]);
System.out.println("Self test: " + test[i] + " mapped to " + ft.getId());
}
}
}
/**
* @return the default instance of this singleton
*/
public static ICFileTypeResolver getDefault() {
synchronized public static ICFileTypeResolver getDefault() {
if (null == instance) {
instance = new CFileTypeResolver();
}
return instance;
}
@ -185,12 +181,13 @@ public class CFileTypeResolver implements ICFileTypeResolver {
* @return true if the language is added, false otherwise
*/
public boolean addLanguage(ICLanguage lang) {
if (CCorePlugin.getDefault().isDebugging()) {
System.out.println("File Type Resolver: adding language " + lang.getId() + " as " + lang.getName());
if (VERBOSE) {
debugLog("+ language " + lang.getId() + " as " + lang.getName());
}
boolean added = false;
if (!fLangMap.containsValue(lang)) {
added = (null != fTypeMap.put(lang.getId(), lang));
fLangMap.put(lang.getId(), lang);
added = true;
}
return added;
}
@ -203,11 +200,11 @@ public class CFileTypeResolver implements ICFileTypeResolver {
* @return true if the language is removed, false otherwise
*/
public boolean removeLanguage(ICLanguage lang) {
if (CCorePlugin.getDefault().isDebugging()) {
System.out.println("File Type Resolver: removing language " + lang.getId() + " as " + lang.getName());
if (VERBOSE) {
debugLog("- language " + lang.getId() + " as " + lang.getName());
}
// TODO: must remove any file types based on this language as well
return (null != fLangMap.remove(lang));
return (null != fLangMap.remove(lang.getId()));
}
/**
@ -222,12 +219,13 @@ public class CFileTypeResolver implements ICFileTypeResolver {
* @return true if the file type is added, false otherwise
*/
public boolean addFileType(ICFileType type) {
if (CCorePlugin.getDefault().isDebugging()) {
System.out.println("File Type Resolver: adding type " + type.getId() + " as " + type.getName());
if (VERBOSE) {
debugLog("+ type " + type.getId() + " as " + type.getName());
}
boolean added = false;
if (!fTypeMap.containsValue(type)) {
added = (null != fTypeMap.put(type.getId(), type));
fTypeMap.put(type.getId(), type);
added = true;
}
return added;
}
@ -240,11 +238,11 @@ public class CFileTypeResolver implements ICFileTypeResolver {
* @return true if the file type is removed, false otherwise
*/
public boolean removeFileType(ICFileType type) {
if (CCorePlugin.getDefault().isDebugging()) {
System.out.println("File Type Resolver: removing type " + type.getId() + " as " + type.getName());
if (VERBOSE) {
debugLog("- type " + type.getId() + " as " + type.getName());
}
// TODO: must remove any associations based on this file type as well
return (null != fTypeMap.remove(type));
return (null != fTypeMap.remove(type.getId()));
}
/**
@ -259,8 +257,8 @@ public class CFileTypeResolver implements ICFileTypeResolver {
* @return true if the association is added, false otherwise
*/
public boolean addFileTypeAssociation(ICFileTypeAssociation assoc) {
if (CCorePlugin.getDefault().isDebugging()) {
System.out.println("File Type Resolver: adding association " + assoc.getPattern() + " as " + assoc.getType().getId());
if (VERBOSE) {
debugLog("+ association " + assoc.getPattern() + " as " + assoc.getType().getId());
}
boolean added = false;
if (!fAssocList.contains(assoc)) {
@ -270,8 +268,8 @@ public class CFileTypeResolver implements ICFileTypeResolver {
}
public boolean removeFileTypeAssociation(ICFileTypeAssociation assoc) {
if (CCorePlugin.getDefault().isDebugging()) {
System.out.println("File Type Resolver: removing association " + assoc.getPattern() + " as " + assoc.getType().getId());
if (VERBOSE) {
debugLog("- association " + assoc.getPattern() + " as " + assoc.getType().getId());
}
return fAssocList.remove(assoc);
}
@ -287,9 +285,14 @@ public class CFileTypeResolver implements ICFileTypeResolver {
for (int i = 0; i < extensions.length; i++) {
elements = extensions[i].getConfigurationElements();
for (int j = 0; j < elements.length; j++) {
String id = elements[j].getAttribute(ATTR_ID);
String id = elements[j].getAttribute(ATTR_ID);
String name = elements[j].getAttribute(ATTR_NAME);
addLanguage(new CLanguage(id, name));
try {
addLanguage(new CLanguage(id, name));
} catch (IllegalArgumentException e) {
CCorePlugin.log(e);
}
}
}
@ -310,7 +313,13 @@ public class CFileTypeResolver implements ICFileTypeResolver {
String lang = elements[j].getAttribute(ATTR_LANGUAGE);
String name = elements[j].getAttribute(ATTR_NAME);
String type = elements[j].getAttribute(ATTR_TYPE);
addFileType(new CFileType(id, lang, name, parseType(type)));
try {
addFileType(new CFileType(id, getLanguageById(lang), name, parseType(type)));
} catch (IllegalArgumentException e) {
CCorePlugin.log(e);
}
}
}
}
@ -381,7 +390,11 @@ public class CFileTypeResolver implements ICFileTypeResolver {
if (null != attr) {
String[] item = attr.split(",");
for (int i = 0; i < item.length; i++) {
addFileTypeAssociation(new CFileTypeAssociation(item[i].trim(), typeRef));
try {
addFileTypeAssociation(new CFileTypeAssociation(item[i].trim(), typeRef));
} catch (IllegalArgumentException e) {
CCorePlugin.log(e);
}
}
}
}
@ -413,7 +426,11 @@ public class CFileTypeResolver implements ICFileTypeResolver {
in = new BufferedReader(new InputStreamReader(fileURL.openStream()));
line = in.readLine();
while (null != line) {
addFileTypeAssociation(new CFileTypeAssociation(line, typeRef));
try {
addFileTypeAssociation(new CFileTypeAssociation(line, typeRef));
} catch (IllegalArgumentException e) {
CCorePlugin.log(e);
}
line = in.readLine();
}
in.close();
@ -421,4 +438,8 @@ public class CFileTypeResolver implements ICFileTypeResolver {
}
}
}
private void debugLog(String message) {
System.out.println("CDT Resolver: " + message);
}
}

View file

@ -21,6 +21,9 @@ public class CLanguage implements ICLanguage {
private String fName;
public CLanguage(String id, String name) {
Argument.check(id);
Argument.check(name);
fId = id;
fName = name;
}
@ -32,4 +35,16 @@ public class CLanguage implements ICLanguage {
public String getName() {
return fName;
}
public boolean equals(Object object) {
if (!(object instanceof ICLanguage)) {
return false;
}
ICLanguage rhs = (ICLanguage) object;
boolean eq = fId.equals(rhs.getId());
if (eq) eq = fName.equals(rhs.getName());
return eq;
}
}

View file

@ -1,3 +1,7 @@
2004-05-19 Alain Magloire
Contribution from Sam Robb, for PR 52864.
2004-05-19 Alain Magloire
Comply with Eclipse-3.0 and use the theme

View file

@ -449,7 +449,7 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IS
IFile file= ((IFileEditorInput) originalElement).getFile();
if (file != null) {
ICFileType type = CCorePlugin.getDefault().getFileType(file.getProject(), file.getName());
oldLanguage = type.getLanguageId();
oldLanguage = type.getLanguage().getId();
if (oldLanguage == null) {
return false;
}
@ -461,7 +461,7 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IS
IFile file = ((IFileEditorInput) movedElement).getFile();
if (file != null) {
ICFileType type = CCorePlugin.getDefault().getFileType(file.getProject(), file.getName());
newLanguage = type.getLanguageId();
newLanguage = type.getLanguage().getId();
if (newLanguage == null) {
return false;
}