KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > normalize > DependNode


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.xquery.normalize;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.xquark.xquery.parser.FLWRExpression;
29
30 public class DependNode {
31
32     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
33     private static final String JavaDoc RCSName = "$Name: $";
34
35     protected ArrayList JavaDoc 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 JavaDoc sons, DependNode parent, FLWRExpression expr) {
47         setSons(sons);
48         setParent(parent);
49         setExpression(expr);
50     }
51
52     public ArrayList JavaDoc getSons() { return sons; }
53
54     public void setSons(ArrayList JavaDoc sons) { this.sons = sons; }
55
56     public void addSon(DependNode son) {
57         if (son == null) return;
58         if (sons == null) sons = new ArrayList JavaDoc(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 JavaDoc(1);
65         this.sons.add(index,son);
66         son.setParent(this);
67     }
68     public void addSons(ArrayList JavaDoc 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     // For debugging
85
public String JavaDoc toString() {
86         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc varLevels = null ;
100     public List JavaDoc varLevels() {
101         if (null == varLevels) {
102             if (null == parent) {
103                 varLevels = new ArrayList JavaDoc();
104                 varLevels.add(getExpression().getVariables().clone());
105             }
106             else {
107                 varLevels = new ArrayList JavaDoc();
108                 varLevels.addAll(parent.varLevels());
109                 List JavaDoc thisLevel = (List JavaDoc)getExpression().getVariables().clone();
110                 for (int i = 0; i < varLevels.size(); i++) {
111                     thisLevel.removeAll((List JavaDoc)varLevels.get(i));
112                 }
113                 varLevels.add(thisLevel);
114             }
115         }
116         return varLevels;
117     }
118
119     protected List JavaDoc sortExprLevels = null ;
120     public List JavaDoc sortExprLevels() {
121
122         ArrayList JavaDoc sortExprList = null;
123         if (null == sortExprLevels) {
124             if (null == parent) {
125                 sortExprLevels = new ArrayList JavaDoc();
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 JavaDoc();
137                 sortExprLevels.addAll(parent.sortExprLevels());
138
139                 sortExprList = getExpression().getOrderBy();
140                 if (null != sortExprList) {
141                     List JavaDoc thisLevel = (ArrayList JavaDoc) sortExprList.clone();
142                     for (int i = 0; i < sortExprLevels.size(); i++) {
143                         List JavaDoc curLevel = (List JavaDoc)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