2
* Copyright 2006-2012 The FLWOR Foundation.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
16
package org.zorbaxquery.api.xqj;
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;
27
* Using the ZorbaXQXmlDataManager one can manage documents and collections.
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.
32
public class ZorbaXQXmlDataManager {
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>();
41
protected ZorbaXQXmlDataManager(Zorba zorba) {
42
dm = zorba.getXmlDataManager();
45
/** \brief Close the XmlDataManager and release all the resources associated with this item.
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.
49
* @throw XQException - if there is an error during closing the item
51
public void close() throws XQException {
52
if (lDocumentManager!=null) {
53
lDocumentManager.close();
55
if (lCollectionManager!=null) {
56
lCollectionManager.close();
58
if (lW3CollectionManager!=null) {
59
lW3CollectionManager.close();
61
for (ZorbaXQSequence exp : sequences ){
62
exp.close(); // Notify the dependents objects to close
70
/** \brief Checks if the ZorbaXQXmlDataManager is closed.
72
* Checks if the ZorbaXQXmlDataManager is closed.
74
* @return boolean true if the ZorbaXQXmlDataManager is in a closed state, false otherwise
76
public boolean isClosed() {
80
public ZorbaXQDocumentManager getDocumentManager() throws XQException {
81
isClosedXQException();
82
if (lDocumentManager==null) {
83
lDocumentManager = new ZorbaXQDocumentManager(dm.getDocumentManager());
85
return lDocumentManager;
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.
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.
93
* @return The collection manager responsible for managing collections.
95
public ZorbaXQCollectionManager getCollectionManager() throws XQException {
96
isClosedXQException();
97
if (lCollectionManager==null) {
98
lCollectionManager = new ZorbaXQCollectionManager(dm.getCollectionManager());
100
return lCollectionManager;
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.
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.
108
* @return The collection manager responsible for managing collections.
110
public ZorbaXQCollectionManager getW3CCollectionManager() throws XQException {
111
isClosedXQException();
112
if (lW3CollectionManager==null) {
113
lW3CollectionManager = new ZorbaXQCollectionManager(dm.getW3CCollectionManager());
115
return lW3CollectionManager;
118
/** \brief Parse an XML document and return an ZorbaXQSequence.
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);
129
private void isClosedXQException() throws XQException {
131
throw new XQException("XmlDataManager is closed");