예제 #1
0
    /**
     * Returns the content of a JDOM Element detached from it.
     *
     * @param elt the element to get the content from.
     * @return a (possibly empty) list of JDOM nodes, detached from their parent.
     */
    private List getDetachedContent(Element elt) {
      List content = elt.getContent();
      List nodes = new ArrayList(content.size());

      while (content.size() != 0) {
        Object o = content.remove(0);
        nodes.add(o);
      }
      return (nodes);
    }
예제 #2
0
  /**
   * Returns the result of an XSL Transformation as a list of JDOM nodes.
   *
   * <p>If the result of the transformation is a JDOM document, this method converts it into a list
   * of JDOM nodes; any subsequent call to {@link #getDocument} will return <code>null</code>.
   *
   * @return the transformation result as a (possibly empty) list of JDOM nodes (Elements, Texts,
   *     Comments, PIs...).
   */
  public List getResult() {
    List nodes = Collections.EMPTY_LIST;

    // Retrieve result from the document builder if not set.
    this.retrieveResult();

    if (result instanceof List) {
      nodes = (List) result;
    } else {
      if ((result instanceof Document) && (queried == false)) {
        List content = ((Document) result).getContent();
        nodes = new ArrayList(content.size());

        while (content.size() != 0) {
          Object o = content.remove(0);
          nodes.add(o);
        }
        result = nodes;
      }
    }
    queried = true;

    return (nodes);
  }
예제 #3
0
  /** Receive notification of the end of an element. */
  @Override
  public void endElement(String uri, String l, String q) {
    /*
     * 1. If current element is a String, update its value from the string buffer.
     * 2. Add the element to parent.
     */
    ElementInfo element = _stack.remove(_stack.size() - 1);
    _logger.fine("endElement " + element);
    if (element.type == null) {
      _logger.warning("Element " + element.name + " not created ");
      return;
    } else if (_chars.length() > 0) {
      try {
        injectProperty(element.data, String.class, _chars.toString(), null, null);
      } catch (Exception x) {
        if (!_lenient) {
          throw new BeanAssemblyException(
              "Failed to set characters to object " + element.type.getName(), x);
        } else {
          _logger.warning("Failed to set characters to parent " + element.data);
        }
      }
    }
    _chars.setLength(0);
    _logger.fine(
        "<<ElementInfo: "
            + element.type.getName()
            + " in "
            + element
            + "\n    @as is "
            + element.inst.get("@as")
            + "\n    @id is "
            + element.inst.get("@id"));

    if (List.class.isAssignableFrom(element.data.getClass()) && element.name.endsWith("...")) {
      List<?> list = (List<?>) element.data;
      Object array = Array.newInstance(element.type, list.size());
      for (int i = 0; i < list.size(); ++i) {
        Array.set(array, i, list.get(i));
      }
      element.data = array;
    }

    String id = element.inst.get("@id");
    if (id != null) {
      // locally stored object - not added to the parent
      _local.put(id, element);
    } else if (!_stack.isEmpty()) {
      // inject into the parent as a property
      ElementInfo parent = _stack.get(_stack.size() - 1);
      _logger.fine("Parent is " + parent.data.getClass().getName());
      try {
        String as = element.inst.get("@as");
        if (as != null) {
          injectProperty(
              parent.data,
              element.type,
              element.data,
              Strings.toCamelCase(as, '-', false),
              element.args.complete());
        } else {
          injectProperty(parent.data, element.type, element.data, null, element.args.complete());
        }
      } catch (Exception x) {
        if (!_lenient) {
          throw new BeanAssemblyException(
              "Failed to set value " + element.data + " to parent " + parent.data, x);
        } else {
          _logger.log(
              Level.WARNING,
              "Failed to set value " + element.data + " to parent " + parent.data,
              x);
        }
      }
    }
    _top = element.data;
  }