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.StringBundler;
020    
021    import java.io.IOException;
022    
023    import java.util.Set;
024    import java.util.TreeSet;
025    
026    /**
027     * @author Andr?? de Oliveira
028     */
029    public abstract class ImportsFormatter {
030    
031            public String format(String imports) throws IOException {
032                    if (imports.contains("/*") || imports.contains("*/") ||
033                            imports.contains("\n//")) {
034    
035                            return imports + "\n";
036                    }
037    
038                    Set<ImportPackage> importPackages = new TreeSet<>();
039    
040                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
041                            new UnsyncStringReader(imports));
042    
043                    String line = null;
044    
045                    while ((line = unsyncBufferedReader.readLine()) != null) {
046                            ImportPackage importPackage = createImportPackage(line);
047    
048                            if (importPackage != null) {
049                                    importPackages.add(importPackage);
050                            }
051                    }
052    
053                    StringBundler sb = new StringBundler(3 * importPackages.size());
054    
055                    ImportPackage previousImportPackage = null;
056    
057                    for (ImportPackage importPackage : importPackages) {
058                            if ((previousImportPackage != null) &&
059                                    !importPackage.isGroupedWith(previousImportPackage)) {
060    
061                                    sb.append("\n");
062                            }
063    
064                            sb.append(importPackage.getLine());
065                            sb.append("\n");
066    
067                            previousImportPackage = importPackage;
068                    }
069    
070                    return sb.toString();
071            }
072    
073            protected abstract ImportPackage createImportPackage(String line);
074    
075    }