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.sourceformatter;
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.ListUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.util.ContentUtil;
024    
025    import java.io.File;
026    import java.io.FileInputStream;
027    import java.io.InputStream;
028    
029    import java.util.Enumeration;
030    import java.util.List;
031    import java.util.Properties;
032    
033    /**
034     * @author Hugo Huijser
035     */
036    public class PropertiesSourceProcessor extends BaseSourceProcessor {
037    
038            @Override
039            protected String doFormat(
040                            File file, String fileName, String absolutePath, String content)
041                    throws Exception {
042    
043                    if (!portalSource && fileName.endsWith("portlet.properties")) {
044                            return formatPortletProperties(content);
045                    }
046    
047                    if (fileName.endsWith("source-formatter.properties")) {
048                            formatSourceFormatterProperties(fileName, content);
049                    }
050                    else {
051                            formatPortalProperties(fileName, content);
052                    }
053    
054                    return content;
055            }
056    
057            @Override
058            protected void format() throws Exception {
059                    _portalPortalPropertiesContent = formatPortalPortalProperties();
060    
061                    String[] includes = null;
062    
063                    if (portalSource) {
064                            includes = new String[] {
065                                    "**\\portal-ext.properties", "**\\portal-legacy-*.properties",
066                                    "**\\source-formatter.properties"
067                            };
068                    }
069                    else {
070                            includes = new String[] {
071                                    "**\\portal.properties", "**\\portal-ext.properties",
072                                    "**\\portlet.properties", "**\\source-formatter.properties"
073                            };
074                    }
075    
076                    List<String> fileNames = getFileNames(new String[0], includes);
077    
078                    for (String fileName : fileNames) {
079                            format(fileName);
080                    }
081            }
082    
083            protected String formatPortalPortalProperties() throws Exception {
084                    if (!portalSource) {
085                            return ContentUtil.get("portal.properties");
086                    }
087    
088                    String fileName = "portal-impl/src/portal.properties";
089    
090                    File file = getFile(fileName, 4);
091    
092                    String content = fileUtil.read(file);
093    
094                    StringBundler sb = new StringBundler();
095    
096                    try (UnsyncBufferedReader unsyncBufferedReader =
097                                    new UnsyncBufferedReader(new UnsyncStringReader(content))) {
098    
099                            String line = null;
100    
101                            while ((line = unsyncBufferedReader.readLine()) != null) {
102                                    line = trimLine(line, true);
103    
104                                    if (line.startsWith(StringPool.TAB)) {
105                                            line = line.replaceFirst(
106                                                    StringPool.TAB, StringPool.FOUR_SPACES);
107                                    }
108    
109                                    sb.append(line);
110                                    sb.append("\n");
111                            }
112                    }
113    
114                    String newContent = sb.toString();
115    
116                    if (newContent.endsWith("\n")) {
117                            newContent = newContent.substring(0, newContent.length() - 1);
118                    }
119    
120                    processFormattedFile(file, fileName, content, newContent);
121    
122                    return newContent;
123            }
124    
125            protected void formatPortalProperties(String fileName, String content)
126                    throws Exception {
127    
128                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
129                            new UnsyncStringReader(content));
130    
131                    int lineCount = 0;
132    
133                    String line = null;
134    
135                    int previousPos = -1;
136    
137                    while ((line = unsyncBufferedReader.readLine()) != null) {
138                            lineCount++;
139    
140                            int pos = line.indexOf(StringPool.EQUAL);
141    
142                            if (pos == -1) {
143                                    continue;
144                            }
145    
146                            String property = line.substring(0, pos + 1);
147    
148                            property = property.trim();
149    
150                            pos = _portalPortalPropertiesContent.indexOf(
151                                    StringPool.FOUR_SPACES + property);
152    
153                            if (pos == -1) {
154                                    continue;
155                            }
156    
157                            if (pos < previousPos) {
158                                    processErrorMessage(
159                                            fileName, "sort " + fileName + " " + lineCount);
160                            }
161    
162                            previousPos = pos;
163                    }
164            }
165    
166            protected String formatPortletProperties(String content) {
167                    if (!content.contains("include-and-override=portlet-ext.properties")) {
168                            content =
169                                    "include-and-override=portlet-ext.properties" + "\n\n" +
170                                            content;
171                    }
172    
173                    return content;
174            }
175    
176            protected void formatSourceFormatterProperties(
177                            String fileName, String content)
178                    throws Exception {
179    
180                    String path = StringPool.BLANK;
181    
182                    int pos = fileName.lastIndexOf(StringPool.SLASH);
183    
184                    if (pos != -1) {
185                            path = fileName.substring(0, pos + 1);
186                    }
187    
188                    Properties properties = new Properties();
189    
190                    InputStream inputStream = new FileInputStream(fileName);
191    
192                    properties.load(inputStream);
193    
194                    Enumeration<String> enu =
195                            (Enumeration<String>)properties.propertyNames();
196    
197                    while (enu.hasMoreElements()) {
198                            String key = enu.nextElement();
199    
200                            if (!key.endsWith("excludes.files")) {
201                                    continue;
202                            }
203    
204                            String value = properties.getProperty(key);
205    
206                            if (Validator.isNull(value)) {
207                                    continue;
208                            }
209    
210                            List<String> propertyFileNames = ListUtil.fromString(
211                                    value, StringPool.COMMA);
212    
213                            for (String propertyFileName : propertyFileNames) {
214                                    pos = propertyFileName.indexOf(StringPool.AT);
215    
216                                    if (pos != -1) {
217                                            propertyFileName = propertyFileName.substring(0, pos);
218                                    }
219    
220                                    if (!fileUtil.exists(path + propertyFileName)) {
221                                            processErrorMessage(
222                                                    fileName,
223                                                    "Incorrect property value: " + propertyFileName + " " +
224                                                            fileName);
225                                    }
226                            }
227                    }
228            }
229    
230            private String _portalPortalPropertiesContent;
231    
232    }