1 /*
2 * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3 * SPDX-License-Identifier: MIT
4 */
5 package com.qulice.pmd;
6
7 import net.sourceforge.pmd.reporting.Report;
8 import net.sourceforge.pmd.reporting.RuleViolation;
9 import org.cactoos.text.FormattedText;
10 import org.cactoos.text.UncheckedText;
11
12 /**
13 * Represents one PMD error (usually it will be violation).
14 * @since 1.0
15 */
16 public interface PmdError {
17
18 /**
19 * Returns error name which is short, fixed, human-readable category of
20 * the error.
21 * @return Error name
22 */
23 String name();
24
25 /**
26 * Returns file name which caused this error.
27 * May return sentinel value if file information is not available.
28 * @return File name
29 */
30 String fileName();
31
32 /**
33 * Returns formatted line range which cause this error.
34 * May return sentinel value if line information is not available.
35 * @return Formatted line range
36 */
37 String lines();
38
39 /**
40 * Returns error description.
41 * @return Description
42 */
43 String description();
44
45 /**
46 * PmdError backed by a RuleViolation.
47 * @since 1.0
48 */
49 final class OfRuleViolation implements PmdError {
50
51 /**
52 * Internal RuleViolation.
53 */
54 private final RuleViolation violation;
55
56 /**
57 * Creates a new PmdError, representing given RuleViolation.
58 * @param violation Internal RuleViolation
59 */
60 public OfRuleViolation(final RuleViolation violation) {
61 this.violation = violation;
62 }
63
64 @Override
65 public String name() {
66 return this.violation.getRule().getName();
67 }
68
69 @Override
70 public String fileName() {
71 return this.violation.getFileId().getAbsolutePath();
72 }
73
74 @Override
75 public String lines() {
76 return String.format(
77 "%d-%d",
78 this.violation.getBeginLine(), this.violation.getEndLine()
79 );
80 }
81
82 @Override
83 public String description() {
84 return this.violation.getDescription();
85 }
86 }
87
88 /**
89 * PmdError backed by a ProcessingError.
90 * @since 1.0
91 */
92 final class OfProcessingError implements PmdError {
93
94 /**
95 * Internal ProcessingError.
96 */
97 private final Report.ProcessingError error;
98
99 /**
100 * Creates a new PmdError, representing given ProcessingError.
101 * @param error Internal ProcessingError
102 */
103 public OfProcessingError(final Report.ProcessingError error) {
104 this.error = error;
105 }
106
107 @Override
108 public String name() {
109 return "ProcessingError";
110 }
111
112 @Override
113 public String fileName() {
114 return this.error.getFileId().getAbsolutePath();
115 }
116
117 @Override
118 public String lines() {
119 return "unknown";
120 }
121
122 @Override
123 public String description() {
124 return new UncheckedText(
125 new FormattedText(
126 "%s: %s",
127 this.error.getMsg(),
128 this.error.getDetail()
129 )
130 ).asString();
131 }
132 }
133
134 /**
135 * PmdError backed by a ConfigError.
136 * @since 1.0
137 */
138 final class OfConfigError implements PmdError {
139
140 /**
141 * Internal ConfigError.
142 */
143 private final Report.ConfigurationError error;
144
145 /**
146 * Creates a new PmdError, representing given ConfigurationError.
147 * @param error Internal ConfigurationError
148 */
149 public OfConfigError(final Report.ConfigurationError error) {
150 this.error = error;
151 }
152
153 @Override
154 public String name() {
155 return "ConfigurationError";
156 }
157
158 @Override
159 public String fileName() {
160 return "unknown";
161 }
162
163 @Override
164 public String lines() {
165 return "unknown";
166 }
167
168 @Override
169 public String description() {
170 return this.error.issue();
171 }
172 }
173 }