|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| SAXModifyElementHandler.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 | /* | |
| 2 | * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. | |
| 3 | * | |
| 4 | * This software is open source. | |
| 5 | * See the bottom of this file for the licence. | |
| 6 | * | |
| 7 | * $Id: SAXModifyElementHandler.java,v 1.1 2004/08/02 18:44:07 maartenc Exp $ | |
| 8 | */ | |
| 9 | ||
| 10 | package org.dom4j.io; | |
| 11 | ||
| 12 | import org.dom4j.Element; | |
| 13 | import org.dom4j.ElementHandler; | |
| 14 | import org.dom4j.ElementPath; | |
| 15 | ||
| 16 | /** | |
| 17 | * This {@link org.dom4j.ElementHandler} is used to trigger {@link ElementModifier) objects | |
| 18 | * in order to modify (parts of) the Document on the fly.<br> | |
| 19 | * When an element is completely parsed, | |
| 20 | * a copy is handed to the associated (if any) {@link ElementModifier) that on his turn returns | |
| 21 | * the modified element that has to come in the tree. | |
| 22 | * | |
| 23 | * @author Wonne Keysers (Realsoftware.be) | |
| 24 | */ | |
| 25 | class SAXModifyElementHandler implements ElementHandler { | |
| 26 | ||
| 27 | private ElementModifier elemModifier; | |
| 28 | private Element modifiedElement; | |
| 29 | ||
| 30 | 0 | public SAXModifyElementHandler(ElementModifier elemModifier) { |
| 31 | 0 | this.elemModifier = elemModifier; |
| 32 | } | |
| 33 | ||
| 34 | 0 | public void onStart(ElementPath elementPath) { |
| 35 | 0 | this.modifiedElement = elementPath.getCurrent(); |
| 36 | } | |
| 37 | ||
| 38 | 0 | public void onEnd(ElementPath elementPath) { |
| 39 | 0 | try { |
| 40 | 0 | Element originalElement = elementPath.getCurrent(); |
| 41 | 0 | Element currentParent = originalElement.getParent(); |
| 42 | ||
| 43 | 0 | if( currentParent != null){ |
| 44 | //Clone sets parent + document to null | |
| 45 | 0 | Element clonedElem = (Element) originalElement.clone(); |
| 46 | ||
| 47 | //Ask for modified element | |
| 48 | 0 | modifiedElement = elemModifier.modifyElement(clonedElem); |
| 49 | ||
| 50 | 0 | if( modifiedElement != null ){ |
| 51 | //Restore parent + document | |
| 52 | 0 | modifiedElement.setParent(originalElement.getParent()); |
| 53 | 0 | modifiedElement.setDocument(originalElement.getDocument()); |
| 54 | ||
| 55 | //Replace old with new element in parent | |
| 56 | 0 | int contentIndex = currentParent.indexOf(originalElement); |
| 57 | 0 | currentParent.content().set(contentIndex, modifiedElement); |
| 58 | } | |
| 59 | ||
| 60 | //Remove the old element | |
| 61 | 0 | originalElement.detach(); |
| 62 | } | |
| 63 | else{ | |
| 64 | 0 | if( originalElement.isRootElement() ){ |
| 65 | //Clone sets parent + document to null | |
| 66 | 0 | Element clonedElem = (Element) originalElement.clone(); |
| 67 | ||
| 68 | //Ask for modified element | |
| 69 | 0 | modifiedElement = elemModifier.modifyElement(clonedElem); |
| 70 | ||
| 71 | 0 | if( modifiedElement != null ){ |
| 72 | //Restore parent + document | |
| 73 | 0 | modifiedElement.setDocument(originalElement.getDocument()); |
| 74 | ||
| 75 | //Replace old with new element in parent | |
| 76 | 0 | originalElement.getDocument().setRootElement(modifiedElement); |
| 77 | } | |
| 78 | ||
| 79 | //Remove the old element | |
| 80 | 0 | originalElement.detach(); |
| 81 | } | |
| 82 | } | |
| 83 | ||
| 84 | //Put the new element on the ElementStack, it might get pruned by the PruningDispatchHandler | |
| 85 | 0 | if( elementPath instanceof ElementStack ){ |
| 86 | 0 | ElementStack elementStack = ((ElementStack)elementPath); |
| 87 | 0 | elementStack.popElement(); |
| 88 | 0 | elementStack.pushElement(modifiedElement); |
| 89 | } | |
| 90 | } | |
| 91 | catch (Exception ex) { | |
| 92 | 0 | throw new SAXModifyException(ex); |
| 93 | } | |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * @return Returns the modified Element. | |
| 98 | */ | |
| 99 | 0 | protected Element getModifiedElement() { |
| 100 | 0 | return modifiedElement; |
| 101 | } | |
| 102 | ||
| 103 | } | |
| 104 | ||
| 105 | ||
| 106 | ||
| 107 | ||
| 108 | /* | |
| 109 | * Redistribution and use of this software and associated documentation | |
| 110 | * ("Software"), with or without modification, are permitted provided | |
| 111 | * that the following conditions are met: | |
| 112 | * | |
| 113 | * 1. Redistributions of source code must retain copyright | |
| 114 | * statements and notices. Redistributions must also contain a | |
| 115 | * copy of this document. | |
| 116 | * | |
| 117 | * 2. Redistributions in binary form must reproduce the | |
| 118 | * above copyright notice, this list of conditions and the | |
| 119 | * following disclaimer in the documentation and/or other | |
| 120 | * materials provided with the distribution. | |
| 121 | * | |
| 122 | * 3. The name "DOM4J" must not be used to endorse or promote | |
| 123 | * products derived from this Software without prior written | |
| 124 | * permission of MetaStuff, Ltd. For written permission, | |
| 125 | * please contact dom4j-info@metastuff.com. | |
| 126 | * | |
| 127 | * 4. Products derived from this Software may not be called "DOM4J" | |
| 128 | * nor may "DOM4J" appear in their names without prior written | |
| 129 | * permission of MetaStuff, Ltd. DOM4J is a registered | |
| 130 | * trademark of MetaStuff, Ltd. | |
| 131 | * | |
| 132 | * 5. Due credit should be given to the DOM4J Project - | |
| 133 | * http://www.dom4j.org | |
| 134 | * | |
| 135 | * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS | |
| 136 | * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT | |
| 137 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 138 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
| 139 | * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 140 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 141 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 142 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 143 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 144 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 145 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 146 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 147 | * | |
| 148 | * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. | |
| 149 | * | |
| 150 | * $Id: SAXModifyElementHandler.java,v 1.1 2004/08/02 18:44:07 maartenc Exp $ | |
| 151 | */ |
|
||||||||||