KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > registry > osgi > RegistryCommandProvider


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.core.internal.registry.osgi;
13
14 import org.eclipse.core.runtime.*;
15 import org.eclipse.osgi.framework.console.CommandInterpreter;
16 import org.eclipse.osgi.framework.console.CommandProvider;
17
18 public class RegistryCommandProvider implements CommandProvider {
19
20     private final static String JavaDoc helpText = "---Extension Registry Commands---\n" + //$NON-NLS-1$
21
"\tns [-v] [name] - display extension points in the namespace; add -v to display extensions\n" + //$NON-NLS-1$
22
"\tpt [-v] uniqueExtensionPointId - display the extension point and extensions; add -v to display config elements"; //$NON-NLS-1$
23

24     private static final String JavaDoc indent = " "; //$NON-NLS-1$
25

26     private boolean verbose = false; // is command run in a "verbose" mode?
27

28     public String JavaDoc getHelp() {
29         return helpText;
30     }
31
32     public void _ns(CommandInterpreter ci) throws Exception JavaDoc {
33         String JavaDoc namespace = getArgument(ci);
34         if (namespace == null) {
35             String JavaDoc[] namespaces = RegistryFactory.getRegistry().getNamespaces();
36             ci.println("Namespace(s):"); //$NON-NLS-1$
37
ci.println("-------------------"); //$NON-NLS-1$
38
for (int i = 0; i < namespaces.length; i++)
39                 ci.println(namespaces[i]);
40             return;
41         }
42
43         IExtensionRegistry registry = RegistryFactory.getRegistry();
44         IExtensionPoint[] extpts = registry.getExtensionPoints(namespace);
45         ci.println("Extension point(s):"); //$NON-NLS-1$
46
ci.println("-------------------"); //$NON-NLS-1$
47
for (int i = 0; i < extpts.length; i++)
48             displayExtensionPoint(extpts[i], ci);
49
50         if (verbose) {
51             ci.println("\nExtension(s):"); //$NON-NLS-1$
52
ci.println("-------------------"); //$NON-NLS-1$
53
IExtension[] exts = RegistryFactory.getRegistry().getExtensions(namespace);
54             for (int j = 0; j < exts.length; j++)
55                 displayExtension(exts[j], ci, true /*full*/);
56         }
57     }
58
59     public void _pt(CommandInterpreter ci) throws Exception JavaDoc {
60         String JavaDoc extensionPointId = getArgument(ci);
61         if (extensionPointId == null)
62             return;
63         IExtensionPoint extpt = RegistryFactory.getRegistry().getExtensionPoint(extensionPointId);
64         if (extpt == null)
65             return;
66         ci.print("Extension point: "); //$NON-NLS-1$
67
displayExtensionPoint(extpt, ci);
68         IExtension[] exts = extpt.getExtensions();
69         ci.println("\nExtension(s):"); //$NON-NLS-1$
70
ci.println("-------------------"); //$NON-NLS-1$
71
for (int i = 0; i < exts.length; i++) {
72             displayExtension(exts[i], ci, false /*short*/);
73             if (verbose) {
74                 IConfigurationElement[] ce = exts[i].getConfigurationElements();
75                 for (int j = 0; j < ce.length; j++)
76                     displayConfigElement(ci, ce[j], 1);
77                 ci.println();
78             }
79         }
80     }
81
82     // This method has a side effect of setting the verbose flag
83
private String JavaDoc getArgument(CommandInterpreter ci) {
84         String JavaDoc firstParm = ci.nextArgument();
85         if ("-v".equals(firstParm)) { //$NON-NLS-1$
86
verbose = true;
87             return ci.nextArgument();
88         }
89
90         verbose = false;
91         return firstParm;
92     }
93
94     private void displayExtensionPoint(IExtensionPoint extentionPoint, CommandInterpreter ci) {
95         if (extentionPoint == null)
96             return;
97         ci.println(extentionPoint.getUniqueIdentifier() + " [from " + extentionPoint.getContributor().getName() + ']'); //$NON-NLS-1$
98
}
99
100     private void displayExtension(IExtension extention, CommandInterpreter ci, boolean full) {
101         if (extention == null)
102             return;
103         if (full) {
104             ci.print("Id: " + extention.getUniqueIdentifier()); //$NON-NLS-1$
105
ci.print(" PointId: " + extention.getExtensionPointUniqueIdentifier()); //$NON-NLS-1$
106
ci.println(" [from " + extention.getContributor().getName() + "]"); //$NON-NLS-1$ //$NON-NLS-2$
107
} else {
108             ci.println(extention.getUniqueIdentifier() + " [from " + extention.getContributor().getName() + "]"); //$NON-NLS-1$ //$NON-NLS-2$
109
}
110     }
111
112     private void displayConfigElement(CommandInterpreter ci, IConfigurationElement ce, int level) throws Exception JavaDoc {
113         String JavaDoc spacing = spacing(ci, level);
114         ci.println(spacing + '<' + ce.getName() + '>');
115         String JavaDoc[] attrs = ce.getAttributeNames();
116         for (int k = 0; k < attrs.length; k++)
117             ci.println(indent + spacing + attrs[k] + " = " + ce.getAttribute(attrs[k])); //$NON-NLS-1$
118
String JavaDoc value = ce.getValue();
119         if (value != null)
120             ci.println(indent + spacing + value);
121         IConfigurationElement[] children = ce.getChildren();
122         for (int z = 0; z < children.length; z++)
123             displayConfigElement(ci, children[z], level + 1);
124         ci.println(spacing + "</" + ce.getName() + '>'); //$NON-NLS-1$
125
}
126
127     private String JavaDoc spacing(CommandInterpreter ci, int level) {
128         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
129         for (int i = 0; i < level; i++)
130             b.append(indent);
131         return b.toString();
132     }
133 }
134
Popular Tags