コード例 #1
0
ファイル: XmlNodeUtil.java プロジェクト: hazendaz/beanio
  /**
   * Returns a sibling element that matches a given definition, or <tt>null</tt> if no match is
   * found.
   *
   * @param sibling the sibling DOM element to begin the search
   * @param target the node to search for
   * @return the matching element, or <tt>null</tt> if not found
   */
  public static Element findSibling(Element sibling, XmlNode target) {
    String xmlName = target.getLocalName();
    String xmlNamespace = target.getNamespace();

    Node node = sibling;
    if (node == null) {
      return null;
    }

    while ((node = node.getNextSibling()) != null) {
      if (node.getNodeType() != Node.ELEMENT_NODE) {
        continue;
      }
      Element element = (Element) node;
      if (!element.getLocalName().equals(xmlName)) {
        continue;
      }
      if (target.isNamespaceAware()) {
        String ns = element.getNamespaceURI();
        if (ns == null) {
          if (xmlNamespace != null) {
            continue;
          }
        } else {
          if (!ns.equals(xmlNamespace)) {
            continue;
          }
        }
      }
      return element;
    }
    return null;
  }
コード例 #2
0
  void removeNamespace(Namespace namespace) {
    Namespace current = getNodeNamespace();

    //    Do not remove in-use namespace
    if (namespace.is(current)) return;
    NamedNodeMap attrs = this.dom.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
      XmlNode attr = XmlNode.createImpl(attrs.item(i));
      if (namespace.is(attr.getNodeNamespace())) return;
    }

    //    TODO    I must confess I am not sure I understand the spec fully.  See ECMA357 13.4.4.31
    String existingPrefix = getExistingPrefixFor(namespace);
    if (existingPrefix != null) {
      if (namespace.isUnspecifiedPrefix()) {
        //    we should remove any namespace with this URI from scope; we do this by declaring a
        // namespace with the same
        //    prefix as the existing prefix and setting its URI to the default namespace
        declareNamespace(existingPrefix, getDefaultNamespace().getUri());
      } else {
        if (existingPrefix.equals(namespace.getPrefix())) {
          declareNamespace(existingPrefix, getDefaultNamespace().getUri());
        }
      }
    } else {
      //    the argument namespace is not declared in this scope, so do nothing.
    }
  }
コード例 #3
0
 private static XmlNode createImpl(Node node) {
   if (node instanceof Document) throw new IllegalArgumentException();
   XmlNode rv = null;
   if (getUserData(node) == null) {
     rv = new XmlNode();
     rv.dom = node;
     setUserData(node, rv);
   } else {
     rv = getUserData(node);
   }
   return rv;
 }
コード例 #4
0
ファイル: XmlNodeUtil.java プロジェクト: hazendaz/beanio
  /**
   * Finds the Nth matching child of a DOM element.
   *
   * @param parent the parent DOM node
   * @param target the node to search for
   * @param offset the occurrence of the matching node
   * @return the matching element, or <tt>null</tt> if no match is found
   */
  public static Element findChild(Node parent, XmlNode target, int offset) {
    Node node = parent;
    if (node != null) {
      node = node.getFirstChild();
    }
    if (node == null) {
      return null;
    }

    String xmlName = target.getLocalName();
    String xmlNamespace = target.getNamespace();

    int count = 0;
    do {
      if (node.getNodeType() != Node.ELEMENT_NODE) {
        continue;
      }
      Element element = (Element) node;
      if (!element.getLocalName().equals(xmlName)) {
        continue;
      }
      if (target.isNamespaceAware()) {
        String ns = element.getNamespaceURI();
        if (ns == null) {
          if (xmlNamespace != null && xmlNamespace.length() != 0) {
            continue;
          }
        } else {
          if (!ns.equals(xmlNamespace)) {
            continue;
          }
        }
      }
      if (count == offset) {
        return element;
      }
      ++count;

    } while ((node = node.getNextSibling()) != null);

    return null;
  }
コード例 #5
0
 void addMatchingChildren(XMLList result, XmlNode.Filter filter) {
   Node node = this.dom;
   NodeList children = node.getChildNodes();
   for (int i = 0; i < children.getLength(); i++) {
     Node childnode = children.item(i);
     XmlNode child = XmlNode.createImpl(childnode);
     if (filter.accept(childnode)) {
       result.addToList(child);
     }
   }
 }
コード例 #6
0
 static XmlNode newElementWithText(
     XmlProcessor processor, XmlNode reference, XmlNode.QName qname, String value) {
   if (reference instanceof org.w3c.dom.Document)
     throw new IllegalArgumentException("Cannot use Document node as reference");
   Document document = null;
   if (reference != null) {
     document = reference.dom.getOwnerDocument();
   } else {
     document = processor.newDocument();
   }
   Node referenceDom = (reference != null) ? reference.dom : null;
   Element e = document.createElementNS(qname.getUri(), qname.qualify(referenceDom));
   if (value != null) {
     e.appendChild(document.createTextNode(value));
   }
   return XmlNode.createImpl(e);
 }
コード例 #7
0
ファイル: XmlNodeUtil.java プロジェクト: hazendaz/beanio
  /**
   * Returns the value of an attribute for an element.
   *
   * @param element the element to check
   * @param definition the definition of the attribute to retrieve from the element
   * @return the defined attribute value, or <tt>null</tt> if the attribute was not found on the
   *     element
   */
  public static String getAttribute(Element element, XmlNode definition) {
    if (element == null) {
      return null;
    }

    if (definition.isNamespaceAware()) {
      if (element.hasAttributeNS(definition.getNamespace(), definition.getLocalName())) {
        return element.getAttributeNS(definition.getNamespace(), definition.getLocalName());
      }
    } else {
      if (element.hasAttribute(definition.getLocalName())) {
        return element.getAttribute(definition.getLocalName());
      }
    }
    return null;
  }