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.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 void doFormat() 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                            File file = new File(BASEDIR + fileName);
044    
045                            fileName = StringUtil.replace(
046                                    fileName, StringPool.BACK_SLASH, StringPool.SLASH);
047    
048                            String content = fileUtil.read(file);
049    
050                            String newContent = trimContent(content, false);
051    
052                            newContent = StringUtil.replace(
053                                    newContent,
054                                    new String[] {
055                                            "else{", "for(", "function (", "if(", "while(", "){\n",
056                                            "= new Array();", "= new Object();"
057                                    },
058                                    new String[] {
059                                            "else {", "for (", "function(", "if (", "while (", ") {\n",
060                                            "= [];", "= {};"
061                                    });
062    
063                            Pattern pattern = Pattern.compile("\t+var \\w+\\, ");
064    
065                            for (;;) {
066                                    Matcher matcher = pattern.matcher(newContent);
067    
068                                    if (!matcher.find()) {
069                                            break;
070                                    }
071    
072                                    String match = matcher.group();
073    
074                                    int pos = match.indexOf("var ");
075    
076                                    StringBundler sb = new StringBundler(4);
077    
078                                    sb.append(match.substring(0, match.length() - 2));
079                                    sb.append(StringPool.SEMICOLON);
080                                    sb.append("\n");
081                                    sb.append(match.substring(0, pos + 4));
082    
083                                    newContent = StringUtil.replace(
084                                            newContent, match, sb.toString());
085                            }
086    
087                            if (newContent.endsWith("\n")) {
088                                    newContent = newContent.substring(0, newContent.length() - 1);
089                            }
090    
091                            checkLanguageKeys(fileName, newContent, languageKeyPattern);
092    
093                            if ((newContent != null) && !content.equals(newContent)) {
094                                    fileUtil.write(file, newContent);
095    
096                                    sourceFormatterHelper.printError(fileName, file);
097                            }
098                    }
099            }
100    
101    }