001/*
002 *  Copyright 2011 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.artisteer.actions;
017
018import java.io.File;
019import java.io.FileNotFoundException;
020import java.io.IOException;
021import java.nio.charset.StandardCharsets;
022import java.util.regex.Pattern;
023
024import org.apache.commons.io.FileUtils;
025
026import org.ametys.web.skin.actions.UploadSkinAction;
027
028/**
029 * This action receive a form with the "importfile" zip file as an artisteer exported skin.
030 * Replace existing skin 
031 */
032public class UploadArtisteerSkinAction extends UploadSkinAction
033{
034    private static final Pattern __UNVALUED_RULE_PATTERN = Pattern.compile("^.*\\{[A-Z][a-zA-Z]*\\}.*$", Pattern.MULTILINE);
035    
036    @Override
037    protected void _filter(File skinDir, String absoluteSkinPath, boolean isModel) throws IOException
038    {
039        super._filter(skinDir, absoluteSkinPath, isModel);
040        
041        File[] files = skinDir.listFiles();
042        for (File file : files)
043        {
044            if ("style.ie6.css".equals(file.getName())
045                   || "style.ie7.css".equals(file.getName())
046                   || "style.css".equals(file.getName())
047                   || "swfobject.js".equals(file.getName())
048                   || "container.swf".equals(file.getName())
049                   || "expressInstall.swf".equals(file.getName())
050                   || "images_artisteer".equals(file.getName()))
051            {
052                FileUtils.deleteQuietly(file);
053            }
054            else if ("images".equals(file.getAbsolutePath().substring(absoluteSkinPath.length() + 1)) && file.isDirectory())
055            {
056                FileUtils.moveDirectory(file, new File(skinDir, "resources/img"));
057            }
058            else if ("favicon.ico".equals(file.getName()) && !file.isDirectory())
059            {
060                if (new File(skinDir, "resources/img").exists())
061                {
062                    FileUtils.moveFile(file, new File(skinDir, "resources/img/favicon.ico"));
063                }
064                else
065                {
066                    FileUtils.moveFile(file, new File(skinDir, "images/favicon.ico"));
067                }
068            }
069            
070            if (file.getName().endsWith(".css") && file.exists())
071            {
072                _removeUnvaluedLines(file);
073            }
074        }
075    }
076    
077    private void _removeUnvaluedLines(File cssFile) throws FileNotFoundException, IOException
078    {
079        // Remove unvalued lines generated by Artisteer such as "border: 1px solid {HoverTextColor}"
080        String content = FileUtils.readFileToString(cssFile, StandardCharsets.UTF_8);
081        content = __UNVALUED_RULE_PATTERN.matcher(content).replaceAll("");
082        FileUtils.write(cssFile, content, StandardCharsets.UTF_8);
083    }
084}