View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2001-2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  /*
21   * $Id: FlowList.java,v 1.2.4.1 2005/09/01 15:21:43 pvedula Exp $
22   */
23  
24  package com.sun.org.apache.xalan.internal.xsltc.compiler;
25  
26  import java.util.Iterator;
27  import java.util.Vector;
28  
29  import com.sun.org.apache.bcel.internal.generic.BranchHandle;
30  import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
31  import com.sun.org.apache.bcel.internal.generic.InstructionList;
32  
33  /**
34   * @author Jacek Ambroziak
35   * @author Santiago Pericas-Geertsen
36   */
37  public final class FlowList {
38      private Vector _elements;
39  
40      public FlowList() {
41          _elements = null;
42      }
43  
44      public FlowList(InstructionHandle bh) {
45          _elements = new Vector();
46          _elements.addElement(bh);
47      }
48  
49      public FlowList(FlowList list) {
50          _elements = list._elements;
51      }
52  
53      public FlowList add(InstructionHandle bh) {
54          if (_elements == null) {
55              _elements = new Vector();
56          }
57          _elements.addElement(bh);
58          return this;
59      }
60  
61      public FlowList append(FlowList right) {
62          if (_elements == null) {
63              _elements = right._elements;
64          }
65          else {
66              final Vector temp = right._elements;
67              if (temp != null) {
68                  final int n = temp.size();
69                  for (int i = 0; i < n; i++) {
70                      _elements.addElement(temp.elementAt(i));
71                  }
72              }
73          }
74          return this;
75      }
76  
77      /**
78       * Back patch a flow list. All instruction handles must be branch handles.
79       */
80      public void backPatch(InstructionHandle target) {
81          if (_elements != null) {
82              final int n = _elements.size();
83              for (int i = 0; i < n; i++) {
84                  BranchHandle bh = (BranchHandle)_elements.elementAt(i);
85                  bh.setTarget(target);
86              }
87              _elements.clear();          // avoid backpatching more than once
88          }
89      }
90  
91      /**
92       * Redirect the handles from oldList to newList. "This" flow list
93       * is assumed to be relative to oldList.
94       */
95      public FlowList copyAndRedirect(InstructionList oldList,
96          InstructionList newList)
97      {
98          final FlowList result = new FlowList();
99          if (_elements == null) {
100             return result;
101         }
102 
103         final int n = _elements.size();
104         final Iterator oldIter = oldList.iterator();
105         final Iterator newIter = newList.iterator();
106 
107         while (oldIter.hasNext()) {
108             final InstructionHandle oldIh = (InstructionHandle) oldIter.next();
109             final InstructionHandle newIh = (InstructionHandle) newIter.next();
110 
111             for (int i = 0; i < n; i++) {
112                 if (_elements.elementAt(i) == oldIh) {
113                     result.add(newIh);
114                 }
115             }
116         }
117         return result;
118     }
119 }