View Javadoc

1   package org.apache.maven.jelly;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.io.IOException;
22  import java.util.Stack;
23  
24  import org.apache.commons.jelly.XMLOutput;
25  import org.apache.tools.ant.BuildEvent;
26  import org.apache.tools.ant.BuildListener;
27  import org.apache.tools.ant.Project;
28  import org.xml.sax.SAXException;
29  
30  public class JellyBuildListener
31      implements BuildListener
32  {
33      private XMLOutput out;
34  
35      private Stack taskStack;
36  
37      private boolean debug;
38  
39      /** Whether or not to use emacs-style output */
40      protected boolean emacsMode = false;
41  
42      public JellyBuildListener( XMLOutput out )
43      {
44          this.taskStack = new Stack();
45          this.out = out;
46          this.debug = false;
47      }
48  
49      /**
50       * Sets this logger to produce emacs (and other editor) friendly output.
51       *
52       * @param emacsMode <code>true</code> if output is to be unadorned so that
53       *                  emacs and other editors can parse files names, etc.
54       */
55      public void setEmacsMode( boolean emacsMode )
56      {
57          this.emacsMode = emacsMode;
58      }
59  
60      public boolean isDebug()
61      {
62          return this.debug;
63      }
64  
65      /**
66       * @deprecated Use setDebug (deprecated for 1.0-RC1)
67       */
68      public void isDebug( boolean newDebug )
69      {
70          setDebug( newDebug );
71      }
72  
73      public void setDebug( boolean debug )
74      {
75          this.debug = debug;
76      }
77  
78      public void buildFinished( BuildEvent event )
79      {
80      }
81  
82      public void buildStarted( BuildEvent event )
83      {
84      }
85  
86      public void messageLogged( BuildEvent event )
87      {
88          if ( ( event.getPriority() > Project.MSG_INFO ) && !isDebug() )
89          {
90              return;
91          }
92  
93          try
94          {
95              if ( emacsMode )
96              {
97                  out.write( event.getMessage() + "\n" );
98                  out.flush();
99                  return;
100             }
101 
102             if ( !this.taskStack.isEmpty() )
103             {
104                 out.write( "    [" + this.taskStack.peek() + "] " );
105             }
106 
107             switch ( event.getPriority() )
108             {
109                 case ( Project.MSG_ERR       ):
110                     out.write( "[ERROR] " );
111                     break;
112                 case ( Project.MSG_WARN       ):
113                     // out.write( "[WARN] ");
114                     break;
115                 case ( Project.MSG_INFO       ):
116                     // normal, do nothing.
117                     break;
118                 case ( Project.MSG_VERBOSE       ):
119                     out.write( "[VERBOSE] " );
120                     break;
121                 case ( Project.MSG_DEBUG       ):
122                     out.write( "[DEBUG] " );
123                     break;
124             }
125 
126             out.write( event.getMessage() + "\n" );
127             out.flush();
128         }
129         catch ( SAXException e )
130         {
131             // fall-back to stderr.
132             System.err.println( event.getMessage() );
133             System.err.flush();
134         }
135         catch ( IOException e )
136         {
137             // ignore
138         }
139 
140     }
141 
142     public void targetFinished( BuildEvent event )
143     {
144     }
145 
146     public void targetStarted( BuildEvent event )
147     {
148     }
149 
150     public void taskFinished( BuildEvent event )
151     {
152         this.taskStack.pop();
153     }
154 
155     public void taskStarted( BuildEvent event )
156     {
157         this.taskStack.push( event.getTask().getTaskName() );
158     }
159 }