Example #1
0
 protected void printHeaders(Bundle bundle) throws Exception {
   String title = ShellUtil.getBundleName(bundle);
   System.out.println("\n" + title);
   System.out.println(ShellUtil.getUnderlineString(title));
   if (indent == 0) {
     Dictionary<String, String> dict = bundle.getHeaders();
     Enumeration<String> keys = dict.keys();
     while (keys.hasMoreElements()) {
       Object k = keys.nextElement();
       Object v = dict.get(k);
       System.out.println(k + " = " + ShellUtil.getValueString(v));
     }
   } else {
     System.out.println(generateFormattedOutput(bundle));
   }
 }
Example #2
0
  protected String generateFormattedOutput(Bundle bundle) {
    StringBuilder output = new StringBuilder();
    Map<String, Object> otherAttribs = new HashMap<String, Object>();
    Map<String, Object> bundleAttribs = new HashMap<String, Object>();
    Map<String, Object> serviceAttribs = new HashMap<String, Object>();
    Map<String, Object> packagesAttribs = new HashMap<String, Object>();
    Dictionary<String, String> dict = bundle.getHeaders();
    Enumeration<String> keys = dict.keys();

    // do an initial loop and separate the attributes in different groups
    while (keys.hasMoreElements()) {
      String k = (String) keys.nextElement();
      Object v = dict.get(k);
      if (k.startsWith(BUNDLE_PREFIX)) {
        // starts with Bundle-xxx
        bundleAttribs.put(k, v);
      } else if (k.endsWith(SERVICE_SUFFIX) || k.endsWith(CAPABILITY_SUFFIX)) {
        // ends with xxx-Service
        serviceAttribs.put(k, v);
      } else if (k.endsWith(PACKAGE_SUFFFIX)) {
        // ends with xxx-Package
        packagesAttribs.put(k, v);
      } else if (k.endsWith(REQUIRE_BUNDLE_ATTRIB)) {
        // require bundle statement
        packagesAttribs.put(k, v);
      } else {
        // the remaining attribs
        otherAttribs.put(k, v);
      }
    }

    // we will display the formatted result like this:
    // Bundle-Name (ID)
    // -----------------------
    // all other attributes
    //
    // all Bundle attributes
    //
    // all Service attributes
    //
    // all Package attributes
    Iterator<Map.Entry<String, Object>> it = otherAttribs.entrySet().iterator();
    while (it.hasNext()) {
      Map.Entry<String, Object> e = it.next();
      output.append(String.format("%s = %s\n", e.getKey(), ShellUtil.getValueString(e.getValue())));
    }
    if (otherAttribs.size() > 0) {
      output.append('\n');
    }

    it = bundleAttribs.entrySet().iterator();
    while (it.hasNext()) {
      Map.Entry<String, Object> e = it.next();
      output.append(String.format("%s = %s\n", e.getKey(), ShellUtil.getValueString(e.getValue())));
    }
    if (bundleAttribs.size() > 0) {
      output.append('\n');
    }

    it = serviceAttribs.entrySet().iterator();
    while (it.hasNext()) {
      Map.Entry<String, Object> e = it.next();
      output.append(e.getKey());
      output.append(" = \n");
      formatHeader(ShellUtil.getValueString(e.getValue()), null, output, indent);
      output.append("\n");
    }
    if (serviceAttribs.size() > 0) {
      output.append('\n');
    }

    Map<String, ClauseFormatter> formatters = new HashMap<String, ClauseFormatter>();
    formatters.put(
        REQUIRE_BUNDLE_ATTRIB,
        new ClauseFormatter() {
          public void pre(Clause clause, StringBuilder output) {
            boolean isSatisfied =
                checkBundle(clause.getName(), clause.getAttribute("bundle-version"));
            Ansi.ansi(output).fg(isSatisfied ? Ansi.Color.DEFAULT : Ansi.Color.RED).a("");
          }

          public void post(Clause clause, StringBuilder output) {
            Ansi.ansi(output).reset().a("");
          }
        });
    formatters.put(
        IMPORT_PACKAGES_ATTRIB,
        new ClauseFormatter() {
          public void pre(Clause clause, StringBuilder output) {
            boolean isSatisfied = checkPackage(clause.getName(), clause.getAttribute("version"));
            boolean isOptional = "optional".equals(clause.getDirective("resolution"));
            Ansi.ansi(output)
                .fg(isSatisfied ? Ansi.Color.DEFAULT : Ansi.Color.RED)
                .a(
                    isSatisfied || isOptional
                        ? Ansi.Attribute.INTENSITY_BOLD_OFF
                        : Ansi.Attribute.INTENSITY_BOLD)
                .a("");
          }

          public void post(Clause clause, StringBuilder output) {
            Ansi.ansi(output).reset().a("");
          }
        });

    it = packagesAttribs.entrySet().iterator();
    while (it.hasNext()) {
      Map.Entry<String, Object> e = it.next();
      output.append(e.getKey());
      output.append(" = \n");
      formatHeader(
          ShellUtil.getValueString(e.getValue()), formatters.get(e.getKey()), output, indent);
      output.append("\n");
    }
    if (packagesAttribs.size() > 0) {
      output.append('\n');
    }

    return output.toString();
  }