001
014
015 package com.liferay.portal.tools.sourceformatter;
016
017 import com.liferay.portal.kernel.util.StringBundler;
018 import com.liferay.portal.kernel.util.StringPool;
019 import com.liferay.portal.kernel.util.StringUtil;
020
021 import java.io.File;
022
023 import java.util.List;
024 import java.util.regex.Matcher;
025 import java.util.regex.Pattern;
026
027
030 public class JSSourceProcessor extends BaseSourceProcessor {
031
032 @Override
033 protected void format() throws Exception {
034 String[] excludes = {
035 "**\\js\\aui\\**", "**\\js\\editor\\**", "**\\js\\misc\\**",
036 "**\\tools\\**", "**\\VAADIN\\**"
037 };
038 String[] includes = {"**\\*.js"};
039
040 List<String> fileNames = getFileNames(excludes, includes);
041
042 for (String fileName : fileNames) {
043 format(fileName);
044 }
045 }
046
047 @Override
048 protected String format(String fileName) throws Exception {
049 File file = new File(BASEDIR + fileName);
050
051 fileName = StringUtil.replace(
052 fileName, StringPool.BACK_SLASH, StringPool.SLASH);
053
054 String content = fileUtil.read(file);
055
056 String newContent = trimContent(content, false);
057
058 newContent = StringUtil.replace(
059 newContent,
060 new String[] {
061 "else{", "for(", "function (", "if(", "while(", "){\n",
062 "= new Array();", "= new Object();"
063 },
064 new String[] {
065 "else {", "for (", "function(", "if (", "while (", ") {\n",
066 "= [];", "= {};"
067 });
068
069 while (true) {
070 Matcher matcher = _multipleVarsOnSingleLinePattern.matcher(
071 newContent);
072
073 if (!matcher.find()) {
074 break;
075 }
076
077 String match = matcher.group();
078
079 int pos = match.indexOf("var ");
080
081 StringBundler sb = new StringBundler(4);
082
083 sb.append(match.substring(0, match.length() - 2));
084 sb.append(StringPool.SEMICOLON);
085 sb.append("\n");
086 sb.append(match.substring(0, pos + 4));
087
088 newContent = StringUtil.replace(newContent, match, sb.toString());
089 }
090
091 if (newContent.endsWith("\n")) {
092 newContent = newContent.substring(0, newContent.length() - 1);
093 }
094
095 checkLanguageKeys(fileName, newContent, languageKeyPattern);
096
097 if (isAutoFix() && (newContent != null) &&
098 !content.equals(newContent)) {
099
100 fileUtil.write(file, newContent);
101
102 sourceFormatterHelper.printError(fileName, file);
103 }
104
105 return newContent;
106 }
107
108 private Pattern _multipleVarsOnSingleLinePattern = Pattern.compile(
109 "\t+var \\w+\\, ");
110
111 }