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. package org.apache.commons.jxpath.ri.axes;
  17. import java.util.HashSet;
  18. import org.apache.commons.jxpath.BasicNodeSet;
  19. import org.apache.commons.jxpath.ri.EvalContext;
  20. import org.apache.commons.jxpath.ri.model.NodePointer;
  21. /**
  22. * EvalContext that represents a union between other contexts - result
  23. * of a union operation like (a | b)
  24. *
  25. * @author Dmitri Plotnikov
  26. * @version $Revision: 1.12 $ $Date: 2004/02/29 14:17:38 $
  27. */
  28. public class UnionContext extends NodeSetContext {
  29. private boolean startedSet = false;
  30. private EvalContext contexts[];
  31. private boolean prepared = false;
  32. public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
  33. super(parentContext, new BasicNodeSet());
  34. this.contexts = contexts;
  35. }
  36. public int getDocumentOrder() {
  37. if (contexts.length > 1) {
  38. return 1;
  39. }
  40. return super.getDocumentOrder();
  41. }
  42. public boolean setPosition(int position) {
  43. if (!prepared) {
  44. prepared = true;
  45. BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
  46. HashSet set = new HashSet();
  47. for (int i = 0; i < contexts.length; i++) {
  48. EvalContext ctx = (EvalContext) contexts[i];
  49. while (ctx.nextSet()) {
  50. while (ctx.nextNode()) {
  51. NodePointer ptr = ctx.getCurrentNodePointer();
  52. if (!set.contains(ptr)) {
  53. nodeSet.add(ptr);
  54. set.add(ptr);
  55. }
  56. }
  57. }
  58. }
  59. }
  60. return super.setPosition(position);
  61. }
  62. }