Statistics
| Revision:

gvsig-projects-pool / org.gvsig.online / trunk / org.gvsig.online / org.gvsig.online.lib / org.gvsig.online.lib.impl / src / main / java / org / gvsig / online / lib / impl / OnlineCategories.java @ 9518

History | View | Annotate | Download (3.42 KB)

1
package org.gvsig.online.lib.impl;
2

    
3
import java.util.AbstractList;
4
import java.util.ArrayList;
5
import java.util.Collections;
6
import java.util.List;
7
import org.apache.commons.lang3.ArrayUtils;
8
import org.apache.commons.lang3.StringUtils;
9
import org.gvsig.tools.dynobject.Tagged;
10
import org.gvsig.tools.dynobject.Tags;
11
import org.gvsig.tools.dynobject.impl.DefaultTags;
12

    
13
/**
14
 *
15
 * @author jjdelcerro
16
 */
17
public class OnlineCategories  extends AbstractList<String> implements List<String> {
18

    
19
    private final String categories_s;
20
    private List<Category> categories;
21

    
22
    private static class Category implements Tagged {        
23

    
24
        private final String category_s;
25
        private String name;
26
        private DefaultTags tags;
27
        
28
        public Category(String category) {
29
            this.category_s = category;
30
            this.parse();
31
        }
32
        
33
        private void parse() {
34
            this.tags = new DefaultTags();
35
            this.name = "unknown";
36
            
37
            String[] ss = StringUtils.split(this.category_s, ";");
38
            if( ArrayUtils.isEmpty(ss) ) {
39
                return;
40
            }
41
            this.name = StringUtils.strip(ss[0]);
42
            for (int i = 1; i < ss.length; i++) {
43
                String s1 = ss[i];
44
                if( StringUtils.isBlank(s1) ) {
45
                    continue;
46
                }
47
                int n = s1.indexOf("=");
48
                if( n<0 ) {
49
                    this.tags.set(StringUtils.strip(s1), null);
50
                } else {
51
                    String tagname = StringUtils.strip(StringUtils.substring(s1, 0, n));
52
                    String tagvalue = StringUtils.stripToNull(StringUtils.substring(s1, n+1));
53
                    this.tags.set(tagname, tagvalue);
54
                }
55
            }
56
        }
57

    
58
        public String getName() {
59
            return this.name;
60
        }
61

    
62
        @Override
63
        public Tags getTags() {
64
            return this.tags;
65
        }
66
        
67
    }
68
    
69
    public OnlineCategories(String categories) {
70
        this.categories_s = categories;
71
        this.categories = null;
72
    }
73

    
74
    private List<Category> getCategories() {
75
        if( this.categories == null ) {
76
            if( StringUtils.isBlank(this.categories_s) ) {
77
                this.categories = Collections.EMPTY_LIST;
78
            } else {
79
                try{
80
                    List<Category> r = new ArrayList<>();
81
                    String[] ss = StringUtils.split(this.categories_s, ",");
82
                    for (String s1 : ss) {
83
                        if (StringUtils.isBlank(s1)) {
84
                            continue;
85
                        }
86
                        Category category = new Category(s1);
87
                        r.add(category);
88
                    }
89
                    this.categories = r;
90
                } catch (Exception ex) {
91
                    this.categories = Collections.EMPTY_LIST;
92
                }
93
            }
94
        }   
95
        return this.categories;
96
    }
97
    
98
    @Override
99
    public String get(int index) {
100
        return this.getCategories().get(index).getName();
101
    }
102

    
103
    @Override
104
    public int size() {
105
        return this.getCategories().size();
106
    }
107

    
108
    public Tags getTags(String name) {
109
        for (Category category : this.getCategories()) {
110
            if( StringUtils.equalsIgnoreCase(name, category.getName()) ) {
111
                return category.getTags();
112
            }
113
        }
114
        return Tags.EMPTY_TAGS;
115
    }
116
}