3 * Copyright (C) 2009-2023 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;
32 import org.sonar.api.issue.Issue;
34 import static java.util.Objects.requireNonNull;
35 import static org.apache.commons.lang.StringUtils.abbreviate;
36 import static org.apache.commons.lang.StringUtils.trim;
37 import static org.sonar.api.utils.Preconditions.checkArgument;
38 import static org.sonar.api.utils.Preconditions.checkState;
40 public class DefaultIssueLocation implements NewIssueLocation, IssueLocation {
42 private InputComponent component;
43 private TextRange textRange;
44 private String message;
45 private final List<MessageFormatting> messageFormattings = new ArrayList<>();
48 public DefaultIssueLocation on(InputComponent component) {
49 checkArgument(component != null, "Component can't be null");
50 checkState(this.component == null, "on() already called");
51 this.component = component;
56 public DefaultIssueLocation at(TextRange location) {
57 checkState(this.component != null, "at() should be called after on()");
58 checkState(this.component.isFile(), "at() should be called only for an InputFile.");
59 DefaultInputFile file = (DefaultInputFile) this.component;
60 file.validate(location);
61 this.textRange = location;
66 public DefaultIssueLocation message(String message) {
67 validateMessage(message);
68 this.message = abbreviate(trim(message), Issue.MESSAGE_MAX_SIZE);
73 public DefaultIssueLocation message(String message, List<NewMessageFormatting> newMessageFormattings) {
74 validateMessage(message);
75 validateFormattings(newMessageFormattings, message);
76 this.message = abbreviate(message, Issue.MESSAGE_MAX_SIZE);
78 for (NewMessageFormatting newMessageFormatting : newMessageFormattings) {
79 DefaultMessageFormatting messageFormatting = (DefaultMessageFormatting) newMessageFormatting;
80 if (messageFormatting.start() > Issue.MESSAGE_MAX_SIZE) {
83 if (messageFormatting.end() > Issue.MESSAGE_MAX_SIZE) {
84 messageFormatting = new DefaultMessageFormatting()
85 .start(messageFormatting.start())
86 .end( Issue.MESSAGE_MAX_SIZE)
87 .type(messageFormatting.type());
89 messageFormattings.add(messageFormatting);
94 private static void validateFormattings(List<NewMessageFormatting> newMessageFormattings, String message) {
95 checkArgument(newMessageFormattings != null, "messageFormattings can't be null");
96 newMessageFormattings.stream()
97 .map(DefaultMessageFormatting.class::cast)
98 .forEach(e -> e.validate(message));
101 private void validateMessage(String message) {
102 requireNonNull(message, "Message can't be null");
103 if (message.contains("\u0000")) {
104 throw new IllegalArgumentException(unsupportedCharacterError(message, component));
109 public NewMessageFormatting newMessageFormatting() {
110 return new DefaultMessageFormatting();
113 private static String unsupportedCharacterError(String message, @Nullable InputComponent component) {
114 String error = "Character \\u0000 is not supported in issue message '" + message + "'";
115 if (component != null) {
116 error += ", on component: " + component.toString();
122 public InputComponent inputComponent() {
123 return this.component;
127 public TextRange textRange() {
132 public String message() {
137 public List<MessageFormatting> messageFormattings() {
138 return this.messageFormattings;