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 Pattern pattern = Pattern.compile("\t+var \\w+\\, ");
070
071 while (true) {
072 Matcher matcher = pattern.matcher(newContent);
073
074 if (!matcher.find()) {
075 break;
076 }
077
078 String match = matcher.group();
079
080 int pos = match.indexOf("var ");
081
082 StringBundler sb = new StringBundler(4);
083
084 sb.append(match.substring(0, match.length() - 2));
085 sb.append(StringPool.SEMICOLON);
086 sb.append("\n");
087 sb.append(match.substring(0, pos + 4));
088
089 newContent = StringUtil.replace(newContent, match, sb.toString());
090 }
091
092 if (newContent.endsWith("\n")) {
093 newContent = newContent.substring(0, newContent.length() - 1);
094 }
095
096 checkLanguageKeys(fileName, newContent, languageKeyPattern);
097
098 if (isAutoFix() && (newContent != null) &&
099 !content.equals(newContent)) {
100
101 fileUtil.write(file, newContent);
102
103 sourceFormatterHelper.printError(fileName, file);
104 }
105
106 return newContent;
107 }
108
109 }