Search in sources :

Example 1 with AnsiPrintingWikiVisitor

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;
}
Also used : PrintStream(java.io.PrintStream) InputStreamReader(java.io.InputStreamReader) Bundle(org.osgi.framework.Bundle) AnsiPrintingWikiVisitor(org.apache.karaf.shell.impl.console.commands.help.wikidoc.AnsiPrintingWikiVisitor) WikiVisitor(org.apache.karaf.shell.impl.console.commands.help.wikidoc.WikiVisitor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WikiParser(org.apache.karaf.shell.impl.console.commands.help.wikidoc.WikiParser) Terminal(org.apache.karaf.shell.api.console.Terminal) URL(java.net.URL) AnsiPrintingWikiVisitor(org.apache.karaf.shell.impl.console.commands.help.wikidoc.AnsiPrintingWikiVisitor) BufferedReader(java.io.BufferedReader) BundleContext(org.osgi.framework.BundleContext)

Example 2 with AnsiPrintingWikiVisitor

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;
}
Also used : PrintStream(java.io.PrintStream) InputStreamReader(java.io.InputStreamReader) Bundle(org.osgi.framework.Bundle) AnsiPrintingWikiVisitor(org.apache.karaf.shell.impl.console.commands.help.wikidoc.AnsiPrintingWikiVisitor) WikiVisitor(org.apache.karaf.shell.impl.console.commands.help.wikidoc.WikiVisitor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) WikiParser(org.apache.karaf.shell.impl.console.commands.help.wikidoc.WikiParser) Terminal(org.apache.karaf.shell.api.console.Terminal) URL(java.net.URL) AnsiPrintingWikiVisitor(org.apache.karaf.shell.impl.console.commands.help.wikidoc.AnsiPrintingWikiVisitor) ActionCommand(org.apache.karaf.shell.impl.action.command.ActionCommand) Command(org.apache.karaf.shell.api.console.Command) ActionCommand(org.apache.karaf.shell.impl.action.command.ActionCommand) BufferedReader(java.io.BufferedReader) HashSet(java.util.HashSet)

Aggregations

BufferedReader (java.io.BufferedReader)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStreamReader (java.io.InputStreamReader)2 PrintStream (java.io.PrintStream)2 URL (java.net.URL)2 Terminal (org.apache.karaf.shell.api.console.Terminal)2 AnsiPrintingWikiVisitor (org.apache.karaf.shell.impl.console.commands.help.wikidoc.AnsiPrintingWikiVisitor)2 WikiParser (org.apache.karaf.shell.impl.console.commands.help.wikidoc.WikiParser)2 WikiVisitor (org.apache.karaf.shell.impl.console.commands.help.wikidoc.WikiVisitor)2 Bundle (org.osgi.framework.Bundle)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Command (org.apache.karaf.shell.api.console.Command)1 ActionCommand (org.apache.karaf.shell.impl.action.command.ActionCommand)1 BundleContext (org.osgi.framework.BundleContext)1