ConcatFunction.java 3.25 KB
Newer Older
1
/* ConcatFunction.java --
Tom Tromey committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
   Copyright (C) 2004 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package gnu.xml.xpath;

40 41
import gnu.java.lang.CPStringBuilder;

Tom Tromey committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.namespace.QName;
import org.w3c.dom.Node;

/**
 * The <code>concat</code> function returns the concatenation of its arguments.
 *
 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
 */
final class ConcatFunction
  extends Expr
{

57
  final List<Expr> args;
Tom Tromey committed
58

59
  ConcatFunction(List<Expr> args)
Tom Tromey committed
60 61 62 63
  {
    this.args = args;
  }

64
  @Override
Tom Tromey committed
65 66
  public Object evaluate(Node context, int pos, int len)
  {
67 68
    CPStringBuilder buf = new CPStringBuilder();
    for (Expr arg : args)
Tom Tromey committed
69 70 71 72 73 74 75 76 77 78
      {
        Object val = arg.evaluate(context, pos, len);
        buf.append(_string(context, val));
      }
    return buf.toString();
  }

  public Expr clone(Object context)
  {
    int len = args.size();
79
    List<Expr> args2 = new ArrayList<Expr>(len);
Tom Tromey committed
80 81
    for (int i = 0; i < len; i++)
      {
82
        args2.add(args.get(i).clone(context));
Tom Tromey committed
83 84 85 86 87 88
      }
    return new ConcatFunction(args2);
  }

  public boolean references(QName var)
  {
89
    for (Iterator<Expr> i = args.iterator(); i.hasNext(); )
Tom Tromey committed
90
      {
91
        if (i.next().references(var))
Tom Tromey committed
92 93 94 95 96 97 98 99 100
          {
            return true;
          }
      }
    return false;
  }

  public String toString()
  {
101
    CPStringBuilder buf = new CPStringBuilder("concat(");
Tom Tromey committed
102 103 104 105 106 107 108 109 110 111 112 113
    int len = args.size();
    for (int i = 0; i < len; i++)
      {
        if (i > 0)
          {
            buf.append(',');
          }
        buf.append(args.get(i));
      }
    buf.append(')');
    return buf.toString();
  }
114

Tom Tromey committed
115
}