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.CharPool;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.io.File;
023    
024    import java.util.List;
025    import java.util.regex.Matcher;
026    import java.util.regex.Pattern;
027    
028    /**
029     * @author Hugo Huijser
030     */
031    public class CSSSourceProcessor extends BaseSourceProcessor {
032    
033            @Override
034            protected String doFormat(
035                            File file, String fileName, String absolutePath, String content)
036                    throws Exception {
037    
038                    String newContent = trimContent(content, false);
039    
040                    newContent = fixComments(newContent);
041    
042                    return fixHexColors(newContent);
043            }
044    
045            protected String fixComments(String content) {
046                    Matcher matcher = _commentPattern.matcher(content);
047    
048                    while (matcher.find()) {
049                            String[] words = StringUtil.split(matcher.group(1), CharPool.SPACE);
050    
051                            for (int i = 1; i < words.length; i++) {
052                                    String previousWord = words[i - 1];
053    
054                                    if (previousWord.endsWith(StringPool.PERIOD) ||
055                                            previousWord.equals(StringPool.SLASH)) {
056    
057                                            continue;
058                                    }
059    
060                                    String word = words[i];
061    
062                                    if ((word.length() > 1) &&
063                                            Character.isUpperCase(word.charAt(0)) &&
064                                            StringUtil.isLowerCase(word.substring(1))) {
065    
066                                            content = StringUtil.replaceFirst(
067                                                    content, word, StringUtil.toLowerCase(word),
068                                                    matcher.start());
069                                    }
070                            }
071                    }
072    
073                    return content;
074            }
075    
076            protected String fixHexColors(String content) {
077                    Matcher matcher = _hexColorPattern.matcher(content);
078    
079                    while (matcher.find()) {
080                            String hexColor = matcher.group(1);
081    
082                            if (Validator.isNumber(hexColor) || (hexColor.length() < 3)) {
083                                    continue;
084                            }
085    
086                            content = StringUtil.replace(
087                                    content, hexColor, StringUtil.toUpperCase(hexColor));
088                    }
089    
090                    return content;
091            }
092    
093            @Override
094            protected void format() throws Exception {
095                    String[] excludes = {
096                            "**\\.ivy\\**", "**\\.sass-cache\\**", "**\\aui_deprecated.css",
097                            "**\\expected\\**", "**\\js\\aui\\**", "**\\js\\editor\\**",
098                            "**\\js\\misc\\**", "**\\VAADIN\\**"
099                    };
100                    String[] includes = {"**\\*.css"};
101    
102                    List<String> fileNames = getFileNames(excludes, includes);
103    
104                    for (String fileName : fileNames) {
105                            format(fileName);
106                    }
107            }
108    
109            private Pattern _commentPattern = Pattern.compile("/\\* -+(.+)-+ \\*/");
110            private Pattern _hexColorPattern = Pattern.compile("#([0-9a-f]+)[\\( ;,]");
111    
112    }