text.replaceData(index + 1, data.length() - index - 1, indent + "    ");
if (index + 1 == data.length()) { text.appendData(indent + "    "); } else { text.replaceData(index + 1, data.length() - index - 1, indent + "    "); }
  public void forceNiceInsertBefore(Node parent, Node newChild, Node referenceChild)  {    updatingDOM = true;    LOOP:    for (Node child = referenceChild == null ? parent.getLastChild() : referenceChild.getPreviousSibling();          child != null;          child = child.getPreviousSibling())    {      switch (child.getNodeType())      {        case Node.TEXT_NODE:        {          Text text = (Text)child;          String data = text.getData();/*          System.out.print("xx " + data.length() + ":: ");          for (int i = 0; i < data.length(); ++i)          {            System.out.print(" " + Integer.toHexString((int)data.charAt(i)));          }          System.out.println();*/          int index = data.lastIndexOf('\n');          if (index != -1)          {// System.out.println("In here");            StringBuffer indent = new StringBuffer();            for (Node ancestor = parent.getParentNode();                  ancestor != null && ancestor.getNodeType() != Node.DOCUMENT_NODE;                  ancestor = ancestor.getParentNode())            {              indent.append("    ");            }            if (index + 1 < data.length() && data.charAt(index + 1) == '\r')            {              ++index;            }            text.replaceData(index + 1, data.length() - index - 1, indent + "    ");            // setCorrectIndentation(indent, newText);            if (referenceChild != null)            {              indent.append("    ");            }            Text newText = parent.getOwnerDocument().createTextNode("\n" + indent);// System.out.println("Inserted..." + newText);            parent.insertBefore(newText, referenceChild);            referenceChild = newText;            break LOOP;          }          break;        }         case Node.ELEMENT_NODE:        {          break LOOP;        }      }    }// System.out.println("Inserted..." + newChild);    parent.insertBefore(newChild, referenceChild);    updatingDOM = false;  }