KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > editors > text > templates > ContributionTemplateStore


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.ui.editors.text.templates;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.PropertyResourceBundle JavaDoc;
21 import java.util.ResourceBundle JavaDoc;
22
23 import org.eclipse.core.runtime.FileLocator;
24 import org.eclipse.core.runtime.IConfigurationElement;
25 import org.eclipse.core.runtime.Path;
26 import org.eclipse.core.runtime.Platform;
27 import org.eclipse.jface.preference.IPreferenceStore;
28 import org.eclipse.jface.text.templates.ContextTypeRegistry;
29 import org.eclipse.jface.text.templates.Template;
30 import org.eclipse.jface.text.templates.TemplateException;
31 import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData;
32 import org.eclipse.jface.text.templates.persistence.TemplateReaderWriter;
33 import org.eclipse.jface.text.templates.persistence.TemplateStore;
34 import org.eclipse.ui.internal.editors.text.EditorsPlugin;
35 import org.eclipse.ui.internal.editors.text.NLSUtility;
36 import org.osgi.framework.Bundle;
37
38
39 /**
40  * Manages templates. Handles reading default templates contributed via XML and
41  * user-defined (or overridden) templates stored in the preferences.
42  * <p>
43  * Clients may instantiate but not subclass this class.
44  * </p>
45  *
46  * @since 3.0
47  */

48 public class ContributionTemplateStore extends TemplateStore {
49     /* extension point string literals */
50     private static final String JavaDoc TEMPLATES_EXTENSION_POINT= "org.eclipse.ui.editors.templates"; //$NON-NLS-1$
51

52     private static final String JavaDoc ID= "id"; //$NON-NLS-1$
53
private static final String JavaDoc NAME= "name"; //$NON-NLS-1$
54

55     private static final String JavaDoc CONTEXT_TYPE_ID= "contextTypeId"; //$NON-NLS-1$
56
private static final String JavaDoc DESCRIPTION= "description"; //$NON-NLS-1$
57
private static final String JavaDoc AUTO_INSERT= "autoinsert"; //$NON-NLS-1$
58

59     private static final String JavaDoc TEMPLATE= "template"; //$NON-NLS-1$
60
private static final String JavaDoc PATTERN= "pattern"; //$NON-NLS-1$
61

62     private static final String JavaDoc INCLUDE= "include"; //$NON-NLS-1$
63
private static final String JavaDoc FILE= "file"; //$NON-NLS-1$
64
private static final String JavaDoc TRANSLATIONS= "translations"; //$NON-NLS-1$
65

66     /**
67      * Creates a new template store.
68      *
69      * @param store the preference store in which to store custom templates
70      * under <code>key</code>
71      * @param key the key into <code>store</code> where to store custom
72      * templates
73      */

74     public ContributionTemplateStore(IPreferenceStore store, String JavaDoc key) {
75         super(store, key);
76     }
77
78     /**
79      * Creates a new template store with a context type registry. Only templates
80      * that specify a context type contained in the registry will be loaded by
81      * this store if the registry is not <code>null</code>.
82      *
83      * @param registry a context type registry, or <code>null</code> if all
84      * templates should be loaded
85      * @param store the preference store in which to store custom templates
86      * under <code>key</code>
87      * @param key the key into <code>store</code> where to store custom
88      * templates
89      */

90     public ContributionTemplateStore(ContextTypeRegistry registry, IPreferenceStore store, String JavaDoc key) {
91         super(registry, store, key);
92     }
93
94     /**
95      * Loads the templates contributed via the templates extension point.
96      *
97      * @throws IOException {@inheritDoc}
98      */

99     protected void loadContributedTemplates() throws IOException JavaDoc {
100         IConfigurationElement[] extensions= getTemplateExtensions();
101         Collection JavaDoc contributed= readContributedTemplates(extensions);
102         for (Iterator JavaDoc it= contributed.iterator(); it.hasNext();) {
103             TemplatePersistenceData data= (TemplatePersistenceData) it.next();
104             internalAdd(data);
105         }
106     }
107
108     private Collection JavaDoc readContributedTemplates(IConfigurationElement[] extensions) throws IOException JavaDoc {
109         Collection JavaDoc templates= new ArrayList JavaDoc();
110         for (int i= 0; i < extensions.length; i++) {
111             if (extensions[i].getName().equals(TEMPLATE))
112                 createTemplate(templates, extensions[i]);
113             else if (extensions[i].getName().equals(INCLUDE)) {
114                 readIncludedTemplates(templates, extensions[i]);
115             }
116         }
117
118         return templates;
119     }
120
121     private void readIncludedTemplates(Collection JavaDoc templates, IConfigurationElement element) throws IOException JavaDoc {
122         String JavaDoc file= element.getAttribute(FILE);
123         if (file != null) {
124             Bundle JavaDoc plugin = Platform.getBundle(element.getContributor().getName());
125             URL JavaDoc url= FileLocator.find(plugin, Path.fromOSString(file), null);
126             if (url != null) {
127                 ResourceBundle JavaDoc bundle= null;
128                 InputStream JavaDoc bundleStream= null;
129                 InputStream JavaDoc stream= null;
130                 try {
131                     String JavaDoc translations= element.getAttribute(TRANSLATIONS);
132                     if (translations != null) {
133                         URL JavaDoc bundleURL= FileLocator.find(plugin, Path.fromOSString(translations), null);
134                         if (bundleURL != null) {
135                             bundleStream= bundleURL.openStream();
136                             bundle= new PropertyResourceBundle JavaDoc(bundleStream);
137                         }
138                     }
139
140                     stream= new BufferedInputStream JavaDoc(url.openStream());
141                     TemplateReaderWriter reader= new TemplateReaderWriter();
142                     TemplatePersistenceData[] datas= reader.read(stream, bundle);
143                     for (int i= 0; i < datas.length; i++) {
144                         TemplatePersistenceData data= datas[i];
145                         if (data.isCustom()) {
146                             if (data.getId() == null)
147                                 EditorsPlugin.logErrorMessage(NLSUtility.format(ContributionTemplateMessages.ContributionTemplateStore_ignore_no_id, data.getTemplate().getName()));
148                             else
149                                 EditorsPlugin.logErrorMessage(NLSUtility.format(ContributionTemplateMessages.ContributionTemplateStore_ignore_deleted, data.getTemplate().getName()));
150                         } else if (!validateTemplate(data.getTemplate())) {
151                             if (contextExists(data.getTemplate().getContextTypeId()))
152                                 EditorsPlugin.logErrorMessage(NLSUtility.format(ContributionTemplateMessages.ContributionTemplateStore_ignore_validation_failed, data.getTemplate().getName()));
153                         } else {
154                             templates.add(data);
155                         }
156                     }
157                 } finally {
158                     try {
159                         if (bundleStream != null)
160                             bundleStream.close();
161                     } catch (IOException JavaDoc x) {
162                     } finally {
163                         try {
164                             if (stream != null)
165                                 stream.close();
166                         } catch (IOException JavaDoc x) {
167                         }
168                     }
169                 }
170             }
171         }
172     }
173
174     /**
175      * Validates a template against the context type registered in the context
176      * type registry. Returns always <code>true</code> if no registry is
177      * present.
178      *
179      * @param template the template to validate
180      * @return <code>true</code> if validation is successful or no context
181      * type registry is specified, <code>false</code> if validation
182      * fails
183      */

184     private boolean validateTemplate(Template template) {
185         String JavaDoc contextTypeId= template.getContextTypeId();
186         if (!contextExists(contextTypeId))
187             return false;
188
189         if (getRegistry() != null) {
190             try {
191                 getRegistry().getContextType(contextTypeId).validate(template.getPattern());
192             } catch (TemplateException e) {
193                 return false;
194             }
195         }
196         return true;
197     }
198
199     /**
200      * Returns <code>true</code> if a context type id specifies a valid context type
201      * or if no context type registry is present.
202      *
203      * @param contextTypeId the context type id to look for
204      * @return <code>true</code> if the context type specified by the id
205      * is present in the context type registry, or if no registry is
206      * specified
207      */

208     private boolean contextExists(String JavaDoc contextTypeId) {
209         return contextTypeId != null && (getRegistry() == null || getRegistry().getContextType(contextTypeId) != null);
210     }
211
212     private static IConfigurationElement[] getTemplateExtensions() {
213         return Platform.getExtensionRegistry().getConfigurationElementsFor(TEMPLATES_EXTENSION_POINT);
214     }
215
216     private void createTemplate(Collection JavaDoc map, IConfigurationElement element) {
217         String JavaDoc contextTypeId= element.getAttribute(CONTEXT_TYPE_ID);
218         // log failures since extension point id and name are mandatory
219
if (contextExists(contextTypeId)) {
220             String JavaDoc id= element.getAttribute(ID);
221             if (isValidTemplateId(id)) {
222
223                 String JavaDoc name= element.getAttribute(NAME);
224                 if (name != null) {
225
226                     String JavaDoc pattern= element.getChildren(PATTERN)[0].getValue();
227                     if (pattern != null) {
228
229                         String JavaDoc desc= element.getAttribute(DESCRIPTION);
230                         if (desc == null)
231                             desc= ""; //$NON-NLS-1$
232

233                         String JavaDoc autoInsert= element.getAttribute(AUTO_INSERT);
234                         boolean bAutoInsert;
235                         if (autoInsert == null)
236                             bAutoInsert= true;
237                         else
238                             bAutoInsert= Boolean.valueOf(autoInsert).booleanValue();
239                         
240                         Template template= new Template(name, desc, contextTypeId, pattern, bAutoInsert);
241                         TemplatePersistenceData data= new TemplatePersistenceData(template, true, id);
242                         if (validateTemplate(template))
243                             map.add(data);
244                     }
245                 }
246             }
247         }
248     }
249
250     private static boolean isValidTemplateId(String JavaDoc id) {
251         return id != null && id.trim().length() != 0; // TODO test validity?
252
}
253     
254     /*
255      * @see org.eclipse.jface.text.templates.persistence.TemplateStore#handleException(java.io.IOException)
256      * @since 3.2
257      */

258     protected void handleException(IOException JavaDoc x) {
259         EditorsPlugin.log(x);
260     }
261 }
262
263
Popular Tags