<commit_before>/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     Nuxeo - initial API and implementation
 *
 * $Id$
 */

package org.nuxeo.theme.bank;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.theme.presets.PaletteIdentifyException;
import org.nuxeo.theme.presets.PaletteParseException;
import org.nuxeo.theme.presets.PaletteParser;
import org.nuxeo.theme.resources.BankManager;
import org.nuxeo.theme.resources.BankUtils;

public class Utils {
    private static final Log log = LogFactory.getLog(Utils.class);

    private static final List<String> PRESET_CATEGORIES = Arrays.asList(
            "color", "background", "font", "border");

    public static List<String> getCollections(String bankName)
            throws IOException {
        return BankManager.getCollections(bankName);
    }

    public static List<String> getItemsInCollection(String bankName,
            String collection, String typeName) throws IOException {
        return BankManager.getItemsInCollection(bankName, collection, typeName);
    }

    public static List<String> listSkinsInCollection(String bankName,
            String collection) throws IOException {
        Map<String, Object> info;
        info = BankManager.getInfo(bankName, collection, "style");
        List<String> skins = new ArrayList<String>();
        if (info != null) {
            for (Map.Entry<String, Object> entry : info.entrySet()) {
                String resource = entry.getKey();
                Map value = (Map) entry.getValue();
                Boolean isSkin = false;
                if (value.containsKey("skin")) {
                    isSkin = (Boolean) value.get("skin");
                }
                if (isSkin) {
                    skins.add(resource);
                }
            }
        }
        return skins;
    }

    public static Properties getPresetProperties(String bank,
            String collection, String category) {
        String path = String.format("%s/%s/preset/%s", bank, collection,
                category);
        File file;
        try {
            file = BankManager.getFile(path);
        } catch (IOException e) {
            throw new ThemeBankException(e.getMessage(), e);
        }
        Properties properties = new Properties();
        if (!file.exists()) {
            return properties;
        }
        for (File f : file.listFiles()) {
            String content;
            try {
                content = BankUtils.getFileContent(f);
            } catch (IOException e) {
                log.warn("Could not read file: " + f.getAbsolutePath());
                continue;
            }
            try {
                properties.putAll(PaletteParser.parse(content.getBytes(),
                        f.getName()));
            } catch (PaletteIdentifyException e) {
                log.warn("Could not identify palette type: "
                        + f.getAbsolutePath());
            } catch (PaletteParseException e) {
                log.warn("Could not parse palette: " + f.getAbsolutePath());
            }
        }
        return properties;
    }

    /*
     * JSON calls
     */
    public static String listBankSkins(String bankName) throws IOException {
        JSONArray skins = new JSONArray();
        for (String collection : BankManager.getCollections(bankName)) {
            Map<String, Object> info = BankManager.getInfo(bankName,
                    collection, "style");
            if (info == null) {
                continue;
            }
            for (Map.Entry<String, Object> entry : info.entrySet()) {
                String resource = entry.getKey();
                Map value = (Map) entry.getValue();
                if (value.containsKey("skin") && (Boolean) value.get("skin")) {
                    Boolean isBase = false;
                    if (value.containsKey("base")) {
                        isBase = (Boolean) value.get("base");
                    }
                    JSONObject skinMap = new JSONObject();
                    skinMap.put("bank", bankName);
                    skinMap.put("collection", collection);
                    skinMap.put("resource", resource);
                    skinMap.put("name", String.format("%s (%s)",
                            resource.replace(".css", ""), collection));
                    skinMap.put("base", isBase);
                    skins.add(skinMap);
                }
            }
        }
        return skins.toString();
    }

    public static String listBankStyles(String bankName) throws IOException {
        JSONArray styles = new JSONArray();
        for (String collection : BankManager.getCollections(bankName)) {
            Map<String, Object> info = BankManager.getInfo(bankName,
                    collection, "style");
            for (String resource : getItemsInCollection(bankName, collection,
                    "style")) {
                if (info != null && info.containsKey(resource)) {
                    Map value = (Map) info.get(resource);
                    if (value.containsKey("skin")
                            && (Boolean) value.get("skin")) {
                        continue;
                    }
                }
                JSONObject styleMap = new JSONObject();
                styleMap.put("bank", bankName);
                styleMap.put("collection", collection);
                styleMap.put("resource", resource);
                styleMap.put("name", String.format("%s (%s)", resource.replace(
                        ".css", ""), collection));
                styles.add(styleMap);
            }
        }
        return styles.toString();
    }

    public static String listBankPresets(String bankName) throws IOException {
        JSONArray presets = new JSONArray();
        for (String collection : BankManager.getCollections(bankName)) {
            for (String category : PRESET_CATEGORIES) {
                for (Map.Entry property : getPresetProperties(bankName,
                        collection, category).entrySet()) {
                    JSONObject presetMap = new JSONObject();
                    presetMap.put("bank", bankName);
                    presetMap.put("collection", collection);
                    presetMap.put("category", category);
                    presetMap.put("name", property.getKey());
                    presetMap.put("value", property.getValue());<commit_msg>NXP-6129: make sure that all styles including skins are loaded<commit_after>/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     Nuxeo - initial API and implementation
 *
 * $Id$
 */

package org.nuxeo.theme.bank;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.theme.presets.PaletteIdentifyException;
import org.nuxeo.theme.presets.PaletteParseException;
import org.nuxeo.theme.presets.PaletteParser;
import org.nuxeo.theme.resources.BankManager;
import org.nuxeo.theme.resources.BankUtils;

public class Utils {
    private static final Log log = LogFactory.getLog(Utils.class);

    private static final List<String> PRESET_CATEGORIES = Arrays.asList(
            "color", "background", "font", "border");

    public static List<String> getCollections(String bankName)
            throws IOException {
        return BankManager.getCollections(bankName);
    }

    public static List<String> getItemsInCollection(String bankName,
            String collection, String typeName) throws IOException {
        return BankManager.getItemsInCollection(bankName, collection, typeName);
    }

    public static List<String> listSkinsInCollection(String bankName,
            String collection) throws IOException {
        Map<String, Object> info;
        info = BankManager.getInfo(bankName, collection, "style");
        List<String> skins = new ArrayList<String>();
        if (info != null) {
            for (Map.Entry<String, Object> entry : info.entrySet()) {
                String resource = entry.getKey();
                Map value = (Map) entry.getValue();
                Boolean isSkin = false;
                if (value.containsKey("skin")) {
                    isSkin = (Boolean) value.get("skin");
                }
                if (isSkin) {
                    skins.add(resource);
                }
            }
        }
        return skins;
    }

    public static Properties getPresetProperties(String bank,
            String collection, String category) {
        String path = String.format("%s/%s/preset/%s", bank, collection,
                category);
        File file;
        try {
            file = BankManager.getFile(path);
        } catch (IOException e) {
            throw new ThemeBankException(e.getMessage(), e);
        }
        Properties properties = new Properties();
        if (!file.exists()) {
            return properties;
        }
        for (File f : file.listFiles()) {
            String content;
            try {
                content = BankUtils.getFileContent(f);
            } catch (IOException e) {
                log.warn("Could not read file: " + f.getAbsolutePath());
                continue;
            }
            try {
                properties.putAll(PaletteParser.parse(content.getBytes(),
                        f.getName()));
            } catch (PaletteIdentifyException e) {
                log.warn("Could not identify palette type: "
                        + f.getAbsolutePath());
            } catch (PaletteParseException e) {
                log.warn("Could not parse palette: " + f.getAbsolutePath());
            }
        }
        return properties;
    }

    /*
     * JSON calls
     */
    public static String listBankSkins(String bankName) throws IOException {
        JSONArray skins = new JSONArray();
        for (String collection : BankManager.getCollections(bankName)) {
            Map<String, Object> info = BankManager.getInfo(bankName,
                    collection, "style");
            if (info == null) {
                continue;
            }
            for (Map.Entry<String, Object> entry : info.entrySet()) {
                String resource = entry.getKey();
                Map value = (Map) entry.getValue();
                if (value.containsKey("skin") && (Boolean) value.get("skin")) {
                    Boolean isBase = false;
                    if (value.containsKey("base")) {
                        isBase = (Boolean) value.get("base");
                    }
                    JSONObject skinMap = new JSONObject();
                    skinMap.put("bank", bankName);
                    skinMap.put("collection", collection);
                    skinMap.put("resource", resource);
                    skinMap.put("name", String.format("%s (%s)",
                            resource.replace(".css", ""), collection));
                    skinMap.put("base", isBase);
                    skins.add(skinMap);
                }
            }
        }
        return skins.toString();
    }

    public static String listBankStyles(String bankName) throws IOException {
        JSONArray styles = new JSONArray();
        for (String collection : BankManager.getCollections(bankName)) {
            for (String resource : getItemsInCollection(bankName, collection,
                    "style")) {
                JSONObject styleMap = new JSONObject();
                styleMap.put("bank", bankName);
                styleMap.put("collection", collection);
                styleMap.put("resource", resource);
                styleMap.put("name", String.format("%s (%s)", resource.replace(
                        ".css", ""), collection));
                styles.add(styleMap);
            }
        }
        return styles.toString();
    }

    public static String listBankPresets(String bankName) throws IOException {
        JSONArray presets = new JSONArray();
        for (String collection : BankManager.getCollections(bankName)) {
            for (String category : PRESET_CATEGORIES) {
                for (Map.Entry property : getPresetProperties(bankName,
                        collection, category).entrySet()) {
                    JSONObject presetMap = new JSONObject();
                    presetMap.put("bank", bankName);
                    presetMap.put("collection", collection);
                    presetMap.put("category", category);
                    presetMap.put("name", property.getKey());
                    presetMap.put("value", property.getValue());
{'fraction_non_alphanumeric': 0.10575227241893909, 'fraction_numerical': 0.001628001628001628, 'mean_word_length': 1.984412955465587, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, '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': '7659056', 'n_tokens_mistral': 3787, 'n_tokens_neox': 3619, 'n_words': 937}