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