Exemplo n.º 1
0
  /**
   * Private worker method to attempt to use org.apache.env.Which.
   *
   * @param myContext an <code>ExpressionContext</code> passed in by the extension mechanism. This
   *     must be an XPathContext.
   * @param factoryDocument providing createElement services, etc.
   * @return a Node with environment info; null if any error
   */
  private static Node checkEnvironmentUsingWhich(
      ExpressionContext myContext, Document factoryDocument) {
    final String WHICH_CLASSNAME = "org.apache.env.Which";
    final String WHICH_METHODNAME = "which";
    final Class WHICH_METHOD_ARGS[] = {
      java.util.Hashtable.class, java.lang.String.class, java.lang.String.class
    };
    try {
      // Use reflection to try to find xml-commons utility 'Which'
      Class clazz =
          ObjectFactory.findProviderClass(WHICH_CLASSNAME, ObjectFactory.findClassLoader(), true);
      if (null == clazz) return null;

      // Fully qualify names since this is the only method they're used in
      java.lang.reflect.Method method = clazz.getMethod(WHICH_METHODNAME, WHICH_METHOD_ARGS);
      Hashtable report = new Hashtable();

      // Call the method with our Hashtable, common options, and ignore return value
      Object[] methodArgs = {report, "XmlCommons;Xalan;Xerces;Crimson;Ant", ""};
      Object returnValue = method.invoke(null, methodArgs);

      // Create a parent to hold the report and append hash to it
      Node resultNode = factoryDocument.createElement("checkEnvironmentExtension");
      org.apache.xml.utils.Hashtree2Node.appendHashToNode(
          report, "whichReport", resultNode, factoryDocument);

      return resultNode;
    } catch (Throwable t) {
      // Simply return null; no need to report error
      return null;
    }
  }
  /**
   * Convert a Hashtable into a Node tree.
   *
   * <p>The hash may have either Hashtables as values (in which case we recurse) or other values, in
   * which case we print them as &lt;item> elements, with a 'key' attribute with the value of the
   * key, and the element contents as the value.
   *
   * <p>If args are null we simply return without doing anything. If we encounter an error, we will
   * attempt to add an 'ERROR' Element with exception info; if that doesn't work we simply return
   * without doing anything else byt printStackTrace().
   *
   * @param hash to get info from (may have sub-hashtables)
   * @param name to use as parent element for appended node futurework could have namespace and
   *     prefix as well
   * @param container Node to append our report to
   * @param factory Document providing createElement, etc. services
   */
  public static void appendHashToNode(
      Hashtable hash, String name, Node container, Document factory) {
    // Required arguments must not be null
    if ((null == container) || (null == factory) || (null == hash)) {
      return;
    }

    // name we will provide a default value for
    String elemName = null;
    if ((null == name) || ("".equals(name))) elemName = "appendHashToNode";
    else elemName = name;

    try {
      Element hashNode = factory.createElement(elemName);
      container.appendChild(hashNode);

      Enumeration keys = hash.keys();
      List v = new ArrayList();

      while (keys.hasMoreElements()) {
        Object key = keys.nextElement();
        String keyStr = key.toString();
        Object item = hash.get(key);

        if (item instanceof Hashtable) {
          // Ensure a pre-order traversal; add this hashes
          //  items before recursing to child hashes
          // Save name and hash in two steps
          v.add(keyStr);
          v.add((Hashtable) item);
        } else {
          try {
            // Add item to node
            Element node = factory.createElement("item");
            node.setAttribute("key", keyStr);
            node.appendChild(factory.createTextNode((String) item));
            hashNode.appendChild(node);
          } catch (Exception e) {
            Element node = factory.createElement("item");
            node.setAttribute("key", keyStr);
            node.appendChild(
                factory.createTextNode("ERROR: Reading " + key + " threw: " + e.toString()));
            hashNode.appendChild(node);
          }
        }
      }

      // Now go back and do the saved hashes
      Iterator it = v.iterator();
      while (it.hasNext()) {
        // Retrieve name and hash in two steps
        String n = (String) it.next();
        Hashtable h = (Hashtable) it.next();

        appendHashToNode(h, n, hashNode, factory);
      }
    } catch (Exception e2) {
      // Ooops, just bail (suggestions for a safe thing
      //  to do in this case appreciated)
      e2.printStackTrace();
    }
  }