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