forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxResources.java
More file actions
173 lines (149 loc) · 3.38 KB
/
mxResources.java
File metadata and controls
173 lines (149 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Copyright (c) 2010, Gaudenz Alder
*/
package com.mxgraph.util;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
public class mxResources
{
private static final Logger log = Logger.getLogger(mxResources.class.getName());
/**
* Ordered list of the inserted resource bundles.
*/
protected static LinkedList<ResourceBundle> bundles = new LinkedList<ResourceBundle>();
/**
* Returns the bundles.
*
* @return Returns the bundles.
*/
public static LinkedList<ResourceBundle> getBundles()
{
return bundles;
}
/**
* Sets the bundles.
*
* @param value
* The bundles to set.
*/
public static void setBundles(LinkedList<ResourceBundle> value)
{
bundles = value;
}
/**
* Adds a resource bundle. This may throw a MissingResourceException that
* should be handled in the calling code.
*
* @param basename
* The basename of the resource bundle to add.
*/
public static void add(String basename)
{
bundles.addFirst(PropertyResourceBundle.getBundle(basename));
}
/**
* Adds a resource bundle. This may throw a MissingResourceException that
* should be handled in the calling code.
*
* @param basename
* The basename of the resource bundle to add.
*/
public static void add(String basename, Locale locale)
{
bundles.addFirst(PropertyResourceBundle.getBundle(basename, locale));
}
/**
*
*/
public static String get(String key)
{
return get(key, null, null);
}
/**
*
*/
public static String get(String key, String defaultValue)
{
return get(key, null, defaultValue);
}
/**
* Returns the value for the specified resource key.
*/
public static String get(String key, String[] params)
{
return get(key, params, null);
}
/**
* Returns the value for the specified resource key.
*/
public static String get(String key, String[] params, String defaultValue)
{
String value = getResource(key);
// Applies default value if required
if (value == null)
{
value = defaultValue;
}
// Replaces the placeholders with the values in the array
if (value != null && params != null)
{
StringBuffer result = new StringBuffer();
String index = null;
for (int i = 0; i < value.length(); i++)
{
char c = value.charAt(i);
if (c == '{')
{
index = "";
}
else if (index != null && c == '}')
{
int tmp = Integer.parseInt(index) - 1;
if (tmp >= 0 && tmp < params.length)
{
result.append(params[tmp]);
}
index = null;
}
else if (index != null)
{
index += c;
}
else
{
result.append(c);
}
}
value = result.toString();
}
return value;
}
/**
* Returns the value for <code>key</code> by searching the resource
* bundles in inverse order or <code>null</code> if no value can be found
* for <code>key</code>.
*/
protected static String getResource(String key)
{
Iterator<ResourceBundle> it = bundles.iterator();
while (it.hasNext())
{
try
{
return it.next().getString(key);
}
catch (MissingResourceException e)
{
// continue looking in other bundles
}
}
log.severe("Resource " + key + " not found in any bundle");
return null;
}
}