KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xpath > internal > objects > XObjectFactory


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: XObjectFactory.java,v 1.5 2004/02/17 04:34:38 minchau Exp $
18  */

19 package com.sun.org.apache.xpath.internal.objects;
20
21 import com.sun.org.apache.xml.internal.dtm.Axis;
22 import com.sun.org.apache.xml.internal.dtm.DTM;
23 import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
24 import com.sun.org.apache.xml.internal.dtm.DTMIterator;
25 import com.sun.org.apache.xpath.internal.XPathContext;
26 import com.sun.org.apache.xpath.internal.axes.OneStepIterator;
27
28
29 public class XObjectFactory
30 {
31   
32   /**
33    * Create the right XObject based on the type of the object passed. This
34    * function can not make an XObject that exposes DOM Nodes, NodeLists, and
35    * NodeIterators to the XSLT stylesheet as node-sets.
36    *
37    * @param val The java object which this object will wrap.
38    *
39    * @return the right XObject based on the type of the object passed.
40    */

41   static public XObject create(Object JavaDoc val)
42   {
43
44     XObject result;
45
46     if (val instanceof XObject)
47     {
48       result = (XObject) val;
49     }
50     else if (val instanceof String JavaDoc)
51     {
52       result = new XString((String JavaDoc) val);
53     }
54     else if (val instanceof Boolean JavaDoc)
55     {
56       result = new XBoolean((Boolean JavaDoc)val);
57     }
58     else if (val instanceof Double JavaDoc)
59     {
60       result = new XNumber(((Double JavaDoc) val));
61     }
62     else
63     {
64       result = new XObject(val);
65     }
66
67     return result;
68   }
69   
70   /**
71    * Create the right XObject based on the type of the object passed.
72    * This function <emph>can</emph> make an XObject that exposes DOM Nodes, NodeLists, and
73    * NodeIterators to the XSLT stylesheet as node-sets.
74    *
75    * @param val The java object which this object will wrap.
76    * @param xctxt The XPath context.
77    *
78    * @return the right XObject based on the type of the object passed.
79    */

80   static public XObject create(Object JavaDoc val, XPathContext xctxt)
81   {
82
83     XObject result;
84
85     if (val instanceof XObject)
86     {
87       result = (XObject) val;
88     }
89     else if (val instanceof String JavaDoc)
90     {
91       result = new XString((String JavaDoc) val);
92     }
93     else if (val instanceof Boolean JavaDoc)
94     {
95       result = new XBoolean((Boolean JavaDoc)val);
96     }
97     else if (val instanceof Number JavaDoc)
98     {
99       result = new XNumber(((Number JavaDoc) val));
100     }
101     else if (val instanceof DTM)
102     {
103       DTM dtm = (DTM)val;
104       try
105       {
106         int dtmRoot = dtm.getDocument();
107         DTMAxisIterator iter = dtm.getAxisIterator(Axis.SELF);
108         iter.setStartNode(dtmRoot);
109         DTMIterator iterator = new OneStepIterator(iter, Axis.SELF);
110         iterator.setRoot(dtmRoot, xctxt);
111         result = new XNodeSet(iterator);
112       }
113       catch(Exception JavaDoc ex)
114       {
115         throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(ex);
116       }
117     }
118     else if (val instanceof DTMAxisIterator)
119     {
120       DTMAxisIterator iter = (DTMAxisIterator)val;
121       try
122       {
123         DTMIterator iterator = new OneStepIterator(iter, Axis.SELF);
124         iterator.setRoot(iter.getStartNode(), xctxt);
125         result = new XNodeSet(iterator);
126       }
127       catch(Exception JavaDoc ex)
128       {
129         throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(ex);
130       }
131     }
132     else if (val instanceof DTMIterator)
133     {
134       result = new XNodeSet((DTMIterator) val);
135     }
136     // This next three instanceofs are a little worrysome, since a NodeList
137
// might also implement a Node!
138
else if (val instanceof org.w3c.dom.Node JavaDoc)
139     {
140       result = new XNodeSetForDOM((org.w3c.dom.Node JavaDoc)val, xctxt);
141     }
142     // This must come after org.w3c.dom.Node, since many Node implementations
143
// also implement NodeList.
144
else if (val instanceof org.w3c.dom.NodeList JavaDoc)
145     {
146       result = new XNodeSetForDOM((org.w3c.dom.NodeList JavaDoc)val, xctxt);
147     }
148     else if (val instanceof org.w3c.dom.traversal.NodeIterator)
149     {
150       result = new XNodeSetForDOM((org.w3c.dom.traversal.NodeIterator)val, xctxt);
151     }
152     else
153     {
154       result = new XObject(val);
155     }
156
157     return result;
158   }
159 }
160
Popular Tags