1. /*
  2. * Copyright 2001-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: FlowList.java,v 1.6 2004/02/16 22:24:29 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler;
  20. import java.util.Iterator;
  21. import java.util.Vector;
  22. import com.sun.org.apache.bcel.internal.generic.BranchHandle;
  23. import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
  24. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  25. /**
  26. * @author Jacek Ambroziak
  27. * @author Santiago Pericas-Geertsen
  28. */
  29. public final class FlowList {
  30. private Vector _elements;
  31. public FlowList() {
  32. _elements = null;
  33. }
  34. public FlowList(InstructionHandle bh) {
  35. _elements = new Vector();
  36. _elements.addElement(bh);
  37. }
  38. public FlowList(FlowList list) {
  39. _elements = list._elements;
  40. }
  41. public FlowList add(InstructionHandle bh) {
  42. if (_elements == null) {
  43. _elements = new Vector();
  44. }
  45. _elements.addElement(bh);
  46. return this;
  47. }
  48. public FlowList append(FlowList right) {
  49. if (_elements == null) {
  50. _elements = right._elements;
  51. }
  52. else {
  53. final Vector temp = right._elements;
  54. if (temp != null) {
  55. final int n = temp.size();
  56. for (int i = 0; i < n; i++) {
  57. _elements.addElement(temp.elementAt(i));
  58. }
  59. }
  60. }
  61. return this;
  62. }
  63. /**
  64. * Back patch a flow list. All instruction handles must be branch handles.
  65. */
  66. public void backPatch(InstructionHandle target) {
  67. if (_elements != null) {
  68. final int n = _elements.size();
  69. for (int i = 0; i < n; i++) {
  70. BranchHandle bh = (BranchHandle)_elements.elementAt(i);
  71. bh.setTarget(target);
  72. }
  73. _elements.clear(); // avoid backpatching more than once
  74. }
  75. }
  76. /**
  77. * Redirect the handles from oldList to newList. "This" flow list
  78. * is assumed to be relative to oldList.
  79. */
  80. public FlowList copyAndRedirect(InstructionList oldList,
  81. InstructionList newList)
  82. {
  83. final FlowList result = new FlowList();
  84. if (_elements == null) {
  85. return result;
  86. }
  87. final int n = _elements.size();
  88. final Iterator oldIter = oldList.iterator();
  89. final Iterator newIter = newList.iterator();
  90. while (oldIter.hasNext()) {
  91. final InstructionHandle oldIh = (InstructionHandle) oldIter.next();
  92. final InstructionHandle newIh = (InstructionHandle) newIter.next();
  93. for (int i = 0; i < n; i++) {
  94. if (_elements.elementAt(i) == oldIh) {
  95. result.add(newIh);
  96. }
  97. }
  98. }
  99. return result;
  100. }
  101. }