001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 /*
019 * $Id: FuncCurrent.java 468655 2006-10-28 07:12:06Z minchau $
020 */
021 package org.apache.xpath.functions;
022
023 import org.apache.xml.dtm.DTM;
024 import org.apache.xml.dtm.DTMIterator;
025 import org.apache.xpath.XPathContext;
026 import org.apache.xpath.axes.LocPathIterator;
027 import org.apache.xpath.axes.PredicatedNodeTest;
028 import org.apache.xpath.objects.XNodeSet;
029 import org.apache.xpath.objects.XObject;
030 import org.apache.xpath.axes.SubContextList;
031 import org.apache.xpath.patterns.StepPattern;
032 import org.apache.xalan.res.XSLMessages;
033 import org.apache.xalan.res.XSLTErrorResources;
034
035
036 /**
037 * Execute the current() function.
038 * @xsl.usage advanced
039 */
040 public class FuncCurrent extends Function
041 {
042 static final long serialVersionUID = 5715316804877715008L;
043
044 /**
045 * Execute the function. The function must return
046 * a valid object.
047 * @param xctxt The current execution context.
048 * @return A valid XObject.
049 *
050 * @throws javax.xml.transform.TransformerException
051 */
052 public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
053 {
054
055 SubContextList subContextList = xctxt.getCurrentNodeList();
056 int currentNode = DTM.NULL;
057
058 if (null != subContextList) {
059 if (subContextList instanceof PredicatedNodeTest) {
060 LocPathIterator iter = ((PredicatedNodeTest)subContextList)
061 .getLocPathIterator();
062 currentNode = iter.getCurrentContextNode();
063 } else if(subContextList instanceof StepPattern) {
064 throw new RuntimeException(XSLMessages.createMessage(
065 XSLTErrorResources.ER_PROCESSOR_ERROR,null));
066 }
067 } else {
068 // not predicate => ContextNode == CurrentNode
069 currentNode = xctxt.getContextNode();
070 }
071 return new XNodeSet(currentNode, xctxt.getDTMManager());
072 }
073
074 /**
075 * No arguments to process, so this does nothing.
076 */
077 public void fixupVariables(java.util.Vector vars, int globalsSize)
078 {
079 // no-op
080 }
081
082 }