Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / resourcesstorage / AbstractResourcesStorage.java @ 2142

History | View | Annotate | Download (3.73 KB)

1
package org.gvsig.tools.resourcesstorage;
2

    
3
import java.util.ArrayList;
4
import java.util.Collections;
5
import java.util.List;
6
import java.util.Locale;
7
import org.gvsig.tools.ToolsLocator;
8
import org.gvsig.tools.i18n.I18nManager;
9

    
10
/**
11
 *
12
 * @author jjdelcerro
13
 */
14
public abstract class AbstractResourcesStorage implements ResourcesStorage {
15

    
16
    public static abstract class AbstractResource implements ResourcesStorage.Resource {
17
      
18
      protected String name;
19
      
20
      protected AbstractResource(String name) {
21
        this.name = name;
22
      }
23
      
24
      @Override
25
      public String getName() {
26
        return this.name;
27
      }
28
    }
29

    
30
    @Override
31
    public String getSeparator() {
32
        return ".";
33
    }
34
    
35
    @Override
36
    public boolean isEmpty() {
37
        return false;
38
    }
39

    
40
    @Override
41
    public boolean isReadOnly() {
42
        return false;
43
    }
44
    
45
    @Override
46
    public List<Resource> getResources(String resourceName) {
47
        if( this.isEmpty() ) {
48
            return null;
49
        }
50
        List<ResourcesStorage.Resource>ress = new ArrayList<>();
51
        int n = 0;
52
        while(true) {
53
            String multiresourceName;
54
            if( n==0 ) {
55
                multiresourceName = resourceName;
56
            } else {
57
                multiresourceName = String.valueOf(n) + this.getSeparator() + resourceName ;
58
            }
59
            Resource res = this.getResource(multiresourceName);
60
            if( !res.exists() ) {
61
                break;
62
            }
63
            ress.add(res);
64
            n++;
65
        }
66
        if( ress.isEmpty() ) {
67
            return null;
68
        }
69
        return ress;
70
    }
71

    
72
    @Override
73
    public boolean exists(String name) {
74
        if( this.isEmpty() ) {
75
            return false;
76
        }
77
        Resource res = null;
78
        try {
79
            res = this.getResource(name);
80
            return res.exists();
81
        } finally {
82
            if( res !=null ) {
83
                res.close();
84
            }
85
        }
86
    }
87

    
88
    @Override
89
    public Resource getLocalizedResource(String resourceName) {
90
        I18nManager i18n = ToolsLocator.getI18nManager();
91
        Locale currentlocale = i18n.getCurrentLocale();
92
        if( currentlocale != null ) {
93
            String localizedName = currentlocale.getLanguage() + this.getSeparator() + resourceName;
94
            if( this.exists(localizedName) ) {
95
                return this.getResource(localizedName);
96
            }
97
            List<Locale> locales = i18n.getPreferredLocales();
98
            if( locales!=null ) {
99
                for (Locale locale : locales) {
100
                    localizedName = locale.getLanguage() + this.getSeparator() + resourceName;
101
                    if( this.exists(localizedName) ) {
102
                        return this.getResource(localizedName);
103
                    }
104
                }
105
            }
106
        }
107
        return this.getResource(resourceName);
108
    }
109
    
110
    @Override
111
    public List<Resource> getLocalizedResources(String resourceName) {
112
        if( this.isEmpty() ) {
113
            return null;
114
        }
115
        List<ResourcesStorage.Resource>ress = new ArrayList<>();
116
        int n = 0;
117
        while(true) {
118
            String multiresourceName;
119
            if( n==0 ) {
120
                multiresourceName = resourceName;
121
            } else {
122
                multiresourceName = resourceName + this.getSeparator() + String.valueOf(n);
123
            }
124
            Resource res = this.getLocalizedResource(multiresourceName);
125
            if( !res.exists() ) {
126
                break;
127
            }
128
            ress.add(res);
129
            n++;
130
        }
131
        if( ress.isEmpty() ) {
132
            return null;
133
        }
134
        return ress;
135
    }
136

    
137
  @Override
138
  public List<String> getResourceNames() {
139
    return Collections.EMPTY_LIST;
140
  }
141

    
142
}