public void testIsAssociatedWith() { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); final SingleNodeScope scope = new SingleNodeScope(); IContentType textContentType = contentTypeManager.getContentType(Platform.PI_RUNTIME + '.' + "text"); IContentTypeSettings localSettings = null; try { localSettings = textContentType.getSettings(scope); } catch (CoreException e) { fail("0.1", e); } // haven't added association yet assertTrue("1.0", !textContentType.isAssociatedWith("hello.foo", scope)); assertTrue("1.1", !textContentType.isAssociatedWith("hello.foo")); try { // associate at the scope level localSettings.addFileSpec("foo", IContentType.FILE_EXTENSION_SPEC); } catch (CoreException e) { fail("1.5", e); } try { localSettings = textContentType.getSettings(scope); } catch (CoreException e) { fail("2.1", e); } // scope-specific settings should contain the filespec we just added String[] fileSpecs = localSettings.getFileSpecs(IContentType.FILE_EXTENSION_SPEC); assertEquals("2.2", 1, fileSpecs.length); assertEquals("2.3", "foo", fileSpecs[0]); // now it is associated at the scope level... assertTrue("2.5", textContentType.isAssociatedWith("hello.foo", scope)); // ...but not at the global level assertTrue("2.6", !textContentType.isAssociatedWith("hello.foo")); }
public IStructuredFormatProcessor getFormatProcessor(String contentTypeId) { if (contentTypeId == null) return null; IStructuredFormatProcessor formatProcessor = null; if (map.containsKey(contentTypeId)) { formatProcessor = (IStructuredFormatProcessor) map.get(contentTypeId); } else { IContentTypeManager manager = Platform.getContentTypeManager(); IContentType queryContentType = manager.getContentType(contentTypeId); boolean found = false; for (Iterator iter = map.keySet().iterator(); iter.hasNext(); ) { String elementContentTypeId = (String) iter.next(); IContentType elementContentType = manager.getContentType(elementContentTypeId); if (queryContentType.isKindOf(elementContentType)) { formatProcessor = (IStructuredFormatProcessor) map.get(elementContentTypeId); map.put(contentTypeId, formatProcessor); found = true; break; } } if (!found) map.put(contentTypeId, null); } return formatProcessor; }
private static IContentType getContentType(ITypedElement element) { if (element == null) return null; String name = element.getName(); IContentType ct = null; if (element instanceof IStreamContentAccessor) { IStreamContentAccessor isa = (IStreamContentAccessor) element; try { InputStream is = isa.getContents(); if (is != null) { InputStream bis = new BufferedInputStream(is); try { ct = fgContentTypeManager.findContentTypeFor(is, name); } catch (IOException e) { // silently ignored } finally { try { bis.close(); } catch (IOException e2) { // silently ignored } } } } catch (CoreException e1) { // silently ignored } } if (ct == null) ct = fgContentTypeManager.findContentTypeFor(name); return ct; }
public void testContentTypeLookup() { IContentTypeManager global = Platform.getContentTypeManager(); final SingleNodeScope scope = new SingleNodeScope(); IContentTypeMatcher local = global.getMatcher(new LocalSelectionPolicy(), scope); IContentType textContentType = global.getContentType(Platform.PI_RUNTIME + '.' + "text"); try { // added "<test case name>.global" to the text content type as a global file spec textContentType.addFileSpec(getName() + ".global", IContentType.FILE_NAME_SPEC); } catch (CoreException e) { fail("0.1", e); } try { // added "<test case name>.local" to the text content type as a local (scope-specific) file // spec textContentType .getSettings(scope) .addFileSpec(getName() + ".local", IContentType.FILE_NAME_SPEC); } catch (CoreException e) { fail("0.2", e); } // make ensure associations are properly recognized when doing content type lookup assertEquals("1.0", textContentType, global.findContentTypeFor(getName() + ".global")); assertEquals("1.1", null, local.findContentTypeFor(getName() + ".global")); assertEquals("2.0", textContentType, local.findContentTypeFor(getName() + ".local")); assertEquals("2.1", null, global.findContentTypeFor(getName() + ".local")); try { textContentType.removeFileSpec(getName() + ".global", IContentType.FILE_NAME_SPEC); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
@Override 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); }
public static boolean isErlangFileContentFileName(final String fileName) { final IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); final IContentType[] contentTypes = contentTypeManager.findContentTypesFor(fileName); for (final IContentType contentType : contentTypes) { if (contentType.getId().equals("org.erlide.core.content.erlang")) { return true; } } return false; }
static { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); if (contentTypeManager != null) { contentTypeManager.addContentTypeChangeListener( new IContentTypeManager.IContentTypeChangeListener() { public void contentTypeChanged(ContentTypeChangeEvent event) { // we can be more specific here, but content types change so rarely, that // I am not concerned about being overly eager to invalidate the cache GROOVY_FILE_NAMES = null; GROOVY_LIKE_EXTENSIONS = null; JAVA_LIKE_BUT_NOT_GROOVY_LIKE_EXTENSIONS = null; } }); } }
private static Set<String> loadGroovyFileNames() { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); if (contentTypeManager != null) { Set<String> names = null; IContentType groovyContentType = contentTypeManager.getContentType(GROOVY_SOURCE_CONTENT_TYPE); for (IContentType contentType : contentTypeManager.getAllContentTypes()) { if (contentType.isKindOf(groovyContentType)) { for (String fileName : contentType.getFileSpecs(IContentType.FILE_NAME_SPEC)) { if (names == null) names = new TreeSet<String>(); names.add(fileName); } } } if (names != null) return names; } return Collections.emptySet(); }
private static Set<String> loadGroovyFileExtensions() { IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); if (contentTypeManager != null) { IContentType groovyContentType = contentTypeManager.getContentType(GROOVY_SOURCE_CONTENT_TYPE); Set<String> extensions = new HashSet<String>(); // https://bugs.eclipse.org/bugs/show_bug.cgi?id=121715 // content types derived from groovy content type should be included for (IContentType contentType : contentTypeManager.getAllContentTypes()) { if (contentType.isKindOf( groovyContentType)) { // note that contentType.isKindOf(javaContentType) == true for (String fileExtension : contentType.getFileSpecs(IContentType.FILE_EXTENSION_SPEC)) { extensions.add(fileExtension); } } } return extensions; } return Collections.singleton("groovy"); }
private EncodingMemento checkForEncodingInContents() throws CoreException, IOException { EncodingMemento result = null; // if encoding memento already set, and no need to get again. if (fEncodingMemento != null) { result = fEncodingMemento; } else { if (fClientSuppliedReader) { fReader.reset(); IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); try { IContentDescription contentDescription = contentTypeManager.getDescriptionFor(fReader, fFilename, IContentDescription.ALL); if (contentDescription != null) { fEncodingMemento = createMemento(contentDescription); } else { fEncodingMemento = CodedIO.createEncodingMemento("UTF-8"); // $NON-NLS-1$ } } catch (NullPointerException e) { // TODO: work around for 5/14 bug in base, should be // removed when move up to 5/21 // just created a simple default one fEncodingMemento = CodedIO.createEncodingMemento("UTF-8"); // $NON-NLS-1$ } result = fEncodingMemento; } else { throw new IllegalStateException( "unexpected state: encodingMemento was null but no input stream supplied"); //$NON-NLS-1$ } } // try { // result = getEncodingDetector().getEncodingMemento(); // if (result != null && !result.isValid() && !forceDefault()) { // throw new UnsupportedCharsetExceptionWithDetail(result); // } // } // finally { // handleStreamClose(fEncodingDetectorStream); // } return result; }