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: ExsltCommon.java 468639 2006-10-28 06:52:33Z minchau $
020     */
021    package org.apache.xalan.lib;
022    
023    import org.apache.xalan.extensions.ExpressionContext;
024    import org.apache.xml.dtm.DTMIterator;
025    import org.apache.xml.dtm.ref.DTMNodeIterator;
026    import org.apache.xpath.NodeSet;
027    
028    /**
029     * This class contains EXSLT common extension functions.
030     * It is accessed by specifying a namespace URI as follows:
031     * <pre>
032     *    xmlns:exslt="http://exslt.org/common"
033     * </pre>
034     * 
035     * The documentation for each function has been copied from the relevant
036     * EXSLT Implementer page.
037     * 
038     * @see <a href="http://www.exslt.org/">EXSLT</a>
039     * @xsl.usage general
040     */
041    public class ExsltCommon
042    {
043      /**
044       * The exsl:object-type function returns a string giving the type of the object passed 
045       * as the argument. The possible object types are: 'string', 'number', 'boolean', 
046       * 'node-set', 'RTF', or 'external'. 
047       * 
048       * Most XSLT object types can be coerced to each other without error. However, there are 
049       * certain coercions that raise errors, most importantly treating anything other than a 
050       * node set as a node set. Authors of utilities such as named templates or user-defined 
051       * extension functions may wish to give some flexibility in the parameter and argument values 
052       * that are accepted by the utility; the exsl:object-type function enables them to do so.
053       * 
054       * The Xalan extensions MethodResolver converts 'object-type' to 'objectType'.
055       * 
056       * @param obj The object to be typed.
057       * @return objectType 'string', 'number', 'boolean', 'node-set', 'RTF', or 'external'.
058       * 
059       * @see <a href="http://www.exslt.org/">EXSLT</a>
060       */
061      public static String objectType (Object obj)
062      {
063        if (obj instanceof String)
064          return "string";
065        else if (obj instanceof Boolean)
066          return "boolean";
067        else if (obj instanceof Number)
068          return "number";
069        else if (obj instanceof DTMNodeIterator)
070        {
071          DTMIterator dtmI = ((DTMNodeIterator)obj).getDTMIterator();
072          if (dtmI instanceof org.apache.xpath.axes.RTFIterator)
073            return "RTF";
074          else
075            return "node-set";
076        }
077        else
078          return "unknown";
079      }
080        
081      /**
082       * The exsl:node-set function converts a result tree fragment (which is what you get 
083       * when you use the content of xsl:variable rather than its select attribute to give 
084       * a variable value) into a node set. This enables you to process the XML that you create 
085       * within a variable, and therefore do multi-step processing. 
086       * 
087       * You can also use this function to turn a string into a text node, which is helpful 
088       * if you want to pass a string to a function that only accepts a node set.
089       * 
090       * The Xalan extensions MethodResolver converts 'node-set' to 'nodeSet'.
091       * 
092       * @param myProcessor is passed in by the Xalan extension processor
093       * @param rtf The result tree fragment to be converted to a node-set.
094       * 
095       * @return node-set with the contents of the result tree fragment.
096       * 
097       * Note: Already implemented in the xalan namespace as nodeset.
098       * 
099       * @see <a href="http://www.exslt.org/">EXSLT</a>
100       */
101      public static NodeSet nodeSet(ExpressionContext myProcessor, Object rtf)
102      {
103        return Extensions.nodeset(myProcessor, rtf);
104      }
105     
106    }