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(); } }
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; }