|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2016-2025 Objectionary.com |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included |
| 14 | + * in all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +package org.eolang.lints.errors; |
| 25 | + |
| 26 | +import com.jcabi.xml.XML; |
| 27 | +import com.jcabi.xml.XSL; |
| 28 | +import com.jcabi.xml.XSLDocument; |
| 29 | +import java.util.Collection; |
| 30 | +import java.util.HashSet; |
| 31 | +import java.util.LinkedList; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Objects; |
| 35 | +import java.util.function.Function; |
| 36 | +import java.util.stream.Collectors; |
| 37 | +import java.util.stream.IntStream; |
| 38 | +import org.cactoos.io.ResourceOf; |
| 39 | +import org.cactoos.io.UncheckedInput; |
| 40 | +import org.cactoos.list.ListOf; |
| 41 | +import org.cactoos.text.TextOf; |
| 42 | +import org.cactoos.text.UncheckedText; |
| 43 | +import org.eolang.lints.Defect; |
| 44 | +import org.eolang.lints.Lint; |
| 45 | +import org.eolang.lints.Severity; |
| 46 | + |
| 47 | +/** |
| 48 | + * All FQNs that have `@atom` in the entire scope must be unique. |
| 49 | + * This lint firstly transforms the original XMIR into XMIR that contains `@fqn` |
| 50 | + * attributes for each atom `o`, and then lints it. |
| 51 | + * |
| 52 | + * @since 0.0.31 |
| 53 | + */ |
| 54 | +public final class LtAtomIsNotUnique implements Lint<Map<String, XML>> { |
| 55 | + |
| 56 | + /** |
| 57 | + * Stylesheet for adding `@fqn` attribute for atoms. |
| 58 | + */ |
| 59 | + private final XSL pre; |
| 60 | + |
| 61 | + /** |
| 62 | + * Ctor. |
| 63 | + */ |
| 64 | + public LtAtomIsNotUnique() { |
| 65 | + this( |
| 66 | + new XSLDocument( |
| 67 | + new UncheckedInput( |
| 68 | + new ResourceOf("org/eolang/funcs/atom-fqns.xsl") |
| 69 | + ).stream() |
| 70 | + ) |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Ctor. |
| 76 | + * |
| 77 | + * @param sheet Sheet |
| 78 | + */ |
| 79 | + public LtAtomIsNotUnique(final XSL sheet) { |
| 80 | + this.pre = sheet; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public String name() { |
| 85 | + return "atom-is-not-unique"; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public Collection<Defect> defects(final Map<String, XML> pkg) { |
| 90 | + final Collection<Defect> defects = new LinkedList<>(); |
| 91 | + final Map<XML, List<String>> index = pkg.values().stream() |
| 92 | + .map(this.pre::transform) |
| 93 | + .collect(Collectors.toMap(Function.identity(), LtAtomIsNotUnique::fqns)); |
| 94 | + final Collection<String> checked = new HashSet<>(0); |
| 95 | + index.forEach( |
| 96 | + (xmir, fqns) -> { |
| 97 | + fqns.stream() |
| 98 | + .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) |
| 99 | + .entrySet() |
| 100 | + .stream() |
| 101 | + .filter(entry -> entry.getValue() > 1L) |
| 102 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)) |
| 103 | + .forEach( |
| 104 | + (aname, count) -> |
| 105 | + IntStream.range(0, Math.toIntExact(count)).forEach( |
| 106 | + pos -> defects.add(this.singleDefect(xmir, aname, pos)) |
| 107 | + ) |
| 108 | + ); |
| 109 | + index.forEach( |
| 110 | + (next, names) -> { |
| 111 | + if (!Objects.equals(next, xmir)) { |
| 112 | + final String pair = LtAtomIsNotUnique.pairHash(xmir, next); |
| 113 | + if (!checked.contains(pair)) { |
| 114 | + checked.add(pair); |
| 115 | + names.stream() |
| 116 | + .filter(fqns::contains) |
| 117 | + .forEach( |
| 118 | + aname -> { |
| 119 | + defects.add(this.sharedDefect(next, xmir, aname)); |
| 120 | + defects.add(this.sharedDefect(xmir, next, aname)); |
| 121 | + } |
| 122 | + ); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + ); |
| 127 | + } |
| 128 | + ); |
| 129 | + return defects; |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public String motive() { |
| 134 | + return new UncheckedText( |
| 135 | + new TextOf( |
| 136 | + new ResourceOf( |
| 137 | + String.format( |
| 138 | + "org/eolang/motives/errors/%s.md", this.name() |
| 139 | + ) |
| 140 | + ) |
| 141 | + ) |
| 142 | + ).asString(); |
| 143 | + } |
| 144 | + |
| 145 | + private Defect singleDefect(final XML xmir, final String fqn, final int pos) { |
| 146 | + return new Defect.Default( |
| 147 | + this.name(), |
| 148 | + Severity.ERROR, |
| 149 | + xmir.xpath("/program/@name").stream() |
| 150 | + .findFirst().orElse("unknown"), |
| 151 | + Integer.parseInt( |
| 152 | + xmir.xpath( |
| 153 | + String.format( |
| 154 | + "//o[@atom and @name='%s']/@line", |
| 155 | + LtAtomIsNotUnique.oname(fqn) |
| 156 | + ) |
| 157 | + ).get(pos) |
| 158 | + ), |
| 159 | + String.format("Atom '%s' is duplicated", fqn) |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + private Defect sharedDefect(final XML xmir, final XML original, final String fqn) { |
| 164 | + return new Defect.Default( |
| 165 | + this.name(), |
| 166 | + Severity.ERROR, |
| 167 | + xmir.xpath("/program/@name").stream().findFirst().orElse("unknown"), |
| 168 | + Integer.parseInt( |
| 169 | + xmir.xpath( |
| 170 | + String.format( |
| 171 | + "//o[@atom and @name='%s']/@line", |
| 172 | + LtAtomIsNotUnique.oname(fqn) |
| 173 | + ) |
| 174 | + ).get(0) |
| 175 | + ), |
| 176 | + String.format( |
| 177 | + "Atom with FQN '%s' is duplicated, original was found in '%s'", |
| 178 | + fqn, original.xpath("/program/@name").stream().findFirst().orElse("unknown") |
| 179 | + ) |
| 180 | + ); |
| 181 | + } |
| 182 | + |
| 183 | + private static List<String> fqns(final XML xmir) { |
| 184 | + final List<String> result; |
| 185 | + final List<String> fqns = xmir.xpath("//o[@fqn]/@fqn"); |
| 186 | + if (xmir.nodes("/program/metas/meta[head='package']").size() == 1) { |
| 187 | + final String pack = xmir.xpath("/program/metas/meta[head='package']/tail/text()") |
| 188 | + .get(0); |
| 189 | + result = fqns.stream().map(fqn -> String.format("%s.%s", pack, fqn)) |
| 190 | + .collect(Collectors.toList()); |
| 191 | + } else { |
| 192 | + result = fqns; |
| 193 | + } |
| 194 | + return result.stream() |
| 195 | + .map(fqn -> String.format("Ф.%s", fqn)) |
| 196 | + .collect(Collectors.toList()); |
| 197 | + } |
| 198 | + |
| 199 | + private static String oname(final String fqn) { |
| 200 | + final String result; |
| 201 | + final List<String> parts = new ListOf<>(fqn.split("\\.")); |
| 202 | + if (parts.size() > 1) { |
| 203 | + result = parts.get(parts.size() - 1); |
| 204 | + } else { |
| 205 | + result = parts.get(0); |
| 206 | + } |
| 207 | + return result; |
| 208 | + } |
| 209 | + |
| 210 | + private static String pairHash(final XML first, final XML second) { |
| 211 | + final String pair; |
| 212 | + if (first.hashCode() < second.hashCode()) { |
| 213 | + pair = String.format("%d:%d", first.hashCode(), second.hashCode()); |
| 214 | + } else { |
| 215 | + pair = String.format("%d:%d", second.hashCode(), first.hashCode()); |
| 216 | + } |
| 217 | + return pair; |
| 218 | + } |
| 219 | +} |
0 commit comments