sboag 01/08/02 19:43:18
Modified: java/src/org/apache/xalan/transformer TransformerImpl.java
java/src/org/apache/xpath NodeSetDTM.java
java/src/org/apache/xpath/functions FuncExtFunction.java
java/src/org/apache/xpath/objects XBoolean.java
XNodeSet.java XNumber.java XObject.java
Added: java/src/org/apache/xpath/objects XNodeSetForDOM.java
Log:
Address http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2925
Create new XObject, XNodeSetForDOM, which is constructed
from a Node, NodeList, or NodeIterator, and acts the same as
XNodeSet, except it returns the original object for object(),
nodeset(), and nodelist().
Add XObject#create(Object val, XPathContext xctxt), which is
called from TransformerImpl#setParameter(s), and
FuncExtFunction#execute (for the return values of the
extension).
XBoolean and XNumber now also have Boolean and Number
constructors, in which case the original object will also be
returned from the object() method.
This should make the conversion from java object to XObject
consistent between function returns and setParameter. These
changes should also fix a bug where NodeLists weren't being converted.
Revision Changes Path
1.110 +4 -4
xml-xalan/java/src/org/apache/xalan/transformer/TransformerImpl.java
Index: TransformerImpl.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xalan/transformer/TransformerImpl.java,v
retrieving revision 1.109
retrieving revision 1.110
diff -u -r1.109 -r1.110
--- TransformerImpl.java 2001/07/28 00:25:58 1.109
+++ TransformerImpl.java 2001/08/03 02:43:17 1.110
@@ -1336,7 +1336,7 @@
VariableStack varstack = getXPathContext().getVarStack();
QName qname = new QName(namespace, name);
- XObject xobject = XObject.create(value);
+ XObject xobject = XObject.create(value, getXPathContext());
StylesheetRoot sroot = m_stylesheetRoot;
Vector vars = sroot.getVariablesAndParamsComposed();
@@ -1383,12 +1383,12 @@
if (null == s2)
{
- replaceOrPushUserParam(new QName(s1), new XObject(value));
+ replaceOrPushUserParam(new QName(s1), XObject.create(value,
getXPathContext()));
setParameter(s1, null, value);
}
else
{
- replaceOrPushUserParam(new QName(s1, s2), new XObject(value));
+ replaceOrPushUserParam(new QName(s1, s2), XObject.create(value,
getXPathContext()));
setParameter(s2, s1, value);
}
}
@@ -3085,7 +3085,7 @@
{
m_isTransformDone = false;
- // Should no longer be needed...
+ // Should no longer be needed...
// if(m_inputContentHandler instanceof TransformerHandlerImpl)
// {
// TransformerHandlerImpl thi =
(TransformerHandlerImpl)m_inputContentHandler;
1.4 +24 -1 xml-xalan/java/src/org/apache/xpath/NodeSetDTM.java
Index: NodeSetDTM.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/src/org/apache/xpath/NodeSetDTM.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- NodeSetDTM.java 2001/07/28 00:26:00 1.3
+++ NodeSetDTM.java 2001/08/03 02:43:17 1.4
@@ -57,7 +57,7 @@
package org.apache.xpath;
import org.w3c.dom.Node;
-//import org.w3c.dom.NodeList;
+import org.w3c.dom.NodeList;
//import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.traversal.NodeIterator;
//import org.w3c.dom.traversal.NodeFilter;
@@ -190,6 +190,29 @@
addNodeInDocOrder(handle, xctxt);
}
}
+
+ /**
+ * Create a NodeSetDTM, and copy the members of the
+ * given DTMIterator into it.
+ *
+ * @param ni Iterator which yields Nodes to be made members of the new set.
+ */
+ public NodeSetDTM(NodeList nodeList, XPathContext xctxt)
+ {
+
+ super();
+
+ m_manager = xctxt.getDTMManager();
+
+ int n = nodeList.getLength();
+ for (int i = 0; i < n; i++)
+ {
+ Node node = nodeList.item(i);
+ int handle = xctxt.getDTMHandleFromNode(node);
+ addNodeInDocOrder(handle, xctxt);
+ }
+ }
+
/**
* Create a NodeSetDTM which contains the given Node.
1.13 +1 -54
xml-xalan/java/src/org/apache/xpath/functions/FuncExtFunction.java
Index: FuncExtFunction.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xpath/functions/FuncExtFunction.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- FuncExtFunction.java 2001/07/06 18:29:17 1.12
+++ FuncExtFunction.java 2001/08/03 02:43:17 1.13
@@ -184,60 +184,7 @@
if (null != val)
{
- if (val instanceof XObject)
- {
- result = (XObject) val;
- }
- else if (val instanceof String)
- {
- result = new XString((String) val);
- }
- else if (val instanceof Boolean)
- {
- result = ((Boolean) val).booleanValue()
- ? XBoolean.S_TRUE : XBoolean.S_FALSE;
- }
- else if (val instanceof Number)
- {
- result = new XNumber(((Number) val).doubleValue());
- }
- else if (val instanceof DocumentFragment)
- {
- int handle = xctxt.getDTMHandleFromNode((DocumentFragment)val);
-
- result = new XRTreeFrag(handle, xctxt);
- }
- else if (val instanceof DTM)
- {
- DTM dtm = (DTM)val;
- DTMIterator iterator = new DescendantIterator();
- iterator.setRoot(dtm.getDocument(), xctxt);
- result = new XNodeSet(iterator);
- }
- else if (val instanceof DTMAxisIterator)
- {
- DTMAxisIterator iter = (DTMAxisIterator)val;
- DTMIterator iterator = new OneStepIterator(iter);
- result = new XNodeSet(iterator);
- }
- else if (val instanceof DTMIterator)
- {
- result = new XNodeSet((DTMIterator) val);
- }
- else if (val instanceof NodeIterator)
- {
- result = new XNodeSet(new
org.apache.xpath.NodeSetDTM(((NodeIterator)val), xctxt));
- }
- else if (val instanceof org.w3c.dom.Node)
- {
- result =
- new XNodeSet(xctxt.getDTMHandleFromNode((org.w3c.dom.Node) val),
- xctxt.getDTMManager());
- }
- else
- {
- result = new XObject(val);
- }
+ result = XObject.create(val, xctxt);
}
else
{
1.9 +18 -1 xml-xalan/java/src/org/apache/xpath/objects/XBoolean.java
Index: XBoolean.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xpath/objects/XBoolean.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- XBoolean.java 2001/06/12 19:16:50 1.8
+++ XBoolean.java 2001/08/03 02:43:17 1.9
@@ -94,6 +94,21 @@
m_val = b;
}
+
+ /**
+ * Construct a XBoolean object.
+ *
+ * @param b Value of the boolean object
+ */
+ public XBoolean(Boolean b)
+ {
+
+ super();
+
+ m_val = b.booleanValue();
+ m_obj = b;
+ }
+
/**
* Tell that this is a CLASS_BOOLEAN.
@@ -154,7 +169,9 @@
*/
public Object object()
{
- return new Boolean(m_val);
+ if(null == m_obj)
+ m_obj = m_val ? S_TRUE : S_FALSE;
+ return m_obj;
}
/**
1.19 +8 -1 xml-xalan/java/src/org/apache/xpath/objects/XNodeSet.java
Index: XNodeSet.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xpath/objects/XNodeSet.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- XNodeSet.java 2001/07/31 16:29:51 1.18
+++ XNodeSet.java 2001/08/03 02:43:17 1.19
@@ -80,11 +80,18 @@
*/
public class XNodeSet extends XObject
{
- private DTMManager m_dtmMgr;
+ protected DTMManager m_dtmMgr;
public DTMManager getDTMMgr()
{
return m_dtmMgr;
+ }
+
+ /**
+ * Default constructor for derived objects.
+ */
+ protected XNodeSet()
+ {
}
/**
1.11 +17 -2 xml-xalan/java/src/org/apache/xpath/objects/XNumber.java
Index: XNumber.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/src/org/apache/xpath/objects/XNumber.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- XNumber.java 2001/07/28 00:45:05 1.10
+++ XNumber.java 2001/08/03 02:43:17 1.11
@@ -79,11 +79,24 @@
*/
public XNumber(double d)
{
-
super();
m_val = d;
}
+
+ /**
+ * Construct a XNodeSet object.
+ *
+ * @param d Value of the object
+ */
+ public XNumber(Number num)
+ {
+
+ super();
+
+ m_val = num.doubleValue();
+ m_obj = num;
+ }
/**
* Tell that this is a CLASS_NUMBER.
@@ -389,7 +402,9 @@
*/
public Object object()
{
- return new Double(m_val);
+ if(null == m_obj)
+ m_obj = new Double(m_val);
+ return m_obj;
}
/**
1.15 +88 -20 xml-xalan/java/src/org/apache/xpath/objects/XObject.java
Index: XObject.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/src/org/apache/xpath/objects/XObject.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- XObject.java 2001/07/31 16:29:51 1.14
+++ XObject.java 2001/08/03 02:43:17 1.15
@@ -61,8 +61,7 @@
//import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.traversal.NodeIterator;
-import org.apache.xml.dtm.DTM;
-import org.apache.xml.dtm.DTMIterator;
+import org.apache.xml.dtm.*;
import java.io.Serializable;
@@ -73,6 +72,8 @@
import org.apache.xalan.res.XSLMessages;
import org.apache.xpath.Expression;
import org.apache.xml.utils.XMLString;
+import org.apache.xpath.axes.OneStepIterator;
+import org.apache.xpath.axes.DescendantIterator;
/**
* <meta name="usage" content="general"/>
@@ -172,7 +173,9 @@
}
/**
- * Create the right XObject based on the type of the object passed.
+ * Create the right XObject based on the type of the object passed. This
+ * function can not make an XObject that exposes DOM Nodes, NodeLists, and
+ * NodeIterators to the XSLT stylesheet as node-sets.
*
* @param val The java object which this object will wrap.
*
@@ -193,34 +196,99 @@
}
else if (val instanceof Boolean)
{
- result = ((Boolean) val).booleanValue()
- ? XBoolean.S_TRUE : XBoolean.S_FALSE;
+ result = new XBoolean((Boolean)val);
}
else if (val instanceof Double)
{
- result = new XNumber(((Double) val).doubleValue());
+ result = new XNumber(((Double) val));
}
- else if (val instanceof org.w3c.dom.DocumentFragment)
+ else
{
-
- // result = new XRTreeFrag((DocumentFragment) val);
- // %REVIEW%
result = new XObject(val);
}
- else if (val instanceof org.w3c.dom.traversal.NodeIterator)
- {
- // result = new XNodeSet((NodeIterator) val);
- // %REVIEW%
- result = new XObject(val);
+ return result;
+ }
+
+ /**
+ * Create the right XObject based on the type of the object passed.
+ * This function <emph>can</emph> make an XObject that exposes DOM Nodes,
NodeLists, and
+ * NodeIterators to the XSLT stylesheet as node-sets.
+ *
+ * @param val The java object which this object will wrap.
+ * @param xctxt The XPath context.
+ *
+ * @return the right XObject based on the type of the object passed.
+ */
+ static public XObject create(Object val, XPathContext xctxt)
+ {
+
+ XObject result;
+
+ if (val instanceof XObject)
+ {
+ result = (XObject) val;
}
+ else if (val instanceof String)
+ {
+ result = new XString((String) val);
+ }
+ else if (val instanceof Boolean)
+ {
+ result = new XBoolean((Boolean)val);
+ }
+ else if (val instanceof Number)
+ {
+ result = new XNumber(((Number) val));
+ }
+ else if (val instanceof DTM)
+ {
+ DTM dtm = (DTM)val;
+ try
+ {
+ int dtmRoot = dtm.getDocument();
+ DTMAxisIterator iter = dtm.getAxisIterator(Axis.SELF);
+ iter.setStartNode(dtmRoot);
+ DTMIterator iterator = new OneStepIterator(iter);
+ result = new XNodeSet(iterator);
+ }
+ catch(Exception ex)
+ {
+ throw new org.apache.xml.utils.WrappedRuntimeException(ex);
+ }
+ }
+ else if (val instanceof DTMAxisIterator)
+ {
+ DTMAxisIterator iter = (DTMAxisIterator)val;
+ try
+ {
+ DTMIterator iterator = new OneStepIterator(iter);
+ result = new XNodeSet(iterator);
+ }
+ catch(Exception ex)
+ {
+ throw new org.apache.xml.utils.WrappedRuntimeException(ex);
+ }
+ }
+ else if (val instanceof DTMIterator)
+ {
+ result = new XNodeSet((DTMIterator) val);
+ }
+ // This next three instanceofs are a little worrysome, since a NodeList
+ // might also implement a Node!
else if (val instanceof org.w3c.dom.Node)
{
-
- // result = new
XNodeSet(xctxt.getDTMHandleFromNode((org.w3c.dom.Node)val),
- // xctxt.getDTMManager());
- // %REVIEW%
- result = new XObject(val);
+ result = new XNodeSetForDOM((org.w3c.dom.Node)val, xctxt);
+ }
+ // This must come after org.w3c.dom.Node, since many Node
implementations
+ // also implement NodeList.
+ else if (val instanceof org.w3c.dom.NodeList)
+ {
+ result = new XNodeSetForDOM((org.w3c.dom.NodeList)val, xctxt);
+ }
+ else if (val instanceof org.w3c.dom.traversal.NodeIterator)
+ {
+ result = new XNodeSetForDOM((org.w3c.dom.traversal.NodeIterator)val,
xctxt);
}
else
{
1.1
xml-xalan/java/src/org/apache/xpath/objects/XNodeSetForDOM.java
Index: XNodeSetForDOM.java
===================================================================
package org.apache.xpath.objects;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.traversal.NodeIterator;
import org.apache.xml.dtm.*;
import org.apache.xpath.NodeSetDTM;
import org.apache.xpath.XPathContext;
/**
* This class overrides the XNodeSet#object() method to provide the original
* Node object, NodeList object, or NodeIterator.
*/
public class XNodeSetForDOM extends XNodeSet
{
Object m_origObj;
public XNodeSetForDOM(Node node, DTMManager dtmMgr)
{
m_dtmMgr = dtmMgr;
m_origObj = node;
int dtmHandle = dtmMgr.getDTMHandleFromNode(node);
m_obj = new NodeSetDTM(dtmMgr);
((NodeSetDTM) m_obj).addNode(dtmHandle);
}
public XNodeSetForDOM(NodeList nodeList, XPathContext xctxt)
{
m_dtmMgr = xctxt.getDTMManager();
m_origObj = nodeList;
m_obj = new org.apache.xpath.NodeSetDTM(nodeList, xctxt);
}
public XNodeSetForDOM(NodeIterator nodeIter, XPathContext xctxt)
{
m_dtmMgr = xctxt.getDTMManager();
m_origObj = nodeIter;
m_obj = new org.apache.xpath.NodeSetDTM(nodeIter, xctxt);
}
/**
* Return the original DOM object that the user passed in. For use
primarily
* by the extension mechanism.
*
* @return The object that this class wraps
*/
public Object object()
{
return m_origObj;
}
/**
* Cast result object to a nodelist. Always issues an error.
*
* @return null
*
* @throws javax.xml.transform.TransformerException
*/
public NodeIterator nodeset() throws
javax.xml.transform.TransformerException
{
return (m_origObj instanceof NodeIterator)
? (NodeIterator)m_origObj : super.nodeset();
}
/**
* Cast result object to a nodelist. Always issues an error.
*
* @return null
*
* @throws javax.xml.transform.TransformerException
*/
public NodeList nodelist() throws javax.xml.transform.TransformerException
{
return (m_origObj instanceof NodeList)
? (NodeList)m_origObj : super.nodelist();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]