1 11 package org.eclipse.core.internal.registry; 12 13 import java.util.Hashtable ; 14 import org.eclipse.core.runtime.*; 15 import org.eclipse.core.runtime.spi.RegistryContributor; 16 import org.eclipse.osgi.util.NLS; 17 18 22 public class ConfigurationElement extends RegistryObject { 23 static final ConfigurationElement[] EMPTY_ARRAY = new ConfigurationElement[0]; 24 25 int parentId; 27 byte parentType; 29 private String [] propertiesAndValue; 35 36 private String name; 38 39 private String contributorId; 43 44 protected ConfigurationElement(ExtensionRegistry registry, boolean persist) { 45 super(registry, persist); 46 } 47 48 protected ConfigurationElement(int self, String contributorId, String name, String [] propertiesAndValue, int[] children, int extraDataOffset, int parent, byte parentType, ExtensionRegistry registry, boolean persist) { 49 super(registry, persist); 50 51 setObjectId(self); 52 this.contributorId = contributorId; 53 this.name = name; 54 this.propertiesAndValue = propertiesAndValue; 55 setRawChildren(children); 56 setExtraDataOffset(extraDataOffset); 57 parentId = parent; 58 this.parentType = parentType; 59 } 60 61 void throwException(String message, Throwable exception) throws CoreException { 62 throw new CoreException(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, IRegistryConstants.PLUGIN_ERROR, message, exception)); 63 } 64 65 protected String getValue() { 66 return getValueAsIs(); 67 } 68 69 String getValueAsIs() { 70 if (propertiesAndValue.length != 0 && propertiesAndValue.length % 2 == 1) 71 return propertiesAndValue[propertiesAndValue.length - 1]; 72 return null; 73 } 74 75 public String getAttribute(String attrName) { 76 return getAttributeAsIs(attrName); 77 } 78 79 String getAttributeAsIs(String attrName) { 80 if (propertiesAndValue.length <= 1) 81 return null; 82 int size = propertiesAndValue.length - (propertiesAndValue.length % 2); 83 for (int i = 0; i < size; i += 2) { 84 if (propertiesAndValue[i].equals(attrName)) 85 return propertiesAndValue[i + 1]; 86 } 87 return null; 88 } 89 90 protected String [] getAttributeNames() { 91 if (propertiesAndValue.length <= 1) 92 return RegistryObjectManager.EMPTY_STRING_ARRAY; 93 94 int size = propertiesAndValue.length / 2; 95 String [] result = new String [size]; 96 for (int i = 0; i < size; i++) { 97 result[i] = propertiesAndValue[i * 2]; 98 } 99 return result; 100 } 101 102 void setProperties(String [] value) { 103 propertiesAndValue = value; 104 } 105 106 protected String [] getPropertiesAndValue() { 107 return propertiesAndValue; 108 } 109 110 void setValue(String value) { 111 if (propertiesAndValue.length == 0) { 112 propertiesAndValue = new String [] {value}; 113 return; 114 } 115 if (propertiesAndValue.length % 2 == 1) { 116 propertiesAndValue[propertiesAndValue.length - 1] = value; 117 return; 118 } 119 String [] newPropertiesAndValue = new String [propertiesAndValue.length + 1]; 120 System.arraycopy(propertiesAndValue, 0, newPropertiesAndValue, 0, propertiesAndValue.length); 121 newPropertiesAndValue[propertiesAndValue.length] = value; 122 propertiesAndValue = newPropertiesAndValue; 123 } 124 125 void setContributorId(String id) { 126 this.contributorId = id; 127 } 128 129 protected String getContributorId() { 130 return contributorId; 131 } 132 133 public ConfigurationElement[] getChildren(String childrenName) { 134 if (getRawChildren().length == 0) 135 return ConfigurationElement.EMPTY_ARRAY; 136 137 ConfigurationElement[] result = new ConfigurationElement[1]; int idx = 0; 139 RegistryObjectManager objectManager = registry.getObjectManager(); 140 for (int i = 0; i < children.length; i++) { 141 ConfigurationElement toTest = (ConfigurationElement) objectManager.getObject(children[i], noExtraData() ? RegistryObjectManager.CONFIGURATION_ELEMENT : RegistryObjectManager.THIRDLEVEL_CONFIGURATION_ELEMENT); 142 if (toTest.name.equals(childrenName)) { 143 if (idx != 0) { 144 ConfigurationElement[] copy = new ConfigurationElement[result.length + 1]; 145 System.arraycopy(result, 0, copy, 0, result.length); 146 result = copy; 147 } 148 result[idx++] = toTest; 149 } 150 } 151 if (idx == 0) 152 result = ConfigurationElement.EMPTY_ARRAY; 153 return result; 154 } 155 156 void setParentId(int objectId) { 157 parentId = objectId; 158 } 159 160 protected String getName() { 161 return name; 162 } 163 164 void setName(String name) { 165 this.name = name; 166 } 167 168 void setParentType(byte type) { 169 parentType = type; 170 } 171 172 public IContributor getContributor() { 173 return registry.getObjectManager().getContributor(contributorId); 174 } 175 176 protected Object createExecutableExtension(String attributeName) throws CoreException { 177 String prop = null; 178 String executable; 179 String contributorName = null; 180 String className = null; 181 Object initData = null; 182 int i; 183 184 if (attributeName != null) 185 prop = getAttribute(attributeName); 186 else { 187 prop = getValue(); 189 if (prop != null) { 190 prop = prop.trim(); 191 if (prop.equals("")) prop = null; 193 } 194 } 195 196 if (prop == null) { 197 ConfigurationElement[] exec; 199 ConfigurationElement[] parms; 200 ConfigurationElement element; 201 Hashtable initParms; 202 String pname; 203 204 exec = getChildren(attributeName); 205 if (exec.length != 0) { 206 element = exec[0]; contributorName = element.getAttribute("plugin"); className = element.getAttribute("class"); parms = element.getChildren("parameter"); if (parms.length != 0) { 211 initParms = new Hashtable (parms.length + 1); 212 for (i = 0; i < parms.length; i++) { 213 pname = parms[i].getAttribute("name"); if (pname != null) 215 initParms.put(pname, parms[i].getAttribute("value")); } 217 if (!initParms.isEmpty()) 218 initData = initParms; 219 } 220 } else { 221 throwException(NLS.bind(RegistryMessages.exExt_extDefNotFound, attributeName), null); 223 } 224 } else { 225 i = prop.indexOf(':'); 227 if (i != -1) { 228 executable = prop.substring(0, i).trim(); 229 initData = prop.substring(i + 1).trim(); 230 } else 231 executable = prop; 232 233 i = executable.indexOf('/'); 234 if (i != -1) { 235 contributorName = executable.substring(0, i).trim(); 236 className = executable.substring(i + 1).trim(); 237 } else 238 className = executable; 239 } 240 241 RegistryContributor defaultContributor = registry.getObjectManager().getContributor(contributorId); 243 Object result = registry.createExecutableExtension(defaultContributor, className, contributorName); 244 245 try { 248 ConfigurationElementHandle confElementHandle = new ConfigurationElementHandle(registry.getObjectManager(), getObjectId()); 250 if (result instanceof IExecutableExtension) 251 ((IExecutableExtension) result).setInitializationData(confElementHandle, attributeName, initData); 252 } catch (CoreException ce) { 253 throw ce; 255 } catch (Exception te) { 256 throwException(NLS.bind(RegistryMessages.plugin_initObjectError, getContributor().getName(), className), te); 258 } 259 260 if (result instanceof IExecutableExtensionFactory) 262 result = ((IExecutableExtensionFactory) result).create(); 263 264 return result; 265 } 266 267 } 268 | Popular Tags |