private static <T> T getParamXPath(
     final Class<T> pClass, final String pXpath, final CompactFragment pBody) throws XmlException {
   // TODO Avoid JAXB where possible, use XMLDeserializer instead
   final boolean string = CharSequence.class.isAssignableFrom(pClass);
   Node match;
   DocumentFragment fragment =
       DomUtil.childrenToDocumentFragment(XMLFragmentStreamReader.from(pBody));
   for (Node n = fragment.getFirstChild(); n != null; n = n.getNextSibling()) {
     match = xpathMatch(n, pXpath);
     if (match != null) {
       if (!string) {
         XmlDeserializer deserializer = pClass.getAnnotation(XmlDeserializer.class);
         if (deserializer != null) {
           try {
             XmlDeserializerFactory<?> factory = deserializer.value().newInstance();
             factory.deserialize(XmlStreaming.newReader(new DOMSource(n)));
           } catch (InstantiationException | IllegalAccessException e) {
             throw new RuntimeException(e);
           }
         } else {
           return JAXB.unmarshal(new DOMSource(match), pClass);
         }
       } else {
         return pClass.cast(nodeToString(match));
       }
     }
   }
   return null;
 }
 private static Object getBody(final Class<?> pClass, final HttpMessage pMessage)
     throws XmlException {
   CompactFragment body = pMessage.getBody();
   if (body != null) {
     return DomUtil.childrenToDocumentFragment(XMLFragmentStreamReader.from(body));
   } else {
     return getAttachment(pClass, null, pMessage);
   }
 }
Esempio n. 3
0
  /**
   * Returns a position info representing the dimensions of all visible child elements of the given
   * panel (excluding elements with position:absolute). If the panel has no visible child elements,
   * it's outer dimensions are returned.
   *
   * <p>
   *
   * @param panel the panel
   * @param levels the levels to traverse down the DOM tree
   * @param includeSelf <code>true</code> to include the outer dimensions of the given panel
   * @return the position info
   */
  public static PositionBean getInnerDimensions(Element panel, int levels, boolean includeSelf) {

    boolean first = true;
    int top = 0;
    int left = 0;
    int bottom = 0;
    int right = 0;
    // if overflow is set to hidden, use the outer dimensions
    if (!Overflow.HIDDEN.getCssName().equals(DomUtil.getCurrentStyle(panel, Style.overflow))) {
      if (!includeSelf) {
        // check for any text content
        NodeList<Node> children = panel.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
          if ((children.getItem(i).getNodeType() == Node.TEXT_NODE)
              && (children.getItem(i).getNodeValue().trim().length() > 0)) {
            includeSelf = true;
            break;
          }
        }
      }
      if (includeSelf) {
        top = panel.getAbsoluteTop();
        left = panel.getAbsoluteLeft();
        bottom = top + panel.getOffsetHeight();
        right = left + panel.getOffsetWidth();
        first = false;
      }
      Element child = panel.getFirstChildElement();
      while (child != null) {
        String tagName = child.getTagName();
        if (tagName.equalsIgnoreCase("br")
            || tagName.equalsIgnoreCase("tr")
            || tagName.equalsIgnoreCase("thead")
            || tagName.equalsIgnoreCase("tfoot")
            || tagName.equalsIgnoreCase("script")
            || tagName.equalsIgnoreCase("style")) {
          // ignore tags with no relevant position info
          child = child.getNextSiblingElement();
          continue;
        }
        String positioning = DomUtil.getCurrentStyle(child, Style.position);
        if (!Display.NONE.getCssName().equals(DomUtil.getCurrentStyle(child, Style.display))
            && !(positioning.equalsIgnoreCase(Position.ABSOLUTE.getCssName())
                || positioning.equalsIgnoreCase(Position.FIXED.getCssName()))) {
          PositionBean childDimensions =
              levels > 0
                  ? getInnerDimensions(child, levels - 1, true)
                  : generatePositionInfo(panel);
          if (first) {
            first = false;
            top = childDimensions.getTop();
            left = childDimensions.getLeft();
            bottom = top + childDimensions.getHeight();
            right = left + childDimensions.getWidth();
          } else {
            int wTop = childDimensions.getTop();
            top = top < wTop ? top : wTop;
            int wLeft = childDimensions.getLeft();
            left = left < wLeft ? left : wLeft;
            int wBottom = wTop + childDimensions.getHeight();
            bottom = bottom > wBottom ? bottom : wBottom;
            int wRight = wLeft + childDimensions.getWidth();
            right = right > wRight ? right : wRight;
          }
        }
        child = child.getNextSiblingElement();
      }
    }
    if (!first) {
      PositionBean result = new PositionBean();
      result.setHeight(bottom - top);
      result.setWidth(right - left);
      result.setTop(top);
      result.setLeft(left);
      return result;
    } else {
      return generatePositionInfo(panel);
    }
  }