Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
StrictDirective |
|
| 3.25;3.25 |
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 lombok.EqualsAndHashCode; | |
33 | import org.w3c.dom.Node; | |
34 | ||
35 | /** | |
36 | * STRICT directive. | |
37 | * | |
38 | * <p>The class is immutable and thread-safe. | |
39 | * | |
40 | * @author Yegor Bugayenko (yegor256@gmail.com) | |
41 | * @version $Id: 94e1d8fdd11a65e99915fa5bbe431c76a30cb7e9 $ | |
42 | * @since 0.3 | |
43 | */ | |
44 | 0 | @EqualsAndHashCode(of = "number") |
45 | final class StrictDirective implements Directive { | |
46 | ||
47 | /** | |
48 | * Number of nodes we're expecting. | |
49 | */ | |
50 | private final transient int number; | |
51 | ||
52 | /** | |
53 | * Public ctor. | |
54 | * @param nodes Number of node expected | |
55 | */ | |
56 | 12 | StrictDirective(final int nodes) { |
57 | 12 | this.number = nodes; |
58 | 12 | } |
59 | ||
60 | @Override | |
61 | public String toString() { | |
62 | 1 | return String.format("STRICT \"%d\"", this.number); |
63 | } | |
64 | ||
65 | @Override | |
66 | public Directive.Cursor exec(final Node dom, | |
67 | final Directive.Cursor cursor, final Directive.Stack stack) | |
68 | throws ImpossibleModificationException { | |
69 | 8 | if (cursor.size() != this.number) { |
70 | 0 | if (cursor.isEmpty()) { |
71 | 0 | throw new ImpossibleModificationException( |
72 | 0 | String.format( |
73 | 0 | "no current nodes while %d expected", this.number |
74 | ) | |
75 | ); | |
76 | } | |
77 | 0 | if (cursor.size() == 1) { |
78 | 0 | throw new ImpossibleModificationException( |
79 | 0 | String.format( |
80 | "one current node '%s' while strictly %d expected", | |
81 | 0 | cursor.iterator().next().getNodeName(), this.number |
82 | ) | |
83 | ); | |
84 | } | |
85 | 0 | throw new ImpossibleModificationException( |
86 | 0 | String.format( |
87 | "%d current nodes [%s] while strictly %d expected", | |
88 | 0 | cursor.size(), this.names(cursor), this.number |
89 | ) | |
90 | ); | |
91 | } | |
92 | 8 | return cursor; |
93 | } | |
94 | ||
95 | /** | |
96 | * Get node names as a string. | |
97 | * @param nodes Collection of nodes | |
98 | * @return Text presentation of them | |
99 | */ | |
100 | private String names(final Iterable<Node> nodes) { | |
101 | 0 | final StringBuilder text = new StringBuilder(0); |
102 | 0 | for (final Node node : nodes) { |
103 | 0 | if (text.length() > 0) { |
104 | 0 | text.append(", "); |
105 | } | |
106 | 0 | final Node parent = node.getParentNode(); |
107 | 0 | if (parent != null) { |
108 | 0 | text.append(parent.getNodeName()); |
109 | } | |
110 | 0 | text.append('/').append(node.getNodeName()); |
111 | 0 | } |
112 | 0 | return text.toString(); |
113 | } | |
114 | ||
115 | } |