Ejemplo n.º 1
0
 private static void addAttributesBefore(XmlTag tag, List<Pair<String, String>> attr2value) {
   XmlAttribute firstAttribute = ArrayUtil.getFirstElement(tag.getAttributes());
   XmlElementFactory factory = XmlElementFactory.getInstance(tag.getProject());
   for (Pair<String, String> pair : attr2value) {
     XmlAttribute xmlAttribute = factory.createXmlAttribute(pair.first, "");
     if (firstAttribute != null) {
       tag.addBefore(xmlAttribute, firstAttribute);
     } else {
       tag.add(xmlAttribute);
     }
   }
 }
  private static void inlineMultiTags(
      XmlTag includeTag, XmlTag includeTagParent, XmlTag mergeTag, Project project)
      throws AndroidRefactoringErrorException {
    final Map<String, String> namespacesFromParent =
        includeTagParent.getLocalNamespaceDeclarations();
    final Map<String, String> namespacesToAddToParent = new HashMap<String, String>();
    final Map<String, String> namespacesToAddToEachTag = new HashMap<String, String>();

    for (Map.Entry<String, String> entry : mergeTag.getLocalNamespaceDeclarations().entrySet()) {
      final String prefix = entry.getKey();
      final String namespace = entry.getValue();
      final String declaredNamespace = namespacesFromParent.get(prefix);

      if (declaredNamespace != null && !declaredNamespace.equals(namespace)) {
        namespacesToAddToEachTag.put(prefix, namespace);
      } else {
        namespacesToAddToParent.put(prefix, namespace);
      }
    }
    final XmlTag mergeTagCopy = (XmlTag) mergeTag.copy();
    final XmlElementFactory xmlElementFactory = XmlElementFactory.getInstance(project);

    for (XmlTag subtag : mergeTagCopy.getSubTags()) {
      final XmlAttribute[] attributes = subtag.getAttributes();
      final XmlAttribute firstAttribute = attributes.length > 0 ? attributes[0] : null;

      for (Map.Entry<String, String> entry : namespacesToAddToEachTag.entrySet()) {
        final String prefix = entry.getKey();
        final String namespace = entry.getValue();

        if (!subtag.getLocalNamespaceDeclarations().containsKey(prefix)) {
          final XmlAttribute xmlnsAttr =
              xmlElementFactory.createXmlAttribute("xmlns:" + prefix, namespace);

          if (firstAttribute != null) {
            subtag.addBefore(xmlnsAttr, firstAttribute);
          } else {
            subtag.add(xmlnsAttr);
          }
        }
      }
    }
    replaceByTagContent(project, includeTag, mergeTagCopy);
    addNamespaceAttributes(includeTagParent, namespacesToAddToParent, project);
  }
  private static void addNamespaceAttributes(
      XmlTag tag, Map<String, String> namespaces, Project project) {
    final XmlAttribute[] parentAttributes = tag.getAttributes();
    final XmlAttribute firstParentAttribute =
        parentAttributes.length > 0 ? parentAttributes[0] : null;
    final XmlElementFactory factory = XmlElementFactory.getInstance(project);

    for (Map.Entry<String, String> entry : namespaces.entrySet()) {
      final String prefix = entry.getKey();
      final String namespace = entry.getValue();

      if (!namespace.equals(tag.getNamespaceByPrefix(prefix))) {
        final XmlAttribute xmlnsAttr = factory.createXmlAttribute("xmlns:" + prefix, namespace);

        if (firstParentAttribute != null) {
          tag.addBefore(xmlnsAttr, firstParentAttribute);
        } else {
          tag.add(xmlnsAttr);
        }
      }
    }
  }