KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > algebra > MapItem


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.extractor.algebra;
24
25 import java.io.DataOutputStream JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.xquark.extractor.metadata.ExtractorMappingInfo;
29 import org.xquark.jdbc.typing.TypeMap;
30 import org.xquark.schema.SimpleType;
31 import org.xquark.xquery.parser.QName;
32 import org.xquark.xquery.parser.XQueryException;
33 import org.xquark.xquery.parser.XQueryExpression;
34
35 public final class MapItem implements Cloneable JavaDoc {
36
37     private static final String JavaDoc RCSRevision = "$Revision: 1.20 $";
38     private static final String JavaDoc RCSName = "$Name: $";
39
40
41     private String JavaDoc _path = null;
42     private QName _qName = null;
43     private String JavaDoc _itemName = null;
44
45     /* corresponding Expression in (most likely) UnOpProject*/
46     private Expression _relatedExpression = null;
47     
48     private ExtractorMappingInfo _mappingInfo = null;
49     
50     /** Path list of children */
51     private List JavaDoc _children = null;
52
53     /** Result of the {@link Mapper#optimize()} method are stored here by
54      * {@link org.xquark.extractor.runtime.SynchronizedResultSet}. These are the map
55      * items corresponding to the paths contained in @link #_children
56      */

57     private List JavaDoc _childChildItems = null;
58     
59     /**
60      * in the case of sorted outer union, this BitMap indicate which parts
61      * this MapItem participates
62      */

63     private long _partOf = 0;
64
65
66     public MapItem()
67     {}
68
69     public Object JavaDoc clone () throws CloneNotSupportedException JavaDoc
70     {
71         MapItem retVal = (MapItem)super.clone();
72         retVal.setPath((getPath() == null) ? null : new String JavaDoc(getPath()) );
73         // change LARS 02/03/04
74
//retVal.setQName((getQName() == null) ? null : (QName)getQName().clone(typeVisitor));
75
try {
76             retVal.setQName((getQName() == null) ? null : new QName(getQName().getNameSpace(),getQName().getPrefixName(),getQName().getLocalName(),null, null));
77         } catch (XQueryException xqe) {
78             throw new CloneNotSupportedException JavaDoc("could not clone : " + retVal.getQName());
79         }
80         retVal.setItemName((getItemName() == null) ? null : new String JavaDoc(getItemName()) );
81         retVal.setRelatedExpression((getRelatedExpression() == null) ? null : (Expression)getRelatedExpression().clone());
82         retVal.setChildren(AlgebraTools.clone(getChildren()));
83         return retVal;
84     }
85
86     public boolean equals(Object JavaDoc o) {
87         if (o == this)
88             return true;
89         if (!(o instanceof MapItem))
90             return false;
91
92         MapItem p = (MapItem) o;
93         if ( ! getPath().equals(p.getPath()))
94             return false;
95         return true;
96     }
97
98     public MapItem (String JavaDoc path, QName qName,String JavaDoc itemName,List JavaDoc children)
99     {
100         _path = path;
101         _qName = qName;
102         _itemName = itemName;
103         _children = children;
104
105     }
106
107     public String JavaDoc getPath()
108     {
109         return _path;
110     }
111
112     public void setPath (String JavaDoc path)
113     {
114         _path = path;
115     }
116
117     public void setQName (QName qName)
118     {
119         _qName = qName;
120     }
121
122     public QName getQName() {
123         return _qName;
124     }
125
126     public String JavaDoc getElementName() {
127         if (null!=_qName) {
128             return _qName.getLocalName();
129         }
130         else {
131             return null;
132         }
133     }
134
135     public String JavaDoc getNamespace() {
136         if (null!=_qName) {
137             return _qName.getNameSpace();
138         }
139         else {
140             return null;
141         }
142     }
143
144
145     public String JavaDoc getItemName()
146     {
147         return _itemName;
148     }
149
150     public void setItemName (String JavaDoc itemName)
151     {
152         _itemName = itemName;
153     }
154
155     public Expression getRelatedExpression()
156     {
157         return _relatedExpression;
158     }
159
160     public void setRelatedExpression (Expression relatedExpression)
161     {
162         _relatedExpression = relatedExpression;
163     }
164
165     public boolean isLeafPath()
166     {
167         return null == _children;
168     }
169
170     public List JavaDoc getChildren()
171     {
172         return _children;
173     }
174
175     public void setChildren(List JavaDoc children)
176     {
177         _children = children;
178     }
179
180     public List JavaDoc getChildItemList()
181     {
182         return _childChildItems;
183     }
184
185     public void setChildItemLis(List JavaDoc childChildItemLis)
186     {
187         _childChildItems = childChildItemLis;
188     }
189
190     public String JavaDoc pprint ()
191     {
192         char[] spaces =
193             (new String JavaDoc(" ")).toCharArray();
194         StringBuffer JavaDoc retVal = new StringBuffer JavaDoc ();
195
196         retVal.append( _path);
197         if (_path.length()< 40) {
198             retVal.append(spaces, 0, 40 - _path.length());
199         }
200         retVal.append("\t") ;
201
202         if (null != _qName) {
203             String JavaDoc qname = _qName.toString();
204             retVal.append( qname);
205             if (qname.length()< 40) {
206                retVal.append(spaces, 0,40- qname.length());
207             }
208         }
209         else {
210             retVal.append( spaces,0,40);
211         }
212         retVal.append("\t") ;
213
214         if (null != _itemName) {
215             retVal.append( _itemName);
216             if (_itemName.length()< 40) {
217                retVal.append(spaces, 0, 40 - _itemName.length());
218             }
219         }
220         else {
221             retVal.append( spaces,0,40);
222         }
223         retVal.append("\t") ;
224
225         retVal.append(Long.toBinaryString(_partOf));
226         retVal.append("\t") ;
227
228         if (null!=_children) {
229             retVal.append ("[");
230             for (int i = 0; i < _children.size(); i++) {
231                 retVal.append(_children.get(i));
232                 retVal.append(",\t");
233             }
234             retVal.append ("]");
235         }
236         else {
237             retVal.append("[null]");
238         }
239
240         return retVal.toString();
241     };
242
243     public void write(DataOutputStream JavaDoc dos) throws java.io.IOException JavaDoc
244     {
245         //Trace.enter(this,"write(DataOutputStream dos)");
246

247         String JavaDoc newLine = System.getProperty("line.separator");
248
249         dos.writeBytes("<MapItem>");
250         dos.writeBytes(newLine);
251             if (null != _path) {
252                 dos.writeBytes("<Path>");
253                 dos.writeBytes(_path);
254                 dos.writeBytes("</Path>");
255                 dos.writeBytes(newLine);
256             }
257
258             if (null != _qName) {
259                 dos.writeBytes("<ElementName>");
260                 dos.writeBytes(_qName.toString());
261                 dos.writeBytes("</ElementName>");
262                 dos.writeBytes(newLine);
263             }
264
265             if (null != _itemName) {
266                 dos.writeBytes("<ItemName>");
267                 dos.writeBytes(_itemName);
268                 dos.writeBytes("</ItemName>");
269                 dos.writeBytes(newLine);
270             }
271
272             if (null != _children) {
273                 dos.writeBytes("<Children>");
274                 dos.writeBytes(newLine);
275                 for (int i = 0; i < _children.size(); i++) {
276                     dos.writeBytes("<Child>");
277                     dos.writeBytes((String JavaDoc)_children.get(i));
278                     dos.writeBytes("</Child>");
279                     dos.writeBytes(newLine);
280                 }
281                 dos.writeBytes("</Children>");
282                 dos.writeBytes(newLine);
283             }
284
285         dos.writeBytes("</MapItem>");
286         dos.writeBytes(newLine);
287
288         //Trace.exit(this,"write(DataOutputStream dos)");
289
}
290
291
292
293     private String JavaDoc fillUp (String JavaDoc str, int length)
294     {
295         return null;
296     }
297
298     public boolean isPartOf(long parts)
299     {
300         return (_partOf & parts) > 0 ;
301     }
302     public void participate (long parts)
303     {
304         _partOf = _partOf | parts;
305     }
306     public long getPart()
307     {
308         return _partOf;
309     }
310     public void setPart(long parts)
311     {
312         _partOf = parts;
313     }
314
315     /**
316      * Returns and generate if necessary a MappingInfo object.
317      * NOTE: Could retrieve the ExtractorMappingInfo generated for metadata.
318      */

319     public ExtractorMappingInfo getMappingInfo() {
320         return _mappingInfo;
321     }
322     public ExtractorMappingInfo getMappingInfo(TypeMap typeMap) throws XQueryException {
323         if (_mappingInfo == null) {
324             Expression algebraExpr = _relatedExpression;
325             // First try to find column mapping info (with column metadata) in
326
// order to find precise info to get data from JDBC
327
if (algebraExpr instanceof RenameItem)
328                 algebraExpr = ((RenameItem)algebraExpr).getOperand();
329             
330             if (algebraExpr instanceof AttributeExpression)
331                 algebraExpr = ((AttributeExpression)algebraExpr).getUnderlyinExpr();
332             
333             if (algebraExpr instanceof Attribute) {
334                 _mappingInfo = ((Attribute)algebraExpr).getAttribute().getMappingInfo();
335             }
336             else { // Normally computed attributes : create dummy mapping info (could do better ?)
337
XQueryExpression xqExpr = _relatedExpression.getOrginalXExpr();
338                 if (xqExpr != null) { // for pseudo-id
339
SimpleType sType = xqExpr.getQType().getSimpleType();
340                     if (sType != null)
341                         _mappingInfo = new ExtractorMappingInfo(sType, typeMap);
342                 }
343             }
344         }
345         if (_mappingInfo != null)
346             _mappingInfo.checkForExtraction();
347         return _mappingInfo;
348     }
349
350 }
351
352
Popular Tags