KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mediator > DOMUtils > BufferTuple


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.mediator.DOMUtils;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 import org.xquark.xpath.datamodel.TypedNode;
29 import org.xquark.xquery.parser.XQueryExpression;
30
31 public class BufferTuple extends ArrayList JavaDoc {
32     // **********************************************************************
33
// * VERSIONING
34
// **********************************************************************
35
private static final String JavaDoc RCSRevision = "$Revision: 1.10 $";
36     private static final String JavaDoc RCSName = "$Name: $";
37     // **********************************************************************
38
// * CLASS VARIABLES
39
// **********************************************************************
40
/**
41      * List of List of String.
42      * A list of String represent a path.
43      */

44 // private ArrayList stepslist = null;
45
private ArrayList JavaDoc pathlist = null;
46     private HashMap JavaDoc indexpaths = null;
47     private long identifier = 0;
48     private ArrayList JavaDoc indexes = null; // list of SortedKeyMap
49
private ArrayList JavaDoc indexesSpec = null; // list of ArrayList of items to be indexed
50
private int idsize = 0;
51
52     // ***********************************************************************
53
// * INITIALIZATION
54
// ***********************************************************************
55
/**
56      * Allocate a new BufferTuple.
57      * The String paths are decomposed in arraylist of strings representing
58      * steps
59      */

60     public BufferTuple(ArrayList JavaDoc list, int idsize) {
61         if (list == null)
62             return;
63         this.idsize = idsize;
64 // stepslist = new ArrayList(list.size());
65
pathlist = new ArrayList JavaDoc(list.size());
66         indexpaths = new HashMap JavaDoc(list.size());
67
68         for (int i = 0; i < list.size(); i++) {
69             XQueryExpression tmpExpri = (XQueryExpression) list.get(i);
70 // String tmpExprStr = tmpExpr.getStringValue(); // to avoid braces !!!
71
// ArrayList tmpList = new ArrayList();
72
// if (tmpExpr instanceof LocatedExpression) {
73
// LocatedExpression tmpLocExpr = (LocatedExpression) tmpExpr;
74
// for (int j = 0; j < tmpLocExpr.getSteps().size(); j++) {
75
// tmpList.add(((Step) tmpLocExpr.getStep(j)).getExpression().getStringValue());
76
// }
77
// } else if (tmpExpr instanceof Variable)
78
// tmpList.add(tmpExprStr);
79
// stepslist.add(tmpList);
80
pathlist.add(tmpExpri.getStringValue());
81             indexpaths.put(tmpExpri.getStringValue(), new Integer JavaDoc(i));
82         }
83     }
84
85     // ***********************************************************************
86
// * METHODS
87
// ***********************************************************************
88
public long getIdentifier() {
89         identifier++;
90         return identifier - 1;
91     }
92     public int getPathIndex(String JavaDoc path) {
93         if (indexpaths == null)
94             return -1;
95         Integer JavaDoc idx = (Integer JavaDoc) indexpaths.get(path);
96         if (idx == null)
97             return -1;
98         return idx.intValue();
99     }
100     public HashMap JavaDoc getIndexPath() {
101         return indexpaths;
102     }
103     public ArrayList JavaDoc getPathList() {
104         return pathlist;
105     }
106
107     // modified methods to accomodate with indexes if any
108
public void add(Tuple tuple) {
109         super.add(tuple);
110         tuple.setParent(this);
111         // calculate indexes if any
112
fillIndexes(tuple);
113     }
114     public void add(int index, Tuple tuple) {
115         super.add(index, tuple);
116         tuple.setParent(this);
117         // calculate indexes if any
118
fillIndexes(tuple);
119     }
120     private void fillIndexes(Tuple tuple) {
121         if (this.indexesSpec != null && !this.indexesSpec.isEmpty()) {
122             if (this.indexes == null)
123                 this.indexes = new ArrayList JavaDoc(indexesSpec.size());
124             for (int i = 0; i < this.indexesSpec.size(); i++) {
125                 ArrayList JavaDoc indexspeci = (ArrayList JavaDoc) this.indexesSpec.get(i);
126                 TupleKey valuelist = new TupleKey(indexspeci.size(), ((XQueryExpression) indexspeci.get(indexspeci.size() - 1)).getOrder(0));
127                 for (int j = 0; j < indexspeci.size(); j++) {
128                     XQueryExpression indexspecistrj = (XQueryExpression) indexspeci.get(j);
129                     ArrayList JavaDoc tmpnodes = tuple.getPath(indexspecistrj.getStringValue());
130                     if (tmpnodes != null)
131                         valuelist.add(((TypedNode) tmpnodes.get(0)).getExtendedTypedValue());
132                 }
133                 if (valuelist.isEmpty())
134                     continue;
135                 if (this.getIndex(i) == null)
136                     this.addIndex(new SortedKeyMap());
137                 SortedKeyMap sortedmap = this.getIndex(i);
138                 ArrayList JavaDoc tuplelist = (ArrayList JavaDoc) sortedmap.get(valuelist);
139                 if (tuplelist == null) {
140                     tuplelist = new ArrayList JavaDoc(1);
141                     sortedmap.put(valuelist, tuplelist);
142                 }
143                 tuplelist.add(tuple);
144             }
145         }
146     }
147
148     public void removeLast() {
149         super.remove(size() - 1);
150     }
151     //----------------------------------------------------
152
public int getPos(String JavaDoc path) {
153         for (int i = 0; i < pathlist.size(); i++) {
154             String JavaDoc pathi = (String JavaDoc) pathlist.get(i);
155             if (pathi.equals(path))
156                 return i;
157         }
158         return -1;
159     }
160     public Tuple newTuple() {
161         Tuple newTuple = new Tuple(pathlist.size(), idsize);
162         newTuple.setParent(this);
163         return newTuple;
164     }
165     // public Tuple newNullTuple() {
166
// return new Tuple(stepslist.size(),idsize);
167
// }
168

169     // methods for indexes specification handling
170
public void setIndexesSpec(ArrayList JavaDoc indexesSpec) {
171         this.indexesSpec = indexesSpec;
172     }
173     public ArrayList JavaDoc getIndexesSpec() {
174         return this.indexesSpec;
175     }
176     public ArrayList JavaDoc getIndexSpec(int index) {
177         return (ArrayList JavaDoc) this.indexesSpec.get(index);
178     }
179     public int addIndexSpec(ArrayList JavaDoc list) {
180         if (this.indexesSpec == null)
181             this.indexesSpec = new ArrayList JavaDoc();
182         this.indexesSpec.add(list);
183         return this.indexesSpec.size();
184     }
185     // methods for indexes handling
186
public ArrayList JavaDoc getIndexes() {
187         return this.indexes;
188     }
189     public SortedKeyMap getIndex(int index) {
190         if (this.indexes == null)
191             return null;
192         if (this.indexes.size() <= index)
193             return null;
194         return (SortedKeyMap) this.indexes.get(index);
195     }
196     public int addIndex(SortedKeyMap map) {
197         if (this.indexes == null)
198             this.indexes = new ArrayList JavaDoc();
199         this.indexes.add(map);
200         return this.indexes.size();
201     }
202     // ***********************************************************************
203
// * DEBUG
204
// ***********************************************************************
205
/**
206      *
207      */

208     public String JavaDoc toString() {
209         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
210         for (int i = 0; i < pathlist.size(); i++) {
211             sb.append(pathlist.get(i));
212             sb.append("\t");
213         }
214         sb.append("\n---------------------------------------------------\n");
215         for (int i = 0; i < size(); i++) {
216             sb.append(((Tuple) get(i)).toCompleteString());
217             sb.append("\n-----------------------------------------------\n");
218         }
219         return sb.toString();
220     }
221 }
222
Popular Tags