01: /*
02: * @(#)setFormatLocale.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.lib;
10:
11: import pnuts.lang.PnutsFunction;
12: import pnuts.lang.Context;
13: import java.util.Locale;
14: import java.util.StringTokenizer;
15:
16: public class setFormatLocale extends PnutsFunction {
17:
18: final static String LOCALE = "pnuts$lib$locale".intern();
19:
20: public setFormatLocale() {
21: super ("setFormatLocale");
22: }
23:
24: public boolean defined(int nargs) {
25: return nargs == 1;
26: }
27:
28: static Locale getLocale(Object loc) {
29: if (loc instanceof Locale) {
30: return (Locale) loc;
31: } else if (loc instanceof String) {
32: StringTokenizer st = new StringTokenizer((String) loc, "_");
33: int n = st.countTokens();
34: if (n == 1) {
35: return new Locale(st.nextToken());
36: } else if (n == 2) {
37: return new Locale(st.nextToken(), st.nextToken());
38: } else if (n > 2) {
39: return new Locale(st.nextToken(), st.nextToken(), st
40: .nextToken());
41: }
42: }
43: throw new IllegalArgumentException(String.valueOf(loc));
44: }
45:
46: protected Object exec(Object[] args, Context context) {
47: if (args.length == 1) {
48: context.set(LOCALE, getLocale(args[0]));
49: } else {
50: undefined(args, context);
51: }
52: return null;
53: }
54:
55: public String toString() {
56: return "function setFormatLocale(String|Locale)";
57: }
58: }
|