All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.devonfw.tools.ide.migrator.xml.XmlNamespaceReplacement Maven / Gradle / Ivy

Go to download

Code for configurator the creates or updates configuration of IDEs (Eclipse, etc.).

There is a newer version: 3.0.0-beta25
Show newest version
package com.devonfw.tools.ide.migrator.xml;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

/**
 * Implementation of {@link AbstractXmlMigration} for a simple "Namespace" replacement.
 */
public class XmlNamespaceReplacement extends AbstractXmlMigration {

  private String search;

  private String replacement;

  /**
   * The constructor.
   *
   * @param search the {@link String} to search for.
   * @param replacement the replacement for the given {@code search} {@link String}.
   */
  public XmlNamespaceReplacement(String search, String replacement) {

    super();
    this.search = search;
    this.replacement = replacement;
  }

  @Override
  public boolean migrateXml(Document xml) throws Exception {

    return migrateXmlElementAttribute(xml.getDocumentElement().getAttributes());
  }

  private boolean migrateXmlElementAttribute(NamedNodeMap childAttr) {

    boolean updated = false;
    for (int i = 0; i < childAttr.getLength(); i++) {
      Node node = childAttr.item(i);
      short nodeType = node.getNodeType();
      if (nodeType == Node.ATTRIBUTE_NODE) {
        if (removeSpaces(this.search).equals(removeSpaces(node.getNodeValue()))) {
          node.setNodeValue(this.replacement);
          updated = true;
        }
      }
    }
    return updated;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy