Пример #1
0
  private void doFix() throws IncorrectOperationException {
    final XmlTag tag = PsiTreeUtil.getParentOfType(myReference.getElement(), XmlTag.class);
    assert tag != null;
    final XmlTag defineTag =
        tag.createChildTag("define", ApplicationLoader.RNG_NAMESPACE, "\n \n", false);
    defineTag.setAttribute("name", myReference.getCanonicalText());

    final RngGrammar grammar = ((DefinitionReference) myReference).getScope();
    if (grammar == null) return;
    final XmlTag root = grammar.getXmlTag();
    if (root == null) return;

    final XmlTag[] tags = root.getSubTags();
    for (XmlTag xmlTag : tags) {
      if (PsiTreeUtil.isAncestor(xmlTag, tag, false)) {
        final XmlElementFactory ef = XmlElementFactory.getInstance(tag.getProject());
        final XmlText text = ef.createDisplayText(" ");
        final PsiElement e = root.addAfter(text, xmlTag);

        root.addAfter(defineTag, e);
        return;
      }
    }
    root.add(defineTag);
  }
Пример #2
0
  /**
   * @param xmlFile
   * @param tagName
   * @param tagValue
   * @param path hierarchy path, this will be the parent of the new tag, ex: root/node1/node2
   * @return
   */
  public static XmlTag createTagInFile(
      final XmlFile xmlFile,
      String tagName,
      String tagValue,
      String path,
      Map<String, String> attributes) {
    XmlTag root = xmlFile.getRootTag();

    String[] pathElements = path.split("/");

    if (pathElements.length > 0 && pathElements[0].equals(root.getName())) {
      XmlTag lastExistentParent = root;
      String curPath = pathElements[0];
      pathElements =
          (String[])
              ArrayUtils.remove(
                  pathElements, 0); // ArrayUtils.removeElement(pathElements, pathElements[0]);
      for (String curTagName : pathElements) {
        lastExistentParent = lastExistentParent.findFirstSubTag(curTagName);
        if (lastExistentParent == null) {
          lastExistentParent = createTagInFile(xmlFile, curTagName, "", curPath);
          if (lastExistentParent == null) {
            return null;
          }
        }
        curPath += "/" + curTagName;
      }
      final XmlTag newTag = lastExistentParent.createChildTag(tagName, "", tagValue, false);

      if (attributes != null) {
        for (Map.Entry<String, String> entry : attributes.entrySet()) {
          newTag.setAttribute(entry.getKey(), entry.getValue());
        }
      }

      final XmlTag parent = lastExistentParent;
      Runnable runnable =
          new Runnable() {
            @Override
            public void run() {
              new WriteCommandAction.Simple(
                  xmlFile.getProject(), "Create Xml Tag in File", xmlFile) {
                @Override
                protected void run() {

                  //                           newTag = (XmlTag)parent.add(newTag);
                  parent.addAfter(newTag, null);
                }
              }.execute();
            }
          };
      runnable.run();
      // PsiDocumentManager.getInstance(xmlFile.getProject()).commitDocument(document);
      return findSubTag(xmlFile, path + "/" + newTag.getName());

    } else {
      return null;
    }
  }
 private static XmlTag createTag(
     @NotNull XmlTag contextTag, @NotNull XmlElementDescriptor descriptor) {
   String namespace = getNamespace(descriptor);
   XmlTag tag = contextTag.createChildTag(descriptor.getName(), namespace, null, false);
   PsiElement lastChild = tag.getLastChild();
   assert lastChild != null;
   lastChild.delete(); // remove XML_EMPTY_ELEMENT_END
   return tag;
 }