286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: ExsltSets.java,v 1.1.2.1 2005/08/01 02:08:50 jeffsuttor Exp $
286N/A */
286N/Apackage com.sun.org.apache.xalan.internal.lib;
286N/A
286N/Aimport java.util.Hashtable;
286N/A
286N/Aimport com.sun.org.apache.xml.internal.utils.DOMHelper;
286N/Aimport com.sun.org.apache.xpath.internal.NodeSet;
286N/Aimport org.w3c.dom.Node;
286N/Aimport org.w3c.dom.NodeList;
286N/A
286N/A/**
286N/A * This class contains EXSLT set extension functions.
286N/A * It is accessed by specifying a namespace URI as follows:
286N/A * <pre>
286N/A * xmlns:set="http://exslt.org/sets"
286N/A * </pre>
286N/A *
286N/A * The documentation for each function has been copied from the relevant
286N/A * EXSLT Implementer page.
286N/A *
286N/A * @see <a href="http://www.exslt.org/">EXSLT</a>
286N/A * @xsl.usage general
286N/A */
286N/Apublic class ExsltSets extends ExsltBase
286N/A{
286N/A /**
286N/A * The set:leading function returns the nodes in the node set passed as the first argument that
286N/A * precede, in document order, the first node in the node set passed as the second argument. If
286N/A * the first node in the second node set is not contained in the first node set, then an empty
286N/A * node set is returned. If the second node set is empty, then the first node set is returned.
286N/A *
286N/A * @param nl1 NodeList for first node-set.
286N/A * @param nl2 NodeList for second node-set.
286N/A * @return a NodeList containing the nodes in nl1 that precede in document order the first
286N/A * node in nl2; an empty node-set if the first node in nl2 is not in nl1; all of nl1 if nl2
286N/A * is empty.
286N/A *
286N/A * @see <a href="http://www.exslt.org/">EXSLT</a>
286N/A */
286N/A public static NodeList leading (NodeList nl1, NodeList nl2)
286N/A {
286N/A if (nl2.getLength() == 0)
286N/A return nl1;
286N/A
286N/A NodeSet ns1 = new NodeSet(nl1);
286N/A NodeSet leadNodes = new NodeSet();
286N/A Node endNode = nl2.item(0);
286N/A if (!ns1.contains(endNode))
286N/A return leadNodes; // empty NodeSet
286N/A
286N/A for (int i = 0; i < nl1.getLength(); i++)
286N/A {
286N/A Node testNode = nl1.item(i);
286N/A if (DOMHelper.isNodeAfter(testNode, endNode)
286N/A && !DOMHelper.isNodeTheSame(testNode, endNode))
286N/A leadNodes.addElement(testNode);
286N/A }
286N/A return leadNodes;
286N/A }
286N/A
286N/A /**
286N/A * The set:trailing function returns the nodes in the node set passed as the first argument that
286N/A * follow, in document order, the first node in the node set passed as the second argument. If
286N/A * the first node in the second node set is not contained in the first node set, then an empty
286N/A * node set is returned. If the second node set is empty, then the first node set is returned.
286N/A *
286N/A * @param nl1 NodeList for first node-set.
286N/A * @param nl2 NodeList for second node-set.
286N/A * @return a NodeList containing the nodes in nl1 that follow in document order the first
286N/A * node in nl2; an empty node-set if the first node in nl2 is not in nl1; all of nl1 if nl2
286N/A * is empty.
286N/A *
286N/A * @see <a href="http://www.exslt.org/">EXSLT</a>
286N/A */
286N/A public static NodeList trailing (NodeList nl1, NodeList nl2)
286N/A {
286N/A if (nl2.getLength() == 0)
286N/A return nl1;
286N/A
286N/A NodeSet ns1 = new NodeSet(nl1);
286N/A NodeSet trailNodes = new NodeSet();
286N/A Node startNode = nl2.item(0);
286N/A if (!ns1.contains(startNode))
286N/A return trailNodes; // empty NodeSet
286N/A
286N/A for (int i = 0; i < nl1.getLength(); i++)
286N/A {
286N/A Node testNode = nl1.item(i);
286N/A if (DOMHelper.isNodeAfter(startNode, testNode)
286N/A && !DOMHelper.isNodeTheSame(startNode, testNode))
286N/A trailNodes.addElement(testNode);
286N/A }
286N/A return trailNodes;
286N/A }
286N/A
286N/A /**
286N/A * The set:intersection function returns a node set comprising the nodes that are within
286N/A * both the node sets passed as arguments to it.
286N/A *
286N/A * @param nl1 NodeList for first node-set.
286N/A * @param nl2 NodeList for second node-set.
286N/A * @return a NodeList containing the nodes in nl1 that are also
286N/A * in nl2.
286N/A *
286N/A * @see <a href="http://www.exslt.org/">EXSLT</a>
286N/A */
286N/A public static NodeList intersection(NodeList nl1, NodeList nl2)
286N/A {
286N/A NodeSet ns1 = new NodeSet(nl1);
286N/A NodeSet ns2 = new NodeSet(nl2);
286N/A NodeSet inter = new NodeSet();
286N/A
286N/A inter.setShouldCacheNodes(true);
286N/A
286N/A for (int i = 0; i < ns1.getLength(); i++)
286N/A {
286N/A Node n = ns1.elementAt(i);
286N/A
286N/A if (ns2.contains(n))
286N/A inter.addElement(n);
286N/A }
286N/A
286N/A return inter;
286N/A }
286N/A
286N/A /**
286N/A * The set:difference function returns the difference between two node sets - those nodes that
286N/A * are in the node set passed as the first argument that are not in the node set passed as the
286N/A * second argument.
286N/A *
286N/A * @param nl1 NodeList for first node-set.
286N/A * @param nl2 NodeList for second node-set.
286N/A * @return a NodeList containing the nodes in nl1 that are not in nl2.
286N/A *
286N/A * @see <a href="http://www.exslt.org/">EXSLT</a>
286N/A */
286N/A public static NodeList difference(NodeList nl1, NodeList nl2)
286N/A {
286N/A NodeSet ns1 = new NodeSet(nl1);
286N/A NodeSet ns2 = new NodeSet(nl2);
286N/A
286N/A NodeSet diff = new NodeSet();
286N/A
286N/A diff.setShouldCacheNodes(true);
286N/A
286N/A for (int i = 0; i < ns1.getLength(); i++)
286N/A {
286N/A Node n = ns1.elementAt(i);
286N/A
286N/A if (!ns2.contains(n))
286N/A diff.addElement(n);
286N/A }
286N/A
286N/A return diff;
286N/A }
286N/A
286N/A /**
286N/A * The set:distinct function returns a subset of the nodes contained in the node-set NS passed
286N/A * as the first argument. Specifically, it selects a node N if there is no node in NS that has
286N/A * the same string value as N, and that precedes N in document order.
286N/A *
286N/A * @param nl NodeList for the node-set.
286N/A * @return a NodeList with nodes from nl containing distinct string values.
286N/A * In other words, if more than one node in nl contains the same string value,
286N/A * only include the first such node found.
286N/A *
286N/A * @see <a href="http://www.exslt.org/">EXSLT</a>
286N/A */
286N/A public static NodeList distinct(NodeList nl)
286N/A {
286N/A NodeSet dist = new NodeSet();
286N/A dist.setShouldCacheNodes(true);
286N/A
286N/A Hashtable stringTable = new Hashtable();
286N/A
286N/A for (int i = 0; i < nl.getLength(); i++)
286N/A {
286N/A Node currNode = nl.item(i);
286N/A String key = toString(currNode);
286N/A
286N/A if (key == null)
286N/A dist.addElement(currNode);
286N/A else if (!stringTable.containsKey(key))
286N/A {
286N/A stringTable.put(key, currNode);
286N/A dist.addElement(currNode);
286N/A }
286N/A }
286N/A
286N/A return dist;
286N/A }
286N/A
286N/A /**
286N/A * The set:has-same-node function returns true if the node set passed as the first argument shares
286N/A * any nodes with the node set passed as the second argument. If there are no nodes that are in both
286N/A * node sets, then it returns false.
286N/A *
286N/A * The Xalan extensions MethodResolver converts 'has-same-node' to 'hasSameNode'.
286N/A *
286N/A * Note: Not to be confused with hasSameNodes in the Xalan namespace, which returns true if
286N/A * the two node sets contain the exactly the same nodes (perhaps in a different order),
286N/A * otherwise false.
286N/A *
286N/A * @see <a href="http://www.exslt.org/">EXSLT</a>
286N/A */
286N/A public static boolean hasSameNode(NodeList nl1, NodeList nl2)
286N/A {
286N/A
286N/A NodeSet ns1 = new NodeSet(nl1);
286N/A NodeSet ns2 = new NodeSet(nl2);
286N/A
286N/A for (int i = 0; i < ns1.getLength(); i++)
286N/A {
286N/A if (ns2.contains(ns1.elementAt(i)))
286N/A return true;
286N/A }
286N/A return false;
286N/A }
286N/A
286N/A}