KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > StringJoin


1 package net.sf.saxon.functions;
2 import net.sf.saxon.expr.*;
3 import net.sf.saxon.om.Item;
4 import net.sf.saxon.om.SequenceIterator;
5 import net.sf.saxon.om.FastStringBuffer;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.type.ItemType;
8 import net.sf.saxon.value.Cardinality;
9 import net.sf.saxon.value.StringValue;
10
11 /**
12 * xf:string-join(string* $sequence, string $separator)
13 */

14
15 public class StringJoin extends SystemFunction {
16
17     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
18         Expression exp = super.optimize(opt, env, contextItemType);
19         if (exp instanceof StringJoin) {
20             return ((StringJoin)exp).simplifySingleton(env);
21         } else {
22             return exp;
23         }
24     }
25
26     private Expression simplifySingleton(StaticContext env) {
27         int card = argument[0].getCardinality();
28         if (!Cardinality.allowsMany(card)) {
29             if (Cardinality.allowsZero(card)) {
30                 FunctionCall f = SystemFunction.makeSystemFunction("string", 1, env.getNamePool());
31                 Expression[] args = {argument[0]};
32                 f.setArguments(args);
33                 return f;
34             } else {
35                 return argument[0];
36             }
37         }
38         return this;
39     }
40
41     public Item evaluateItem(XPathContext c) throws XPathException {
42
43         // This rather tortuous code is designed to ensure that we don't evaluate the
44
// separator argument unless there are at least two items in the sequence.
45

46         SequenceIterator iter = argument[0].iterate(c);
47         Item it = iter.next();
48         if (it==null) {
49             return StringValue.EMPTY_STRING;
50         }
51
52         String JavaDoc first = it.getStringValue();
53
54         it = iter.next();
55         if (it==null) {
56             return StringValue.makeStringValue(first);
57         }
58
59         FastStringBuffer sb = new FastStringBuffer(1024);
60         sb.append(first);
61
62         // Type checking ensures that the separator is not an empty sequence
63
CharSequence JavaDoc sep = argument[1].evaluateItem(c).getStringValueCS();
64         sb.append(sep);
65         sb.append(it.getStringValueCS());
66
67         while (true) {
68             it = iter.next();
69             if (it == null) {
70                 return StringValue.makeStringValue(sb.condense());
71             }
72             sb.append(sep);
73             sb.append(it.getStringValueCS());
74         }
75     }
76
77 }
78
79
80
81 //
82
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
83
// you may not use this file except in compliance with the License. You may obtain a copy of the
84
// License at http://www.mozilla.org/MPL/
85
//
86
// Software distributed under the License is distributed on an "AS IS" basis,
87
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
88
// See the License for the specific language governing rights and limitations under the License.
89
//
90
// The Original Code is: all this file.
91
//
92
// The Initial Developer of the Original Code is Michael H. Kay
93
//
94
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
95
//
96
// Contributor(s): none.
97
//
98
Popular Tags