Class XmlElement


public class XmlElement extends OrphanXmlElement
Xml Element which is mergeable.

A mergeable element can contains 3 types of children :

  • a child element, which itself may or may not be mergeable.
  • xml attributes which are related to the element.
  • tools oriented attributes to trigger specific behaviors from the merging tool
The two main responsibilities of this class is to be capable of comparing itself against another instance of the same type as well as providing XML element merging capabilities.
  • Constructor Details

  • Method Details

    • applyToFirstChildElementOfType

      public void applyToFirstChildElementOfType(@NonNull com.android.manifmerger.ManifestModel.NodeTypes nodeType, Consumer<XmlElement> nodeConsumer)
      Applies the provided consumer on the first child element of the provided node type
      Parameters:
      nodeType -
      nodeConsumer -
    • elementUsesNamespacePrefix

      public boolean elementUsesNamespacePrefix(@NonNull String prefix)
      Check whether this element or any of its descendants have an attribute with the given namespace
      Parameters:
      prefix - the namespace prefix under consideration
      Returns:
      true if element or any of its descendants have an attribute with the given namespace, false otherwise.
    • getDocument

      @NonNull public XmlDocument getDocument()
      Returns the owning XmlDocument
    • getAttributes

      @NonNull public List<XmlAttribute> getAttributes()
      Returns the list of attributes for this xml element.
    • removeChild

      @Nullable public Node removeChild(Node oldChild)
      Removes given child from DOM and from this list of mMergeableChildren
      Parameters:
      oldChild - Child element to be deleted.
      Returns:
      Child element we deleted, null if the child could not be deleted.
    • removeChild

      @Nullable public Node removeChild(XmlElement oldChild)
    • getAttributeCount

      public int getAttributeCount()
    • getAttributeNames

      @NonNull public com.google.common.collect.ImmutableList<String> getAttributeNames(Predicate<Node> nodePredicate)
    • insertBefore

      @Nullable public Node insertBefore(Node newChild, Node refChild)
      Inserts the node newChild before the existing child node refChild.
      Parameters:
      newChild - The node to insert.
      refChild - The node before which the new node must be inserted.
      Returns:
      The inserted node.
    • insertBefore

      @Nullable public Node insertBefore(XmlElement newChild, Node refChild)
    • appendChild

      @Nullable public Node appendChild(XmlElement newChild)
    • appendChild

      @Nullable public Node appendChild(Node newChild)
    • addChildElement

      @NonNull public XmlElement addChildElement(String childTagName)
      Add a new Child Element with no Attributes.
      Parameters:
      childTagName - tag name of the child added to the element
      Returns:
      child XmlElement object.
      Throws:
      RuntimeException - if we fail to add child element
    • addChildElementWithSingleAttribute

      public void addChildElementWithSingleAttribute(String childTagName, String nsUri, String attrName, String attrValue)
      Add a new Element with a single specified Attribute to parentElement
    • findMergeableChild

      @NonNull public Optional<XmlElement> findMergeableChild(Element childElement)
    • createOrGetElementOfType

      @NonNull public XmlElement createOrGetElementOfType(XmlDocument document, com.android.manifmerger.ManifestModel.NodeTypes nodeType, Consumer<XmlElement> postCreationAction)
    • setAttribute

      public void setAttribute(String name, String value)
    • setAttribute

      public void setAttribute(XmlAttribute attribute, String value)
    • addAttribute

      public void addAttribute(XmlAttribute attribute, String value)
    • addAttribute

      public void addAttribute(String nsUri, String attrName, String attrValue)
    • removeAttributeNS

      public void removeAttributeNS(String namespaceURI, String localName)
    • removeAttribute

      public void removeAttribute(String name)
    • setAttributeNS

      public void setAttributeNS(String namespaceURI, String qualifiedName, String value)
    • getAttribute

      public Optional<XmlAttribute> getAttribute(XmlNode.NodeName attributeName)
      Returns the XmlAttribute for an attribute present on this xml element, or Optional.empty() if not present.
      Parameters:
      attributeName - the attribute name.
    • getOperationType

      @NonNull public NodeOperationType getOperationType()
      Get the node operation type as optionally specified by the user. If the user did not explicitly specify how conflicting elements should be handled, a NodeOperationType.MERGE will be returned.
    • getAttributeOperationType

      @NonNull public com.android.manifmerger.AttributeOperationType getAttributeOperationType(XmlNode.NodeName attributeName)
      Get the attribute operation type as optionally specified by the user. If the user did not explicitly specify how conflicting attributes should be handled, a AttributeOperationType.STRICT will be returned.
    • getAttributeOperations

      @NonNull public Collection<Map.Entry<XmlNode.NodeName,com.android.manifmerger.AttributeOperationType>> getAttributeOperations()
    • getOverrideUsesSdkLibrarySelectors

      @NonNull public List<com.android.manifmerger.OverrideLibrarySelector> getOverrideUsesSdkLibrarySelectors()
    • getPosition

      @NonNull public com.android.ide.common.blame.SourcePosition getPosition()
      Description copied from class: XmlNode
      Returns the element's position
      Overrides:
      getPosition in class OrphanXmlElement
    • getSourceFile

      @NonNull public com.android.ide.common.blame.SourceFile getSourceFile()
      Description copied from class: XmlNode
      Returns the element's document xml source file location.
      Overrides:
      getSourceFile in class OrphanXmlElement
    • mergeWithLowerPriorityNode

      public void mergeWithLowerPriorityNode(@NonNull XmlElement lowerPriorityNode, @NonNull MergingReport.Builder mergingReport, ManifestMerger2.ProcessCancellationChecker processCancellationChecker)
      Merge this xml element with a lower priority node.

      For now, attributes will be merged. If present on both xml elements, a warning will be issued and the attribute merge will be rejected.

      Parameters:
      lowerPriorityNode - lower priority Xml element to merge with.
      mergingReport - the merging report to log errors and actions.
      processCancellationChecker - checks if the request to merge is cancelled.
    • getMergeableElements

      @NonNull public com.google.common.collect.ImmutableList<XmlElement> getMergeableElements()
    • getNodeByTypeAndKey

      @NonNull public Optional<XmlElement> getNodeByTypeAndKey(com.android.manifmerger.ManifestModel.NodeTypes type, @Nullable String keyValue)
      Returns a child of a particular type and a particular key.
      Parameters:
      type - the requested child type.
      keyValue - the requested child key.
      Returns:
      the child of Optional.empty() ()} if no child of this type and key exist.
    • getAllNodesByType

      @NonNull public com.google.common.collect.ImmutableList<XmlElement> getAllNodesByType(com.android.manifmerger.ManifestModel.NodeTypes type)
      Returns all immediate children of this node for a particular type, irrespective of their key.
      Parameters:
      type - the type of children element requested.
      Returns:
      the list (potentially empty) of children.
    • mergeChildren

      public void mergeChildren(@NonNull XmlElement lowerPriorityNode, @NonNull MergingReport.Builder mergingReport, @NonNull ManifestMerger2.ProcessCancellationChecker processCancellationChecker)
    • supportsSelector

      public boolean supportsSelector()
      Returns true if this element supports having a tools:selector decoration, false otherwise.
    • enforceNamespaceDeclaration

      public void enforceNamespaceDeclaration(String nsUri, String defaultPrefix)
      Check that given namespace is declared on the element and create one if not.
      Parameters:
      nsUri - The namespace URI of which the prefix is to be found
      defaultPrefix - The default prefix (root) to use if the namespace is not found. If null, do not create a new namespace if this URI is not defined for the document.
      See Also:
      • XmlUtils.lookupNamespacePrefix(Node, String, boolean)
    • isEquals

      public boolean isEquals(XmlElement otherNode)
    • getSelector

      @Nullable public Selector getSelector()
      Returns a potentially null (if not present) selector decoration on this element.
    • compareTo

      @NonNull public Optional<String> compareTo(Object other)
      Compares this element with another XmlElement ignoring all attributes belonging to the SdkConstants.TOOLS_URI namespace.
      Parameters:
      other - the other element to compare against.
      Returns:
      a String describing the differences between the two XML elements or Optional.empty() ()} if they are equals.