fr.mael.microrss.util.Tools.java Source code

Java tutorial

Introduction

Here is the source code for fr.mael.microrss.util.Tools.java

Source

/*
   Copyright  2013 Mael Le Guvel
   This work is free. You can redistribute it and/or modify it under the
   terms of the Do What The Fuck You Want To Public License, Version 2,
   as published by Sam Hocevar. See the COPYING file for more details.
*/
package fr.mael.microrss.util;

import java.beans.PropertyDescriptor;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.commons.beanutils.PropertyUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fr.mael.microrss.domain.UserConfig;

public abstract class Tools {

    private static Logger LOG = LoggerFactory.getLogger(Tools.class);

    public static String getMessage(String message, Locale locale) {
        ResourceBundle bundle = ResourceBundle.getBundle("fr.mael.microrss.i18n.messages", locale);
        return bundle.getString(message);
    }

    public static String getBaseUrl(String completeUrl) throws MalformedURLException {
        URL url = new URL(completeUrl);
        String host = url.getHost();
        return new StringBuffer("http://").append(host).toString();
    }

    /**
     * Convert ResourceBundle into a Map object.
     *
     * @param resource a resource bundle to convert.
     * @return Map a map version of the resource bundle.
     * @throws UnsupportedEncodingException 
     */
    public static Map<String, String> convertResourceBundleToMap(ResourceBundle resource)
            throws UnsupportedEncodingException {
        Map<String, String> map = new HashMap<String, String>();

        for (String key : resource.keySet()) {
            map.put(key, resource.getString(key));
        }

        return map;
    }

    /**
     * Cleans an html source
     * @param source
     * @return
     */
    public static String cleanHTML(String source) {
        if (source == null) {
            return null;
        }
        Document doc = Jsoup.parse(source);
        doc.select("link").remove();
        doc.select("*[style]").removeAttr("style");
        doc.select("style,script").remove();
        return doc.select("body").html().toString();
    }

    /**
     * Reads an image from an url and converts it to byte array
     * @param url
     * @return a byte array representing the image
     */
    public static byte[] getImage(String url) {
        try {
            URL imgUrl = new URL(url);
            InputStream inputStream = imgUrl.openStream();
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];

            int n = 0;
            while (-1 != (n = inputStream.read(buffer))) {
                output.write(buffer, 0, n);
            }
            inputStream.close();

            byte[] data = output.toByteArray();
            return data;
        } catch (Exception e) {
            LOG.info("Error get image " + url, e);
        }
        return null;

    }

    public static HashMap<String, Object> toMap(UserConfig conf)
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        HashMap<String, Object> options = new HashMap<String, Object>();
        PropertyDescriptor[] descs = PropertyUtils.getPropertyDescriptors(UserConfig.class);
        for (PropertyDescriptor desc : descs) {
            if (!desc.getName().equals("class") && !desc.getName().equals("user")) {
                options.put(desc.getName(), PropertyUtils.getProperty(conf, desc.getName()));
            }
        }
        return options;

    }

    public static void merge(Map<String, Object> map1, Map<String, Object> map2) {
        for (String key : map2.keySet()) {
            map1.put(key, map2.get(key));
        }
    }

}