package org.eclipse.core.internal.registry;
import java.util.Hashtable;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.spi.RegistryContributor;
import org.eclipse.osgi.util.NLS;
public class ConfigurationElement extends RegistryObject {
static final ConfigurationElement[] EMPTY_ARRAY = new ConfigurationElement[0];
int parentId;
byte parentType;
protected String[] propertiesAndValue;
private String name;
private String contributorId;
protected ConfigurationElement(ExtensionRegistry registry, boolean persist) {
super(registry, persist);
}
protected ConfigurationElement(int self, String contributorId, String name, String[] propertiesAndValue, int[] children, int extraDataOffset, int parent, byte parentType, ExtensionRegistry registry, boolean persist) {
super(registry, persist);
setObjectId(self);
this.contributorId = contributorId;
this.name = name;
this.propertiesAndValue = propertiesAndValue;
setRawChildren(children);
setExtraDataOffset(extraDataOffset);
parentId = parent;
this.parentType = parentType;
}
void throwException(String message, Throwable exception) throws CoreException {
throw new CoreException(new Status(IStatus.ERROR, RegistryMessages.OWNER_NAME, IRegistryConstants.PLUGIN_ERROR, message, exception));
}
protected String getValue() {
return getValueAsIs();
}
String getValueAsIs() {
if (propertiesAndValue.length != 0 && propertiesAndValue.length % 2 == 1)
return propertiesAndValue[propertiesAndValue.length - 1];
return null;
}
public String getAttribute(String attrName) {
return getAttributeAsIs(attrName);
}
String getAttributeAsIs(String attrName) {
if (propertiesAndValue.length <= 1)
return null;
int size = propertiesAndValue.length - (propertiesAndValue.length % 2);
for (int i = 0; i < size; i += 2) {
if (propertiesAndValue[i].equals(attrName))
return propertiesAndValue[i + 1];
}
return null;
}
protected String[] getAttributeNames() {
if (propertiesAndValue.length <= 1)
return RegistryObjectManager.EMPTY_STRING_ARRAY;
int size = propertiesAndValue.length / 2;
String[] result = new String[size];
for (int i = 0; i < size; i++) {
result[i] = propertiesAndValue[i * 2];
}
return result;
}
void setProperties(String[] value) {
propertiesAndValue = value;
}
protected String[] getPropertiesAndValue() {
return propertiesAndValue;
}
void setValue(String value) {
if (propertiesAndValue.length == 0) {
propertiesAndValue = new String[] {value};
return;
}
if (propertiesAndValue.length % 2 == 1) {
propertiesAndValue[propertiesAndValue.length - 1] = value;
return;
}
String[] newPropertiesAndValue = new String[propertiesAndValue.length + 1];
System.arraycopy(propertiesAndValue, 0, newPropertiesAndValue, 0, propertiesAndValue.length);
newPropertiesAndValue[propertiesAndValue.length] = value;
propertiesAndValue = newPropertiesAndValue;
}
void setContributorId(String id) {
this.contributorId = id;
}
protected String getContributorId() {
return contributorId;
}
public ConfigurationElement[] getChildren(String childrenName) {
if (getRawChildren().length == 0)
return ConfigurationElement.EMPTY_ARRAY;
ConfigurationElement[] result = new ConfigurationElement[1];
int idx = 0;
RegistryObjectManager objectManager = registry.getObjectManager();
for (int i = 0; i < children.length; i++) {
ConfigurationElement toTest = (ConfigurationElement) objectManager.getObject(children[i], noExtraData() ? RegistryObjectManager.CONFIGURATION_ELEMENT : RegistryObjectManager.THIRDLEVEL_CONFIGURATION_ELEMENT);
if (toTest.name.equals(childrenName)) {
if (idx != 0) {
ConfigurationElement[] copy = new ConfigurationElement[result.length + 1];
System.arraycopy(result, 0, copy, 0, result.length);
result = copy;
}
result[idx++] = toTest;
}
}
if (idx == 0)
result = ConfigurationElement.EMPTY_ARRAY;
return result;
}
void setParentId(int objectId) {
parentId = objectId;
}
protected String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
void setParentType(byte type) {
parentType = type;
}
public IContributor getContributor() {
return registry.getObjectManager().getContributor(contributorId);
}
protected Object createExecutableExtension(String attributeName) throws CoreException {
String prop = null;
String executable;
String contributorName = null;
String className = null;
Object initData = null;
int i;
if (attributeName != null)
prop = getAttribute(attributeName);
else {
prop = getValue();
if (prop != null) {
prop = prop.trim();
if (prop.equals(""))
prop = null;
}
}
if (prop == null) {
ConfigurationElement[] exec;
ConfigurationElement[] parms;
ConfigurationElement element;
Hashtable<String, String> initParms;
String pname;
exec = getChildren(attributeName);
if (exec.length != 0) {
element = exec[0];
contributorName = element.getAttribute("plugin");
className = element.getAttribute("class");
parms = element.getChildren("parameter");
if (parms.length != 0) {
initParms = new Hashtable<>(parms.length + 1);
for (i = 0; i < parms.length; i++) {
pname = parms[i].getAttribute("name");
if (pname != null)
initParms.put(pname, parms[i].getAttribute("value"));
}
if (!initParms.isEmpty())
initData = initParms;
}
} else {
throwException(NLS.bind(RegistryMessages.exExt_extDefNotFound, attributeName), null);
}
} else {
i = prop.indexOf(':');
if (i != -1) {
executable = prop.substring(0, i).trim();
initData = prop.substring(i + 1).trim();
} else
executable = prop;
i = executable.indexOf('/');
if (i != -1) {
contributorName = executable.substring(0, i).trim();
className = executable.substring(i + 1).trim();
} else
className = executable;
}
RegistryContributor defaultContributor = registry.getObjectManager().getContributor(contributorId);
Object result = registry.createExecutableExtension(defaultContributor, className, contributorName);
try {
ConfigurationElementHandle confElementHandle = new ConfigurationElementHandle(registry.getObjectManager(), getObjectId());
if (result instanceof IExecutableExtension)
((IExecutableExtension) result).setInitializationData(confElementHandle, attributeName, initData);
} catch (CoreException ce) {
throw ce;
} catch (Exception te) {
throwException(NLS.bind(RegistryMessages.plugin_initObjectError, getContributor().getName(), className), te);
}
if (result instanceof IExecutableExtensionFactory)
result = ((IExecutableExtensionFactory) result).create();
return result;
}
String getAttribute(String attrName, String locale) {
registry.logMultiLangError();
return getAttribute(attrName);
}
String getValue(String locale) {
registry.logMultiLangError();
return getValue();
}
}