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.CharPool;
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 CSSSourceProcessor extends BaseSourceProcessor {
031    
032            protected String fixComments(String content) {
033                    Matcher matcher = _commentPattern.matcher(content);
034    
035                    while (matcher.find()) {
036                            String[] words = StringUtil.split(matcher.group(1), CharPool.SPACE);
037    
038                            for (int i = 1; i < words.length; i++) {
039                                    String previousWord = words[i - 1];
040    
041                                    if (previousWord.endsWith(StringPool.PERIOD) ||
042                                            previousWord.equals(StringPool.SLASH)) {
043    
044                                            continue;
045                                    }
046    
047                                    String word = words[i];
048    
049                                    if ((word.length() > 1) &&
050                                            Character.isUpperCase(word.charAt(0)) &&
051                                            StringUtil.isLowerCase(word.substring(1))) {
052    
053                                            content = StringUtil.replaceFirst(
054                                                    content, word, StringUtil.toLowerCase(word),
055                                                    matcher.start());
056                                    }
057                            }
058                    }
059    
060                    return content;
061            }
062    
063            @Override
064            protected void format() throws Exception {
065                    String[] excludes = {
066                            "**\\.sass-cache\\**", "**\\aui_deprecated.css", "**\\js\\aui\\**",
067                            "**\\js\\editor\\**", "**\\js\\misc\\**", "**\\VAADIN\\**"
068                    };
069                    String[] includes = {"**\\*.css"};
070    
071                    List<String> fileNames = getFileNames(excludes, includes);
072    
073                    for (String fileName : fileNames) {
074                            format(fileName);
075                    }
076            }
077    
078            @Override
079            protected String format(String fileName) throws Exception {
080                    File file = new File(BASEDIR + fileName);
081    
082                    fileName = StringUtil.replace(
083                            fileName, StringPool.BACK_SLASH, StringPool.SLASH);
084    
085                    String content = fileUtil.read(file);
086    
087                    String newContent = trimContent(content, false);
088    
089                    newContent = fixComments(newContent);
090    
091                    if (isAutoFix() && (newContent != null) &&
092                            !content.equals(newContent)) {
093    
094                            fileUtil.write(file, newContent);
095    
096                            sourceFormatterHelper.printError(fileName, file);
097                    }
098    
099                    return newContent;
100            }
101    
102            private Pattern _commentPattern = Pattern.compile("/\\* -+(.+)-+ \\*/");
103    
104    }