001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools;
016    
017    import com.liferay.portal.kernel.util.StringUtil;
018    import com.liferay.portal.kernel.util.Validator;
019    
020    import java.io.IOException;
021    
022    import java.util.regex.Matcher;
023    import java.util.regex.Pattern;
024    
025    /**
026     * @author Carlos Sierra Andr??s
027     * @author Andr?? de Oliveira
028     * @author Raymond Aug??
029     * @author Hugo Huijser
030     */
031    public class JavaImportsFormatter extends BaseImportsFormatter {
032    
033            public static String getImports(String content) {
034                    Matcher matcher = _importsPattern.matcher(content);
035    
036                    if (matcher.find()) {
037                            return matcher.group();
038                    }
039    
040                    return null;
041            }
042    
043            @Override
044            protected ImportPackage createImportPackage(String line) {
045                    return createJavaImportPackage(line);
046            }
047    
048            @Override
049            protected String doFormat(
050                            String content, Pattern importPattern, String packagePath,
051                            String className)
052                    throws IOException {
053    
054                    String imports = getImports(content);
055    
056                    if (Validator.isNull(imports)) {
057                            return content;
058                    }
059    
060                    String newImports = stripUnusedImports(
061                            imports, content, packagePath, className, "\\*");
062    
063                    newImports = sortAndGroupImports(newImports);
064    
065                    if (!imports.equals(newImports)) {
066                            content = StringUtil.replaceFirst(content, imports, newImports);
067                    }
068    
069                    content = content.replaceFirst(
070                            "(?m)^[ \t]*(package .*;)\\s*^[ \t]*import", "$1\n\nimport");
071    
072                    content = content.replaceFirst(
073                            "(?m)^[ \t]*((?:package|import) .*;)\\s*^[ \t]*/\\*\\*",
074                            "$1\n\n/**");
075    
076                    return ToolsUtil.stripFullyQualifiedClassNames(
077                            content, newImports, packagePath);
078            }
079    
080            private static final Pattern _importsPattern = Pattern.compile(
081                    "(^[ \t]*import\\s+.*;\n+)+", Pattern.MULTILINE);
082    
083    }