KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > content > ContentTypeMatcher


1 /*******************************************************************************
2  * Copyright (c) 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
21  * @since 3.1
22  */

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     /**
34      * @see IContentTypeMatcher
35      */

36     public IContentType findContentTypeFor(InputStream contents, String JavaDoc 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     /**
43      * @see IContentTypeMatcher
44      */

45     public IContentType findContentTypeFor(String JavaDoc fileName) {
46         // basic implementation just gets all content types
47
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     /**
53      * @see IContentTypeMatcher
54      */

55     public IContentType[] findContentTypesFor(InputStream contents, String JavaDoc 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     /**
66      * @see IContentTypeMatcher
67      */

68     public IContentType[] findContentTypesFor(String JavaDoc 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     /**
83      * @see IContentTypeMatcher
84      */

85     public IContentDescription getDescriptionFor(InputStream contents, String JavaDoc fileName, QualifiedName[] options) throws IOException {
86         return getCatalog().getDescriptionFor(this, contents, fileName, options);
87     }
88
89     /**
90      * @see IContentTypeMatcher
91      */

92     public IContentDescription getDescriptionFor(Reader contents, String JavaDoc 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     /**
105      * Enumerates all content types whose settings satisfy the given file spec type mask.
106      */

107     public Collection getDirectlyAssociated(final ContentTypeCatalog catalog, final String JavaDoc fileSpec, final int typeMask) {
108         //TODO: make sure we include built-in associations as well
109
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 JavaDoc[] 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             // no need for specific content descriptions
137
return description;
138         // default description
139
if (description instanceof DefaultDescription)
140             // return an context specific description instead
141
return new DefaultDescription(new ContentTypeSettings((ContentType) description.getContentTypeInfo(), context));
142         // non-default description
143
// replace info object with context specific settings
144
((ContentDescription) description).setContentTypeInfo(new ContentTypeSettings((ContentType) description.getContentTypeInfo(), context));
145         return description;
146     }
147 }
148
Popular Tags