Coverage Report - org.xembly.RemoveDirective
 
Classes in this File Line Coverage Branch Coverage Complexity
RemoveDirective
88%
15/17
50%
5/10
3
 
 1  
 /**
 2  
  * Copyright (c) 2013-2017, xembly.org
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the xembly.org nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package org.xembly;
 31  
 
 32  
 import java.util.Collection;
 33  
 import java.util.HashSet;
 34  
 import lombok.EqualsAndHashCode;
 35  
 import org.w3c.dom.Attr;
 36  
 import org.w3c.dom.Element;
 37  
 import org.w3c.dom.Node;
 38  
 
 39  
 /**
 40  
  * REMOVE directive.
 41  
  *
 42  
  * <p>The class is immutable and thread-safe.
 43  
  *
 44  
  * @author Yegor Bugayenko (yegor256@gmail.com)
 45  
  * @version $Id: f6a560fa77336e306b7b76d5a7b10e651f4267e4 $
 46  
  * @since 0.2
 47  
  */
 48  0
 @EqualsAndHashCode
 49  7
 final class RemoveDirective implements Directive {
 50  
 
 51  
     @Override
 52  
     public String toString() {
 53  1
         return "REMOVE";
 54  
     }
 55  
 
 56  
     @Override
 57  
     public Directive.Cursor exec(final Node dom,
 58  
         final Directive.Cursor cursor, final Directive.Stack stack) {
 59  6
         final Collection<Node> parents = new HashSet<Node>(cursor.size());
 60  6
         for (final Node node : cursor) {
 61  
             final Node parent;
 62  7
             if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
 63  1
                 final Attr attr = Attr.class.cast(node);
 64  1
                 parent = attr.getOwnerElement();
 65  1
                 Element.class.cast(parent).removeAttributeNode(attr);
 66  1
             } else {
 67  6
                 parent = node.getParentNode();
 68  6
                 if (parent == null) {
 69  0
                     throw new IllegalArgumentException(
 70  
                         "you can't delete root document element form XML"
 71  
                     );
 72  
                 }
 73  6
                 parent.removeChild(node);
 74  
             }
 75  7
             parents.add(parent);
 76  7
         }
 77  6
         return new DomCursor(parents);
 78  
     }
 79  
 
 80  
 }