package org.eclipse.core.internal.registry.osgi;
import org.eclipse.core.runtime.*;
import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;
public class RegistryCommandProvider implements CommandProvider {
private final static String NEW_LINE = "\r\n";
private static final String indent = " ";
private boolean verbose = false;
@Override
public String getHelp() {
return getHelp(null);
}
private String getHelp(String commandName) {
boolean all = commandName == null;
StringBuilder sb = new StringBuilder();
if (all) {
sb.append("---Extension Registry Commands---");
sb.append(NEW_LINE);
}
if (all || "ns".equals(commandName)) {
sb.append("\tns [-v] [name] - display extension points in the namespace; add -v to display extensions");
sb.append(NEW_LINE);
}
if (all || "pt".equals(commandName)) {
sb.append("\tpt [-v] uniqueExtensionPointId - display the extension point and extensions; add -v to display config elements");
sb.append(NEW_LINE);
}
return sb.toString();
}
public void _ns(CommandInterpreter ci) throws Exception {
String namespace = getArgument(ci);
if (namespace == null) {
String[] namespaces = RegistryFactory.getRegistry().getNamespaces();
ci.println("Namespace(s):");
ci.println("-------------------");
for (String n : namespaces) {
ci.println(n);
}
return;
}
IExtensionRegistry registry = RegistryFactory.getRegistry();
IExtensionPoint[] extpts = registry.getExtensionPoints(namespace);
ci.println("Extension point(s):");
ci.println("-------------------");
for (IExtensionPoint extpt : extpts) {
displayExtensionPoint(extpt, ci);
}
if (verbose) {
ci.println("\nExtension(s):");
ci.println("-------------------");
IExtension[] exts = RegistryFactory.getRegistry().getExtensions(namespace);
for (IExtension ext : exts) {
displayExtension(ext, ci, true );
}
}
}
public void _pt(CommandInterpreter ci) throws Exception {
String extensionPointId = getArgument(ci);
if (extensionPointId == null)
return;
IExtensionPoint extpt = RegistryFactory.getRegistry().getExtensionPoint(extensionPointId);
if (extpt == null)
return;
ci.print("Extension point: ");
displayExtensionPoint(extpt, ci);
IExtension[] exts = extpt.getExtensions();
ci.println("\nExtension(s):");
ci.println("-------------------");
for (IExtension ext : exts) {
displayExtension(ext, ci, false );
if (verbose) {
IConfigurationElement[] ce = ext.getConfigurationElements();
for (IConfigurationElement ce1 : ce) {
displayConfigElement(ci, ce1, 1);
}
ci.println();
}
}
}
public Object _help(CommandInterpreter intp) {
String commandName = intp.nextArgument();
if (commandName == null) {
return Boolean.FALSE;
}
String help = getHelp(commandName);
if (help.length() > 0) {
return help;
}
return Boolean.FALSE;
}
private String getArgument(CommandInterpreter ci) {
String firstParm = ci.nextArgument();
if ("-v".equals(firstParm)) {
verbose = true;
return ci.nextArgument();
}
verbose = false;
return firstParm;
}
private void displayExtensionPoint(IExtensionPoint extentionPoint, CommandInterpreter ci) {
if (extentionPoint == null)
return;
ci.println(extentionPoint.getUniqueIdentifier() + " [from " + extentionPoint.getContributor().getName() + ']');
}
private void displayExtension(IExtension extention, CommandInterpreter ci, boolean full) {
if (extention == null)
return;
if (full) {
ci.print("Id: " + extention.getUniqueIdentifier());
ci.print(" PointId: " + extention.getExtensionPointUniqueIdentifier());
ci.println(" [from " + extention.getContributor().getName() + "]");
} else {
ci.println(extention.getUniqueIdentifier() + " [from " + extention.getContributor().getName() + "]");
}
}
private void displayConfigElement(CommandInterpreter ci, IConfigurationElement ce, int level) throws Exception {
String spacing = spacing(ci, level);
ci.println(spacing + '<' + ce.getName() + '>');
String[] attrs = ce.getAttributeNames();
for (String attr : attrs) {
ci.println(indent + spacing + attr + " = " + ce.getAttribute(attr));
}
String value = ce.getValue();
if (value != null)
ci.println(indent + spacing + value);
IConfigurationElement[] children = ce.getChildren();
for (IConfigurationElement child : children) {
displayConfigElement(ci, child, level + 1);
}
ci.println(spacing + "</" + ce.getName() + '>');
}
private String spacing(CommandInterpreter ci, int level) {
StringBuilder b = new StringBuilder();
for (int i = 0; i < level; i++)
b.append(indent);
return b.toString();
}
}