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.io.unsync.UnsyncBufferedReader;
018    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.ClassUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    
025    import java.io.IOException;
026    
027    import java.util.Set;
028    import java.util.regex.Matcher;
029    import java.util.regex.Pattern;
030    
031    /**
032     * @author Carlos Sierra Andr??s
033     * @author Andr?? de Oliveira
034     * @author Raymond Aug??
035     */
036    public class JavaImportsFormatter extends ImportsFormatter {
037    
038            public static String getImports(String content) {
039                    Matcher matcher = _importsPattern.matcher(content);
040    
041                    if (matcher.find()) {
042                            return matcher.group();
043                    }
044    
045                    return null;
046            }
047    
048            public static String stripJavaImports(
049                            String content, String packageDir, String className)
050                    throws IOException {
051    
052                    String imports = getImports(content);
053    
054                    if (Validator.isNull(imports)) {
055                            return content;
056                    }
057    
058                    Set<String> classes = ClassUtil.getClasses(
059                            new UnsyncStringReader(content), className);
060    
061                    StringBundler sb = new StringBundler();
062    
063                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
064                            new UnsyncStringReader(imports));
065    
066                    String line = null;
067    
068                    while ((line = unsyncBufferedReader.readLine()) != null) {
069                            int x = line.indexOf("import ");
070    
071                            if (x == -1) {
072                                    continue;
073                            }
074    
075                            int y = line.lastIndexOf(CharPool.PERIOD);
076    
077                            String importPackage = line.substring(x + 7, y);
078    
079                            if (importPackage.equals(packageDir) ||
080                                    importPackage.equals("java.lang")) {
081    
082                                    continue;
083                            }
084    
085                            String importClass = line.substring(y + 1, line.length() - 1);
086    
087                            if (importClass.equals("*") || classes.contains(importClass)) {
088                                    sb.append(line);
089                                    sb.append("\n");
090                            }
091                    }
092    
093                    ImportsFormatter importsFormatter = new JavaImportsFormatter();
094    
095                    String newImports = importsFormatter.format(sb.toString());
096    
097                    if (!imports.equals(newImports)) {
098                            content = StringUtil.replaceFirst(content, imports, newImports);
099                    }
100    
101                    // Ensure a blank line exists between the package and the first import
102    
103                    content = content.replaceFirst(
104                            "(?m)^[ \t]*(package .*;)\\s*^[ \t]*import", "$1\n\nimport");
105    
106                    // Ensure a blank line exists between the last import (or package if
107                    // there are no imports) and the class comment
108    
109                    content = content.replaceFirst(
110                            "(?m)^[ \t]*((?:package|import) .*;)\\s*^[ \t]*/\\*\\*",
111                            "$1\n\n/**");
112    
113                    return content;
114            }
115    
116            @Override
117            protected ImportPackage createImportPackage(String line) {
118                    Matcher matcher = _javaImportPattern.matcher(line);
119    
120                    if (!matcher.find()) {
121                            return null;
122                    }
123    
124                    boolean isStatic = false;
125    
126                    if (Validator.isNotNull(matcher.group(1))) {
127                            isStatic = true;
128                    }
129    
130                    String importString = matcher.group(2);
131    
132                    return new ImportPackage(importString, isStatic, line);
133            }
134    
135            private static final Pattern _importsPattern = Pattern.compile(
136                    "(^[ \t]*import\\s+.*;\n+)+", Pattern.MULTILINE);
137            private static final Pattern _javaImportPattern = Pattern.compile(
138                    "import( static)? ([^;]+);");
139    
140    }