You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by el...@apache.org on 2002/07/10 23:29:14 UTC

cvs commit: xml-xerces/java/src/org/apache/xerces/dom TextImpl.java

elena       2002/07/10 14:29:13

  Modified:    java/src/org/apache/xerces/dom TextImpl.java
  Log:
  Add implementation to replaceWholeText and getWholeText.
  
  Revision  Changes    Path
  1.17      +115 -6    xml-xerces/java/src/org/apache/xerces/dom/TextImpl.java
  
  Index: TextImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/dom/TextImpl.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- TextImpl.java	11 Jun 2002 17:41:24 -0000	1.16
  +++ TextImpl.java	10 Jul 2002 21:29:13 -0000	1.17
  @@ -88,7 +88,7 @@
   
       /** Serialization version. */
       static final long serialVersionUID = -5294980852957403469L;
  -    
  +        
       //
       // Constructors
       //
  @@ -165,18 +165,127 @@
        * @since DOM Level 3
        */
       public String getWholeText(){
  +        
  +        if (needsSyncData()) {
  +            synchronizeData();
  +        }
  +        if (nextSibling == null) {
  +            return data;
  +        }
  +        StringBuffer buffer = new StringBuffer();
  +        if (data != null && data.length() != 0) {
  +            buffer.append(data);
  +        }
  +        getWholeText(nextSibling, buffer);
  +        return buffer.toString();
  +    
  +    }
   
  -        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, 
  -                               "getWholeText() not implemented.");
  +    /**
  +     * Concatenates the text of all logically-adjacent text nodes
  +     * 
  +     * @param node
  +     * @param buffer
  +     * @return true - if execution was stopped because the type of node
  +     *         other than EntityRef, Text, CDATA is encountered, otherwise
  +     *         return false
  +     */
  +    private boolean getWholeText(Node node, StringBuffer buffer){
  +        String text;
  +        while (node != null) {
  +            short type = node.getNodeType();
  +            if (type == Node.ENTITY_REFERENCE_NODE) {
  +                if (getWholeText(node.getFirstChild(), buffer)){
  +                    return true;
  +                }
  +            }
  +            else if (type == Node.TEXT_NODE || 
  +                     type == Node.CDATA_SECTION_NODE) {
  +                ((NodeImpl)node).getTextContent(buffer);
  +            }
  +            else {
  +                return true; 
  +            }
  +
  +            node = node.getNextSibling();
  +        }
  +        return false;
       }
  +
       /**
       * DOM Level 3 WD - Experimental.
       */
       public Text replaceWholeText(String content)
                                    throws DOMException{
  -        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, 
  -                       "replaceWholeText() not implemented.");
   
  +        if (needsSyncData()) {
  +            synchronizeData();
  +        }
  +
  +        // make sure we can make the replacement
  +        if (!canModify(nextSibling)) {
  +            throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, 
  +                           "No modification is allowed");
  +        }
  +
  +        Node parent = this.getParentNode();
  +        if (content == null || content.length() == 0) {
  +            // remove current node
  +            if (parent !=null) { // check if node in the tree 
  +                parent.removeChild(this);
  +                return null;
  +            }
  +        }
  +        Text currentNode = null;
  +    	if (isReadOnly()){
  +            Text newNode = this.ownerDocument().createTextNode(content);
  +            if (parent !=null) { // check if node in the tree                
  +                parent.insertBefore(newNode, this);
  +                parent.removeChild(this);
  +                currentNode = newNode;
  +            } else {
  +                return newNode;
  +            }
  +        }  else {
  +            this.setData(content);
  +            currentNode = this;
  +        }
  +        Node sibling =  currentNode.getNextSibling();
  +        while ( sibling !=null) {
  +            parent.removeChild(sibling);
  +            sibling = currentNode.getNextSibling();
  +        }
  +
  +        return currentNode;
  +    }
  +
  +    /**
  +     * If any EntityReference to be removed has descendants
  +     * that are not EntityReference, Text, or CDATASection
  +     * nodes, the replaceWholeText method must fail before
  +     * performing any modification of the document, raising a
  +     * DOMException with the code NO_MODIFICATION_ALLOWED_ERR.
  +     * 
  +     * @param node
  +     * @return true - can replace text
  +     *         false - can't replace exception must be raised
  +     */
  +    private boolean canModify(Node node){
  +        while (node != null) {
  +            short type = node.getNodeType();
  +            if (type == Node.ENTITY_REFERENCE_NODE) {
  +                if (!canModify(node.getFirstChild())){
  +                    return false;
  +                }
  +            }
  +            else if (type != Node.TEXT_NODE && 
  +                     type != Node.CDATA_SECTION_NODE) {
  +                return false;
  +            }
  +
  +            node = node.getNextSibling();
  +        }
  +        return true;
       }
   
       /**
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-cvs-help@xml.apache.org