1 11 package org.eclipse.ui.editors.text.templates; 12 13 import java.io.BufferedInputStream ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.net.URL ; 17 import java.util.ArrayList ; 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 import java.util.PropertyResourceBundle ; 21 import java.util.ResourceBundle ; 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 48 public class ContributionTemplateStore extends TemplateStore { 49 50 private static final String TEMPLATES_EXTENSION_POINT= "org.eclipse.ui.editors.templates"; 52 private static final String ID= "id"; private static final String NAME= "name"; 55 private static final String CONTEXT_TYPE_ID= "contextTypeId"; private static final String DESCRIPTION= "description"; private static final String AUTO_INSERT= "autoinsert"; 59 private static final String TEMPLATE= "template"; private static final String PATTERN= "pattern"; 62 private static final String INCLUDE= "include"; private static final String FILE= "file"; private static final String TRANSLATIONS= "translations"; 66 74 public ContributionTemplateStore(IPreferenceStore store, String key) { 75 super(store, key); 76 } 77 78 90 public ContributionTemplateStore(ContextTypeRegistry registry, IPreferenceStore store, String key) { 91 super(registry, store, key); 92 } 93 94 99 protected void loadContributedTemplates() throws IOException { 100 IConfigurationElement[] extensions= getTemplateExtensions(); 101 Collection contributed= readContributedTemplates(extensions); 102 for (Iterator it= contributed.iterator(); it.hasNext();) { 103 TemplatePersistenceData data= (TemplatePersistenceData) it.next(); 104 internalAdd(data); 105 } 106 } 107 108 private Collection readContributedTemplates(IConfigurationElement[] extensions) throws IOException { 109 Collection templates= new ArrayList (); 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 templates, IConfigurationElement element) throws IOException { 122 String file= element.getAttribute(FILE); 123 if (file != null) { 124 Bundle plugin = Platform.getBundle(element.getContributor().getName()); 125 URL url= FileLocator.find(plugin, Path.fromOSString(file), null); 126 if (url != null) { 127 ResourceBundle bundle= null; 128 InputStream bundleStream= null; 129 InputStream stream= null; 130 try { 131 String translations= element.getAttribute(TRANSLATIONS); 132 if (translations != null) { 133 URL bundleURL= FileLocator.find(plugin, Path.fromOSString(translations), null); 134 if (bundleURL != null) { 135 bundleStream= bundleURL.openStream(); 136 bundle= new PropertyResourceBundle (bundleStream); 137 } 138 } 139 140 stream= new BufferedInputStream (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 x) { 162 } finally { 163 try { 164 if (stream != null) 165 stream.close(); 166 } catch (IOException x) { 167 } 168 } 169 } 170 } 171 } 172 } 173 174 184 private boolean validateTemplate(Template template) { 185 String 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 208 private boolean contextExists(String 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 map, IConfigurationElement element) { 217 String contextTypeId= element.getAttribute(CONTEXT_TYPE_ID); 218 if (contextExists(contextTypeId)) { 220 String id= element.getAttribute(ID); 221 if (isValidTemplateId(id)) { 222 223 String name= element.getAttribute(NAME); 224 if (name != null) { 225 226 String pattern= element.getChildren(PATTERN)[0].getValue(); 227 if (pattern != null) { 228 229 String desc= element.getAttribute(DESCRIPTION); 230 if (desc == null) 231 desc= ""; 233 String 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 id) { 251 return id != null && id.trim().length() != 0; } 253 254 258 protected void handleException(IOException x) { 259 EditorsPlugin.log(x); 260 } 261 } 262 263 | Popular Tags |