001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.maven.enforcer.rules; 020 021import org.apache.maven.enforcer.rule.api.AbstractEnforcerRule; 022import org.apache.maven.model.InputLocation; 023import org.apache.maven.project.MavenProject; 024 025/** 026 * Abstract help rule. 027 * 028 * @author Slawomir Jaranowski 029 * @since 3.2.0 030 */ 031public abstract class AbstractStandardEnforcerRule extends AbstractEnforcerRule { 032 033 private String message; 034 035 public String getMessage() { 036 return message; 037 } 038 039 public void setMessage(String message) { 040 this.message = message; 041 } 042 043 /** 044 * Returns an identifier of a given project. 045 * @param project the project 046 * @return the identifier of the project in the format {@code <groupId>:<artifactId>:<version>} 047 */ 048 private static String getProjectId(MavenProject project) { 049 StringBuilder buffer = new StringBuilder(128); 050 051 buffer.append( 052 (project.getGroupId() != null && project.getGroupId().length() > 0) 053 ? project.getGroupId() 054 : "[unknown-group-id]"); 055 buffer.append(':'); 056 buffer.append( 057 (project.getArtifactId() != null && project.getArtifactId().length() > 0) 058 ? project.getArtifactId() 059 : "[unknown-artifact-id]"); 060 buffer.append(':'); 061 buffer.append( 062 (project.getVersion() != null && project.getVersion().length() > 0) 063 ? project.getVersion() 064 : "[unknown-version]"); 065 066 return buffer.toString(); 067 } 068 069 /** 070 * Creates a string with line/column information for problems originating directly from this POM. Inspired by 071 * {@code o.a.m.model.building.ModelProblemUtils.formatLocation(...)}. 072 * 073 * @param project the current project. 074 * @param location The location which should be formatted, must not be {@code null}. 075 * @return The formatted problem location or an empty string if unknown, never {@code null}. 076 */ 077 protected static String formatLocation(MavenProject project, InputLocation location) { 078 StringBuilder buffer = new StringBuilder(); 079 080 if (!location.getSource().getModelId().equals(getProjectId(project))) { 081 buffer.append(location.getSource().getModelId()); 082 083 if (location.getSource().getLocation().length() > 0) { 084 if (buffer.length() > 0) { 085 buffer.append(", "); 086 } 087 buffer.append(location.getSource().getLocation()); 088 } 089 } 090 if (location.getLineNumber() > 0) { 091 if (buffer.length() > 0) { 092 buffer.append(", "); 093 } 094 buffer.append("line ").append(location.getLineNumber()); 095 } 096 if (location.getColumnNumber() > 0) { 097 if (buffer.length() > 0) { 098 buffer.append(", "); 099 } 100 buffer.append("column ").append(location.getColumnNumber()); 101 } 102 return buffer.toString(); 103 } 104}