Project dom4j 1.5.2 [5/2/05 10:13 PM]
 
Coverage - org/dom4j/io/SAXModifyElementHandler.java
  /*
   * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
   * 
   * This software is open source. 
   * See the bottom of this file for the licence.
   * 
   * $Id: SAXModifyElementHandler.java,v 1.1 2004/08/02 18:44:07 maartenc Exp $
   */
 
  package org.dom4j.io;
 
  import org.dom4j.Element;
  import org.dom4j.ElementHandler;
  import org.dom4j.ElementPath;
 
  /**
   * This {@link org.dom4j.ElementHandler} is used to trigger {@link ElementModifier) objects
   * in order to modify (parts of) the Document on the fly.<br>
   * When an element is completely parsed,
   * a copy is handed to the associated (if any) {@link ElementModifier) that on his turn returns
   * the modified element that has to come in the tree.
   *  
   * @author Wonne Keysers (Realsoftware.be)
   */
  class SAXModifyElementHandler implements ElementHandler {
 
     private ElementModifier elemModifier;
     private Element modifiedElement;
 
0x      public SAXModifyElementHandler(ElementModifier elemModifier) {
0x         this.elemModifier = elemModifier;
0x      }
 
     public void onStart(ElementPath elementPath) {
0x         this.modifiedElement = elementPath.getCurrent();
0x      }
 
     public void onEnd(ElementPath elementPath) {
        try {
0x            Element originalElement = elementPath.getCurrent();
0x            Element currentParent = originalElement.getParent();
           
0/2 0x            if( currentParent != null){
              //Clone sets parent + document to null
0x               Element clonedElem = (Element) originalElement.clone();
              
              //Ask for modified element
0x               modifiedElement = elemModifier.modifyElement(clonedElem);
              
0/2 0x               if( modifiedElement != null ){            
                  //Restore parent + document
0x                   modifiedElement.setParent(originalElement.getParent());
0x                   modifiedElement.setDocument(originalElement.getDocument());
      
                  //Replace old with new element in parent
0x                   int contentIndex = currentParent.indexOf(originalElement);
0x                   currentParent.content().set(contentIndex, modifiedElement);
              }
 
              //Remove the old element
0x               originalElement.detach();
           }
           else{
0/2 0x               if( originalElement.isRootElement() ){
               //Clone sets parent + document to null
0x               Element clonedElem = (Element) originalElement.clone();
              
              //Ask for modified element
0x               modifiedElement = elemModifier.modifyElement(clonedElem);
              
0/2 0x               if( modifiedElement != null ){               
                  //Restore parent + document
0x                   modifiedElement.setDocument(originalElement.getDocument());
      
                  //Replace old with new element in parent
0x                   originalElement.getDocument().setRootElement(modifiedElement);
              }
              
              //Remove the old element
0x               originalElement.detach();
              }
           }        
           
           //Put the new element on the ElementStack, it might get pruned by the PruningDispatchHandler
0/2 0x            if( elementPath instanceof ElementStack ){
0x               ElementStack elementStack = ((ElementStack)elementPath);
0x               elementStack.popElement();
0x               elementStack.pushElement(modifiedElement);
           }
        }
        catch (Exception ex) {
0x            throw new SAXModifyException(ex);
0x         }
0x      }
 
     /**
      * @return Returns the modified Element.
      */
     protected Element getModifiedElement() {
0x         return modifiedElement;
     }
 
  }
 
 
 
 
  /*
   * Redistribution and use of this software and associated documentation
   * ("Software"), with or without modification, are permitted provided
   * that the following conditions are met:
   *
   * 1. Redistributions of source code must retain copyright
   *    statements and notices.  Redistributions must also contain a
   *    copy of this document.
   *
   * 2. Redistributions in binary form must reproduce the
   *    above copyright notice, this list of conditions and the
   *    following disclaimer in the documentation and/or other
   *    materials provided with the distribution.
   *
   * 3. The name "DOM4J" must not be used to endorse or promote
   *    products derived from this Software without prior written
   *    permission of MetaStuff, Ltd.  For written permission,
   *    please contact dom4j-info@metastuff.com.
   *
   * 4. Products derived from this Software may not be called "DOM4J"
   *    nor may "DOM4J" appear in their names without prior written
   *    permission of MetaStuff, Ltd. DOM4J is a registered
   *    trademark of MetaStuff, Ltd.
   *
   * 5. Due credit should be given to the DOM4J Project - 
   *    http://www.dom4j.org
   *
   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
   * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
   *
   * $Id: SAXModifyElementHandler.java,v 1.1 2004/08/02 18:44:07 maartenc Exp $
   */