public static void rewriteContent(@NotNull Element parent, @NotNull RewriteContentHandler rewriteContentHandler) {

    // iterate through content list and build new content list
    List<Content> originalContent = parent.getContent();
    List<Content> newContent = new ArrayList<Content>();
    for (Content contentElement : originalContent) {

      // handle element
      if (contentElement instanceof Element) {
        Element element = (Element)contentElement;

        // check if rewrite is needed for element
        List<Content> rewriteContent = rewriteContentHandler.rewriteElement(element);
        if (rewriteContent != null) {
          // element was removed
          if (rewriteContent.isEmpty()) {
            // do not add to newContent
          }

          // element is the same - rewrite child elements
          else if (rewriteContent.size() == 1 && rewriteContent.get(0) == element) { //NOPMD
            rewriteContent(element, rewriteContentHandler);
            newContent.add(element);
          }

          // element was replaced with other content - rewrite and add instead
          else {
            for (Content newContentItem : rewriteContent) {
              if (newContentItem instanceof Element) {
                Element newElement = (Element)newContentItem;
                rewriteContent(newElement, rewriteContentHandler);
              }
              newContent.add(newContentItem.clone());
            }
          }
        }

        // nothing to rewrite - do nothing, but rewrite child element
        else {
          rewriteContent(element, rewriteContentHandler);
          newContent.add(element);
        }

      }

      // handle text node
      else if (contentElement instanceof Text) {
        Text text = (Text)contentElement;

        // check if rewrite is needed for text node
        List<Content> rewriteContent = rewriteContentHandler.rewriteText(text);
        if (rewriteContent != null) {
          // element was removed
          if (rewriteContent.isEmpty()) {
            // do not add to newContent
          }

          // element is the same - ignore
          else if (rewriteContent.size() == 1 && rewriteContent.get(0) == text) { //NOPMD
            // add original element
            newContent.add(text);
          }

          // element was replaced with other content - add instead
          else {
            for (Content newContentItem : rewriteContent) {
              newContent.add(newContentItem.clone());
            }
          }
        }

        // nothing to rewrite - do nothing, but add original text element
        else {
          newContent.add(text);
        }

      }

      // unknown element - just add to new content
      else {
        newContent.add(contentElement);
      }

    }

    // replace original content with new content
    parent.removeContent();
    parent.addContent(newContent);

  }
public static void rewriteContent ( @ NotNull Element parent , @ NotNull RewriteContentHandler rewriteContentHandler ) { List < Content > originalContent = parent . getContent ( ) ; List < Content > newContent = new ArrayList < Content > ( ) ; for ( Content contentElement : originalContent ) { if ( contentElement instanceof Element ) { Element element = ( Element ) contentElement ; List < Content > rewriteContent = rewriteContentHandler . rewriteElement ( element ) ; if ( rewriteContent != null ) { if ( rewriteContent . isEmpty ( ) ) { } else if ( rewriteContent . size ( ) == 1 && rewriteContent . get ( 0 ) == element ) { rewriteContent ( element , rewriteContentHandler ) ; newContent . add ( element ) ; } else { for ( Content newContentItem : rewriteContent ) { if ( newContentItem instanceof Element ) { Element newElement = ( Element ) newContentItem ; rewriteContent ( newElement , rewriteContentHandler ) ; } newContent . add ( newContentItem . clone ( ) ) ; } } } else { rewriteContent ( element , rewriteContentHandler ) ; newContent . add ( element ) ; } } else if ( contentElement instanceof Text ) { Text text = ( Text ) contentElement ; List < Content > rewriteContent = rewriteContentHandler . rewriteText ( text ) ; if ( rewriteContent != null ) { if ( rewriteContent . isEmpty ( ) ) { } else if ( rewriteContent . size ( ) == 1 && rewriteContent . get ( 0 ) == text ) { newContent . add ( text ) ; } else { for ( Content newContentItem : rewriteContent ) { newContent . add ( newContentItem . clone ( ) ) ; } } } else { newContent . add ( text ) ; } } else { newContent . add ( contentElement ) ; } } parent . removeContent ( ) ; parent . addContent ( newContent ) ; }
Rewrites all children/sub-tree of the given parent element.
For rewrite operations the given rewrite content handler is called.
@param parent Parent element
@param rewriteContentHandler Rewrite content handler