View Javadoc
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.message;
20  
21  /**
22   * Message builder that supports configurable styling.
23   *
24   * @since 3.10.0
25   * @see MessageBuilderFactory
26   */
27  public interface MessageBuilder extends Appendable {
28  
29      /**
30       * Append message content in trace style.
31       * By default, bold magenta
32       *
33       * @param message the message to append
34       * @return the current builder
35       */
36      default MessageBuilder trace(Object message) {
37          return style("." + Constants.MAVEN_STYLE_TRACE_NAME + ":-" + Constants.MAVEN_STYLE_TRACE_DEFAULT, message);
38      }
39  
40      /**
41       * Append message content in debug style.
42       * By default, bold cyan
43       *
44       * @param message the message to append
45       * @return the current builder
46       */
47      default MessageBuilder debug(Object message) {
48          return style("." + Constants.MAVEN_STYLE_DEBUG_NAME + ":-" + Constants.MAVEN_STYLE_DEBUG_DEFAULT, message);
49      }
50  
51      /**
52       * Append message content in info style.
53       * By default, bold blue
54       *
55       * @param message the message to append
56       * @return the current builder
57       */
58      default MessageBuilder info(Object message) {
59          return style("." + Constants.MAVEN_STYLE_INFO_NAME + ":-" + Constants.MAVEN_STYLE_INFO_DEFAULT, message);
60      }
61  
62      /**
63       * Append message content in warning style.
64       * By default, bold yellow
65       *
66       * @param message the message to append
67       * @return the current builder
68       */
69      default MessageBuilder warning(Object message) {
70          return style("." + Constants.MAVEN_STYLE_WARNING_NAME + ":-" + Constants.MAVEN_STYLE_WARNING_DEFAULT, message);
71      }
72  
73      /**
74       * Append message content in error style.
75       * By default, bold red
76       *
77       * @param message the message to append
78       * @return the current builder
79       */
80      default MessageBuilder error(Object message) {
81          return style("." + Constants.MAVEN_STYLE_ERROR_NAME + ":-" + Constants.MAVEN_STYLE_ERROR_DEFAULT, message);
82      }
83  
84      /**
85       * Append message content in success style.
86       * By default, bold green
87       *
88       * @param message the message to append
89       * @return the current builder
90       */
91      default MessageBuilder success(Object message) {
92          return style("." + Constants.MAVEN_STYLE_SUCCESS_NAME + ":-" + Constants.MAVEN_STYLE_SUCCESS_DEFAULT, message);
93      }
94  
95      /**
96       * Append message content in failure style.
97       * By default, bold red
98       *
99       * @param message the message to append
100      * @return the current builder
101      */
102     default MessageBuilder failure(Object message) {
103         return style("." + Constants.MAVEN_STYLE_FAILURE_NAME + ":-" + Constants.MAVEN_STYLE_FAILURE_DEFAULT, message);
104     }
105 
106     /**
107      * Append message content in strong style.
108      * By default, bold
109      *
110      * @param message the message to append
111      * @return the current builder
112      */
113     default MessageBuilder strong(Object message) {
114         return style("." + Constants.MAVEN_STYLE_STRONG_NAME + ":-" + Constants.MAVEN_STYLE_STRONG_DEFAULT, message);
115     }
116 
117     /**
118      * Append message content in mojo style.
119      * By default, green
120      *
121      * @param message the message to append
122      * @return the current builder
123      */
124     default MessageBuilder mojo(Object message) {
125         return style("." + Constants.MAVEN_STYLE_MOJO_NAME + ":-" + Constants.MAVEN_STYLE_MOJO_DEFAULT, message);
126     }
127 
128     /**
129      * Append message content in project style.
130      * By default, cyan
131      *
132      * @param message the message to append
133      * @return the current builder
134      */
135     default MessageBuilder project(Object message) {
136         return style("." + Constants.MAVEN_STYLE_PROJECT_NAME + ":-" + Constants.MAVEN_STYLE_PROJECT_DEFAULT, message);
137     }
138 
139     default MessageBuilder style(String style, Object message) {
140         return style(style).a(message).resetStyle();
141     }
142 
143     MessageBuilder style(String style);
144 
145     MessageBuilder resetStyle();
146 
147     //
148     // message building methods modelled after Ansi methods
149     //
150 
151     @Override
152     MessageBuilder append(CharSequence cs);
153 
154     @Override
155     MessageBuilder append(CharSequence cs, int start, int end);
156 
157     @Override
158     MessageBuilder append(char c);
159 
160     /**
161      * Append content to the message buffer.
162      *
163      * @param value the content to append
164      * @param offset the index of the first {@code char} to append
165      * @param len the number of {@code char}s to append
166      * @return the current builder
167      */
168     default MessageBuilder a(char[] value, int offset, int len) {
169         return append(String.valueOf(value, offset, len));
170     }
171 
172     /**
173      * Append content to the message buffer.
174      *
175      * @param value the content to append
176      * @return the current builder
177      */
178     default MessageBuilder a(char[] value) {
179         return append(String.valueOf(value));
180     }
181 
182     /**
183      * Append content to the message buffer.
184      *
185      * @param value the content to append
186      * @param start the starting index of the subsequence to be appended
187      * @param end the end index of the subsequence to be appended
188      * @return the current builder
189      */
190     default MessageBuilder a(CharSequence value, int start, int end) {
191         return append(value, start, end);
192     }
193 
194     /**
195      * Append content to the message buffer.
196      *
197      * @param value the content to append
198      * @return the current builder
199      */
200     default MessageBuilder a(CharSequence value) {
201         return append(value);
202     }
203 
204     /**
205      * Append content to the message buffer.
206      *
207      * @param value the content to append
208      * @return the current builder
209      */
210     default MessageBuilder a(Object value) {
211         return append(String.valueOf(value));
212     }
213 
214     /**
215      * Append newline to the message buffer.
216      *
217      * @return the current builder
218      */
219     default MessageBuilder newline() {
220         return append(System.lineSeparator());
221     }
222 
223     /**
224      * Append formatted content to the buffer.
225      * @see String#format(String, Object...)
226      *
227      * @param pattern a <a href="../util/Formatter.html#syntax">format string</a>
228      * @param args arguments referenced by the format specifiers in the format string
229      * @return the current builder
230      */
231     default MessageBuilder format(String pattern, Object... args) {
232         return append(String.format(pattern, args));
233     }
234 
235     /**
236      * Set the buffer length.
237      *
238      * @param length the new length
239      * @return the current builder
240      */
241     MessageBuilder setLength(int length);
242 
243     /**
244      * Return the built message.
245      *
246      * @return the message
247      */
248     String build();
249 
250     class Constants {
251         // Style Names
252         public static final String MAVEN_STYLE_TRANSFER_NAME = "transfer";
253         public static final String MAVEN_STYLE_TRACE_NAME = "trace";
254         public static final String MAVEN_STYLE_DEBUG_NAME = "debug";
255         public static final String MAVEN_STYLE_INFO_NAME = "info";
256         public static final String MAVEN_STYLE_WARNING_NAME = "warning";
257         public static final String MAVEN_STYLE_ERROR_NAME = "error";
258         public static final String MAVEN_STYLE_SUCCESS_NAME = "success";
259         public static final String MAVEN_STYLE_FAILURE_NAME = "failure";
260         public static final String MAVEN_STYLE_STRONG_NAME = "strong";
261         public static final String MAVEN_STYLE_MOJO_NAME = "mojo";
262         public static final String MAVEN_STYLE_PROJECT_NAME = "project";
263 
264         // Default Values
265         public static final String MAVEN_STYLE_TRANSFER_DEFAULT = "f:bright-black";
266         public static final String MAVEN_STYLE_TRACE_DEFAULT = "bold,f:magenta";
267         public static final String MAVEN_STYLE_DEBUG_DEFAULT = "bold,f:cyan";
268         public static final String MAVEN_STYLE_INFO_DEFAULT = "bold,f:blue";
269         public static final String MAVEN_STYLE_WARNING_DEFAULT = "bold,f:yellow";
270         public static final String MAVEN_STYLE_ERROR_DEFAULT = "bold,f:red";
271         public static final String MAVEN_STYLE_SUCCESS_DEFAULT = "bold,f:green";
272         public static final String MAVEN_STYLE_FAILURE_DEFAULT = "bold,f:red";
273         public static final String MAVEN_STYLE_STRONG_DEFAULT = "bold";
274         public static final String MAVEN_STYLE_MOJO_DEFAULT = "f:green";
275         public static final String MAVEN_STYLE_PROJECT_DEFAULT = "f:cyan";
276     }
277 }