1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the  "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18/*
19 * $Id: FuncCurrent.java 468655 2006-10-28 07:12:06Z minchau $
20 */
21package org.apache.xpath.functions;
22
23import org.apache.xml.dtm.DTM;
24import org.apache.xml.dtm.DTMIterator;
25import org.apache.xpath.XPathContext;
26import org.apache.xpath.axes.LocPathIterator;
27import org.apache.xpath.axes.PredicatedNodeTest;
28import org.apache.xpath.objects.XNodeSet;
29import org.apache.xpath.objects.XObject;
30import org.apache.xpath.axes.SubContextList;
31import org.apache.xpath.patterns.StepPattern;
32import org.apache.xalan.res.XSLMessages;
33import org.apache.xalan.res.XSLTErrorResources;
34
35
36/**
37 * Execute the current() function.
38 * @xsl.usage advanced
39 */
40public class FuncCurrent extends Function
41{
42    static final long serialVersionUID = 5715316804877715008L;
43
44  /**
45   * Execute the function.  The function must return
46   * a valid object.
47   * @param xctxt The current execution context.
48   * @return A valid XObject.
49   *
50   * @throws javax.xml.transform.TransformerException
51   */
52  public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
53  {
54
55    SubContextList subContextList = xctxt.getCurrentNodeList();
56    int currentNode = DTM.NULL;
57
58    if (null != subContextList) {
59        if (subContextList instanceof PredicatedNodeTest) {
60            LocPathIterator iter = ((PredicatedNodeTest)subContextList)
61                                                          .getLocPathIterator();
62            currentNode = iter.getCurrentContextNode();
63         } else if(subContextList instanceof StepPattern) {
64           throw new RuntimeException(XSLMessages.createMessage(
65              XSLTErrorResources.ER_PROCESSOR_ERROR,null));
66         }
67    } else {
68        // not predicate => ContextNode == CurrentNode
69        currentNode = xctxt.getContextNode();
70    }
71    return new XNodeSet(currentNode, xctxt.getDTMManager());
72  }
73
74  /**
75   * No arguments to process, so this does nothing.
76   */
77  public void fixupVariables(java.util.Vector vars, int globalsSize)
78  {
79    // no-op
80  }
81
82}
83