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: VariableSafeAbsRef.java,v 1.5 2004/02/17 04:35:12 minchau Exp $
  18. */
  19. package com.sun.org.apache.xpath.internal.operations;
  20. import com.sun.org.apache.xml.internal.dtm.DTMManager;
  21. import com.sun.org.apache.xpath.internal.Expression;
  22. import com.sun.org.apache.xpath.internal.XPathContext;
  23. import com.sun.org.apache.xpath.internal.objects.XNodeSet;
  24. import com.sun.org.apache.xpath.internal.objects.XObject;
  25. /**
  26. * This is a "smart" variable reference that is used in situations where
  27. * an absolute path is optimized into a variable reference, but may
  28. * be used in some situations where the document context may have changed.
  29. * For instance, in select="document(doc/@href)//name[//salary > 7250]", the
  30. * root in the predicate will be different for each node in the set. While
  31. * this is easy to detect statically in this case, in other cases static
  32. * detection would be very hard or impossible. So, this class does a dynamic check
  33. * to make sure the document context of the referenced variable is the same as
  34. * the current document context, and, if it is not, execute the referenced variable's
  35. * expression with the current context instead.
  36. */
  37. public class VariableSafeAbsRef extends Variable
  38. {
  39. /**
  40. * Dereference the variable, and return the reference value. Note that lazy
  41. * evaluation will occur. If a variable within scope is not found, a warning
  42. * will be sent to the error listener, and an empty nodeset will be returned.
  43. *
  44. *
  45. * @param xctxt The runtime execution context.
  46. *
  47. * @return The evaluated variable, or an empty nodeset if not found.
  48. *
  49. * @throws javax.xml.transform.TransformerException
  50. */
  51. public XObject execute(XPathContext xctxt, boolean destructiveOK)
  52. throws javax.xml.transform.TransformerException
  53. {
  54. XNodeSet xns = (XNodeSet)super.execute(xctxt, destructiveOK);
  55. DTMManager dtmMgr = xctxt.getDTMManager();
  56. int context = xctxt.getContextNode();
  57. if(dtmMgr.getDTM(xns.getRoot()).getDocument() !=
  58. dtmMgr.getDTM(context).getDocument())
  59. {
  60. Expression expr = (Expression)xns.getContainedIter();
  61. xns = (XNodeSet)expr.asIterator(xctxt, context);
  62. }
  63. return xns;
  64. }
  65. }