| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| XsetDirective |
|
| 2.3333333333333335;2.333 |
| 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.Map; | |
| 33 | import java.util.concurrent.ConcurrentHashMap; | |
| 34 | import java.util.concurrent.ConcurrentMap; | |
| 35 | import javax.xml.xpath.XPath; | |
| 36 | import javax.xml.xpath.XPathConstants; | |
| 37 | import javax.xml.xpath.XPathExpressionException; | |
| 38 | import javax.xml.xpath.XPathFactory; | |
| 39 | import lombok.EqualsAndHashCode; | |
| 40 | import org.w3c.dom.Node; | |
| 41 | ||
| 42 | /** | |
| 43 | * XSET directive. | |
| 44 | * | |
| 45 | * <p>The class is immutable and thread-safe. | |
| 46 | * | |
| 47 | * @author Yegor Bugayenko (yegor256@gmail.com) | |
| 48 | * @version $Id: 4756ac89ea2c1ac3e8fd86b6dac69de321a2fac8 $ | |
| 49 | * @since 0.1 | |
| 50 | */ | |
| 51 | 0 | @EqualsAndHashCode(of = "expr") |
| 52 | final class XsetDirective implements Directive { | |
| 53 | ||
| 54 | /** | |
| 55 | * XPath factory. | |
| 56 | */ | |
| 57 | 1 | private static final XPathFactory FACTORY = XPathFactory.newInstance(); |
| 58 | ||
| 59 | /** | |
| 60 | * XPath to use. | |
| 61 | */ | |
| 62 | private final transient Arg expr; | |
| 63 | ||
| 64 | /** | |
| 65 | * Public ctor. | |
| 66 | * @param val Text value to set | |
| 67 | * @throws XmlContentException If invalid input | |
| 68 | */ | |
| 69 | 3 | XsetDirective(final String val) throws XmlContentException { |
| 70 | 3 | this.expr = new Arg(val); |
| 71 | 3 | } |
| 72 | ||
| 73 | @Override | |
| 74 | public String toString() { | |
| 75 | 0 | return String.format("XSET %s", this.expr); |
| 76 | } | |
| 77 | ||
| 78 | @Override | |
| 79 | public Directive.Cursor exec(final Node dom, | |
| 80 | final Directive.Cursor cursor, final Directive.Stack stack) | |
| 81 | throws ImpossibleModificationException { | |
| 82 | 3 | final XPath xpath = XsetDirective.FACTORY.newXPath(); |
| 83 | 3 | final ConcurrentMap<Node, String> values = |
| 84 | new ConcurrentHashMap<Node, String>(0); | |
| 85 | 3 | for (final Node node : cursor) { |
| 86 | try { | |
| 87 | 6 | values.put( |
| 88 | node, | |
| 89 | 3 | xpath.evaluate( |
| 90 | 3 | this.expr.raw(), node, XPathConstants.STRING |
| 91 | 3 | ).toString() |
| 92 | ); | |
| 93 | 0 | } catch (final XPathExpressionException ex) { |
| 94 | 0 | throw new ImpossibleModificationException( |
| 95 | 0 | String.format("invalid XPath expr '%s'", this.expr), ex |
| 96 | ); | |
| 97 | 3 | } |
| 98 | 3 | } |
| 99 | 3 | for (final Map.Entry<Node, String> entry : values.entrySet()) { |
| 100 | 3 | entry.getKey().setTextContent(entry.getValue()); |
| 101 | 3 | } |
| 102 | 3 | return cursor; |
| 103 | } | |
| 104 | ||
| 105 | } |