Skip to content
mycoin edited this page Jul 3, 2016 · 1 revision
package net.breakidea.common.core;

import java.io.Writer;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author apple
 *
 */
public class ResourceMap {

    private Map<String, Object> cache = new HashMap<String, Object>();

    /**
     * @param url
     */
    public void addScript( List<String> url ) {
        List<String> array = getGroup("script");
        for (String item : url) {
            array.add(item);
        }
    }

    /**
     * @param url
     */
    public void addScript( String url ) {
        getGroup("script").add(url);
    }

    /**
     * @param url
     */
    public void addStyle( List<String> url ) {
        List<String> array = getGroup("style");
        for (String item : url) {
            array.add(item);
        }
    }

    /**
     * @param url
     */
    public void addStyle( String url ) {
        getGroup("style").add(url);
    }

    /**
     * @return
     */
    public void cleanScript() {
        cache.remove("script");
    }

    /**
     * @return
     */
    public void cleanStyle() {
        cache.remove("style");
    }

    /**
     * @return the body
     */
    public Writer getBody() {
        return getContent("body");
    }

    /**
     * @param string
     * @return
     */
    public Writer getContent( String string ) {
        return (Writer) cache.get("content".concat(string));
    }

    /**
     * @param key
     * @return
     */
    public String getEnv( String key ) {
        String value = System.getenv(key);
        if (value == null) {
            value = System.getProperty(key);
        }
        return value;
    }

    /**
     * @return
     */
    public InetAddress getInet() {
        try {
            return InetAddress.getLocalHost();
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * @return the layout
     */
    public String getLayout() {
        return (String) cache.get("layout");
    }

    /**
     * @param name
     * @param content
     */
    public String getMeta( String name ) {
        return (String) cache.get("meta".concat(name));
    }

    /**
     * @param key
     * @param data
     * @return
     */
    public Object getObject( String key ) {
        return cache.get("object".concat(key));
    }

    /**
     * @return
     */
    public List<String> getScripts() {
        return getGroup("script");
    }

    /**
         * @return
         */
    public List<String> getStyles() {
        return getGroup("style");
    }

    /**
     * @return
     */
    public String getTitle() {
        return (String) cache.get("title");
    }

    /**
     * @return
     */
    public boolean isLayout() {
        return getBody() != null;
    }

    /**
     * @return
     */
    public boolean isLayoutEnabled() {
        return getLayout() != null;
    }

    /**
     * @param body the body to set
     */
    public void setBody( Writer body ) {
        setContent("body", body);
    }

    /**
     * @param name
     * @param value
     * @return
     */
    public void setContent( String name, Writer value ) {
        cache.put("content".concat(name), value);
    }

    /**
     * @param layout the layout to set
     */
    public void setLayout( String layout ) {
        cache.put("layout", layout);
    }

    /**
     * @param enabled
     */
    public void setLayoutEnabled( boolean enabled ) {
        if (!enabled) {
            setLayout(null);
        }
    }

    /**
     * @param name
     * @param content
     */
    public void setMeta( String name, String content ) {
        cache.put("meta".concat(name), content);
    }

    /**
     * @param key
     * @param data
     */
    public void setObject( String key, Object data ) {
        cache.put("object".concat(key), data);
    }

    /**
     * @param title
     */
    public void setTitle( String title ) {
        cache.put("title", title);
    }

    /**
     * @param string
     * @return
     */
    @SuppressWarnings("unchecked")
    private List<String> getGroup( String type ) {
        Object data = cache.get(type);
        if (data == null || !(data instanceof List)) {
            data = new ArrayList<String>();
            cache.put(type, data);
        }
        return (List<String>) data;
    }
}
Clone this wiki locally