001    package org.andromda.core.common;
002    
003    import de.plushnikov.doctorjim.ImportProcessor;
004    import de.plushnikov.doctorjim.javaparser.ParseException;
005    import java.io.File;
006    import org.apache.log4j.Logger;
007    
008    /**
009     * Implementation of PostProcessor interface to organize imports in java files
010     * @author Plushnikov Michail
011     */
012    public class ImportBeautifierPostProcessorImpl implements PostProcessor
013    {
014        /** The logger instance. */
015        private static final Logger LOGGER = Logger.getLogger(ImportBeautifierPostProcessorImpl.class);
016    
017        /**
018         * Determines if this file should be postprocessed in current postprocessor
019         * @param pFile file for postprocessing
020         * @return true if postprocessing should be done, false sonst
021         */
022        public boolean acceptFile(File pFile)
023        {
024            return null!=pFile && pFile.getName().endsWith(".java");
025        }
026    
027        /**
028         * Postprocess the source
029         * @param pSource the Source for postprocessing
030         * @param pPreviousData the Source of existing file, may be null or empty
031         * @return postprocessed source
032         * @throws Exception on errors occurred
033         */
034        public String postProcess(String pSource, String pPreviousData) throws Exception
035        {
036            try
037            {
038                return new ImportProcessor().organizeImports(pSource);
039            }
040            catch (ParseException ex)
041            {
042                LOGGER.debug("Error PostProcessing ", ex);
043                throw ex;
044            }
045        }
046    }