Java Code Examples for org.w3c.dom.DOMException#INVALID_STATE_ERR

The following examples show how to use org.w3c.dom.DOMException#INVALID_STATE_ERR . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RangeImpl.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
public void selectNodeContents(Node refNode)
    throws RangeException
{
    if (fDocument.errorChecking) {
        if( fDetach) {
            throw new DOMException(
                    DOMException.INVALID_STATE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
        }
        if ( !isLegalContainer(refNode)) {
            throw new RangeExceptionImpl(
                    RangeException.INVALID_NODE_TYPE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null));
        }
        if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) {
            throw new DOMException(
                    DOMException.WRONG_DOCUMENT_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null));
        }
    }
    fStartContainer = refNode;
    fEndContainer = refNode;
    Node first = refNode.getFirstChild();
    fStartOffset = 0;
    if (first == null) {
        fEndOffset = 0;
    } else {
        int i = 0;
        for (Node n = first; n!=null; n = n.getNextSibling()) {
            i++;
        }
        fEndOffset = i;
    }

}
 
Example 2
Source File: RangeImpl.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
public void setEnd(Node refNode, int offset)
                   throws RangeException, DOMException
{
    if (fDocument.errorChecking) {
        if (fDetach) {
            throw new DOMException(
                    DOMException.INVALID_STATE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
        }
        if ( !isLegalContainer(refNode)) {
            throw new RangeExceptionImpl(
                    RangeException.INVALID_NODE_TYPE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null));
        }
        if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) {
            throw new DOMException(
                    DOMException.WRONG_DOCUMENT_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null));
        }
    }

    checkIndex(refNode, offset);

    fEndContainer = refNode;
    fEndOffset = offset;

    // If one boundary-point of a Range is set to have a root container
    // other
    // than the current one for the Range, the Range should be collapsed to
    // the new position.
    // The start position of a Range should never be after the end position.
    if (getCommonAncestorContainer() == null
            || (fStartContainer == fEndContainer && fEndOffset < fStartOffset)) {
        collapse(false);
    }
}
 
Example 3
Source File: RangeImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void setStart(Node refNode, int offset)
                     throws RangeException, DOMException
{
    if (fDocument.errorChecking) {
        if ( fDetach) {
            throw new DOMException(
                    DOMException.INVALID_STATE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
        }
        if ( !isLegalContainer(refNode)) {
            throw new RangeExceptionImpl(
                    RangeException.INVALID_NODE_TYPE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null));
        }
        if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) {
            throw new DOMException(
                    DOMException.WRONG_DOCUMENT_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null));
        }
    }

    checkIndex(refNode, offset);

    fStartContainer = refNode;
    fStartOffset = offset;

    // If one boundary-point of a Range is set to have a root container
    // other
    // than the current one for the Range, the Range should be collapsed to
    // the new position.
    // The start position of a Range should never be after the end position.
    if (getCommonAncestorContainer() == null
            || (fStartContainer == fEndContainer && fEndOffset < fStartOffset)) {
        collapse(true);
    }
}
 
Example 4
Source File: RangeImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void selectNode(Node refNode)
    throws RangeException
{
    if (fDocument.errorChecking) {
        if (fDetach) {
            throw new DOMException(
                    DOMException.INVALID_STATE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
        }
        if ( !isLegalContainer( refNode.getParentNode() ) ||
                !isLegalContainedNode( refNode ) ) {
            throw new RangeExceptionImpl(
                    RangeException.INVALID_NODE_TYPE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null));
        }
        if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) {
            throw new DOMException(
                    DOMException.WRONG_DOCUMENT_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null));
        }
    }
    Node parent = refNode.getParentNode();
    if (parent != null ) // REVIST: what to do if it IS null?
    {
        fStartContainer = parent;
        fEndContainer = parent;
        int i = 0;
        for (Node n = refNode; n!=null; n = n.getPreviousSibling()) {
            i++;
        }
        fStartOffset = i-1;
        fEndOffset = fStartOffset+1;
    }
}
 
Example 5
Source File: RangeImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public boolean getCollapsed() {
    if ( fDetach ) {
        throw new DOMException(
            DOMException.INVALID_STATE_ERR,
            DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
    }
    return (fStartContainer == fEndContainer
         && fStartOffset == fEndOffset);
}
 
Example 6
Source File: RangeImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void setEndAfter(Node refNode)
    throws RangeException
{
    if (fDocument.errorChecking) {
        if( fDetach) {
            throw new DOMException(
                    DOMException.INVALID_STATE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
        }
        if ( !hasLegalRootContainer(refNode) ||
                !isLegalContainedNode(refNode)) {
            throw new RangeExceptionImpl(
                    RangeException.INVALID_NODE_TYPE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null));
        }
        if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) {
            throw new DOMException(
                    DOMException.WRONG_DOCUMENT_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null));
        }
    }
    fEndContainer = refNode.getParentNode();
    int i = 0;
    for (Node n = refNode; n!=null; n = n.getPreviousSibling()) {
        i++;
    }
    fEndOffset = i;

    // If one boundary-point of a Range is set to have a root container
    // other
    // than the current one for the Range, the Range should be collapsed to
    // the new position.
    // The start position of a Range should never be after the end position.
    if (getCommonAncestorContainer() == null
            || (fStartContainer == fEndContainer && fEndOffset < fStartOffset)) {
        collapse(false);
    }
}
 
Example 7
Source File: RangeImpl.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
public Node getStartContainer() {
    if ( fDetach ) {
        throw new DOMException(
            DOMException.INVALID_STATE_ERR,
            DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
    }
    return fStartContainer;
}
 
Example 8
Source File: RangeImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void setStart(Node refNode, int offset)
                     throws RangeException, DOMException
{
    if (fDocument.errorChecking) {
        if ( fDetach) {
            throw new DOMException(
                    DOMException.INVALID_STATE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
        }
        if ( !isLegalContainer(refNode)) {
            throw new RangeExceptionImpl(
                    RangeException.INVALID_NODE_TYPE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null));
        }
        if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) {
            throw new DOMException(
                    DOMException.WRONG_DOCUMENT_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null));
        }
    }

    checkIndex(refNode, offset);

    fStartContainer = refNode;
    fStartOffset = offset;

    // If one boundary-point of a Range is set to have a root container
    // other
    // than the current one for the Range, the Range should be collapsed to
    // the new position.
    // The start position of a Range should never be after the end position.
    if (getCommonAncestorContainer() == null
            || (fStartContainer == fEndContainer && fEndOffset < fStartOffset)) {
        collapse(true);
    }
}
 
Example 9
Source File: RangeImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void setEndAfter(Node refNode)
    throws RangeException
{
    if (fDocument.errorChecking) {
        if( fDetach) {
            throw new DOMException(
                    DOMException.INVALID_STATE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
        }
        if ( !hasLegalRootContainer(refNode) ||
                !isLegalContainedNode(refNode)) {
            throw new RangeExceptionImpl(
                    RangeException.INVALID_NODE_TYPE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null));
        }
        if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) {
            throw new DOMException(
                    DOMException.WRONG_DOCUMENT_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null));
        }
    }
    fEndContainer = refNode.getParentNode();
    int i = 0;
    for (Node n = refNode; n!=null; n = n.getPreviousSibling()) {
        i++;
    }
    fEndOffset = i;

    // If one boundary-point of a Range is set to have a root container
    // other
    // than the current one for the Range, the Range should be collapsed to
    // the new position.
    // The start position of a Range should never be after the end position.
    if (getCommonAncestorContainer() == null
            || (fStartContainer == fEndContainer && fEndOffset < fStartOffset)) {
        collapse(false);
    }
}
 
Example 10
Source File: RangeImpl.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
public Node getCommonAncestorContainer() {
    if ( fDetach ) {
        throw new DOMException(
            DOMException.INVALID_STATE_ERR,
            DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
    }
    Vector startV = new Vector();
    Node node;
    for (node=fStartContainer; node != null;
         node=node.getParentNode())
    {
        startV.addElement(node);
    }
    Vector endV = new Vector();
    for (node=fEndContainer; node != null;
         node=node.getParentNode())
    {
        endV.addElement(node);
    }
    int s = startV.size()-1;
    int e = endV.size()-1;
    Object result = null;
    while (s>=0 && e>=0) {
        if (startV.elementAt(s) == endV.elementAt(e)) {
            result = startV.elementAt(s);
        } else {
            break;
        }
        --s;
        --e;
    }
    return (Node)result;
}
 
Example 11
Source File: RangeImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public Node getStartContainer() {
    if ( fDetach ) {
        throw new DOMException(
            DOMException.INVALID_STATE_ERR,
            DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
    }
    return fStartContainer;
}
 
Example 12
Source File: RangeImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public void selectNode(Node refNode)
    throws RangeException
{
    if (fDocument.errorChecking) {
        if (fDetach) {
            throw new DOMException(
                    DOMException.INVALID_STATE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
        }
        if ( !isLegalContainer( refNode.getParentNode() ) ||
                !isLegalContainedNode( refNode ) ) {
            throw new RangeExceptionImpl(
                    RangeException.INVALID_NODE_TYPE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null));
        }
        if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) {
            throw new DOMException(
                    DOMException.WRONG_DOCUMENT_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null));
        }
    }
    Node parent = refNode.getParentNode();
    if (parent != null ) // REVIST: what to do if it IS null?
    {
        fStartContainer = parent;
        fEndContainer = parent;
        int i = 0;
        for (Node n = refNode; n!=null; n = n.getPreviousSibling()) {
            i++;
        }
        fStartOffset = i-1;
        fEndOffset = fStartOffset+1;
    }
}
 
Example 13
Source File: RangeImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void selectNode(Node refNode)
    throws RangeException
{
    if (fDocument.errorChecking) {
        if (fDetach) {
            throw new DOMException(
                    DOMException.INVALID_STATE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
        }
        if ( !isLegalContainer( refNode.getParentNode() ) ||
                !isLegalContainedNode( refNode ) ) {
            throw new RangeExceptionImpl(
                    RangeException.INVALID_NODE_TYPE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null));
        }
        if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) {
            throw new DOMException(
                    DOMException.WRONG_DOCUMENT_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null));
        }
    }
    Node parent = refNode.getParentNode();
    if (parent != null ) // REVIST: what to do if it IS null?
    {
        fStartContainer = parent;
        fEndContainer = parent;
        int i = 0;
        for (Node n = refNode; n!=null; n = n.getPreviousSibling()) {
            i++;
        }
        fStartOffset = i-1;
        fEndOffset = fStartOffset+1;
    }
}
 
Example 14
Source File: RangeImpl.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
public int getStartOffset() {
    if ( fDetach ) {
        throw new DOMException(
            DOMException.INVALID_STATE_ERR,
            DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
    }
    return fStartOffset;
}
 
Example 15
Source File: RangeImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public void selectNodeContents(Node refNode)
    throws RangeException
{
    if (fDocument.errorChecking) {
        if( fDetach) {
            throw new DOMException(
                    DOMException.INVALID_STATE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
        }
        if ( !isLegalContainer(refNode)) {
            throw new RangeExceptionImpl(
                    RangeException.INVALID_NODE_TYPE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null));
        }
        if ( fDocument != refNode.getOwnerDocument() && fDocument != refNode) {
            throw new DOMException(
                    DOMException.WRONG_DOCUMENT_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null));
        }
    }
    fStartContainer = refNode;
    fEndContainer = refNode;
    Node first = refNode.getFirstChild();
    fStartOffset = 0;
    if (first == null) {
        fEndOffset = 0;
    } else {
        int i = 0;
        for (Node n = first; n!=null; n = n.getNextSibling()) {
            i++;
        }
        fEndOffset = i;
    }

}
 
Example 16
Source File: DOMParserImpl.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parse an XML document from a resource identified by an
 * <code>LSInput</code>.
 *
 */
public Document parse (LSInput is) throws LSException {

    // need to wrap the LSInput with an XMLInputSource
    XMLInputSource xmlInputSource = dom2xmlInputSource (is);
    if ( fBusy ) {
        String msg = DOMMessageFormatter.formatMessage (
        DOMMessageFormatter.DOM_DOMAIN,
        "INVALID_STATE_ERR",null);
        throw new DOMException ( DOMException.INVALID_STATE_ERR,msg);
    }

    try {
        currentThread = Thread.currentThread();
                    fBusy = true;
        parse (xmlInputSource);
        fBusy = false;
        if (abortNow && currentThread.isInterrupted()) {
            //reset interrupt state
            abortNow = false;
            Thread.interrupted();
        }
    } catch (Exception e) {
        fBusy = false;
        if (abortNow && currentThread.isInterrupted()) {
            Thread.interrupted();
        }
        if (abortNow) {
            abortNow = false;
            restoreHandlers();
            return null;
        }
        // Consume this exception if the user
        // issued an interrupt or an abort.
        if (e != Abort.INSTANCE) {
            if (!(e instanceof XMLParseException) && fErrorHandler != null) {
               DOMErrorImpl error = new DOMErrorImpl ();
               error.fException = e;
               error.fMessage = e.getMessage ();
               error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
               fErrorHandler.getErrorHandler().handleError (error);
            }
            if (DEBUG) {
               e.printStackTrace ();
            }
            throw (LSException) DOMUtil.createLSException(LSException.PARSE_ERR, e).fillInStackTrace();
        }
    }
    Document doc = getDocument();
    dropDocumentReferences();
    return doc;
}
 
Example 17
Source File: DOMParserImpl.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Parse an XML document from a location identified by an URI reference.
 * If the URI contains a fragment identifier (see section 4.1 in ), the
 * behavior is not defined by this specification.
 *
 */
public Document parseURI (String uri) throws LSException {

    //If DOMParser insstance is already busy parsing another document when this
    // method is called, then raise INVALID_STATE_ERR according to DOM L3 LS spec
    if ( fBusy ) {
        String msg = DOMMessageFormatter.formatMessage (
        DOMMessageFormatter.DOM_DOMAIN,
        "INVALID_STATE_ERR",null);
        throw new DOMException ( DOMException.INVALID_STATE_ERR,msg);
    }

    XMLInputSource source = new XMLInputSource (null, uri, null);
    try {
        currentThread = Thread.currentThread();
                    fBusy = true;
        parse (source);
        fBusy = false;
        if (abortNow && currentThread.isInterrupted()) {
            //reset interrupt state
            abortNow = false;
            Thread.interrupted();
        }
    } catch (Exception e){
        fBusy = false;
        if (abortNow && currentThread.isInterrupted()) {
            Thread.interrupted();
        }
        if (abortNow) {
            abortNow = false;
            restoreHandlers();
            return null;
        }
        // Consume this exception if the user
        // issued an interrupt or an abort.
        if (e != Abort.INSTANCE) {
            if (!(e instanceof XMLParseException) && fErrorHandler != null) {
                DOMErrorImpl error = new DOMErrorImpl ();
                error.fException = e;
                error.fMessage = e.getMessage ();
                error.fSeverity = DOMError.SEVERITY_FATAL_ERROR;
                fErrorHandler.getErrorHandler ().handleError (error);
            }
            if (DEBUG) {
                e.printStackTrace ();
            }
            throw (LSException) DOMUtil.createLSException(LSException.PARSE_ERR, e).fillInStackTrace();
        }
    }
    Document doc = getDocument();
    dropDocumentReferences();
    return doc;
}
 
Example 18
Source File: RangeImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public void insertNode(Node newNode)
    throws DOMException, RangeException
{
    if ( newNode == null ) return; //throw exception?

    int type = newNode.getNodeType();

    if (fDocument.errorChecking) {
        if (fDetach) {
            throw new DOMException(
                    DOMException.INVALID_STATE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
        }
        if ( fDocument != newNode.getOwnerDocument() ) {
            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "WRONG_DOCUMENT_ERR", null));
        }

        if (type == Node.ATTRIBUTE_NODE
                || type == Node.ENTITY_NODE
                || type == Node.NOTATION_NODE
                || type == Node.DOCUMENT_NODE)
        {
            throw new RangeExceptionImpl(
                    RangeException.INVALID_NODE_TYPE_ERR,
                    DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null));
        }
    }
    Node cloneCurrent;
    Node current;
    int currentChildren = 0;
    fInsertedFromRange = true;

    //boolean MULTIPLE_MODE = false;
    if (fStartContainer.getNodeType() == Node.TEXT_NODE) {

        Node parent = fStartContainer.getParentNode();
        currentChildren = parent.getChildNodes().getLength(); //holds number of kids before insertion
        // split text node: results is 3 nodes..
        cloneCurrent = fStartContainer.cloneNode(false);
        ((TextImpl)cloneCurrent).setNodeValueInternal(
                (cloneCurrent.getNodeValue()).substring(fStartOffset));
        ((TextImpl)fStartContainer).setNodeValueInternal(
                (fStartContainer.getNodeValue()).substring(0,fStartOffset));
        Node next = fStartContainer.getNextSibling();
        if (next != null) {
                if (parent !=  null) {
                    parent.insertBefore(newNode, next);
                    parent.insertBefore(cloneCurrent, next);
                }
        } else {
                if (parent != null) {
                    parent.appendChild(newNode);
                    parent.appendChild(cloneCurrent);
                }
        }
         //update ranges after the insertion
         if ( fEndContainer == fStartContainer) {
              fEndContainer = cloneCurrent; //endContainer is the new Node created
              fEndOffset -= fStartOffset;
         }
         else if ( fEndContainer == parent ) {    //endContainer was not a text Node.
              //endOffset + = number_of_children_added
               fEndOffset += (parent.getChildNodes().getLength() - currentChildren);
         }

         // signal other Ranges to update their start/end containers/offsets
         signalSplitData(fStartContainer, cloneCurrent, fStartOffset);


    } else { // ! TEXT_NODE
        if ( fEndContainer == fStartContainer )      //need to remember number of kids
            currentChildren= fEndContainer.getChildNodes().getLength();

        current = fStartContainer.getFirstChild();
        int i = 0;
        for(i = 0; i < fStartOffset && current != null; i++) {
            current=current.getNextSibling();
        }
        if (current != null) {
            fStartContainer.insertBefore(newNode, current);
        } else {
            fStartContainer.appendChild(newNode);
        }
        //update fEndOffset. ex:<body><p/></body>. Range(start;end): body,0; body,1
        // insert <h1>: <body></h1><p/></body>. Range(start;end): body,0; body,2
        if ( fEndContainer == fStartContainer && fEndOffset != 0 ) {     //update fEndOffset if not 0
            fEndOffset += (fEndContainer.getChildNodes().getLength() - currentChildren);
        }
    }
    fInsertedFromRange = false;
}
 
Example 19
Source File: RangeImpl.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public String toString(){
    if( fDetach) {
            throw new DOMException(
            DOMException.INVALID_STATE_ERR,
            DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
    }

    Node node = fStartContainer;
    Node stopNode = fEndContainer;
    StringBuffer sb = new StringBuffer();
    if (fStartContainer.getNodeType() == Node.TEXT_NODE
     || fStartContainer.getNodeType() == Node.CDATA_SECTION_NODE
    ) {
        if (fStartContainer == fEndContainer) {
            sb.append(fStartContainer.getNodeValue().substring(fStartOffset, fEndOffset));
            return sb.toString();
        }
        sb.append(fStartContainer.getNodeValue().substring(fStartOffset));
        node=nextNode (node,true); //fEndContainer!=fStartContainer

    }
    else {  //fStartContainer is not a TextNode
        node=node.getFirstChild();
        if (fStartOffset>0) { //find a first node within a range, specified by fStartOffset
           int counter=0;
           while (counter<fStartOffset && node!=null) {
               node=node.getNextSibling();
               counter++;
           }
        }
        if (node == null) {
               node = nextNode(fStartContainer,false);
        }
    }
    if ( fEndContainer.getNodeType()!= Node.TEXT_NODE &&
         fEndContainer.getNodeType()!= Node.CDATA_SECTION_NODE ){
         int i=fEndOffset;
         stopNode = fEndContainer.getFirstChild();
         while( i>0 && stopNode!=null ){
             --i;
             stopNode = stopNode.getNextSibling();
         }
         if ( stopNode == null )
             stopNode = nextNode( fEndContainer, false );
     }
     while (node != stopNode) {  //look into all kids of the Range
         if (node == null) break;
         if (node.getNodeType() == Node.TEXT_NODE
         ||  node.getNodeType() == Node.CDATA_SECTION_NODE) {
             sb.append(node.getNodeValue());
         }

         node = nextNode(node, true);
     }

    if (fEndContainer.getNodeType() == Node.TEXT_NODE
     || fEndContainer.getNodeType() == Node.CDATA_SECTION_NODE) {
        sb.append(fEndContainer.getNodeValue().substring(0,fEndOffset));
    }
    return sb.toString();
}
 
Example 20
Source File: RangeImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public String toString(){
    if( fDetach) {
            throw new DOMException(
            DOMException.INVALID_STATE_ERR,
            DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null));
    }

    Node node = fStartContainer;
    Node stopNode = fEndContainer;
    StringBuffer sb = new StringBuffer();
    if (fStartContainer.getNodeType() == Node.TEXT_NODE
     || fStartContainer.getNodeType() == Node.CDATA_SECTION_NODE
    ) {
        if (fStartContainer == fEndContainer) {
            sb.append(fStartContainer.getNodeValue().substring(fStartOffset, fEndOffset));
            return sb.toString();
        }
        sb.append(fStartContainer.getNodeValue().substring(fStartOffset));
        node=nextNode (node,true); //fEndContainer!=fStartContainer

    }
    else {  //fStartContainer is not a TextNode
        node=node.getFirstChild();
        if (fStartOffset>0) { //find a first node within a range, specified by fStartOffset
           int counter=0;
           while (counter<fStartOffset && node!=null) {
               node=node.getNextSibling();
               counter++;
           }
        }
        if (node == null) {
               node = nextNode(fStartContainer,false);
        }
    }
    if ( fEndContainer.getNodeType()!= Node.TEXT_NODE &&
         fEndContainer.getNodeType()!= Node.CDATA_SECTION_NODE ){
         int i=fEndOffset;
         stopNode = fEndContainer.getFirstChild();
         while( i>0 && stopNode!=null ){
             --i;
             stopNode = stopNode.getNextSibling();
         }
         if ( stopNode == null )
             stopNode = nextNode( fEndContainer, false );
     }
     while (node != stopNode) {  //look into all kids of the Range
         if (node == null) break;
         if (node.getNodeType() == Node.TEXT_NODE
         ||  node.getNodeType() == Node.CDATA_SECTION_NODE) {
             sb.append(node.getNodeValue());
         }

         node = nextNode(node, true);
     }

    if (fEndContainer.getNodeType() == Node.TEXT_NODE
     || fEndContainer.getNodeType() == Node.CDATA_SECTION_NODE) {
        sb.append(fEndContainer.getNodeValue().substring(0,fEndOffset));
    }
    return sb.toString();
}