1 22 23 package org.xquark.xquery.normalize; 24 25 import java.util.ArrayList ; 26 import java.util.List ; 27 28 import org.xquark.xquery.parser.FLWRExpression; 29 30 public class DependNode { 31 32 private static final String RCSRevision = "$Revision: 1.1 $"; 33 private static final String RCSName = "$Name: $"; 34 35 protected ArrayList sons; 36 protected DependNode parent = null; 37 protected FLWRExpression expr = null; 38 39 40 public DependNode(DependNode son, DependNode parent, FLWRExpression expr) { 41 addSon(son); 42 setParent(parent); 43 setExpression(expr); 44 } 45 46 public DependNode(ArrayList sons, DependNode parent, FLWRExpression expr) { 47 setSons(sons); 48 setParent(parent); 49 setExpression(expr); 50 } 51 52 public ArrayList getSons() { return sons; } 53 54 public void setSons(ArrayList sons) { this.sons = sons; } 55 56 public void addSon(DependNode son) { 57 if (son == null) return; 58 if (sons == null) sons = new ArrayList (1); 59 this.sons.add(son); 60 son.setParent(this); 61 } 62 public void addSon(int index, DependNode son) { 63 if (son == null) return; 64 if (sons == null) sons = new ArrayList (1); 65 this.sons.add(index,son); 66 son.setParent(this); 67 } 68 public void addSons(ArrayList sons) { 69 if (sons == null) return; 70 if (this.sons == null) this.sons = sons; 71 else this.sons.addAll(sons); 72 for (int i=0;i<sons.size();i++) ((DependNode)sons.get(i)).setParent(this); 73 } 74 75 public DependNode getParent() { return parent; } 76 77 public void setParent(DependNode parent) { this.parent = parent; } 78 79 public FLWRExpression getExpression() { return expr; } 80 81 public void setExpression(FLWRExpression expr) { this.expr = expr; } 82 83 84 public String toString() { 86 StringBuffer sb = new StringBuffer (); 87 sb.append(expr.getParentExpression().get(0)); 88 if (sons != null && sons.size() > 0) { 89 sb.append(" ["); 90 for (int i=0;i<sons.size();i++) { 91 sb.append(" "); 92 sb.append(((DependNode)sons.get(i)).toString()); 93 } 94 sb.append(" ] "); 95 } 96 return sb.toString(); 97 } 98 99 protected List varLevels = null ; 100 public List varLevels() { 101 if (null == varLevels) { 102 if (null == parent) { 103 varLevels = new ArrayList (); 104 varLevels.add(getExpression().getVariables().clone()); 105 } 106 else { 107 varLevels = new ArrayList (); 108 varLevels.addAll(parent.varLevels()); 109 List thisLevel = (List )getExpression().getVariables().clone(); 110 for (int i = 0; i < varLevels.size(); i++) { 111 thisLevel.removeAll((List )varLevels.get(i)); 112 } 113 varLevels.add(thisLevel); 114 } 115 } 116 return varLevels; 117 } 118 119 protected List sortExprLevels = null ; 120 public List sortExprLevels() { 121 122 ArrayList sortExprList = null; 123 if (null == sortExprLevels) { 124 if (null == parent) { 125 sortExprLevels = new ArrayList (); 126 sortExprList = getExpression().getOrderBy(); 127 if (null != sortExprList) { 128 sortExprLevels.add(sortExprList.clone()); 129 } 130 else { 131 sortExprLevels.add(null); 132 } 133 134 } 135 else { 136 sortExprLevels = new ArrayList (); 137 sortExprLevels.addAll(parent.sortExprLevels()); 138 139 sortExprList = getExpression().getOrderBy(); 140 if (null != sortExprList) { 141 List thisLevel = (ArrayList ) sortExprList.clone(); 142 for (int i = 0; i < sortExprLevels.size(); i++) { 143 List curLevel = (List )sortExprLevels.get(i); 144 if (null != curLevel) { 145 thisLevel.removeAll(curLevel); 146 } 147 } 148 sortExprLevels.add(thisLevel); 149 } 150 else { 151 sortExprLevels.add(null); 152 } 153 } 154 } 155 return sortExprLevels; 156 } 157 158 } 159 | Popular Tags |