1 11 package org.eclipse.core.internal.content; 12 13 import java.io.*; 14 import java.util.*; 15 import org.eclipse.core.runtime.QualifiedName; 16 import org.eclipse.core.runtime.content.*; 17 import org.eclipse.core.runtime.preferences.*; 18 import org.osgi.service.prefs.BackingStoreException; 19 20 23 public class ContentTypeMatcher implements IContentTypeMatcher { 24 25 private IScopeContext context; 26 private IContentTypeManager.ISelectionPolicy policy; 27 28 public ContentTypeMatcher(IContentTypeManager.ISelectionPolicy policy, IScopeContext context) { 29 this.policy = policy; 30 this.context = context; 31 } 32 33 36 public IContentType findContentTypeFor(InputStream contents, String fileName) throws IOException { 37 ContentTypeCatalog currentCatalog = getCatalog(); 38 IContentType[] all = currentCatalog.findContentTypesFor(this, contents, fileName); 39 return all.length > 0 ? new ContentTypeHandler((ContentType) all[0], currentCatalog.getGeneration()) : null; 40 } 41 42 45 public IContentType findContentTypeFor(String fileName) { 46 ContentTypeCatalog currentCatalog = getCatalog(); 48 IContentType[] associated = currentCatalog.findContentTypesFor(this, fileName); 49 return associated.length == 0 ? null : new ContentTypeHandler((ContentType) associated[0], currentCatalog.getGeneration()); 50 } 51 52 55 public IContentType[] findContentTypesFor(InputStream contents, String fileName) throws IOException { 56 ContentTypeCatalog currentCatalog = getCatalog(); 57 IContentType[] types = currentCatalog.findContentTypesFor(this, contents, fileName); 58 IContentType[] result = new IContentType[types.length]; 59 int generation = currentCatalog.getGeneration(); 60 for (int i = 0; i < result.length; i++) 61 result[i] = new ContentTypeHandler((ContentType) types[i], generation); 62 return result; 63 } 64 65 68 public IContentType[] findContentTypesFor(String fileName) { 69 ContentTypeCatalog currentCatalog = getCatalog(); 70 IContentType[] types = currentCatalog.findContentTypesFor(this, fileName); 71 IContentType[] result = new IContentType[types.length]; 72 int generation = currentCatalog.getGeneration(); 73 for (int i = 0; i < result.length; i++) 74 result[i] = new ContentTypeHandler((ContentType) types[i], generation); 75 return result; 76 } 77 78 private ContentTypeCatalog getCatalog() { 79 return ContentTypeManager.getInstance().getCatalog(); 80 } 81 82 85 public IContentDescription getDescriptionFor(InputStream contents, String fileName, QualifiedName[] options) throws IOException { 86 return getCatalog().getDescriptionFor(this, contents, fileName, options); 87 } 88 89 92 public IContentDescription getDescriptionFor(Reader contents, String fileName, QualifiedName[] options) throws IOException { 93 return getCatalog().getDescriptionFor(this, contents, fileName, options); 94 } 95 96 public IScopeContext getContext() { 97 return context; 98 } 99 100 public IContentTypeManager.ISelectionPolicy getPolicy() { 101 return policy; 102 } 103 104 107 public Collection getDirectlyAssociated(final ContentTypeCatalog catalog, final String fileSpec, final int typeMask) { 108 final IEclipsePreferences root = context.getNode(ContentTypeManager.CONTENT_TYPE_PREF_NODE); 110 final Set result = new HashSet(3); 111 try { 112 root.accept(new IPreferenceNodeVisitor() { 113 public boolean visit(IEclipsePreferences node) { 114 if (node == root) 115 return true; 116 String [] fileSpecs = ContentTypeSettings.getFileSpecs(node, typeMask); 117 for (int i = 0; i < fileSpecs.length; i++) 118 if (fileSpecs[i].equalsIgnoreCase(fileSpec)) { 119 ContentType associated = catalog.getContentType(node.name()); 120 if (associated != null) 121 result.add(associated); 122 break; 123 } 124 return false; 125 } 126 127 }); 128 } catch (BackingStoreException bse) { 129 ContentType.log(ContentMessages.content_errorLoadingSettings, bse); 130 } 131 return result == null ? Collections.EMPTY_SET : result; 132 } 133 134 public IContentDescription getSpecificDescription(BasicDescription description) { 135 if (description == null || ContentTypeManager.getInstance().getContext().equals(getContext())) 136 return description; 138 if (description instanceof DefaultDescription) 140 return new DefaultDescription(new ContentTypeSettings((ContentType) description.getContentTypeInfo(), context)); 142 ((ContentDescription) description).setContentTypeInfo(new ContentTypeSettings((ContentType) description.getContentTypeInfo(), context)); 145 return description; 146 } 147 } 148 | Popular Tags |