View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jxpath.ri.axes;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  
22  import org.apache.commons.jxpath.BasicNodeSet;
23  import org.apache.commons.jxpath.Pointer;
24  import org.apache.commons.jxpath.ri.EvalContext;
25  import org.apache.commons.jxpath.ri.model.NodePointer;
26  
27  /**
28   * EvalContext that represents a union between other contexts - result
29   * of a union operation like (a | b)
30   *
31   * @author Dmitri Plotnikov
32   * @version $Revision: 1523199 $ $Date: 2013-09-14 11:45:47 +0200 (Sa, 14 Sep 2013) $
33   */
34  public class UnionContext extends NodeSetContext {
35      private EvalContext[] contexts;
36      private boolean prepared;
37  
38      /**
39       * Create a new UnionContext.
40       * @param parentContext parent context
41       * @param contexts child contexts
42       */
43      public UnionContext(EvalContext parentContext, EvalContext[] contexts) {
44          super(parentContext, new BasicNodeSet());
45          this.contexts = contexts;
46      }
47  
48      public int getDocumentOrder() {
49          return contexts.length > 1 ? 1 : super.getDocumentOrder();
50      }
51  
52      public boolean setPosition(int position) {
53          if (!prepared) {
54              prepared = true;
55              BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
56              ArrayList pointers = new ArrayList();
57              for (int i = 0; i < contexts.length; i++) {
58                  EvalContext ctx = contexts[i];
59                  while (ctx.nextSet()) {
60                      while (ctx.nextNode()) {
61                          NodePointer ptr = ctx.getCurrentNodePointer();
62                          if (!pointers.contains(ptr)) {
63                              pointers.add(ptr);
64                          }
65                      }
66                  }
67              }
68              sortPointers(pointers);
69  
70              for (Iterator it = pointers.iterator(); it.hasNext();) {
71                  nodeSet.add((Pointer) it.next());
72              }
73          }
74          return super.setPosition(position);
75      }
76  }