Exemple #1
0
 public static synchronized Element getChildWithName(String name, Element el) {
   Element[] children = XMLUtil.getChildElementsAsArray(el);
   for (int i = 0; i < children.length; i++) {
     if (name.equalsIgnoreCase(children[i].getNodeName())) return children[i];
   }
   return null;
 }
Exemple #2
0
  /**
   * returns a property from a XMl Node
   *
   * @param node
   * @param key
   * @param caseSensitive
   * @return Object matching key
   * @throws SAXException
   */
  public static Object getProperty(Node node, Collection.Key k, boolean caseSensitive)
      throws SAXException {
    // String lcKey=StringUtil.toLowerCase(key);
    if (k.getLowerString().startsWith("xml")) {
      // Comment
      if (k.equals(XMLCOMMENT)) {
        StringBuffer sb = new StringBuffer();
        NodeList list = node.getChildNodes();
        int len = list.getLength();
        for (int i = 0; i < len; i++) {
          Node n = list.item(i);
          if (n instanceof Comment) {
            sb.append(((Comment) n).getData());
          }
        }
        return sb.toString();
      }
      // NS URI
      if (k.equals(XMLNSURI)) {
        undefinedInRoot(k, node);
        return param(node.getNamespaceURI(), "");
      }
      // Prefix
      if (k.equals(XMLNSPREFIX)) {
        undefinedInRoot(k, node);
        return param(node.getPrefix(), "");
      }
      // Root
      else if (k.equals(XMLROOT)) {
        Element re = getRootElement(node, caseSensitive);
        if (re == null)
          throw new SAXException(
              "Attribute [" + k.getString() + "] not found in XML, XML is empty");
        return param(re, "");
      }
      // Parent
      else if (k.equals(XMLPARENT)) {

        Node parent = getParentNode(node, caseSensitive);
        if (parent == null) {
          if (node.getNodeType() == Node.DOCUMENT_NODE)
            throw new SAXException(
                "Attribute ["
                    + k.getString()
                    + "] not found in XML, there is no parent element, you are already at the root element");
          throw new SAXException(
              "Attribute [" + k.getString() + "] not found in XML, there is no parent element");
        }
        return parent;
      }
      // Name
      else if (k.equals(XMLNAME)) {
        return node.getNodeName();
      }
      // Value
      else if (k.equals(XMLVALUE)) {
        return StringUtil.toStringEmptyIfNull(node.getNodeValue());
      }
      // type
      else if (k.equals(XMLTYPE)) {
        return getTypeAsString(node, true);
      }
      // Attributes
      else if (k.equals(XMLATTRIBUTES)) {
        NamedNodeMap attr = node.getAttributes();

        if (attr == null) throw undefined(k, node);
        return new XMLAttributes(node.getOwnerDocument(), attr, caseSensitive);
      }
      // Text
      else if (k.equals(XMLTEXT)) {
        undefinedInRoot(k, node);
        StringBuffer sb = new StringBuffer();
        NodeList list = node.getChildNodes();
        int len = list.getLength();
        for (int i = 0; i < len; i++) {
          Node n = list.item(i);
          if (n instanceof Text || n instanceof CDATASection) {
            sb.append(((CharacterData) n).getData());
          }
        }
        return sb.toString();
      } else if (k.equals(XMLCDATA)) {
        undefinedInRoot(k, node);
        StringBuffer sb = new StringBuffer();
        NodeList list = node.getChildNodes();
        int len = list.getLength();
        for (int i = 0; i < len; i++) {
          Node n = list.item(i);
          if (n instanceof Text || n instanceof CDATASection) {
            sb.append(((CharacterData) n).getData());
          }
        }
        return sb.toString();
      }
      // children
      else if (k.equals(XMLCHILDREN)) {
        return new XMLNodeList(node, caseSensitive);
      }
    }

    if (node instanceof Document) {
      node = ((Document) node).getDocumentElement();
      if (node == null)
        throw new SAXException("Attribute [" + k.getString() + "] not found in XML, XML is empty");

      // if((!caseSensitive && node.getNodeName().equalsIgnoreCase(k.getString())) || (caseSensitive
      // && node.getNodeName().equals(k.getString()))) {
      if (nameEqual(node, k.getString(), caseSensitive)) {
        return XMLStructFactory.newInstance(node, caseSensitive);
      }
    } else if (node.getNodeType() == Node.ELEMENT_NODE && Decision.isInteger(k)) {
      int index = Caster.toIntValue(k, 0);
      int count = 0;
      Node parent = node.getParentNode();
      String nodeName = node.getNodeName();
      Element[] children = XMLUtil.getChildElementsAsArray(parent);

      for (int i = 0; i < children.length; i++) {
        if (XMLUtil.nameEqual(children[i], nodeName, caseSensitive)) count++;

        if (count == index) return XMLCaster.toXMLStruct(children[i], caseSensitive);
      }
      String detail;
      if (count == 0) detail = "there are no Elements with this name";
      else if (count == 1) detail = "there is only 1 Element with this name";
      else detail = "there are only " + count + " Elements with this name";
      throw new SAXException(
          "invalid index ["
              + k.getString()
              + "] for Element with name ["
              + node.getNodeName()
              + "], "
              + detail);
    } else {
      List<Node> children =
          XMLUtil.getChildNodesAsList(node, Node.ELEMENT_NODE, caseSensitive, null);
      int len = children.size();
      Array array = null; // new ArrayImpl();
      Element el;
      XMLStruct sct = null, first = null;
      for (int i = 0; i < len; i++) {
        el = (Element) children.get(i); // XMLCaster.toXMLStruct(getChildNode(index),caseSensitive);
        if (XMLUtil.nameEqual(el, k.getString(), caseSensitive)) {
          sct = XMLCaster.toXMLStruct(el, caseSensitive);

          if (array != null) {
            array.appendEL(sct);
          } else if (first != null) {
            array = new ArrayImpl();
            array.appendEL(first);
            array.appendEL(sct);
          } else {
            first = sct;
          }
        }
      }

      if (array != null) {
        try {
          return new XMLMultiElementStruct(array, false);
        } catch (PageException e) {
        }
      }
      if (first != null) return first;
    }
    throw new SAXException("Attribute [" + k.getString() + "] not found");
  }