Statistics
| Revision:

gvsig-projects-pool / org.gvsig.topology / trunk / org.gvsig.topology / org.gvsig.topology.lib / org.gvsig.topology.lib.api / src / main / java / org / gvsig / topology / lib / spi / RuleResourceLoaderUtils.java @ 712

History | View | Annotate | Download (4.12 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.topology.lib.spi;
7

    
8
import java.io.File;
9
import java.io.InputStream;
10
import java.net.MalformedURLException;
11
import java.net.URL;
12
import java.util.List;
13
import java.util.Locale;
14
import java.util.logging.Level;
15
import org.apache.commons.io.IOUtils;
16
import org.apache.commons.lang3.StringUtils;
17
import org.json.JSONArray;
18
import org.json.JSONObject;
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
21

    
22
/**
23
 *
24
 * @author jjdelcerro
25
 */
26
@SuppressWarnings("UseSpecificCatch")
27
public class RuleResourceLoaderUtils {
28
    
29
    private static Logger LOGGER = LoggerFactory.getLogger(RuleResourceLoaderUtils.class);
30
    
31
    public static URL getRuleURL(String idRule) {
32
        String lang = Locale.getDefault().getLanguage();
33
        URL url = RuleResourceLoaderUtils.class.getResource("/org/gvsig/topology/rules/"+lang+"/"+idRule+".json");
34
        if( url == null ) {
35
            url = RuleResourceLoaderUtils.class.getResource("/org/gvsig/topology/rules/en/"+idRule+".json");
36
            if( url == null ) {
37
                return null;
38
            }
39
        }
40
        return url;
41
    }
42
    
43
    public static JSONObject getRule(URL url) {
44
        if( url == null ) {
45
            return null;
46
        }
47
        InputStream is = null;
48
        JSONObject json;
49
        try {
50
            is = url.openStream();
51
            List<String> lines = IOUtils.readLines(is);
52
            json = new JSONObject(StringUtils.join(lines,  "\n"));
53
        } catch (Exception ex) {
54
            LOGGER.warn("Can't load resource from json file '"+url.toString()+"'.", ex);
55
            return null;
56
        } finally {
57
            IOUtils.closeQuietly(is);
58
        }
59
        return json;
60
    }
61

    
62
    public static JSONObject getRule(String idRuleOrPathname) {
63
        if( idRuleOrPathname==null ) {
64
            return null;
65
        }
66
        if( idRuleOrPathname.endsWith(".json") ) {
67
            // Asumimos que es una ruta al fichero json.
68
            return getRule(new File(idRuleOrPathname));
69
        }
70
        // Asumimos que es un ID de regla
71
        URL url = getRuleURL(idRuleOrPathname);
72
        if( url == null ) {
73
            return null;
74
        }
75
        return getRule(url);
76
    }
77
    
78
    public static JSONObject getRule(File jsonfile) {
79
        if( jsonfile == null || !jsonfile.exists() ) {
80
            return null;
81
        }
82
        try {
83
            return getRule(jsonfile.toURI().toURL());
84
        } catch (MalformedURLException ex) {
85
            LOGGER.warn("Can't load resource from json file '"+jsonfile.toString()+"'.", ex);
86
            return null;
87
        }
88
    }
89

    
90
    public static String getDescription(String idRule, JSONObject json) {
91
        String description = null;
92
        
93
        if( json.has("description") ) {
94
            Object x = json.get("description");
95
            if( x instanceof String ) {
96
                description = (String) x;
97
            } else if( x instanceof JSONArray ) {
98
                StringBuilder builder = new StringBuilder();
99
                for (int i = 0; i < ((JSONArray)x).length(); i++) {
100
                    if( i>0 ) {
101
                        builder.append(" ");
102
                    }
103
                    builder.append(((JSONArray)x).getString(i));
104
                }
105
                description = builder.toString();
106
            } else {
107
                description = x.toString();
108
            }
109
            if( description.contains("@@@") ) {
110
                description = StringUtils.replace(
111
                        description, 
112
                        "@@@", 
113
                        getRuleURL(idRule).toString()
114
                );
115
            }
116
        }
117
        return description;
118
    }
119
    
120
    public static JSONObject getAction(JSONObject rule, String idAction) {
121
        if( !rule.has("actions") ) {
122
            return null;
123
        }
124
        if( !rule.getJSONObject("actions").has(idAction) ) {
125
            return null;
126
        }
127
        JSONObject action = rule.getJSONObject("actions").getJSONObject(idAction);
128
        return action;
129
    }
130

    
131
}