KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > parser > primitivefunctions > fnfunctions > FunctionCOLLECTION


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.parser.primitivefunctions.fnfunctions;
24
25 import java.util.ArrayList JavaDoc;
26
27 import org.xquark.xquery.parser.*;
28 import org.xquark.xquery.typing.TypeException;
29
30 public class FunctionCOLLECTION extends InputFunctionCall {
31     
32     private static final String JavaDoc RCSRevision = "$Revision: 1.8 $";
33     private static final String JavaDoc RCSName = "$Name: $";
34     
35     private final static String JavaDoc STAR = "*";
36     private ArrayList JavaDoc sources = new ArrayList JavaDoc(1);
37     private String JavaDoc collectionName = STAR;
38     
39     // #############################################################################
40
// VISITOR STUFF
41
// #############################################################################
42

43     public void accept(ParserVisitor visitor) throws XQueryException {
44         visitor.visit(this);
45     }
46     
47     // #############################################################################
48
// CONSTRUCTOR STUFF
49
// #############################################################################
50

51     public FunctionCOLLECTION(ArrayList JavaDoc args, XQueryModule parentModule) throws TypeException, XQueryException {
52         super(new QName("collection",null),null);
53         if (args == null)
54             args = new ArrayList JavaDoc(1);
55         if (args.size() == 0) {
56             args.add(new ValueString("*:*",parentModule));
57         }
58         setArguments(args);
59         setParentModule(parentModule);
60         if (parentModule != null && parentModule.getStaticContext().getTypeVisitor() != null)
61             accept(parentModule.getStaticContext().getTypeVisitor());
62     }
63     
64     public void setArguments(ArrayList JavaDoc arguments) throws XQueryException {
65         // verify that arguments are strings
66
// of the following format "abc" or "*.abc" or "*" or "abc.*" or "abc.def" or "*.*"
67
// transforms () -> ("*.*")
68
// transforms ("*") -> ("*.*")
69
// transforms ("abc") -> ("*.abc")
70

71         if (arguments == null || arguments.size() == 0) {
72             return;
73         } else if (arguments.size() == 1) {
74             for (int i=0;i<arguments.size();i++) {
75                 ValueString valStr = null;
76                 try {
77                     valStr = (ValueString)arguments.get(i);
78                 }
79                 catch (ClassCastException JavaDoc e) {
80                     throw new XQueryException("Argument of collection function must be a literal string");
81                 }
82                 String JavaDoc arg = valStr.getValue();
83                 // change LARS 07/04/04
84
int colonIndex = -1;
85                 int startIndex = 0;
86                 while ((colonIndex = arg.indexOf(":",startIndex)) != -1) {
87                     sources.add(arg.substring(startIndex,colonIndex));
88                     startIndex = colonIndex+1;
89                 }
90                 collectionName = arg.substring(startIndex);
91                 if (sources.isEmpty()) {
92                     sources.add(STAR);
93                     valStr.setValue("*:"+arg);
94                 }
95             }
96         }
97         else {
98             throw new XQueryException("Function collection should have at most one argument");
99         }
100         super.setArguments(arguments);
101     }
102     
103     public String JavaDoc getCollectionName() {
104         return collectionName;
105     }
106     public String JavaDoc getSourceName() {
107         return (String JavaDoc)sources.get(0);
108      }
109     public void eraseSourceName() throws XQueryException {
110         sources.remove(0);
111         if (sources.isEmpty()) {
112             sources.add(STAR);
113         }
114         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
115         for (int i=0;i<sources.size();i++) {
116             buf.append(sources.get(i));
117             buf.append(":");
118         }
119         buf.append(collectionName);
120         ((ValueString)arguments.get(0)).setValue(buf.toString());
121     }
122         
123 }
124
Popular Tags