ZorbaXQXmlDataManager.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 org.zorbaxquery.api.ItemSequence;
22 import org.zorbaxquery.api.XmlDataManager;
23 import org.zorbaxquery.api.Zorba;
24 
25 
26  /**
27  * Using the ZorbaXQXmlDataManager one can manage documents and collections.
28  *
29  * The ZorbaXQXmlDataManager is a singleton instance. The XQConnection object is reponsible for maintaining its lifetime. The instance can be accessed by calling getXmlDataManager() on the XQConnection object. It may not be accessed anymore after XQConnection is closed.
30  *
31  */
32 public class ZorbaXQXmlDataManager {
33 
34  private boolean closed = false;
35  private XmlDataManager dm;
36  private ZorbaXQDocumentManager lDocumentManager;
37  private ZorbaXQCollectionManager lCollectionManager;
38  private ZorbaXQCollectionManager lW3CollectionManager;
39  private Collection<ZorbaXQSequence> sequences = new ArrayList<ZorbaXQSequence>();
40 
41  protected ZorbaXQXmlDataManager(Zorba zorba) {
42  dm = zorba.getXmlDataManager();
43  }
44 
45  /** \brief Close the XmlDataManager and release all the resources associated with this item.
46  *
47  * Close the XmlDataManager and release all the resources associated with this item. No method other than the isClosed or close method may be called once the XmlDataManager is closed. Calling close on an XmlDataManager object that is already closed has no effect.
48  *
49  * @throw XQException - if there is an error during closing the item
50  */
51  public void close() throws XQException {
52  if (lDocumentManager!=null) {
53  lDocumentManager.close();
54  }
55  if (lCollectionManager!=null) {
56  lCollectionManager.close();
57  }
58  if (lW3CollectionManager!=null) {
59  lW3CollectionManager.close();
60  }
61  for (ZorbaXQSequence exp : sequences ){
62  exp.close(); // Notify the dependents objects to close
63  }
64  if (dm!=null) {
65  dm.delete();
66  }
67  closed = true;
68  }
69 
70  /** \brief Checks if the ZorbaXQXmlDataManager is closed.
71  *
72  * Checks if the ZorbaXQXmlDataManager is closed.
73  *
74  * @return boolean true if the ZorbaXQXmlDataManager is in a closed state, false otherwise
75  */
76  public boolean isClosed() {
77  return closed;
78  }
79 
80  public ZorbaXQDocumentManager getDocumentManager() throws XQException {
81  isClosedXQException();
82  if (lDocumentManager==null) {
83  lDocumentManager = new ZorbaXQDocumentManager(dm.getDocumentManager());
84  }
85  return lDocumentManager;
86  }
87 
88  /** \brief Returns a CollectionManager responsible for all collections.
89  * The collection manager provides a set of functions for managing collections identified by a QName and their contents.
90  *
91  * Please note that the resulting manager is only responsible for dynamic collections identified by a QName, i.e. those that are not declared in the prolog of a module or identified by a URI.
92  *
93  * @return The collection manager responsible for managing collections.
94  */
95  public ZorbaXQCollectionManager getCollectionManager() throws XQException {
96  isClosedXQException();
97  if (lCollectionManager==null) {
98  lCollectionManager = new ZorbaXQCollectionManager(dm.getCollectionManager());
99  }
100  return lCollectionManager;
101  }
102 
103  /** \brief Returns a CollectionManager responsible for collections identified by a URI.
104  * The collection manager provides a set of functions for managing collections identified by a URI and their contents.
105  *
106  * Please note that the resulting manager is only responsible for dynamic collections identified by a URI, i.e. those that are not declared in the prolog of a module or identified by a QName.
107  *
108  * @return The collection manager responsible for managing collections.
109  */
110  public ZorbaXQCollectionManager getW3CCollectionManager() throws XQException {
111  isClosedXQException();
112  if (lW3CollectionManager==null) {
113  lW3CollectionManager = new ZorbaXQCollectionManager(dm.getW3CCollectionManager());
114  }
115  return lW3CollectionManager;
116  }
117 
118  /** \brief Parse an XML document and return an ZorbaXQSequence.
119  *
120  */
121  public ZorbaXQSequence parseXML(String xmlText) throws XQException {
122  isClosedXQException();
123  ItemSequence zSequence = new ItemSequence(dm.parseXMLtoItem(xmlText));
124  ZorbaXQSequence result = new ZorbaXQSequence(zSequence);
125  sequences.add(result);
126  return result;
127  }
128 
129  private void isClosedXQException() throws XQException {
130  if (closed) {
131  throw new XQException("XmlDataManager is closed");
132  }
133  }
134 }