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