001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.xwork;
006:
007: import com.opensymphony.xwork.util.LocalizedTextUtil;
008: import com.opensymphony.xwork.util.OgnlValueStack;
009:
010: import java.io.ObjectStreamException;
011: import java.io.Serializable;
012: import java.util.List;
013: import java.util.ResourceBundle;
014: import java.util.ArrayList;
015: import java.util.Arrays;
016: import java.text.MessageFormat;
017:
018: /**
019: * DefaultTextProvider gets texts from only the default resource bundles associated with the
020: * LocalizedTextUtil.
021: *
022: * @author Jason Carreira <jcarreira@gmail.com>
023: * @author Rainer Hermanns
024: * @see LocalizedTextUtil#addDefaultResourceBundle(String)
025: */
026: public class DefaultTextProvider implements TextProvider, Serializable,
027: Unchainable {
028:
029: private static final long serialVersionUID = 5559215734038422388L;
030:
031: private static final Object[] EMPTY_ARGS = new Object[0];
032:
033: public static final DefaultTextProvider INSTANCE = new DefaultTextProvider();
034:
035: private DefaultTextProvider() {
036: }
037:
038: public boolean hasKey(String key) {
039: return getText(key) == null ? false : true;
040: }
041:
042: public String getText(String key) {
043: return LocalizedTextUtil.findDefaultText(key, ActionContext
044: .getContext().getLocale());
045: }
046:
047: public String getText(String key, String defaultValue) {
048: String text = getText(key);
049: if (text == null) {
050: return defaultValue;
051: }
052: return text;
053: }
054:
055: public String getText(String key, List args) {
056: Object[] params;
057: if (args != null) {
058: params = args.toArray();
059: } else {
060: params = EMPTY_ARGS;
061: }
062:
063: return LocalizedTextUtil.findDefaultText(key, ActionContext
064: .getContext().getLocale(), params);
065: }
066:
067: public String getText(String key, String[] args) {
068: Object[] params;
069: if (args != null) {
070: params = args;
071: } else {
072: params = EMPTY_ARGS;
073: }
074:
075: return LocalizedTextUtil.findDefaultText(key, ActionContext
076: .getContext().getLocale(), params);
077: }
078:
079: public String getText(String key, String defaultValue, List args) {
080: String text = getText(key, args);
081: if (text == null) {
082: MessageFormat format = new MessageFormat(defaultValue);
083: format.setLocale(ActionContext.getContext().getLocale());
084: format.applyPattern(defaultValue);
085:
086: Object[] params;
087: if (args != null) {
088: params = args.toArray();
089: } else {
090: params = EMPTY_ARGS;
091: }
092:
093: return format.format(params);
094: }
095: return text;
096: }
097:
098: public String getText(String key, String defaultValue, String[] args) {
099: String text = getText(key, args);
100: if (text == null) {
101: MessageFormat format = new MessageFormat(defaultValue);
102: format.setLocale(ActionContext.getContext().getLocale());
103: format.applyPattern(defaultValue);
104:
105: if (args == null) {
106: return format.format(EMPTY_ARGS);
107: }
108:
109: return format.format(args);
110: }
111: return text;
112: }
113:
114: public String getText(String key, String defaultValue, String obj) {
115: List args = new ArrayList(1);
116: args.add(obj);
117: return getText(key, defaultValue, args);
118: }
119:
120: public String getText(String key, String defaultValue, List args,
121: OgnlValueStack stack) {
122: //we're not using the value stack here
123: return getText(key, defaultValue, args);
124: }
125:
126: public String getText(String key, String defaultValue,
127: String[] args, OgnlValueStack stack) {
128: //we're not using the value stack here
129: return getText(key, defaultValue, Arrays.asList(args));
130: }
131:
132: public ResourceBundle getTexts(String bundleName) {
133: return LocalizedTextUtil.findResourceBundle(bundleName,
134: ActionContext.getContext().getLocale());
135: }
136:
137: public ResourceBundle getTexts() {
138: return null;
139: }
140:
141: private Object readResolve() throws ObjectStreamException {
142: return INSTANCE;
143: }
144: }
|