001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2007-2010 ADEQUATE SYSTEMS SARL
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.generator.ant;
022    
023    import java.io.PrintWriter;
024    import java.io.StringWriter;
025    
026    import org.apache.tools.ant.Project;
027    import org.apache.tools.ant.Task;
028    import org.granite.generator.Input;
029    import org.granite.generator.Listener;
030    import org.granite.generator.Output;
031    
032    /**
033     * @author Franck WOLFF
034     */
035    public class AntListener implements Listener {
036    
037        private final Task task;
038    
039        public AntListener(Task task) {
040            this.task = task;
041        }
042    
043            public void generating(Input<?> input, Output<?> output) {
044                    log("  Generating: " + output.getDescription() + " (" + output.getMessage() + ")", Project.MSG_INFO, null);
045            }
046    
047            public void generating(String file, String message) {
048                    log("  Generating: " + file + " (" + message + ")", Project.MSG_INFO, null);
049            }
050    
051            public void skipping(Input<?> input, Output<?> output) {
052                    log("  Skipping: " + output.getDescription() + " (" + output.getMessage() + ")", Project.MSG_DEBUG, null);
053            }
054    
055            public void skipping(String file, String message) {
056                    log("  Skipping: " + file + " (" + message + ")", Project.MSG_DEBUG, null);
057            }
058    
059            public void debug(String message) {
060            log(message, Project.MSG_DEBUG, null);
061            }
062    
063            public void debug(String message, Throwable t) {
064            log(message, Project.MSG_DEBUG, t);
065            }
066    
067        public void info(String message) {
068            log(message, Project.MSG_INFO, null);
069        }
070        public void info(String message, Throwable t) {
071            log(message, Project.MSG_INFO, t);
072        }
073    
074        public void warn(String message) {
075            log(message, Project.MSG_WARN, null);
076        }
077        public void warn(String message, Throwable t) {
078            log(message, Project.MSG_WARN, t);
079        }
080    
081        public void error(String message) {
082            log(message, Project.MSG_ERR, null);
083        }
084        public void error(String message, Throwable t) {
085            log(message, Project.MSG_ERR, t);
086        }
087    
088        private void log(String message, int level, Throwable t) {
089            if (t != null)
090                message += "\n" + getStackTrace(t);
091            task.log(message, level);
092        }
093    
094        private static String getStackTrace(Throwable t) {
095            StringWriter sw = new StringWriter();
096            PrintWriter pw = new PrintWriter(sw);
097            t.printStackTrace(pw);
098            return sw.toString();
099        }
100    }