001    /**
002     * Copyright (c) 2000-2013 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.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.util.ContentUtil;
023    
024    import java.io.File;
025    import java.io.IOException;
026    
027    import java.util.List;
028    
029    /**
030     * @author Hugo Huijser
031     */
032    public class PropertiesSourceProcessor extends BaseSourceProcessor {
033    
034            @Override
035            protected void format() throws Exception {
036                    formatPortalProperties();
037            }
038    
039            protected void formatPortalPortalProperties(File file, String fileName)
040                    throws Exception {
041    
042                    StringBundler sb = new StringBundler();
043    
044                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
045                            new UnsyncStringReader(_portalPortalProperties));
046    
047                    String line = null;
048    
049                    while ((line = unsyncBufferedReader.readLine()) != null) {
050                            line = trimLine(line, true);
051    
052                            if (line.startsWith(StringPool.TAB)) {
053                                    line = line.replaceFirst(
054                                            StringPool.TAB, StringPool.FOUR_SPACES);
055                            }
056    
057                            sb.append(line);
058                            sb.append("\n");
059                    }
060    
061                    unsyncBufferedReader.close();
062    
063                    String newContent = sb.toString();
064    
065                    if (newContent.endsWith("\n")) {
066                            newContent = newContent.substring(0, newContent.length() - 1);
067                    }
068    
069                    if (isAutoFix() && (newContent != null) &&
070                            !_portalPortalProperties.equals(newContent)) {
071    
072                            fileUtil.write(file, newContent);
073    
074                            sourceFormatterHelper.printError(fileName, file);
075                    }
076            }
077    
078            protected void formatPortalProperties() throws Exception {
079                    if (portalSource) {
080                            String portalPortalPropertiesfileName =
081                                    "portal-impl/src/portal.properties";
082    
083                            File portalPortalPropertiesFile = new File(
084                                    BASEDIR + portalPortalPropertiesfileName);
085    
086                            _portalPortalProperties = fileUtil.read(portalPortalPropertiesFile);
087    
088                            formatPortalPortalProperties(
089                                    portalPortalPropertiesFile, portalPortalPropertiesfileName);
090                    }
091                    else {
092                            _portalPortalProperties = ContentUtil.get("portal.properties");
093                    }
094    
095                    String[] excludes = null;
096                    String[] includes = null;
097    
098                    if (portalSource) {
099                            excludes = new String[] {"**\\bin\\**", "**\\classes\\**"};
100                            includes = new String[] {
101                                    "**\\portal-ext.properties", "**\\portal-legacy-*.properties"
102                            };
103                    }
104                    else {
105                            excludes = new String[0];
106                            includes = new String[] {
107                                    "**\\portal.properties", "**\\portal-ext.properties"
108                            };
109                    }
110    
111                    List<String> fileNames = getFileNames(excludes, includes);
112    
113                    for (String fileName : fileNames) {
114                            File file = new File(BASEDIR + fileName);
115    
116                            fileName = StringUtil.replace(
117                                    fileName, StringPool.BACK_SLASH, StringPool.SLASH);
118    
119                            String content = fileUtil.read(file);
120    
121                            formatPortalProperties(fileName, content);
122                    }
123            }
124    
125            protected void formatPortalProperties(String fileName, String content)
126                    throws IOException {
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 = _portalPortalProperties.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            private String _portalPortalProperties;
167    
168    }