1 11 package org.eclipse.ui.internal.texteditor.rulers; 12 13 import java.net.URL ; 14 15 import org.osgi.framework.Bundle; 16 17 import com.ibm.icu.text.MessageFormat; 18 19 import org.eclipse.core.runtime.Assert; 20 import org.eclipse.core.runtime.FileLocator; 21 import org.eclipse.core.runtime.IConfigurationElement; 22 import org.eclipse.core.runtime.IExtension; 23 import org.eclipse.core.runtime.ILog; 24 import org.eclipse.core.runtime.IStatus; 25 import org.eclipse.core.runtime.InvalidRegistryObjectException; 26 import org.eclipse.core.runtime.Path; 27 import org.eclipse.core.runtime.Platform; 28 import org.eclipse.core.runtime.Status; 29 30 import org.eclipse.ui.internal.texteditor.TextEditorPlugin; 31 32 public final class ExtensionPointHelper { 33 34 private final IConfigurationElement fElement; 35 private final String fName; 36 private final ILog fLog; 37 38 public ExtensionPointHelper(IConfigurationElement element, ILog log) throws InvalidRegistryObjectException { 39 Assert.isLegal(element != null); 40 Assert.isLegal(log != null); 41 fLog= log; 42 fElement= element; 43 fName= element.getName(); 44 } 46 47 public String getDefaultAttribute(String attribute, String dflt) throws InvalidRegistryObjectException { 48 String value= fElement.getAttribute(attribute); 49 return value == null ? dflt : value; 50 } 51 52 public String getNonNullAttribute(String attribute) throws InvalidRegistryObjectException { 53 String value= fElement.getAttribute(attribute); 54 if (value == null) 55 fail(MessageFormat.format(RulerColumnMessages.ExtensionPointHelper_missing_attribute_msg, new Object [] {fName, attribute})); 56 return value; 57 } 58 59 public float getDefaultAttribute(String attribute, float dflt) { 60 String value= getDefaultAttribute(attribute, null); 61 if (value == null) 62 return dflt; 63 64 try { 65 return Float.valueOf(value).floatValue(); 66 } catch (NumberFormatException x) { 67 fail(MessageFormat.format(RulerColumnMessages.ExtensionPointHelper_invalid_number_attribute_msg, new Object [] {attribute, fName})); 68 return dflt; 69 } 70 } 71 72 public boolean getDefaultAttribute(String attribute, boolean dflt) { 73 String value= getDefaultAttribute(attribute, null); 74 if (value == null) 75 return dflt; 76 77 try { 78 return Boolean.valueOf(value).booleanValue(); 79 } catch (NumberFormatException x) { 80 fail(MessageFormat.format(RulerColumnMessages.ExtensionPointHelper_invalid_number_attribute_msg, new Object [] {fName, attribute})); 81 return dflt; 82 } 83 } 84 85 public void fail(String message) throws InvalidRegistryObjectException { 86 String id= findId(fElement); 87 String extensionPointId= fElement.getDeclaringExtension().getExtensionPointUniqueIdentifier(); 88 Object [] args= { fElement.getContributor().getName(), id, extensionPointId }; 89 String blame= MessageFormat.format(RulerColumnMessages.ExtensionPointHelper_invalid_contribution_msg, args); 90 91 IStatus status= new Status(IStatus.WARNING, TextEditorPlugin.PLUGIN_ID, IStatus.OK, blame + message, null); 92 fLog.log(status); 93 throw new InvalidRegistryObjectException(); 94 } 95 96 public static String findId(IConfigurationElement element) { 97 String id= null; 98 while (element != null && id == null) { 99 id= element.getAttribute("id"); if (id != null) 101 break; 102 Object parent= element.getParent(); 103 if (parent instanceof IExtension) { 104 id= ((IExtension) parent).getUniqueIdentifier(); 105 break; 106 } else if (parent instanceof IConfigurationElement) { 107 element= (IConfigurationElement) parent; 108 } else { 109 break; 110 } 111 } 112 return id == null ? "<unknown>" : id; } 114 115 public URL getDefaultResourceURL(String attribute, URL dflt) { 116 String value= getDefaultAttribute(attribute, null); 117 if (value == null) 118 return dflt; 119 120 Bundle bundle= getBundle(); 121 if (bundle == null) 122 return dflt; 123 124 Path path= new Path(value); 125 return FileLocator.find(bundle, path, null); 126 } 127 128 private Bundle getBundle() { 129 String namespace= fElement.getDeclaringExtension().getContributor().getName(); 130 Bundle bundle= Platform.getBundle(namespace); 131 return bundle; 132 } 133 } 134 | Popular Tags |