19f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* 29f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one 39f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * or more contributor license agreements. See the NOTICE file 49f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed with this work for additional information 59f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * regarding copyright ownership. The ASF licenses this file 69f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * to you under the Apache License, Version 2.0 (the "License"); 79f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * you may not use this file except in compliance with the License. 89f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * You may obtain a copy of the License at 99f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Unless required by applicable law or agreed to in writing, software 139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * See the License for the specific language governing permissions and 169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * limitations under the License. 179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* 199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * $Id: XObjectFactory.java 468655 2006-10-28 07:12:06Z minchau $ 209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpackage org.apache.xpath.objects; 229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.Axis; 249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.DTM; 259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.DTMAxisIterator; 269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.DTMIterator; 279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.XPathContext; 289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.axes.OneStepIterator; 299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpublic class XObjectFactory 329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson{ 339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Create the right XObject based on the type of the object passed. This 369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * function can not make an XObject that exposes DOM Nodes, NodeLists, and 379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * NodeIterators to the XSLT stylesheet as node-sets. 389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param val The java object which this object will wrap. 409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return the right XObject based on the type of the object passed. 429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson static public XObject create(Object val) 449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson XObject result; 479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (val instanceof XObject) 499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson result = (XObject) val; 519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else if (val instanceof String) 539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson result = new XString((String) val); 559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else if (val instanceof Boolean) 579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson result = new XBoolean((Boolean)val); 599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else if (val instanceof Double) 619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson result = new XNumber(((Double) val)); 639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else 659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson result = new XObject(val); 679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return result; 709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Create the right XObject based on the type of the object passed. 749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * This function <emph>can</emph> make an XObject that exposes DOM Nodes, NodeLists, and 759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * NodeIterators to the XSLT stylesheet as node-sets. 769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param val The java object which this object will wrap. 789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param xctxt The XPath context. 799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return the right XObject based on the type of the object passed. 819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson static public XObject create(Object val, XPathContext xctxt) 839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson XObject result; 869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (val instanceof XObject) 889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson result = (XObject) val; 909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else if (val instanceof String) 929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson result = new XString((String) val); 949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else if (val instanceof Boolean) 969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson result = new XBoolean((Boolean)val); 989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else if (val instanceof Number) 1009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson result = new XNumber(((Number) val)); 1029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else if (val instanceof DTM) 1049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson DTM dtm = (DTM)val; 1069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson try 1079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int dtmRoot = dtm.getDocument(); 1099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson DTMAxisIterator iter = dtm.getAxisIterator(Axis.SELF); 1109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson iter.setStartNode(dtmRoot); 1119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson DTMIterator iterator = new OneStepIterator(iter, Axis.SELF); 1129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson iterator.setRoot(dtmRoot, xctxt); 1139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson result = new XNodeSet(iterator); 1149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson catch(Exception ex) 1169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson throw new org.apache.xml.utils.WrappedRuntimeException(ex); 1189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else if (val instanceof DTMAxisIterator) 1219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson DTMAxisIterator iter = (DTMAxisIterator)val; 1239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson try 1249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson DTMIterator iterator = new OneStepIterator(iter, Axis.SELF); 1269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson iterator.setRoot(iter.getStartNode(), xctxt); 1279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson result = new XNodeSet(iterator); 1289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson catch(Exception ex) 1309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson throw new org.apache.xml.utils.WrappedRuntimeException(ex); 1329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else if (val instanceof DTMIterator) 1359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson result = new XNodeSet((DTMIterator) val); 1379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // This next three instanceofs are a little worrysome, since a NodeList 1399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // might also implement a Node! 1409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else if (val instanceof org.w3c.dom.Node) 1419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson result = new XNodeSetForDOM((org.w3c.dom.Node)val, xctxt); 1439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // This must come after org.w3c.dom.Node, since many Node implementations 1459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // also implement NodeList. 1469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else if (val instanceof org.w3c.dom.NodeList) 1479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson result = new XNodeSetForDOM((org.w3c.dom.NodeList)val, xctxt); 1499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else if (val instanceof org.w3c.dom.traversal.NodeIterator) 1519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson result = new XNodeSetForDOM((org.w3c.dom.traversal.NodeIterator)val, xctxt); 1539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else 1559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson result = new XObject(val); 1579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return result; 1609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson} 162