001    /**
002     * Copyright (c) 2000-present 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.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    /**
028     * @author Hugo Huijser
029     */
030    public class JSSourceProcessor extends BaseSourceProcessor {
031    
032            @Override
033            protected String doFormat(
034                            File file, String fileName, String absolutePath, String content)
035                    throws Exception {
036    
037                    String newContent = trimContent(content, false);
038    
039                    newContent = StringUtil.replace(
040                            newContent,
041                            new String[] {
042                                    "else{", "for(", "function (", "if(", "while(", "){\n",
043                                    "= new Array();", "= new Object();"
044                            },
045                            new String[] {
046                                    "else {", "for (", "function(", "if (", "while (", ") {\n",
047                                    "= [];", "= {};"
048                            });
049    
050                    while (true) {
051                            Matcher matcher = _multipleVarsOnSingleLinePattern.matcher(
052                                    newContent);
053    
054                            if (!matcher.find()) {
055                                    break;
056                            }
057    
058                            String match = matcher.group();
059    
060                            int pos = match.indexOf("var ");
061    
062                            StringBundler sb = new StringBundler(4);
063    
064                            sb.append(match.substring(0, match.length() - 2));
065                            sb.append(StringPool.SEMICOLON);
066                            sb.append("\n");
067                            sb.append(match.substring(0, pos + 4));
068    
069                            newContent = StringUtil.replace(newContent, match, sb.toString());
070                    }
071    
072                    if (newContent.endsWith("\n")) {
073                            newContent = newContent.substring(0, newContent.length() - 1);
074                    }
075    
076                    checkLanguageKeys(fileName, newContent, languageKeyPattern);
077    
078                    if (newContent.contains("debugger.")) {
079                            processErrorMessage(fileName, "debugger " + fileName);
080                    }
081    
082                    return newContent;
083            }
084    
085            @Override
086            protected void format() throws Exception {
087                    String[] excludes = {
088                            "**\\js\\aui\\**", "**\\js\\editor\\**", "**\\js\\jquery\\**",
089                            "**\\js\\lodash\\**", "**\\js\\misc\\**", "**\\r2.js",
090                            "**\\tools\\**", "**\\VAADIN\\**"
091                    };
092                    String[] includes = {"**\\*.js"};
093    
094                    List<String> fileNames = getFileNames(excludes, includes);
095    
096                    for (String fileName : fileNames) {
097                            format(fileName);
098                    }
099            }
100    
101            private final Pattern _multipleVarsOnSingleLinePattern = Pattern.compile(
102                    "\t+var \\w+\\, ");
103    
104    }