001    /*
002     * Copyright (C) 2010 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.groovy;
021    
022    import org.codehaus.groovy.ast.ASTNode;
023    import org.codehaus.groovy.ast.ClassHelper;
024    import org.codehaus.groovy.control.CompilePhase;
025    import org.codehaus.groovy.control.SourceUnit;
026    import org.codehaus.groovy.transform.ASTTransformation;
027    import org.codehaus.groovy.transform.GroovyASTTransformation;
028    import org.crsh.cmdline.annotations.Argument;
029    import org.crsh.cmdline.annotations.Command;
030    import org.crsh.cmdline.annotations.Required;
031    import org.crsh.cmdline.annotations.Usage;
032    import org.crsh.cmdline.annotations.Man;
033    import org.crsh.cmdline.annotations.Option;
034    import org.crsh.command.CRaSHCommand;
035    import org.crsh.command.InvocationContext;
036    import org.crsh.command.ScriptException;
037    import org.crsh.text.Color;
038    import org.crsh.text.Decoration;
039    import org.crsh.text.Style;
040    import org.slf4j.Logger;
041    import org.slf4j.LoggerFactory;
042    
043    /**
044     * This class is a Groovy transformation that adds default import to CRaSH commands.
045     *
046     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
047     * @version $Revision$
048     */
049    @GroovyASTTransformation(phase= CompilePhase.CONVERSION)
050    public class DefaultImportTransformer implements ASTTransformation {
051    
052      /** . */
053      private static final Logger log = LoggerFactory.getLogger(DefaultImportTransformer.class);
054    
055      /** . */
056      private static final Class<?>[] defaultImports = {
057        Required.class,
058        Man.class,
059        Usage.class,
060        Argument.class,
061        Option.class,
062        Command.class,
063        ScriptException.class,
064        InvocationContext.class,
065        CRaSHCommand.class,
066      };
067    
068      /** . */
069      private static final Class<?>[] defaultStaticImports = {
070        Color.class,
071        Decoration.class,
072        Style.class
073      };
074    
075      public void visit(ASTNode[] nodes, final SourceUnit source) {
076        log.debug("Transforming source to add default import package");
077        for (Class<?> defaultImport : defaultImports) {
078          log.debug("Adding default import for class " + defaultImport.getName());
079          if (source.getAST().getImport(defaultImport.getSimpleName()) == null) {
080            source.getAST().addImport(defaultImport.getSimpleName(), ClassHelper.make(defaultImport));
081          }
082        }
083        for (Class<?> defaultStaticImport : defaultStaticImports) {
084          log.debug("Adding default static import for class " + defaultStaticImport.getName());
085          if (!source.getAST().getStaticStarImports().containsKey(defaultStaticImport.getSimpleName())) {
086            source.getAST().addStaticStarImport(defaultStaticImport.getSimpleName(), ClassHelper.make(defaultStaticImport));
087          }
088        }
089      }
090    }