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 format() 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                            format(fileName);
043                    }
044            }
045    
046            @Override
047            protected String format(String fileName) throws Exception {
048                    File file = new File(BASEDIR + fileName);
049    
050                    String content = fileUtil.read(file);
051    
052                    String newContent = formatSQL(content);
053    
054                    if (isAutoFix() && (newContent != null) &&
055                            !content.equals(newContent)) {
056    
057                            fileUtil.write(file, newContent);
058    
059                            fileName = StringUtil.replace(
060                                    fileName, StringPool.BACK_SLASH, StringPool.SLASH);
061    
062                            sourceFormatterHelper.printError(fileName, file);
063                    }
064    
065                    return newContent;
066            }
067    
068            protected String formatSQL(String content) throws IOException {
069                    StringBundler sb = new StringBundler();
070    
071                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
072                            new UnsyncStringReader(content));
073    
074                    String line = null;
075    
076                    String previousLineSqlCommand = StringPool.BLANK;
077    
078                    while ((line = unsyncBufferedReader.readLine()) != null) {
079                            line = trimLine(line, false);
080    
081                            if (Validator.isNotNull(line) && !line.startsWith(StringPool.TAB)) {
082                                    String sqlCommand = StringUtil.split(line, CharPool.SPACE)[0];
083    
084                                    if (Validator.isNotNull(previousLineSqlCommand) &&
085                                            !previousLineSqlCommand.equals(sqlCommand)) {
086    
087                                            sb.append("\n");
088                                    }
089    
090                                    previousLineSqlCommand = sqlCommand;
091                            }
092                            else {
093                                    previousLineSqlCommand = StringPool.BLANK;
094                            }
095    
096                            String strippedQuotesLine = stripQuotes(line, CharPool.APOSTROPHE);
097    
098                            if (strippedQuotesLine.contains(StringPool.QUOTE)) {
099                                    line = StringUtil.replace(
100                                            line, StringPool.QUOTE, StringPool.APOSTROPHE);
101                            }
102    
103                            sb.append(line);
104                            sb.append("\n");
105                    }
106    
107                    unsyncBufferedReader.close();
108    
109                    content = sb.toString();
110    
111                    if (content.endsWith("\n")) {
112                            content = content.substring(0, content.length() - 1);
113                    }
114    
115                    return content;
116            }
117    
118    }