View Javadoc

1   /*
2    [The "BSD license"]
3    Copyright (c) 2012 Terence Parr
4    Copyright (c) 2012 Sam Harwell
5    All rights reserved.
6   
7    Redistribution and use in source and binary forms, with or without
8    modification, are permitted provided that the following conditions
9    are met:
10   1. Redistributions of source code must retain the above copyright
11      notice, this list of conditions and the following disclaimer.
12   2. Redistributions in binary form must reproduce the above copyright
13      notice, this list of conditions and the following disclaimer in the
14      documentation and/or other materials provided with the distribution.
15   3. The name of the author may not be used to endorse or promote products
16      derived from this software without specific prior written permission.
17  
18   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29  package org.antlr.mojo.antlr4;
30  
31  import org.antlr.v4.Tool;
32  import org.antlr.v4.runtime.misc.NotNull;
33  import org.antlr.v4.tool.ANTLRMessage;
34  import org.antlr.v4.tool.ANTLRToolListener;
35  import org.apache.maven.plugin.logging.Log;
36  import org.sonatype.plexus.build.incremental.BuildContext;
37  import org.stringtemplate.v4.ST;
38  
39  import java.io.File;
40  
41  /**
42   * This implementation of {@link ANTLRToolListener} reports messages to the
43   * {@link Log} instance provided by Maven.
44   *
45   * @author Sam Harwell
46   */
47  public class Antlr4ErrorLog implements ANTLRToolListener {
48  
49      private final Tool tool;
50      private final BuildContext buildContext;
51      private final Log log;
52  
53      /**
54       * Creates an instance of {@link Antlr4ErrorLog}.
55       *
56       * @param log The Maven log
57       */
58      public Antlr4ErrorLog(@NotNull Tool tool, @NotNull BuildContext buildContext, @NotNull Log log) {
59          this.tool = tool;
60          this.buildContext = buildContext;
61          this.log = log;
62      }
63  
64      /**
65       * {@inheritDoc}
66       * <p/>
67       * This implementation passes the message to the Maven log.
68       *
69       * @param message The message to send to Maven
70       */
71      @Override
72      public void info(String message) {
73          if (tool.errMgr.formatWantsSingleLineMessage()) {
74              message = message.replace('\n', ' ');
75          }
76          log.info(message);
77      }
78  
79      /**
80       * {@inheritDoc}
81       * <p/>
82       * This implementation passes the message to the Maven log.
83       *
84       * @param message The message to send to Maven.
85       */
86      @Override
87      public void error(ANTLRMessage message) {
88          ST msgST = tool.errMgr.getMessageTemplate(message);
89          String outputMsg = msgST.render();
90          if (tool.errMgr.formatWantsSingleLineMessage()) {
91              outputMsg = outputMsg.replace('\n', ' ');
92          }
93  
94          log.error(outputMsg);
95  
96          if (message.fileName != null) {
97              String text = message.getMessageTemplate(false).render();
98              buildContext.addMessage(new File(message.fileName), message.line, message.charPosition, text, BuildContext.SEVERITY_ERROR, message.getCause());
99          }
100     }
101 
102     /**
103      * {@inheritDoc}
104      * <p/>
105      * This implementation passes the message to the Maven log.
106      *
107      * @param message
108      */
109     @Override
110     public void warning(ANTLRMessage message) {
111         ST msgST = tool.errMgr.getMessageTemplate(message);
112         String outputMsg = msgST.render();
113         if (tool.errMgr.formatWantsSingleLineMessage()) {
114             outputMsg = outputMsg.replace('\n', ' ');
115         }
116 
117         log.warn(outputMsg);
118 
119         if (message.fileName != null) {
120             String text = message.getMessageTemplate(false).render();
121             buildContext.addMessage(new File(message.fileName), message.line, message.charPosition, text, BuildContext.SEVERITY_WARNING, message.getCause());
122         }
123     }
124 }