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.CharPool;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    
025    import java.io.File;
026    import java.io.IOException;
027    
028    import java.util.List;
029    
030    /**
031     * @author Hugo Huijser
032     */
033    public class SQLSourceProcessor extends BaseSourceProcessor {
034    
035            @Override
036            protected void doFormat() throws Exception {
037                    String[] includes = new String[] {"**\\sql\\*.sql"};
038    
039                    List<String> fileNames = getFileNames(new String[0], includes);
040    
041                    for (String fileName : fileNames) {
042                            File file = new File(BASEDIR + fileName);
043    
044                            String content = fileUtil.read(file);
045    
046                            String newContent = formatSQL(content);
047    
048                            if ((newContent != null) && !content.equals(newContent)) {
049                                    fileUtil.write(file, newContent);
050    
051                                    fileName = StringUtil.replace(
052                                            fileName, StringPool.BACK_SLASH, StringPool.SLASH);
053    
054                                    sourceFormatterHelper.printError(fileName, file);
055                            }
056                    }
057            }
058    
059            protected String formatSQL(String content) throws IOException {
060                    StringBundler sb = new StringBundler();
061    
062                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
063                            new UnsyncStringReader(content));
064    
065                    String line = null;
066    
067                    String previousLineSqlCommand = StringPool.BLANK;
068    
069                    while ((line = unsyncBufferedReader.readLine()) != null) {
070                            line = trimLine(line, false);
071    
072                            if (Validator.isNotNull(line) && !line.startsWith(StringPool.TAB)) {
073                                    String sqlCommand = StringUtil.split(line, CharPool.SPACE)[0];
074    
075                                    if (Validator.isNotNull(previousLineSqlCommand) &&
076                                            !previousLineSqlCommand.equals(sqlCommand)) {
077    
078                                            sb.append("\n");
079                                    }
080    
081                                    previousLineSqlCommand = sqlCommand;
082                            }
083                            else {
084                                    previousLineSqlCommand = StringPool.BLANK;
085                            }
086    
087                            sb.append(line);
088                            sb.append("\n");
089                    }
090    
091                    unsyncBufferedReader.close();
092    
093                    content = sb.toString();
094    
095                    if (content.endsWith("\n")) {
096                            content = content.substring(0, content.length() - 1);
097                    }
098    
099                    return content;
100            }
101    
102    }