KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > StorageMergerRegistry


1 /*******************************************************************************
2  * Copyright (c) 2006 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.team.internal.core;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.core.runtime.content.IContentType;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.team.core.mapping.IStorageMerger;
20
21 public class StorageMergerRegistry {
22     
23     private final static String JavaDoc ID_ATTRIBUTE = "id"; //$NON-NLS-1$
24
private final static String JavaDoc EXTENSIONS_ATTRIBUTE = "extensions"; //$NON-NLS-1$
25
private final static String JavaDoc CONTENT_TYPE_ID_ATTRIBUTE = "contentTypeId"; //$NON-NLS-1$
26
private static final String JavaDoc STORAGE_MERGER_EXTENSION_POINT = "storageMergers"; //$NON-NLS-1$
27
private static final Object JavaDoc STORAGE_MERGER = "storageMerger"; //$NON-NLS-1$
28
private static final String JavaDoc CONTENT_TYPE_BINDING= "contentTypeBinding"; //$NON-NLS-1$
29
private static final String JavaDoc STORAGE_MERGER_ID_ATTRIBUTE= "storageMergerId"; //$NON-NLS-1$
30

31     private static boolean NORMALIZE_CASE= true;
32     
33     private static StorageMergerRegistry instance;
34
35     private HashMap JavaDoc fIdMap; // maps ids to datas
36
private HashMap JavaDoc fExtensionMap; // maps extensions to datas
37
private HashMap JavaDoc fContentTypeBindings; // maps content type bindings to datas
38
private boolean fRegistriesInitialized;
39     
40     public static StorageMergerRegistry getInstance() {
41         if (instance == null) {
42             instance = new StorageMergerRegistry();
43         }
44         return instance;
45     }
46
47     /**
48      * Returns a stream merger for the given type.
49      *
50      * @param type the type for which to find a stream merger
51      * @return a stream merger for the given type, or <code>null</code> if no
52      * stream merger has been registered
53      */

54     public IStorageMerger createStreamMerger(String JavaDoc type) {
55         initializeRegistry();
56         StorageMergerDescriptor descriptor= (StorageMergerDescriptor) search(type);
57         if (descriptor != null)
58             return descriptor.createStreamMerger();
59         return null;
60     }
61     
62     /**
63      * Returns a stream merger for the given content type.
64      *
65      * @param type the type for which to find a stream merger
66      * @return a stream merger for the given type, or <code>null</code> if no
67      * stream merger has been registered
68      */

69     public IStorageMerger createStreamMerger(IContentType type) {
70         initializeRegistry();
71         StorageMergerDescriptor descriptor= (StorageMergerDescriptor) search(type);
72         if (descriptor != null)
73             return descriptor.createStreamMerger();
74         return null;
75     }
76     
77     private void initializeRegistry() {
78         if (!fRegistriesInitialized) {
79             registerExtensions();
80             fRegistriesInitialized= true;
81         }
82     }
83     
84     /**
85      * Registers all stream mergers, structure creators, content merge viewers, and structure merge viewers
86      * that are found in the XML plugin files.
87      */

88     private void registerExtensions() {
89         IExtensionRegistry registry= Platform.getExtensionRegistry();
90         
91         // collect all IStreamMergers
92
IConfigurationElement[] elements= registry.getConfigurationElementsFor(TeamPlugin.ID, STORAGE_MERGER_EXTENSION_POINT);
93         for (int i= 0; i < elements.length; i++) {
94             IConfigurationElement element= elements[i];
95                 if (STORAGE_MERGER.equals(element.getName()))
96                     register(element, new StorageMergerDescriptor(element));
97                 else if (CONTENT_TYPE_BINDING.equals(element.getName()))
98                     createBinding(element, STORAGE_MERGER_ID_ATTRIBUTE);
99         }
100     }
101     
102     private static String JavaDoc normalizeCase(String JavaDoc s) {
103         if (NORMALIZE_CASE && s != null)
104             return s.toUpperCase();
105         return s;
106     }
107     
108     void register(IConfigurationElement element, Object JavaDoc data) {
109         String JavaDoc id = element.getAttribute(ID_ATTRIBUTE);
110         if (id != null) {
111             if (fIdMap == null)
112                 fIdMap = new HashMap JavaDoc();
113             fIdMap.put(id, data);
114         }
115
116         String JavaDoc types = element.getAttribute(EXTENSIONS_ATTRIBUTE);
117         if (types != null) {
118             if (fExtensionMap == null)
119                 fExtensionMap = new HashMap JavaDoc();
120             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(types, ","); //$NON-NLS-1$
121
while (tokenizer.hasMoreElements()) {
122                 String JavaDoc extension = tokenizer.nextToken().trim();
123                 fExtensionMap.put(normalizeCase(extension), data);
124             }
125         }
126     }
127
128     void createBinding(IConfigurationElement element, String JavaDoc idAttributeName) {
129         String JavaDoc type = element.getAttribute(CONTENT_TYPE_ID_ATTRIBUTE);
130         String JavaDoc id = element.getAttribute(idAttributeName);
131         if (id == null)
132             logErrorMessage(NLS.bind("Target attribute id '{0}' missing", idAttributeName)); //$NON-NLS-1$
133
if (type != null && id != null && fIdMap != null) {
134             Object JavaDoc o = fIdMap.get(id);
135             if (o != null) {
136                 IContentType ct = Platform.getContentTypeManager().getContentType(type);
137                 if (ct != null) {
138                     if (fContentTypeBindings == null)
139                         fContentTypeBindings = new HashMap JavaDoc();
140                     fContentTypeBindings.put(ct, o);
141                 } else {
142                     logErrorMessage(NLS.bind("Content type id '{0}' not found", type)); //$NON-NLS-1$
143
}
144             } else {
145                 logErrorMessage(NLS.bind("Target '{0}' not found", id)); //$NON-NLS-1$$
146
}
147         }
148     }
149
150     private void logErrorMessage(String JavaDoc string) {
151         TeamPlugin.log(IStatus.ERROR, string, null);
152     }
153
154     Object JavaDoc search(IContentType type) {
155         if (fContentTypeBindings != null) {
156             for (; type != null; type = type.getBaseType()) {
157                 Object JavaDoc data = fContentTypeBindings.get(type);
158                 if (data != null)
159                     return data;
160             }
161         }
162         return null;
163     }
164
165     Object JavaDoc search(String JavaDoc extension) {
166         if (fExtensionMap != null)
167             return fExtensionMap.get(normalizeCase(extension));
168         return null;
169     }
170 }
171
Popular Tags