001: /*
002: * @(#)join.java 1.2 04/12/06
003: *
004: * Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.lib;
010:
011: import pnuts.lang.PnutsFunction;
012: import pnuts.lang.Context;
013: import pnuts.lang.Runtime;
014: import pnuts.lang.Generator;
015: import java.util.List;
016: import java.util.Collection;
017: import java.util.Enumeration;
018: import java.util.Iterator;
019: import java.lang.reflect.Array;
020:
021: public class join extends PnutsFunction {
022:
023: public join() {
024: super ("join");
025: }
026:
027: public boolean defined(int narg) {
028: return (narg == 2);
029: }
030:
031: static String concat(final String delim, Object target,
032: Context context) {
033: final StringBuffer sbuf = new StringBuffer();
034: Object elem;
035: if (target instanceof Object[]) {
036: Object[] array = (Object[]) target;
037: int len = array.length;
038: for (int i = 0; i < len - 1; i++) {
039: elem = array[i];
040: if (elem != null) {
041: sbuf.append(elem.toString());
042: }
043: sbuf.append(delim);
044: }
045: if (len > 0) {
046: elem = array[len - 1];
047: if (elem != null) {
048: sbuf.append(elem.toString());
049: }
050: }
051: } else if (target instanceof List) {
052: List lst = (List) target;
053: int len = lst.size();
054: for (int i = 0; i < len - 1; i++) {
055: elem = lst.get(i);
056: if (elem != null) {
057: sbuf.append(elem.toString());
058: }
059: sbuf.append(delim);
060: }
061: if (len > 0) {
062: elem = lst.get(len - 1);
063: if (elem != null) {
064: sbuf.append(elem.toString());
065: }
066: }
067: } else if (Runtime.isArray(target)) {
068: int len = Array.getLength(target);
069: for (int i = 0; i < len - 1; i++) {
070: elem = Array.get(target, i);
071: if (elem != null) {
072: sbuf.append(elem.toString());
073: }
074: sbuf.append(delim);
075: }
076: if (len > 0) {
077: elem = Array.get(target, len - 1);
078: if (elem != null) {
079: sbuf.append(elem.toString());
080: }
081: }
082: } else if (target instanceof Iterator) {
083: Iterator it = (Iterator) target;
084: if (it.hasNext()) {
085: elem = it.next();
086: if (elem != null) {
087: sbuf.append(elem.toString());
088: }
089: }
090: while (it.hasNext()) {
091: sbuf.append(delim);
092: elem = it.next();
093: if (elem != null) {
094: sbuf.append(elem.toString());
095: }
096: }
097: } else if (target instanceof Enumeration) {
098: Enumeration en = (Enumeration) target;
099: if (en.hasMoreElements()) {
100: elem = en.nextElement();
101: if (elem != null) {
102: sbuf.append(elem.toString());
103: }
104: }
105: while (en.hasMoreElements()) {
106: sbuf.append(delim);
107: elem = en.nextElement();
108: if (elem != null) {
109: sbuf.append(elem.toString());
110: }
111: }
112: } else if (target instanceof Collection) {
113: Iterator it = ((Collection) target).iterator();
114: if (it.hasNext()) {
115: elem = it.next();
116: if (elem != null) {
117: sbuf.append(elem.toString());
118: }
119: }
120: while (it.hasNext()) {
121: sbuf.append(delim);
122: elem = it.next();
123: if (elem != null) {
124: sbuf.append(elem.toString());
125: }
126: }
127: } else if (target instanceof Generator) {
128: class F extends PnutsFunction {
129: boolean first = true;
130:
131: protected Object exec(Object[] args, Context c) {
132: Object arg = args[0];
133: if (arg != null) {
134: if (first) {
135: first = false;
136: } else {
137: sbuf.append(delim);
138: }
139: sbuf.append(arg.toString());
140: }
141: return null;
142: }
143: }
144: Runtime
145: .applyGenerator((Generator) target, new F(),
146: context);
147: } else if (target == null) {
148: return "";
149: } else {
150: throw new IllegalArgumentException(String.valueOf(target));
151: }
152: return sbuf.toString();
153: }
154:
155: static void call(final Object delim, Object target,
156: final PnutsFunction func, final Context context) {
157: final Object[] arg = new Object[1];
158: if (target instanceof Object[]) {
159: Object[] array = (Object[]) target;
160: int len = array.length;
161: for (int i = 0; i < len - 1; i++) {
162: arg[0] = array[i];
163: func.call(arg, context);
164: arg[0] = delim;
165: func.call(arg, context);
166: }
167: if (len > 0) {
168: arg[0] = array[len - 1];
169: func.call(arg, context);
170: }
171: } else if (target instanceof List) {
172: List lst = (List) target;
173: int len = lst.size();
174: for (int i = 0; i < len - 1; i++) {
175: arg[0] = lst.get(i);
176: func.call(arg, context);
177: arg[0] = delim;
178: func.call(arg, context);
179: }
180: if (len > 0) {
181: arg[0] = lst.get(len - 1);
182: func.call(arg, context);
183: }
184: } else if (Runtime.isArray(target)) {
185: int len = Array.getLength(target);
186: for (int i = 0; i < len - 1; i++) {
187: arg[0] = Array.get(target, i);
188: func.call(arg, context);
189: arg[0] = delim;
190: func.call(arg, context);
191: }
192: if (len > 0) {
193: arg[0] = Array.get(target, len - 1);
194: func.call(arg, context);
195: }
196: } else if (target instanceof Iterator) {
197: Iterator it = (Iterator) target;
198: if (it.hasNext()) {
199: arg[0] = it.next();
200: func.call(arg, context);
201: }
202: while (it.hasNext()) {
203: arg[0] = delim;
204: func.call(arg, context);
205: arg[0] = it.next();
206: func.call(arg, context);
207: }
208: } else if (target instanceof Enumeration) {
209: Enumeration en = (Enumeration) target;
210: if (en.hasMoreElements()) {
211: arg[0] = en.nextElement();
212: func.call(arg, context);
213: }
214: while (en.hasMoreElements()) {
215: arg[0] = delim;
216: func.call(arg, context);
217: arg[0] = en.nextElement();
218: func.call(arg, context);
219: }
220: } else if (target instanceof Collection) {
221: Iterator it = ((Collection) target).iterator();
222: if (it.hasNext()) {
223: arg[0] = it.next();
224: func.call(arg, context);
225: }
226: while (it.hasNext()) {
227: arg[0] = delim;
228: func.call(arg, context);
229: arg[0] = it.next();
230: func.call(arg, context);
231: }
232: } else if (target instanceof Generator) {
233: class F extends PnutsFunction {
234: boolean first = true;
235:
236: protected Object exec(Object[] args, Context c) {
237: Object a0 = args[0];
238: if (first) {
239: first = false;
240: } else {
241: arg[0] = delim;
242: func.call(arg, context);
243: }
244: arg[0] = a0;
245: func.call(arg, context);
246: return null;
247: }
248: }
249: ((Generator) target).apply(new F(), context);
250: } else if (target == null) {
251: // skip
252: } else {
253: throw new IllegalArgumentException();
254: }
255: }
256:
257: protected Object exec(Object args[], Context context) {
258: if (args.length == 2) {
259: Object delim = args[0];
260: Object target = args[1];
261: return concat(delim.toString(), target, context);
262: } else if (args.length == 3) {
263: Object delim = args[0];
264: Object target = args[1];
265: PnutsFunction callback = (PnutsFunction) args[2];
266: call(delim, target, callback, context);
267: return null;
268: } else {
269: undefined(args, context);
270: return null;
271: }
272: }
273:
274: public String toString() {
275: return "function join(delimiter, objects {, func(elem) } )";
276: }
277: }
|