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: XObjectFactory.java 468655 2006-10-28 07:12:06Z minchau $
020     */
021    package org.apache.xpath.objects;
022    
023    import org.apache.xml.dtm.Axis;
024    import org.apache.xml.dtm.DTM;
025    import org.apache.xml.dtm.DTMAxisIterator;
026    import org.apache.xml.dtm.DTMIterator;
027    import org.apache.xpath.XPathContext;
028    import org.apache.xpath.axes.OneStepIterator;
029    
030    
031    public class XObjectFactory
032    {
033      
034      /**
035       * Create the right XObject based on the type of the object passed.  This 
036       * function can not make an XObject that exposes DOM Nodes, NodeLists, and 
037       * NodeIterators to the XSLT stylesheet as node-sets.
038       *
039       * @param val The java object which this object will wrap.
040       *
041       * @return the right XObject based on the type of the object passed.
042       */
043      static public XObject create(Object val)
044      {
045    
046        XObject result;
047    
048        if (val instanceof XObject)
049        {
050          result = (XObject) val;
051        }
052        else if (val instanceof String)
053        {
054          result = new XString((String) val);
055        }
056        else if (val instanceof Boolean)
057        {
058          result = new XBoolean((Boolean)val);
059        }
060        else if (val instanceof Double)
061        {
062          result = new XNumber(((Double) val));
063        }
064        else
065        {
066          result = new XObject(val);
067        }
068    
069        return result;
070      }
071      
072      /**
073       * Create the right XObject based on the type of the object passed.
074       * This function <emph>can</emph> make an XObject that exposes DOM Nodes, NodeLists, and 
075       * NodeIterators to the XSLT stylesheet as node-sets.
076       *
077       * @param val The java object which this object will wrap.
078       * @param xctxt The XPath context.
079       *
080       * @return the right XObject based on the type of the object passed.
081       */
082      static public XObject create(Object val, XPathContext xctxt)
083      {
084    
085        XObject result;
086    
087        if (val instanceof XObject)
088        {
089          result = (XObject) val;
090        }
091        else if (val instanceof String)
092        {
093          result = new XString((String) val);
094        }
095        else if (val instanceof Boolean)
096        {
097          result = new XBoolean((Boolean)val);
098        }
099        else if (val instanceof Number)
100        {
101          result = new XNumber(((Number) val));
102        }
103        else if (val instanceof DTM)
104        {
105          DTM dtm = (DTM)val;
106          try
107          {
108            int dtmRoot = dtm.getDocument();
109            DTMAxisIterator iter = dtm.getAxisIterator(Axis.SELF);
110            iter.setStartNode(dtmRoot);
111            DTMIterator iterator = new OneStepIterator(iter, Axis.SELF);
112            iterator.setRoot(dtmRoot, xctxt);
113            result = new XNodeSet(iterator);
114          }
115          catch(Exception ex)
116          {
117            throw new org.apache.xml.utils.WrappedRuntimeException(ex);
118          }
119        }
120        else if (val instanceof DTMAxisIterator)
121        {
122          DTMAxisIterator iter = (DTMAxisIterator)val;
123          try
124          {
125            DTMIterator iterator = new OneStepIterator(iter, Axis.SELF);
126            iterator.setRoot(iter.getStartNode(), xctxt);
127            result = new XNodeSet(iterator);
128          }
129          catch(Exception ex)
130          {
131            throw new org.apache.xml.utils.WrappedRuntimeException(ex);
132          }
133        }
134        else if (val instanceof DTMIterator)
135        {
136          result = new XNodeSet((DTMIterator) val);
137        }
138        // This next three instanceofs are a little worrysome, since a NodeList 
139        // might also implement a Node!
140        else if (val instanceof org.w3c.dom.Node)
141        {
142          result = new XNodeSetForDOM((org.w3c.dom.Node)val, xctxt);
143        }
144        // This must come after org.w3c.dom.Node, since many Node implementations 
145        // also implement NodeList.
146        else if (val instanceof org.w3c.dom.NodeList)
147        {
148          result = new XNodeSetForDOM((org.w3c.dom.NodeList)val, xctxt);
149        }
150        else if (val instanceof org.w3c.dom.traversal.NodeIterator)
151        {
152          result = new XNodeSetForDOM((org.w3c.dom.traversal.NodeIterator)val, xctxt);
153        }
154        else
155        {
156          result = new XObject(val);
157        }
158    
159        return result;
160      }
161    }