1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package javax.faces.internal; |
17 | |
|
18 | |
import java.util.Locale; |
19 | |
|
20 | |
import javax.faces.component.UIViewRoot; |
21 | |
import javax.faces.context.FacesContext; |
22 | |
|
23 | |
import org.seasar.framework.convention.NamingConvention; |
24 | |
import org.seasar.framework.log.Logger; |
25 | |
import org.seasar.framework.message.MessageResourceBundle; |
26 | |
import org.seasar.teeda.core.util.DIContainerUtil; |
27 | |
import org.seasar.teeda.core.util.ViewHandlerUtil; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | 0 | public class LabelUtil { |
33 | |
|
34 | 3 | private static Logger logger = Logger.getLogger(LabelUtil.class); |
35 | |
|
36 | |
public static String getLabelValue(String defaultKey) { |
37 | 74 | final FacesContext context = FacesContext.getCurrentInstance(); |
38 | 74 | if (context == null) { |
39 | 1 | return null; |
40 | |
} |
41 | 73 | final UIViewRoot viewRoot = context.getViewRoot(); |
42 | 73 | if (viewRoot == null) { |
43 | 1 | logger.log("WTDA0202", new Object[0]); |
44 | 1 | return null; |
45 | |
} |
46 | 72 | String viewId = viewRoot.getViewId(); |
47 | 72 | if (viewId == null) { |
48 | 67 | return null; |
49 | |
} |
50 | 5 | final NamingConvention nc = (NamingConvention) DIContainerUtil |
51 | |
.getComponentNoException(NamingConvention.class); |
52 | 5 | if (nc == null || !nc.isValidViewRootPath(viewId)) { |
53 | 1 | return null; |
54 | |
} |
55 | 4 | String pageName = nc.fromPathToPageName(viewId); |
56 | 4 | return getLabelValue(defaultKey, pageName); |
57 | |
} |
58 | |
|
59 | |
public static String getLabelValue(String defaultKey, String pageName) { |
60 | 4 | if (defaultKey == null) { |
61 | 0 | return null; |
62 | |
} |
63 | 4 | int index = defaultKey.lastIndexOf('-'); |
64 | 4 | if (index > 0) { |
65 | 1 | defaultKey = defaultKey.substring(0, index); |
66 | |
} |
67 | 4 | final NamingConvention nc = (NamingConvention) DIContainerUtil |
68 | |
.getComponentNoException(NamingConvention.class); |
69 | 4 | if (nc == null) { |
70 | 0 | return null; |
71 | |
} |
72 | 4 | String key = getKey(nc, pageName, defaultKey); |
73 | 4 | String propertiesName = getPropertiesName(nc, pageName); |
74 | 4 | String defaultPropertiesName = getDefaultApplicationPropertiesName(nc, |
75 | |
pageName); |
76 | 4 | return getLabelValue(key, propertiesName, defaultKey, |
77 | |
defaultPropertiesName); |
78 | |
} |
79 | |
|
80 | |
private static String getKey(final NamingConvention nc, |
81 | |
final String pageName, final String defaultKey) { |
82 | 4 | final String labelKeySuffix = getLabelKeySuffix(nc, pageName); |
83 | 4 | return new String(new StringBuffer(labelKeySuffix.length() + |
84 | |
defaultKey.length() + 1).append(labelKeySuffix).append( |
85 | |
InternalConstants.DOT).append(defaultKey)); |
86 | |
} |
87 | |
|
88 | |
public static String getLabelValue(String key, String propertiesName, |
89 | |
String defaultKey, String defaultPropertiesName) { |
90 | 4 | final Locale locale = ViewHandlerUtil.getLocale(); |
91 | 4 | String value = null; |
92 | 4 | if (propertiesName != null) { |
93 | 4 | MessageResourceBundle bundle = MessageResourceBundleChainFactory |
94 | |
.createChain(propertiesName, locale); |
95 | 4 | value = (bundle != null) ? (String) bundle.get(key) : null; |
96 | 4 | if (value == null && bundle != null) { |
97 | 4 | value = bundle.get(defaultKey); |
98 | |
} |
99 | |
} |
100 | 4 | if (value == null) { |
101 | 1 | if (defaultPropertiesName != null) { |
102 | 1 | MessageResourceBundle bundle = MessageResourceBundleChainFactory |
103 | |
.createChain(defaultPropertiesName, locale); |
104 | 1 | value = (bundle != null) ? (String) bundle.get(defaultKey) |
105 | |
: null; |
106 | |
} |
107 | |
} |
108 | 4 | return value; |
109 | |
} |
110 | |
|
111 | |
public static String getPropertiesName(NamingConvention nc, String pageName) { |
112 | 5 | String packageName = NamingConventionUtil.getPackageName(nc, pageName); |
113 | 5 | return (packageName != null) ? new String(new StringBuffer(packageName |
114 | |
.length() + 5).append(packageName) |
115 | |
.append(InternalConstants.DOT).append(InternalConstants.LABEL)) |
116 | |
: null; |
117 | |
} |
118 | |
|
119 | |
public static String getLabelKeySuffix(NamingConvention nc, String pageName) { |
120 | 5 | String path = nc.fromPageNameToPath(pageName); |
121 | 5 | String defaultSuffix = FacesConfigOptions.getDefaultSuffix(); |
122 | 5 | if (path.endsWith(defaultSuffix)) { |
123 | 5 | path = path.substring(0, path.lastIndexOf(defaultSuffix)); |
124 | |
} |
125 | 5 | int lastIndex = path.lastIndexOf('/'); |
126 | 5 | if (lastIndex > 0) { |
127 | 5 | path = path.substring(lastIndex + 1); |
128 | |
} |
129 | 5 | return path; |
130 | |
} |
131 | |
|
132 | |
public static String getDefaultApplicationPropertiesName( |
133 | |
NamingConvention nc, String pageName) { |
134 | 6 | String subAppRoot = nc.getSubApplicationRootPackageName(); |
135 | 6 | String defaultPropertiesName = null; |
136 | 6 | String packageName = NamingConventionUtil.getPackageName(nc, pageName); |
137 | 6 | if (packageName == null) { |
138 | 1 | return null; |
139 | |
} |
140 | 5 | if (packageName.lastIndexOf(subAppRoot) > 0) { |
141 | 5 | final int len = packageName.lastIndexOf(subAppRoot) + |
142 | |
subAppRoot.length(); |
143 | 5 | final String s = packageName.substring(0, len); |
144 | 5 | defaultPropertiesName = new String(new StringBuffer(s.length() + 5) |
145 | |
.append(s).append(InternalConstants.DOT).append( |
146 | |
InternalConstants.LABEL)); |
147 | |
} |
148 | 5 | return defaultPropertiesName; |
149 | |
} |
150 | |
|
151 | |
} |