ZorbaXQCollection.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 javax.xml.xquery.XQException;
20 import javax.xml.xquery.XQItem;
21 import javax.xml.xquery.XQSequence;
22 import org.zorbaxquery.api.Collection;
23 import org.zorbaxquery.api.ItemSequence;
24 
25 /** \brief A Collection is a persistent sequence of node items.
26  *
27  * Instances of this class can be used to modify or retrieve the contents
28  * of a collection.
29  *
30  */
31 public class ZorbaXQCollection {
32 
33  private boolean closed = false;
34  private Collection collection = null;
35  private java.util.Collection<XQSequence> sequences = new ArrayList<XQSequence>();
36 
37  protected ZorbaXQCollection(Collection col) {
38  collection = col;
39  }
40 
41  /** \brief Closes the collection.
42  *
43  * Once the collection is closed, no method other than close or the isClosed method may be called on the collection object. Calling close on an ZorbaXQCollection object that is already closed has no effect.
44  *
45  * @throw XQException - if there is an error during closing the collection.
46  */
47  public void close() throws XQException {
48  for (XQSequence exp : sequences ){
49  exp.close(); // Notify the dependents objects to close
50  }
51  if (collection!=null) {
52  collection.delete();
53  }
54  closed = true;
55  }
56 
57  /** \brief Checks if the collection is closed.
58  *
59  * @return true if the collection is in a closed state, false otherwise
60  */
61  public boolean isClosed() {
62  return closed;
63  }
64 
65  /**
66  * \brief This function returns the sequence of nodes of the collection.
67  *
68  * @return The sequence contained in the given collection.
69  *
70  */
71  public XQSequence contents() throws XQException {
72  isClosedXQException();
73  XQSequence result = new org.zorbaxquery.api.xqj.ZorbaXQSequence(collection.contents().getIterator());
74  sequences.add(result);
75  return result;
76  }
77 
78  /**
79  * \brief This function deletes the first node from a collection.
80  *
81  * @throw XQException if the collection doesn't contain any node.
82  *
83  */
84  public void deleteNodeFirst() throws XQException {
85  isClosedXQException();
86  collection.deleteNodeFirst();
87  }
88 
89  /**
90  * \brief This function deletes the last node from a collection.
91  *
92  * @throw XQException if the collection doesn't contain any node.
93  *
94  */
95  public void deleteNodeLast() throws XQException {
96  isClosedXQException();
97  collection.deleteNodeLast();
98  }
99 
100  /**
101  * \brief This function deletes zero of more nodes from a collection.
102  *
103  * @param aNodes the nodes in the collection that should be deleted.
104  *
105  * @throw XQException if any nodes in the given sequence is not a member of a collection
106  * or not all nodes of the sequence belong to the same collection.
107  *
108  */
109  public void deleteNodes(XQSequence aNodes ) throws XQException {
110  isClosedXQException();
111  try {
112  ItemSequence sequence = ((org.zorbaxquery.api.xqj.ZorbaXQSequence)aNodes).getItemSequence();
113  collection.deleteNodes(sequence);
114  } catch (XQException e) {
115  throw e;
116  }
117  }
118 
119  /**
120  * \brief This function deletes the n first nodes from a collection.
121  *
122  * @throw XQException if the collection doesn't contain any node.
123  *
124  */
125  public void deleteNodesFirst(long aNumNodes ) throws XQException {
126  isClosedXQException();
127  collection.deleteNodesFirst(aNumNodes);
128  }
129 
130  /**
131  * \brief This function deletes the n last nodes from a collection.
132  *
133  * @throw XQException if the collection doesn't contain any node.
134  *
135  */
136  public void deleteNodesLast(long aNumNodes ) throws XQException {
137  isClosedXQException();
138  collection.deleteNodesLast(aNumNodes);
139  }
140 
141  /**
142  * \brief Get the name of the collection.
143  *
144  * @return The name of the collection.
145  */
146  public String getName() throws XQException {
147  isClosedXQException();
148  return collection.getName().getStringValue();
149  }
150 
151  /**
152  * \brief Retrieves the sequence type for this (static declared) collection.
153  *
154  * @return the sequence type for the said collection, or 0
155  * if this collection is not statically declared.
156  *
157  * @see isStatic()
158  */
159  public ZorbaXQItemType getType() throws XQException {
160  isClosedXQException();
161  return new ZorbaXQItemType(collection.getType());
162  }
163 
164  /**
165  * \brief This function returns the index of the given node in the collection.
166  *
167  * @param aNode The node to retrieve the index from.
168  *
169  * @return Returns the position of the given node in the collection.
170  *
171  * @throw XQException if node is not contained in any collection.
172  *
173  */
174  public long indexOf(XQItem aNode ) throws XQException {
175  isClosedXQException();
176  return collection.indexOf(((org.zorbaxquery.api.xqj.ZorbaXQItem)aNode).getZorbaItem());
177  }
178 
179  /**
180  * This function inserts copies of the given
181  * nodes into a collection at the position directly following the
182  * given target node.
183  *
184  * @param aTarget the node in the collection after which the
185  * sequence should be inserted.
186  * @param aNodes The sequences of nodes whose copies should
187  * be added to the collection.
188  *
189  * @throw XQException if any nodes in the sequence is not a member of a collection
190  * or not all nodes of the sequence belong to the same collection.
191  *
192  */
193  public void insertNodesAfter(XQItem aTarget, XQSequence aNodes ) throws XQException {
194  isClosedXQException();
195  try {
196  ItemSequence sequence = ((org.zorbaxquery.api.xqj.ZorbaXQSequence)aNodes).getItemSequence();
197  collection.insertNodesAfter(((org.zorbaxquery.api.xqj.ZorbaXQItem)aTarget).getZorbaItem(), sequence);
198  } catch (XQException e) {
199  throw e;
200  }
201  }
202 
203  /**
204  * This function inserts copies of the given
205  * nodes into a collection at the position directly preceding the
206  * given target node.
207  *
208  * @param aTarget the node in the collection before which the
209  * sequence should be inserted.
210  * @param aNodes The sequences of nodes whose copies should
211  * be added to the collection.
212  *
213  * @throw XQException if any nodes in the sequence is not a member of a collection
214  * or not all nodes of the sequence belong to the same collection.
215  *
216  */
217  public void insertNodesBefore(XQItem aTarget, XQSequence aNodes ) throws XQException {
218  isClosedXQException();
219  try {
220  ItemSequence sequence = ((org.zorbaxquery.api.xqj.ZorbaXQSequence)aNodes).getItemSequence();
221  collection.insertNodesBefore(((org.zorbaxquery.api.xqj.ZorbaXQItem)aTarget).getZorbaItem(), sequence);
222  } catch (XQException e) {
223  throw e;
224  }
225  }
226 
227  /**
228  * This function inserts copies of the
229  * given nodes at the beginning of the collection.
230  *
231  * @param aNodes The sequences of nodes whose copies
232  * should be added to the collection.
233  *
234  */
235  public void insertNodesFirst(XQSequence aNodes ) throws XQException {
236  isClosedXQException();
237  try {
238  ItemSequence sequence = ((org.zorbaxquery.api.xqj.ZorbaXQSequence)aNodes).getItemSequence();
239  collection.insertNodesFirst(sequence);
240  } catch (XQException e) {
241  throw e;
242  }
243  }
244 
245  /**
246  * This function inserts copies of the
247  * given nodes at the end of the collection.
248  *
249  * @param aNodes The sequences of nodes whose copies
250  * should be added to the collection.
251  *
252  */
253  public void insertNodesLast(XQSequence aNodes ) throws XQException {
254  isClosedXQException();
255  try {
256  ItemSequence sequence = ((org.zorbaxquery.api.xqj.ZorbaXQSequence)aNodes).getItemSequence();
257  collection.insertNodesLast(sequence);
258  } catch (XQException e) {
259  throw e;
260  }
261  }
262 
263  /**
264  * The function checks if this collection has been statically declared.
265  *
266  * @return true if the collection is a static collection, false otherwise.
267  */
268  public boolean isStatic() throws XQException {
269  isClosedXQException();
270  return collection.isStatic();
271  }
272 
273  private void isClosedXQException() throws XQException {
274  if (closed) {
275  throw new XQException("Collection is closed");
276  }
277  }
278 
279 
280 }
blog comments powered by Disqus