ZorbaXQStaticCollectionManager.java
Go to the documentation of this file.
1 /*
2  * Copyright 2006-2012 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.zorbaxquery.api.xqj;
17 
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import javax.xml.xquery.XQException;
21 import javax.xml.xquery.XQItem;
22 import javax.xml.xquery.XQSequence;
23 import org.zorbaxquery.api.StaticCollectionManager;
24 
25 
26  /**
27  * Using the ZorbaXQStaticCollectionManager one can retrieve information about statically declared collections and indexes as well as manage them.
28  *
29  * The ZorbaXQStaticCollectionManager can be retrieved from (1) a compiled XQuery or (2) a XQStaticContext object. In both cases, this class provides access to information for the collections and indexes that are declared in (1) all the modules (transitively) imported by the main query or (2) the module that resulted in the compilation of the StaticContext, respectively. Moreover, this class allows to create or delete such collections and indexes.
30  */
32 
33  private boolean closed = false;
34  private StaticCollectionManager collectionManager = null;
35  private Collection<XQSequence> sequences = new ArrayList<XQSequence>();
36  private Collection<ZorbaXQCollection> collections = new ArrayList<ZorbaXQCollection>();
37 
38  protected ZorbaXQStaticCollectionManager (StaticCollectionManager cm) {
39  collectionManager = cm;
40  }
41 
42  public void close() throws XQException {
43  for (XQSequence exp : sequences ){
44  exp.close(); // Notify the dependents objects to close
45  }
46  for (ZorbaXQCollection exp : collections ){
47  exp.close(); // Notify the dependents objects to close
48  }
49  closed = true;
50  }
51 
52  public boolean isClosed() {
53  return closed;
54  }
55 
56  /** \brief This function returns a sequence of names of the collections that are available.
57  *
58  * If this is an instance of the StaticCollectionManager class (i.e. returned by any of the getStaticCollectionManager methods), the collections returned by this function are also statically declared.
59  *
60  * @return ZorbaXQSequence - The list of names of the available collections.
61  */
62  public XQSequence availableCollections() throws XQException {
63  isClosedXQException();
64  XQSequence result = new org.zorbaxquery.api.xqj.ZorbaXQSequence(collectionManager.availableCollections());
65  sequences.add(result);
66  return result;
67  }
68 
69  /** \brief This function creates the collection with the given name.
70  *
71  * @param aName The name of the collection to create.
72  * @throw XQException- if a collection with the given name already exists.
73  */
74  public void createCollection(XQItem aName ) throws XQException {
75  isClosedXQException();
76  collectionManager.createCollection(((org.zorbaxquery.api.xqj.ZorbaXQItem)aName).getZorbaItem());
77  }
78 
79  /** \brief This function removes the collection with the given name.
80  *
81  * @param aName - The name of the collection to delete.
82  * @throw XQException - if the collection does not exist.
83  */
84  public void deleteCollection(XQItem aName ) throws XQException {
85  isClosedXQException();
86  collectionManager.deleteCollection(((org.zorbaxquery.api.xqj.ZorbaXQItem)aName).getZorbaItem());
87  }
88 
89  /** \brief Returns a instance of the Collection class which can be used to modify and retrieve the contents of the collection identified by the given name.
90  *
91  * @param aName - The name of the collection to retrieve.
92  * @return ZorbaXQCollection - The collection if available.
93  * @throw XQException - if the collection does not exist.
94  */
95  public ZorbaXQCollection getCollection(XQItem aName ) throws XQException {
96  isClosedXQException();
97  ZorbaXQCollection result = new ZorbaXQCollection ( collectionManager.getCollection(((org.zorbaxquery.api.xqj.ZorbaXQItem)aName).getZorbaItem()) );
98  collections.add(result);
99  return result;
100  }
101 
102  /** \brief This function returns true if a collection with the given name is available.
103  *
104  * If this is an instance of the StaticCollectionManager class (i.e. returned by any of the getStaticCollectionManager() methods), the collection also needs to be statically declared.
105  *
106  * @param aName - The name of the collection that is being checked.
107  * @return true if the collection is available and false otherwise.
108  * @throw XQException - if the Collection Manager is closed
109  */
110  public boolean isAvailableCollection(XQItem aName ) throws XQException {
111  isClosedXQException();
112  return collectionManager.isAvailableCollection( ((org.zorbaxquery.api.xqj.ZorbaXQItem)aName).getZorbaItem() );
113  }
114 
115  private void isClosedXQException() throws XQException {
116  if (closed) {
117  throw new XQException("CollectionManager is closed");
118  }
119  }
120 
121 }
ZorbaXQCollection getCollection(XQItem aName)
Returns a instance of the Collection class which can be used to modify and retrieve the contents of t...
A Collection is a persistent sequence of node items.
XQSequence availableCollections()
This function returns a sequence of names of the collections that are available.
void createCollection(XQItem aName)
This function creates the collection with the given name.
Using the ZorbaXQStaticCollectionManager one can retrieve information about statically declared colle...
This interface represents a sequence of items as defined in the XDM.
boolean isAvailableCollection(XQItem aName)
This function returns true if a collection with the given name is available.
void deleteCollection(XQItem aName)
This function removes the collection with the given name.