<commit_before>/**
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 * If a copy of the MPL was not distributed with this file, You can obtain one at
 * http://mozilla.org/MPL/2.0/.
 * 
 * This Source Code Form is also subject to the terms of the Health-Related Additional
 * Disclaimer of Warranty and Limitation of Liability available at
 * http://www.carewebframework.org/licensing/disclaimer.
 */
package org.carewebframework.maven.plugin.theme;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.io.filefilter.WildcardFileFilter;

import org.codehaus.plexus.util.FileUtils;

/**
 * Generates a new theme directly from a CSS file.
 */
class ThemeGeneratorCSS extends ThemeGeneratorBase {
    
    /**
     * Applies a map template to the source CSS to generate additional CSS.
     */
    protected class CSSProcessor extends ResourceProcessor {
        
        private final Map<String, String> map = new HashMap<String, String>();
        
        @Override
        protected void process(IThemeResource resource) throws Exception {
            ThemeResourceCSS css = (ThemeResourceCSS) resource;
            File mapper = css.getMapper();
            
            if (mapper == null) {
                super.process(resource);
                return;
            }
            InputStream in = new FileInputStream(mapper);
            LineIterator lines = IOUtils.lineIterator(in, "UTF-8");
            
            while (lines.hasNext()) {
                String[] pcs = lines.nextLine().split("\\=", 2);
                
                if (pcs.length == 2) {
                    map.put(pcs[0], pcs[1]);
                }
            }
            
            IOUtils.closeQuietly(in);
            super.process(resource);
        }
        
        @Override
        protected void process() throws Exception {
            int c = 0;
            int state = 0;
            List<String> list = new ArrayList<String>();
            StringBuilder sb = new StringBuilder();
            List<String> templates = new ArrayList<String>();
            Map<String, String> styles = new HashMap<String, String>();
            String prop = null;
            
            while (c != -1) {
                c = inputStream.read();
                
                if (c == -1) {
                    state = -1;
                } else {
                    outputStream.write(c);
                }
                
                switch (state) {
                    case -1: // Process template
                        for (String template : templates) {
                            for (Entry<String, String> entry : styles.entrySet()) {
                                String replace = '|' + entry.getKey() + '|';
                                template = template.replace(replace, entry.getValue());
                            }
                            <commit_msg>Map file supports line continuation. Duplicate entries are now additive.<commit_after>/**
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 * If a copy of the MPL was not distributed with this file, You can obtain one at
 * http://mozilla.org/MPL/2.0/.
 * 
 * This Source Code Form is also subject to the terms of the Health-Related Additional
 * Disclaimer of Warranty and Limitation of Liability available at
 * http://www.carewebframework.org/licensing/disclaimer.
 */
package org.carewebframework.maven.plugin.theme;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.io.filefilter.WildcardFileFilter;

import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.StringUtils;

/**
 * Generates a new theme directly from a CSS file.
 */
class ThemeGeneratorCSS extends ThemeGeneratorBase {
    
    /**
     * Applies a map template to the source CSS to generate additional CSS.
     */
    protected class CSSProcessor extends ResourceProcessor {
        
        private final Map<String, String> map = new HashMap<String, String>();
        
        @Override
        protected void process(IThemeResource resource) throws Exception {
            ThemeResourceCSS css = (ThemeResourceCSS) resource;
            File mapper = css.getMapper();
            
            if (mapper == null) {
                super.process(resource);
                return;
            }
            InputStream in = new FileInputStream(mapper);
            LineIterator lines = IOUtils.lineIterator(in, "UTF-8");
            String line = "";
            
            while (lines.hasNext()) {
                line += lines.nextLine();
                
                if (line.endsWith("\\")) {
                    line = StringUtils.left(line, line.length() - 1);
                } else {
                    addMapEntry(line);
                    line = "";
                }
            }
            
            addMapEntry(line);
            IOUtils.closeQuietly(in);
            super.process(resource);
        }
        
        /**
         * Parses and adds the entry to the map.
         * 
         * @param s String entry.
         */
        private void addMapEntry(String s) {
            String[] pcs = s.split("\\=", 2);
            
            if (pcs.length == 2) {
                if (map.containsKey(pcs[0])) {
                    map.put(pcs[0], map.get(pcs[0]) + pcs[1]);
                } else {
                    map.put(pcs[0], pcs[1]);
                }
            }
        }
        
        @Override
        protected void process() throws Exception {
            int c = 0;
            int state = 0;
            List<String> list = new ArrayList<String>();
            StringBuilder sb = new StringBuilder();
            List<String> templates = new ArrayList<String>();
            Map<String, String> styles = new HashMap<String, String>();
            String prop = null;
            
            while (c != -1) {
                c = inputStream.read();
                
                if (c == -1) {
                    state = -1;
                } else {
                    outputStream.write(c);
                }
                
                switch (state) {
                    case -1: // Process template
                        for (String template : templates) {
                            for (Entry<String, String> entry : styles.entrySet()) {
                                String replace = '|' + entry.getKey() + '|';
                                template = template.replace(replace, entry.getValue());
                            }
                            
{'fraction_non_alphanumeric': 0.09973310858266611, 'fraction_numerical': 0.004916420845624385, 'mean_word_length': 1.5185709232401838, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6547600', 'n_tokens_mistral': 1823, 'n_tokens_neox': 1766, 'n_words': 505}