01: package net.sf.saxon.functions;
02:
03: import net.sf.saxon.expr.*;
04: import net.sf.saxon.om.Item;
05: import net.sf.saxon.om.SequenceIterator;
06: import net.sf.saxon.om.FastStringBuffer;
07: import net.sf.saxon.trans.XPathException;
08: import net.sf.saxon.type.ItemType;
09: import net.sf.saxon.value.Cardinality;
10: import net.sf.saxon.value.StringValue;
11:
12: /**
13: * xf:string-join(string* $sequence, string $separator)
14: */
15:
16: public class StringJoin extends SystemFunction {
17:
18: public Expression optimize(Optimizer opt, StaticContext env,
19: ItemType contextItemType) throws XPathException {
20: Expression exp = super .optimize(opt, env, contextItemType);
21: if (exp instanceof StringJoin) {
22: return ((StringJoin) exp).simplifySingleton(env);
23: } else {
24: return exp;
25: }
26: }
27:
28: private Expression simplifySingleton(StaticContext env) {
29: int card = argument[0].getCardinality();
30: if (!Cardinality.allowsMany(card)) {
31: if (Cardinality.allowsZero(card)) {
32: FunctionCall f = SystemFunction.makeSystemFunction(
33: "string", 1, env.getNamePool());
34: Expression[] args = { argument[0] };
35: f.setArguments(args);
36: return f;
37: } else {
38: return argument[0];
39: }
40: }
41: return this ;
42: }
43:
44: public Item evaluateItem(XPathContext c) throws XPathException {
45:
46: // This rather tortuous code is designed to ensure that we don't evaluate the
47: // separator argument unless there are at least two items in the sequence.
48:
49: SequenceIterator iter = argument[0].iterate(c);
50: Item it = iter.next();
51: if (it == null) {
52: return StringValue.EMPTY_STRING;
53: }
54:
55: String first = it.getStringValue();
56:
57: it = iter.next();
58: if (it == null) {
59: return StringValue.makeStringValue(first);
60: }
61:
62: FastStringBuffer sb = new FastStringBuffer(1024);
63: sb.append(first);
64:
65: // Type checking ensures that the separator is not an empty sequence
66: CharSequence sep = argument[1].evaluateItem(c)
67: .getStringValueCS();
68: sb.append(sep);
69: sb.append(it.getStringValueCS());
70:
71: while (true) {
72: it = iter.next();
73: if (it == null) {
74: return StringValue.makeStringValue(sb.condense());
75: }
76: sb.append(sep);
77: sb.append(it.getStringValueCS());
78: }
79: }
80:
81: }
82:
83: //
84: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
85: // you may not use this file except in compliance with the License. You may obtain a copy of the
86: // License at http://www.mozilla.org/MPL/
87: //
88: // Software distributed under the License is distributed on an "AS IS" basis,
89: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
90: // See the License for the specific language governing rights and limitations under the License.
91: //
92: // The Original Code is: all this file.
93: //
94: // The Initial Developer of the Original Code is Michael H. Kay
95: //
96: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
97: //
98: // Contributor(s): none.
99: //
|