Home | History | Annotate | Download | only in objects
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one
      3  * or more contributor license agreements. See the NOTICE file
      4  * distributed with this work for additional information
      5  * regarding copyright ownership. The ASF licenses this file
      6  * to you under the Apache License, Version 2.0 (the  "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *     http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  */
     18 /*
     19  * $Id: XObjectFactory.java 468655 2006-10-28 07:12:06Z minchau $
     20  */
     21 package org.apache.xpath.objects;
     22 
     23 import org.apache.xml.dtm.Axis;
     24 import org.apache.xml.dtm.DTM;
     25 import org.apache.xml.dtm.DTMAxisIterator;
     26 import org.apache.xml.dtm.DTMIterator;
     27 import org.apache.xpath.XPathContext;
     28 import org.apache.xpath.axes.OneStepIterator;
     29 
     30 
     31 public class XObjectFactory
     32 {
     33 
     34   /**
     35    * Create the right XObject based on the type of the object passed.  This
     36    * function can not make an XObject that exposes DOM Nodes, NodeLists, and
     37    * NodeIterators to the XSLT stylesheet as node-sets.
     38    *
     39    * @param val The java object which this object will wrap.
     40    *
     41    * @return the right XObject based on the type of the object passed.
     42    */
     43   static public XObject create(Object val)
     44   {
     45 
     46     XObject result;
     47 
     48     if (val instanceof XObject)
     49     {
     50       result = (XObject) val;
     51     }
     52     else if (val instanceof String)
     53     {
     54       result = new XString((String) val);
     55     }
     56     else if (val instanceof Boolean)
     57     {
     58       result = new XBoolean((Boolean)val);
     59     }
     60     else if (val instanceof Double)
     61     {
     62       result = new XNumber(((Double) val));
     63     }
     64     else
     65     {
     66       result = new XObject(val);
     67     }
     68 
     69     return result;
     70   }
     71 
     72   /**
     73    * Create the right XObject based on the type of the object passed.
     74    * This function <emph>can</emph> make an XObject that exposes DOM Nodes, NodeLists, and
     75    * NodeIterators to the XSLT stylesheet as node-sets.
     76    *
     77    * @param val The java object which this object will wrap.
     78    * @param xctxt The XPath context.
     79    *
     80    * @return the right XObject based on the type of the object passed.
     81    */
     82   static public XObject create(Object val, XPathContext xctxt)
     83   {
     84 
     85     XObject result;
     86 
     87     if (val instanceof XObject)
     88     {
     89       result = (XObject) val;
     90     }
     91     else if (val instanceof String)
     92     {
     93       result = new XString((String) val);
     94     }
     95     else if (val instanceof Boolean)
     96     {
     97       result = new XBoolean((Boolean)val);
     98     }
     99     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 }
    162