if ((offset == el.getStartOffset() || offset == el.getEndOffset()) && space == 0 && el.isLeaf()) return new Element[2];     Element[] res = new Element[2]; if (el instanceof BranchElement) { int index = el.getElementIndex(offset); Element child = el.getElement(index); Element[] result = split(child, offset, space, editIndex); Element[] removed; Element[] added; Element[] newAdded;  int count = el.getElementCount(); if (result[1] != null) {  if (result[0] == null) { removed = new Element[count - index - 1]; newAdded = new Element[count - index - 1]; added = new Element[] {};  }   else { removed = new Element[count - index]; newAdded = new Element[count - index]; added = new Element[] { result[0] }; } newAdded[0] = result[1]; for (int i = index; i < count; i++) { Element el2 = el.getElement(i); int ind = i - count + removed.length; removed[ind] = el2; if (ind != 0) newAdded[ind] = el2; }
index1 = index0; } } splitEnd = true; }   pos = offs; child = ec.e.getElement(index0); ec.removed.add(child); e = createLeafElement(ec.e, child.getAttributes(), child.getStartOffset(), pos); ec.added.add(e); e = createLeafElement(ec.e, child.getAttributes(), pos, child.getEndOffset()); ec.added.add(e);   for (int i = index0 + 1; i < index1; i++) { child = ec.e.getElement(i); ec.removed.add(child); ec.added.add(child); }  if (index1 != index0) { child = ec.e.getElement(index1); pos = offs + len; ec.removed.add(child); e = createLeafElement(ec.e, child.getAttributes(), child.getStartOffset(), pos); ec.added.add(e); e = createLeafElement(ec.e, child.getAttributes(), pos, child.getEndOffset());
    private Element[] split(Element el, int offset, int space, int editIndex)        {      // If we are at an element boundary, then return an empty array.      if ((offset == el.getStartOffset() || offset == el.getEndOffset())          && space == 0 && el.isLeaf())        return new Element[2];      // If the element is an instance of BranchElement, then we      // recursivly      // call this method to perform the split.      Element[] res = new Element[2];      if (el instanceof BranchElement)        {          int index = el.getElementIndex(offset);          Element child = el.getElement(index);          Element[] result = split(child, offset, space, editIndex);          Element[] removed;          Element[] added;          Element[] newAdded;          int count = el.getElementCount();          if (result[1] != null)            {              // This is the case when we can keep the first element.              if (result[0] == null)                {                  removed = new Element[count - index - 1];                  newAdded = new Element[count - index - 1];                  added = new Element[] {};            }              // This is the case when we may not keep the first              // element.              else            {                  removed = new Element[count - index];                  newAdded = new Element[count - index];                  added = new Element[] { result[0] };                }              newAdded[0] = result[1];              for (int i = index; i < count; i++)                {                  Element el2 = el.getElement(i);                  int ind = i - count + removed.length;                  removed[ind] = el2;                  if (ind != 0)                    newAdded[ind] = el2;                }                            Edit edit = getEditForParagraphAndIndex((BranchElement) el, editIndex);              edit.addRemovedElements(removed);              edit.addAddedElements(added);                            BranchElement newPar =                (BranchElement) createBranchElement(el.getParentElement(),                                                                       el.getAttributes());              newPar.replace(0, 0, newAdded);              res = new Element[] { null, newPar };            }          else            {              removed = new Element[count - index];              for (int i = index; i < count; ++i)                removed[i - index] = el.getElement(i);                            Edit edit = getEditForParagraphAndIndex((BranchElement) el, editIndex);              edit.addRemovedElements(removed);                            BranchElement newPar = (BranchElement) createBranchElement(el.getParentElement(),                                                                       el.getAttributes());              newPar.replace(0, 0, removed);              res = new Element[] { null, newPar };            }        }      else if (el instanceof LeafElement)        {          BranchElement par = (BranchElement) el.getParentElement();          Element el1 = createLeafElement(par, el.getAttributes(),                                          el.getStartOffset(), offset);          Element el2 = createLeafElement(par, el.getAttributes(),                                           offset + space,                                          el.getEndOffset());          res = new Element[] { el1, el2 };        }      return res;    }