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.skinfactory.parameters;
017
018import java.io.File;
019import java.io.FileInputStream;
020import java.io.FileOutputStream;
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.OutputStream;
024import java.util.HashMap;
025import java.util.Map;
026import java.util.regex.Matcher;
027import java.util.regex.Pattern;
028
029import org.apache.cocoon.xml.AttributesImpl;
030import org.apache.cocoon.xml.XMLUtils;
031import org.apache.commons.io.IOUtils;
032import org.xml.sax.ContentHandler;
033import org.xml.sax.SAXException;
034
035import org.ametys.runtime.i18n.I18nizableText;
036import org.ametys.web.skin.SkinModel;
037
038/**
039 * Implementation of {@link AbstractSkinParameter} for a i18niable text
040 */
041public class TextParameter extends AbstractSkinParameter
042{
043    private String _defaultValue;
044    private File _xslFile;
045    
046    /**
047     * Constructor
048     * @param id the unique id
049     * @param label the label
050     * @param description the description
051     * @param xslFile the XSL file
052     * @param defaultValue the default value in the default language
053     */
054    public TextParameter(String id, I18nizableText label, I18nizableText description, File xslFile, String defaultValue)
055    {
056        super(id, label, description);
057        _xslFile = xslFile;
058        _defaultValue = defaultValue;
059    }
060    
061    /**
062     * Get the CSS file path
063     * @return the CSS file path
064     */
065    public File getFile ()
066    {
067        return _xslFile;
068    }
069    
070    @Override
071    public SkinParameterType getType()
072    {
073        return SkinParameterType.TEXT;
074    }
075    
076    @Override
077    public void apply(File tempDir, File modelDir, Object value, String lang)
078    {
079        String relPath = _xslFile.getAbsolutePath().substring(modelDir.getAbsolutePath().length() + 1);
080        File cssFile = new File (tempDir, relPath);
081        String string = _filetoString(cssFile);
082        
083        Pattern pattern = Pattern.compile("^[^>]*>([^<]*)<!--\\s*AMETYS\\s*\\(\\s*\"([^\"]+)\"\\s*(?:,\\s*([^,\"\\s]+|\"[^\"]*\")\\s*)?(?:,\\s*([^,\"\\s]+|\"[^\"]*\")\\s*)?\\)\\s*-->.*$$", Pattern.MULTILINE);
084        Matcher matcher = pattern.matcher(string);
085        
086        try (OutputStream os = new FileOutputStream (cssFile))
087        {
088            int startIndex = 0;
089            
090            while (matcher.find())
091            {
092                int propertyStart = matcher.start(1);
093                
094                String beforeValue = string.substring(startIndex, propertyStart);
095                IOUtils.write(beforeValue, os, "UTF-8");
096                IOUtils.write((String) value, os, "UTF-8");
097                
098                startIndex = matcher.end(1);
099            }
100            
101            IOUtils.write(string.substring(startIndex), os, "UTF-8");
102        }
103        catch (IOException e)
104        {
105            throw new SkinParameterException ("Unable to apply text parameter '" + getId() + "'", e);
106        }
107    }
108    
109    private String _filetoString (File file)
110    {
111        try (InputStream is = new FileInputStream(file))
112        {
113            return org.apache.commons.io.IOUtils.toString(is, "UTF-8");
114        }
115        catch (IOException e)
116        {
117            throw new SkinParameterException ("Unable to parse file '" + file.getName() + "'", e);
118        }
119    }
120    
121    @Override
122    public void toSAX(ContentHandler contentHandler, String modelName) throws SAXException
123    {
124        AttributesImpl attrs = new AttributesImpl();
125        attrs.addCDATAAttribute("id", getId());
126        attrs.addCDATAAttribute("type", SkinParameterType.TEXT.name().toLowerCase());
127        
128        XMLUtils.startElement(contentHandler, "parameter", attrs);
129        
130        getLabel().toSAX(contentHandler, "label");
131        getDescription().toSAX(contentHandler, "description");
132        
133        XMLUtils.endElement(contentHandler, "parameter");
134        
135    }
136    
137    @Override
138    public Map<String, Object> toJson(String modelName)
139    {
140        Map<String, Object> jsonObject = new HashMap<>();
141        
142        jsonObject.put("id", getId());
143        jsonObject.put("type", SkinParameterType.TEXT.name().toLowerCase());
144        jsonObject.put("label", getLabel());
145        jsonObject.put("description", getDescription());
146        
147        return jsonObject;
148    }
149    
150    @Override
151    public String getDefaultValue(SkinModel model)
152    {
153        return _defaultValue;
154    }
155    
156    @Override
157    public String getDefaultValue(SkinModel model, String lang)
158    {
159        return getDefaultValue(model);
160    }
161}