01: /*
02: * xtc - The eXTensible Compiler
03: * Copyright (C) 2006-2007 New York University
04: *
05: * This program is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU General Public License
07: * version 2 as published by the Free Software Foundation.
08: *
09: * This program is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License
15: * along with this program; if not, write to the Free Software
16: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17: * USA.
18: */
19: package xtc.xform;
20:
21: import java.util.List;
22:
23: /**
24: * SubStringFunction external method class.
25: *
26: * @version $Revision: 1.8 $
27: */
28: class SubStringFunction implements Function {
29: /**
30: * Get the name of the function.
31: * @return the function name
32: */
33: public String getName() {
34: return "substring";
35: }
36:
37: /**
38: * Return the sub substring specified by args1 and 2
39: * @param args The target string and the start and end index of the
40: * substring
41: * @return The specified substring
42: *
43: */
44: public Object apply(List<Object> args) {
45: Engine.Sequence<?> arg = (Engine.Sequence) args.get(0);
46: Item item = (Item) arg.get(0);
47: String val = (String) item.object;
48:
49: Item resItem = null;
50: Integer s = (Integer) args.get(1);
51: int start = s.intValue();
52:
53: if (2 == args.size()) {
54: resItem = new Item(val.substring(start), null, 0);
55: } else if (3 == args.size()) {
56: Integer l = (Integer) args.get(2);
57: int len = l.intValue();
58: resItem = new Item(val.substring(start, start + len), null,
59: 0);
60: }
61: Engine.Sequence<Item> result = new Engine.Sequence<Item>();
62: result.add(resItem);
63: return result;
64: }
65: }
|