1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.enforcer.rules;
20
21 import org.apache.maven.enforcer.rule.api.AbstractEnforcerRule;
22 import org.apache.maven.model.InputLocation;
23 import org.apache.maven.project.MavenProject;
24
25 /**
26 * Abstract help rule.
27 *
28 * @author Slawomir Jaranowski
29 * @since 3.2.0
30 */
31 public abstract class AbstractStandardEnforcerRule extends AbstractEnforcerRule {
32
33 private String message;
34
35 public String getMessage() {
36 return message;
37 }
38
39 public void setMessage(String message) {
40 this.message = message;
41 }
42
43 /**
44 * Returns an identifier of a given project.
45 * @param project the project
46 * @return the identifier of the project in the format {@code <groupId>:<artifactId>:<version>}
47 */
48 private static String getProjectId(MavenProject project) {
49 StringBuilder buffer = new StringBuilder(128);
50
51 buffer.append(
52 (project.getGroupId() != null && project.getGroupId().length() > 0)
53 ? project.getGroupId()
54 : "[unknown-group-id]");
55 buffer.append(':');
56 buffer.append(
57 (project.getArtifactId() != null && project.getArtifactId().length() > 0)
58 ? project.getArtifactId()
59 : "[unknown-artifact-id]");
60 buffer.append(':');
61 buffer.append(
62 (project.getVersion() != null && project.getVersion().length() > 0)
63 ? project.getVersion()
64 : "[unknown-version]");
65
66 return buffer.toString();
67 }
68
69 /**
70 * Creates a string with line/column information for problems originating directly from this POM. Inspired by
71 * {@code o.a.m.model.building.ModelProblemUtils.formatLocation(...)}.
72 *
73 * @param project the current project.
74 * @param location The location which should be formatted, must not be {@code null}.
75 * @return The formatted problem location or an empty string if unknown, never {@code null}.
76 */
77 protected static String formatLocation(MavenProject project, InputLocation location) {
78 StringBuilder buffer = new StringBuilder();
79
80 if (!location.getSource().getModelId().equals(getProjectId(project))) {
81 buffer.append(location.getSource().getModelId());
82
83 if (location.getSource().getLocation().length() > 0) {
84 if (buffer.length() > 0) {
85 buffer.append(", ");
86 }
87 buffer.append(location.getSource().getLocation());
88 }
89 }
90 if (location.getLineNumber() > 0) {
91 if (buffer.length() > 0) {
92 buffer.append(", ");
93 }
94 buffer.append("line ").append(location.getLineNumber());
95 }
96 if (location.getColumnNumber() > 0) {
97 if (buffer.length() > 0) {
98 buffer.append(", ");
99 }
100 buffer.append("column ").append(location.getColumnNumber());
101 }
102 return buffer.toString();
103 }
104 }