Element next = getCharacterElement(offset + len); BranchElement nextParent = (BranchElement) next.getParentElement(); Element join = createLeafElement(nextParent, tag.getAttributes(),
BranchElement paragraph = (BranchElement) elementStack.peek(); int currentIndex = paragraph.getElementIndex(offset); Element current = paragraph.getElement(currentIndex); Element next = paragraph.getElement(currentIndex + 1);  Element newEl1 = createLeafElement(paragraph, current.getAttributes(), current.getStartOffset(), offset); Element newEl2 = createLeafElement(paragraph, current.getAttributes(),
    void insertContentTag(ElementSpec tag)    {      int len = tag.getLength();      int dir = tag.getDirection();      if (dir == ElementSpec.JoinPreviousDirection)        {          Element prev = getCharacterElement(offset);          BranchElement prevParent = (BranchElement) prev.getParentElement();          Element join = createLeafElement(prevParent, tag.getAttributes(),                                           prev.getStartOffset(),                                           Math.max(prev.getEndOffset(),                                                    offset + len));          int ind = prevParent.getElementIndex(offset);          if (ind == -1)            ind = 0;          Element[] add = new Element[]{join};          prevParent.replace(ind, 1, add);          // Add this action to the document event.          ElementEdit edit = new ElementEdit(prevParent, ind,                                             new Element[]{prev}, add);          documentEvent.addEdit(edit);        }      else if (dir == ElementSpec.JoinNextDirection)        {          Element next = getCharacterElement(offset + len);          BranchElement nextParent = (BranchElement) next.getParentElement();          Element join = createLeafElement(nextParent, tag.getAttributes(),                                           offset,                                           next.getEndOffset());          int ind = nextParent.getElementIndex(offset + len);          if (ind == -1)            ind = 0;          Element[] add = new Element[]{join};          nextParent.replace(ind, 1, add);          // Add this action to the document event.          ElementEdit edit = new ElementEdit(nextParent, ind,                                             new Element[]{next}, add);          documentEvent.addEdit(edit);        }      else        {          BranchElement par = (BranchElement) getParagraphElement(offset);          int ind = par.getElementIndex(offset);          // Make room for the element.          // Cut previous element.          Element prev = par.getElement(ind);          if (prev != null && prev.getStartOffset() < offset)            {              Element cutPrev = createLeafElement(par, prev.getAttributes(),                                                  prev.getStartOffset(),                                                  offset);              Element[] remove = new Element[]{prev};              Element[] add = new Element[]{cutPrev};              if (prev.getEndOffset() > offset + len)                {                  Element rem = createLeafElement(par, prev.getAttributes(),                                                  offset + len,                                                  prev.getEndOffset());                  add = new Element[]{cutPrev, rem};                }              par.replace(ind, 1, add);              documentEvent.addEdit(new ElementEdit(par, ind, remove, add));              ind++;            }          // ind now points to the next element.          // Cut next element if necessary.          Element next = par.getElement(ind);          if (next != null && next.getStartOffset() < offset + len)            {              Element cutNext = createLeafElement(par, next.getAttributes(),                                                  offset + len,                                                  next.getEndOffset());              Element[] remove = new Element[]{next};              Element[] add = new Element[]{cutNext};              par.replace(ind, 1, add);              documentEvent.addEdit(new ElementEdit(par, ind, remove,                                                    add));            }          // Insert new element.          Element newEl = createLeafElement(par, tag.getAttributes(),                                            offset, offset + len);          Element[] added = new Element[]{newEl};          par.replace(ind, 0, added);          // Add this action to the document event.          ElementEdit edit = new ElementEdit(par, ind, new Element[0],                                             added);          documentEvent.addEdit(edit);        }      offset += len;    }