3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.api.batch.sensor.issue.internal;
22 import java.util.ArrayList;
23 import java.util.List;
24 import javax.annotation.Nullable;
25 import org.sonar.api.batch.fs.InputComponent;
26 import org.sonar.api.batch.fs.TextRange;
27 import org.sonar.api.batch.fs.internal.DefaultInputFile;
28 import org.sonar.api.batch.sensor.issue.IssueLocation;
29 import org.sonar.api.batch.sensor.issue.MessageFormatting;
30 import org.sonar.api.batch.sensor.issue.NewIssueLocation;
31 import org.sonar.api.batch.sensor.issue.NewMessageFormatting;
33 import static java.util.Objects.requireNonNull;
34 import static org.apache.commons.lang.StringUtils.abbreviate;
35 import static org.apache.commons.lang.StringUtils.trim;
36 import static org.sonar.api.utils.Preconditions.checkArgument;
37 import static org.sonar.api.utils.Preconditions.checkState;
39 public class DefaultIssueLocation implements NewIssueLocation, IssueLocation {
41 private InputComponent component;
42 private TextRange textRange;
43 private String message;
44 private final List<MessageFormatting> messageFormattings = new ArrayList<>();
47 public DefaultIssueLocation on(InputComponent component) {
48 checkArgument(component != null, "Component can't be null");
49 checkState(this.component == null, "on() already called");
50 this.component = component;
55 public DefaultIssueLocation at(TextRange location) {
56 checkState(this.component != null, "at() should be called after on()");
57 checkState(this.component.isFile(), "at() should be called only for an InputFile.");
58 DefaultInputFile file = (DefaultInputFile) this.component;
59 file.validate(location);
60 this.textRange = location;
65 public DefaultIssueLocation message(String message) {
66 validateMessage(message);
67 this.message = abbreviate(trim(message), MESSAGE_MAX_SIZE);
72 public NewIssueLocation message(String message, List<NewMessageFormatting> newMessageFormattings) {
73 validateMessage(message);
74 validateFormattings(newMessageFormattings, message);
75 this.message = abbreviate(trim(message), MESSAGE_MAX_SIZE);
77 for (NewMessageFormatting newMessageFormatting : newMessageFormattings) {
78 messageFormattings.add((MessageFormatting) newMessageFormatting);
83 private static void validateFormattings(List<NewMessageFormatting> newMessageFormattings, String message) {
84 checkArgument(newMessageFormattings != null, "messageFormattings can't be null");
85 newMessageFormattings.stream()
86 .map(DefaultMessageFormatting.class::cast)
87 .forEach(e -> e.validate(message));
90 private void validateMessage(String message) {
91 requireNonNull(message, "Message can't be null");
92 if (message.contains("\u0000")) {
93 throw new IllegalArgumentException(unsupportedCharacterError(message, component));
98 public NewMessageFormatting newMessageFormatting() {
99 return new DefaultMessageFormatting();
102 private static String unsupportedCharacterError(String message, @Nullable InputComponent component) {
103 String error = "Character \\u0000 is not supported in issue message '" + message + "'";
104 if (component != null) {
105 error += ", on component: " + component.toString();
111 public InputComponent inputComponent() {
112 return this.component;
116 public TextRange textRange() {
121 public String message() {
126 public List<MessageFormatting> messageFormattings() {
127 return this.messageFormattings;