use of org.apache.karaf.shell.impl.console.commands.help.wikidoc.AnsiPrintingWikiVisitor in project karaf by apache.
the class BundleHelpProvider method getHelp.
@Override
public String getHelp(Session session, String path) {
if (path.indexOf('|') > 0) {
if (path.startsWith("bundle|")) {
path = path.substring("bundle|".length());
} else {
return null;
}
}
if (path.matches("[0-9]*")) {
long id = Long.parseLong(path);
BundleContext bundleContext = FrameworkUtil.getBundle(getClass()).getBundleContext();
Bundle bundle = bundleContext.getBundle(id);
if (bundle != null) {
String title = ShellUtil.getBundleName(bundle);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
ps.println("\n" + title);
ps.println(ShellUtil.getUnderlineString(title));
URL bundleInfo = bundle.getEntry("OSGI-INF/bundle.info");
if (bundleInfo != null) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(bundleInfo.openStream()))) {
int maxSize = 80;
Terminal terminal = session.getTerminal();
if (terminal != null) {
maxSize = terminal.getWidth();
}
WikiVisitor visitor = new AnsiPrintingWikiVisitor(ps, maxSize);
WikiParser parser = new WikiParser(visitor);
parser.parse(reader);
} catch (Exception e) {
// ignore
}
}
ps.close();
return baos.toString();
}
}
return null;
}
use of org.apache.karaf.shell.impl.console.commands.help.wikidoc.AnsiPrintingWikiVisitor in project karaf by apache.
the class ShellHelpProvider method getHelp.
public String getHelp(Session session, String path) {
if (path.indexOf('|') > 0) {
if (path.startsWith("shell|")) {
path = path.substring("shell|".length());
} else {
return null;
}
}
// Retrieve matching commands
Set<Command> commands = getCommands(session, path);
// Compute the scopes and matching bundles
Set<Bundle> bundles = new HashSet<>();
Set<String> scopes = new HashSet<>();
for (Command command : commands) {
if (command instanceof ActionCommand) {
Class<? extends Action> action = ((ActionCommand) command).getActionClass();
bundles.add(FrameworkUtil.getBundle(action));
}
scopes.add(command.getScope());
}
// use that one instead
if (scopes.size() == 1 && bundles.size() == 1 && path.equals(scopes.iterator().next())) {
Bundle bundle = bundles.iterator().next();
URL resource = bundle.getResource("OSGI-INF/shell-" + path + ".info");
if (resource != null) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.openStream()))) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
int maxSize = 80;
Terminal terminal = session.getTerminal();
if (terminal != null) {
maxSize = terminal.getWidth();
}
WikiVisitor visitor = new AnsiPrintingWikiVisitor(ps, maxSize);
WikiParser parser = new WikiParser(visitor);
parser.parse(reader);
return baos.toString();
} catch (IOException e) {
// ignore
}
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
printShellHelp(session, new PrintStream(baos), path);
return baos.toString();
}
return null;
}
Aggregations