001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: XObjectFactory.java,v 1.5 2004/02/17 04:34:38 minchau Exp $
018: */
019: package org.apache.xpath.objects;
020:
021: import org.apache.xml.dtm.Axis;
022: import org.apache.xml.dtm.DTM;
023: import org.apache.xml.dtm.DTMAxisIterator;
024: import org.apache.xml.dtm.DTMIterator;
025: import org.apache.xpath.XPathContext;
026: import org.apache.xpath.axes.OneStepIterator;
027:
028: public class XObjectFactory {
029:
030: /**
031: * Create the right XObject based on the type of the object passed. This
032: * function can not make an XObject that exposes DOM Nodes, NodeLists, and
033: * NodeIterators to the XSLT stylesheet as node-sets.
034: *
035: * @param val The java object which this object will wrap.
036: *
037: * @return the right XObject based on the type of the object passed.
038: */
039: static public XObject create(Object val) {
040:
041: XObject result;
042:
043: if (val instanceof XObject) {
044: result = (XObject) val;
045: } else if (val instanceof String) {
046: result = new XString((String) val);
047: } else if (val instanceof Boolean) {
048: result = new XBoolean((Boolean) val);
049: } else if (val instanceof Double) {
050: result = new XNumber(((Double) val));
051: } else {
052: result = new XObject(val);
053: }
054:
055: return result;
056: }
057:
058: /**
059: * Create the right XObject based on the type of the object passed.
060: * This function <emph>can</emph> make an XObject that exposes DOM Nodes, NodeLists, and
061: * NodeIterators to the XSLT stylesheet as node-sets.
062: *
063: * @param val The java object which this object will wrap.
064: * @param xctxt The XPath context.
065: *
066: * @return the right XObject based on the type of the object passed.
067: */
068: static public XObject create(Object val, XPathContext xctxt) {
069:
070: XObject result;
071:
072: if (val instanceof XObject) {
073: result = (XObject) val;
074: } else if (val instanceof String) {
075: result = new XString((String) val);
076: } else if (val instanceof Boolean) {
077: result = new XBoolean((Boolean) val);
078: } else if (val instanceof Number) {
079: result = new XNumber(((Number) val));
080: } else if (val instanceof DTM) {
081: DTM dtm = (DTM) val;
082: try {
083: int dtmRoot = dtm.getDocument();
084: DTMAxisIterator iter = dtm.getAxisIterator(Axis.SELF);
085: iter.setStartNode(dtmRoot);
086: DTMIterator iterator = new OneStepIterator(iter,
087: Axis.SELF);
088: iterator.setRoot(dtmRoot, xctxt);
089: result = new XNodeSet(iterator);
090: } catch (Exception ex) {
091: throw new org.apache.xml.utils.WrappedRuntimeException(
092: ex);
093: }
094: } else if (val instanceof DTMAxisIterator) {
095: DTMAxisIterator iter = (DTMAxisIterator) val;
096: try {
097: DTMIterator iterator = new OneStepIterator(iter,
098: Axis.SELF);
099: iterator.setRoot(iter.getStartNode(), xctxt);
100: result = new XNodeSet(iterator);
101: } catch (Exception ex) {
102: throw new org.apache.xml.utils.WrappedRuntimeException(
103: ex);
104: }
105: } else if (val instanceof DTMIterator) {
106: result = new XNodeSet((DTMIterator) val);
107: }
108: // This next three instanceofs are a little worrysome, since a NodeList
109: // might also implement a Node!
110: else if (val instanceof org.w3c.dom.Node) {
111: result = new XNodeSetForDOM((org.w3c.dom.Node) val, xctxt);
112: }
113: // This must come after org.w3c.dom.Node, since many Node implementations
114: // also implement NodeList.
115: else if (val instanceof org.w3c.dom.NodeList) {
116: result = new XNodeSetForDOM((org.w3c.dom.NodeList) val,
117: xctxt);
118: } else if (val instanceof org.w3c.dom.traversal.NodeIterator) {
119: result = new XNodeSetForDOM(
120: (org.w3c.dom.traversal.NodeIterator) val, xctxt);
121: } else {
122: result = new XObject(val);
123: }
124:
125: return result;
126: }
127: }
|