gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / resourcesstorage / AbstractResourcesStorage.java @ 1961
History | View | Annotate | Download (3.16 KB)
1 |
package org.gvsig.tools.resourcesstorage; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.List; |
5 |
import java.util.Locale; |
6 |
import org.gvsig.tools.ToolsLocator; |
7 |
import org.gvsig.tools.i18n.I18nManager; |
8 |
|
9 |
/**
|
10 |
*
|
11 |
* @author jjdelcerro
|
12 |
*/
|
13 |
public abstract class AbstractResourcesStorage implements ResourcesStorage { |
14 |
|
15 |
@Override
|
16 |
public boolean isEmpty() { |
17 |
return false; |
18 |
} |
19 |
|
20 |
@Override
|
21 |
public boolean isReadOnly() { |
22 |
return false; |
23 |
} |
24 |
|
25 |
@Override
|
26 |
public List<Resource> getResources(String resourceName) { |
27 |
if( this.isEmpty() ) { |
28 |
return null; |
29 |
} |
30 |
List<ResourcesStorage.Resource>ress = new ArrayList<>(); |
31 |
int n = 0; |
32 |
while(true) { |
33 |
String multiresourceName;
|
34 |
if( n==0 ) { |
35 |
multiresourceName = resourceName; |
36 |
} else {
|
37 |
multiresourceName = String.valueOf(n) + "." + resourceName ; |
38 |
} |
39 |
Resource res = this.getResource(multiresourceName);
|
40 |
if( !res.exists() ) {
|
41 |
break;
|
42 |
} |
43 |
ress.add(res); |
44 |
n++; |
45 |
} |
46 |
if( ress.isEmpty() ) {
|
47 |
return null; |
48 |
} |
49 |
return ress;
|
50 |
} |
51 |
|
52 |
@Override
|
53 |
public boolean exists(String name) { |
54 |
if( this.isEmpty() ) { |
55 |
return false; |
56 |
} |
57 |
Resource res = null;
|
58 |
try {
|
59 |
res = this.getResource(name);
|
60 |
return res.exists();
|
61 |
} finally {
|
62 |
if( res !=null ) { |
63 |
res.close(); |
64 |
} |
65 |
} |
66 |
} |
67 |
|
68 |
@Override
|
69 |
public Resource getLocalizedResource(String resourceName) { |
70 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
71 |
Locale currentlocale = i18n.getCurrentLocale();
|
72 |
if( currentlocale != null ) { |
73 |
String localizedName = currentlocale.getLanguage() +"."+ resourceName; |
74 |
if( this.exists(localizedName) ) { |
75 |
return this.getResource(localizedName); |
76 |
} |
77 |
List<Locale> locales = i18n.getPreferredLocales(); |
78 |
if( locales!=null ) { |
79 |
for (Locale locale : locales) { |
80 |
localizedName = locale.getLanguage() +"."+ resourceName;
|
81 |
if( this.exists(localizedName) ) { |
82 |
return this.getResource(localizedName); |
83 |
} |
84 |
} |
85 |
} |
86 |
} |
87 |
return this.getResource(resourceName); |
88 |
} |
89 |
|
90 |
@Override
|
91 |
public List<Resource> getLocalizedResources(String resourceName) { |
92 |
if( this.isEmpty() ) { |
93 |
return null; |
94 |
} |
95 |
List<ResourcesStorage.Resource>ress = new ArrayList<>(); |
96 |
int n = 0; |
97 |
while(true) { |
98 |
String multiresourceName;
|
99 |
if( n==0 ) { |
100 |
multiresourceName = resourceName; |
101 |
} else {
|
102 |
multiresourceName = resourceName + "." + String.valueOf(n); |
103 |
} |
104 |
Resource res = this.getLocalizedResource(multiresourceName);
|
105 |
if( !res.exists() ) {
|
106 |
break;
|
107 |
} |
108 |
ress.add(res); |
109 |
n++; |
110 |
} |
111 |
if( ress.isEmpty() ) {
|
112 |
return null; |
113 |
} |
114 |
return ress;
|
115 |
} |
116 |
|
117 |
} |