19 import java.io.BufferedReader;
20 import java.io.FileReader;
22 import java.net.URISyntaxException;
23 import javax.xml.namespace.QName;
24 import javax.xml.xquery.*;
25 import org.zorbaxquery.api.*;
26 import org.zorbaxquery.api.xqj.*;
32 System.loadLibrary (
"zorba_api" );
35 static boolean example_1()
throws XQException
37 ZorbaXQDataSource xqds =
new ZorbaXQDataSource();
38 XQConnection xqc = xqds.getConnection();
39 XQExpression xqe = xqc.createExpression();
40 org.zorbaxquery.api.xqj.ZorbaXQResultSequence xqs = (org.zorbaxquery.api.xqj.ZorbaXQResultSequence) xqe.executeQuery(
"1,2,3");
41 ZorbaXQStaticCollectionManager colManager = xqs.getStaticCollectionManager();
47 static boolean example_2 ()
throws XQException
49 StringBuilder strBuilder =
new StringBuilder();
51 BufferedReader in =
new BufferedReader(
new FileReader(
"managers/module1.xq"));
53 while ((str = in.readLine()) != null) {
54 strBuilder.append(str);
57 }
catch (Exception e) {
58 throw new XQException(
"Error reading file for test: " + e.getLocalizedMessage());
60 ZorbaXQDataSource xqds =
new ZorbaXQDataSource();
61 XQConnection xqc = xqds.getConnection();
62 XQExpression xqe = xqc.createExpression();
63 org.zorbaxquery.api.xqj.ZorbaXQResultSequence xqs = (org.zorbaxquery.api.xqj.ZorbaXQResultSequence) xqe.executeQuery(strBuilder.toString());
64 ZorbaXQStaticCollectionManager colManager = xqs.getStaticCollectionManager();
65 boolean resultAdding =
false;
66 boolean resultDeleting =
true;
69 XQItemType type = null;
71 uri =
new URI(
"http://www.mod2.com/");
72 qname =
new QName(
"http://www.mod2.com/",
"coll");
73 type = xqc.createAtomicType( XQItemType.XQBASETYPE_QNAME, qname, uri);
74 }
catch (URISyntaxException e) {
75 throw new XQException(
"Error creating QName: " + e.getLocalizedMessage());
77 XQItem colName = xqc.createItemFromString(
"coll", type);
78 colManager.createCollection(colName);
79 resultAdding = colManager.isAvailableCollection(colName);
80 colManager.deleteCollection(colName);
81 resultDeleting = !colManager.isAvailableCollection(colName);
83 return resultAdding && resultDeleting;
86 static boolean example_3a()
throws XQException
88 StringBuilder strBuilder =
new StringBuilder();
90 BufferedReader in =
new BufferedReader(
new FileReader(
"managers/module1.xq"));
92 while ((str = in.readLine()) != null) {
93 strBuilder.append(str);
96 }
catch (Exception e) {
97 throw new XQException(
"Error reading file for test: " + e.getLocalizedMessage());
99 ZorbaXQDataSource xqds =
new ZorbaXQDataSource();
100 org.zorbaxquery.api.xqj.ZorbaXQConnection xqc = (org.zorbaxquery.api.xqj.ZorbaXQConnection) xqds.getConnection();
101 XQExpression xqe = xqc.createExpression();
102 org.zorbaxquery.api.xqj.ZorbaXQResultSequence xqs = (org.zorbaxquery.api.xqj.ZorbaXQResultSequence) xqe.executeQuery(strBuilder.toString());
103 ZorbaXQStaticCollectionManager colManager = xqs.getStaticCollectionManager();
106 XQItemType type = null;
108 uri =
new URI(
"http://www.mod2.com/");
109 qname =
new QName(
"http://www.mod2.com/",
"coll");
110 type = xqc.createAtomicType( XQItemType.XQBASETYPE_QNAME, qname, uri);
111 }
catch (URISyntaxException e) {
112 throw new XQException(
"Error creating QName: " + e.getLocalizedMessage());
114 XQItem colName = xqc.createItemFromString(
"coll", type);
115 colManager.createCollection(colName);
116 ZorbaXQCollection collection = colManager.getCollection(colName);
118 ZorbaXQXmlDataManager manager = xqc.getXmlDataManager();
119 XQSequence data = manager.parseXML(
"<books><book>Book 1</book><book>Book 2</book></books>");
120 collection.insertNodesFirst(data);
122 colManager.deleteCollection(colName);
123 boolean resultDeleting = !colManager.isAvailableCollection(colName);
125 return resultDeleting;
128 static boolean example_3b()
throws XQException
130 StringBuilder strBuilder =
new StringBuilder();
132 BufferedReader in =
new BufferedReader(
new FileReader(
"managers/module1.xq"));
134 while ((str = in.readLine()) != null) {
135 strBuilder.append(str);
138 }
catch (Exception e) {
139 throw new XQException(
"Error reading file for test: " + e.getLocalizedMessage());
141 InMemoryStore store = InMemoryStore.getInstance();
142 Zorba zorba = Zorba.getInstance ( store );
143 XQuery query = zorba.compileQuery(strBuilder.toString());
144 StaticCollectionManager manager = query.getStaticCollectionManager();
147 ItemFactory factory = zorba.getItemFactory();
148 Item name = factory.createQName(
"http://www.mod2.com/",
"coll");
150 manager.createCollection(name);
151 Collection collection = manager.getCollection(name);
153 XmlDataManager xmlManager = zorba.getXmlDataManager();
154 Item data = xmlManager.parseXMLtoItem(
"<books><book>Book 1</book><book>Book 2</book></books>");
155 ItemSequence sequence =
new ItemSequence(data);
156 collection.insertNodesLast(sequence);
159 InMemoryStore.shutdown ( store );
164 static boolean example_4()
throws XQException
166 ZorbaXQDataSource xqds =
new ZorbaXQDataSource();
167 org.zorbaxquery.api.xqj.ZorbaXQConnection xqc = (org.zorbaxquery.api.xqj.ZorbaXQConnection) xqds.getConnection();
168 XQExpression xqe = xqc.createExpression();
169 ZorbaXQXmlDataManager xmlManager = xqc.getXmlDataManager();
170 ZorbaXQCollectionManager colManager = xmlManager.getCollectionManager();
176 static boolean example_5()
throws XQException
178 ZorbaXQDataSource xqds =
new ZorbaXQDataSource();
179 org.zorbaxquery.api.xqj.ZorbaXQConnection xqc = (org.zorbaxquery.api.xqj.ZorbaXQConnection) xqds.getConnection();
180 ZorbaXQXmlDataManager xmlManager = xqc.getXmlDataManager();
181 ZorbaXQCollectionManager colManager = xmlManager.getCollectionManager();
182 boolean resultAdding =
false;
183 boolean resultDeleting =
true;
186 XQItemType type = null;
188 uri =
new URI(
"http://www.mod2.com/");
189 qname =
new QName(
"http://www.mod2.com/",
"col2");
190 type = xqc.createAtomicType( XQItemType.XQBASETYPE_QNAME, qname, uri);
191 }
catch (URISyntaxException e) {
192 throw new XQException(
"Error creating QName: " + e.getLocalizedMessage());
194 XQItem colName = xqc.createItemFromString(
"col2", type);
195 colManager.createCollection(colName);
196 resultAdding = colManager.isAvailableCollection(colName);
197 colManager.deleteCollection(colName);
198 resultDeleting = !colManager.isAvailableCollection(colName);
200 return resultAdding && resultDeleting;
202 static boolean example_6a()
throws XQException
204 InMemoryStore store = InMemoryStore.getInstance();
205 Zorba zorba = Zorba.getInstance ( store );
206 XmlDataManager xmlManager =
new XmlDataManager(zorba.getXmlDataManager());
207 CollectionManager manager =
new CollectionManager(xmlManager.getCollectionManager());
209 ItemFactory factory = zorba.getItemFactory();
210 Item name = factory.createQName(
"http://www.zorba-xquery.com/",
"aaa");
211 manager.createCollection(name);
212 boolean resultAdding = manager.isAvailableCollection(name);
213 Collection collection = null;
216 collection = manager.getCollection(name);
217 Item data = xmlManager.parseXMLtoItem(
"<books><book>Book 1</book><book>Book 2</book></books>");
218 collection.insertNodesLast(
new ItemSequence(data));
223 InMemoryStore.shutdown ( store );
227 static boolean example_6b()
throws XQException
229 ZorbaXQDataSource xqds =
new ZorbaXQDataSource();
230 org.zorbaxquery.api.xqj.ZorbaXQConnection xqc = (org.zorbaxquery.api.xqj.ZorbaXQConnection) xqds.getConnection();
231 ZorbaXQXmlDataManager xmlManager = xqc.getXmlDataManager();
232 ZorbaXQCollectionManager colManager = xmlManager.getCollectionManager();
235 XQItemType type = null;
237 uri =
new URI(
"http://www.mod2.com/");
238 qname =
new QName(
"http://www.mod2.com/",
"col2");
239 type = xqc.createAtomicType( XQItemType.XQBASETYPE_QNAME, qname, uri);
240 }
catch (URISyntaxException e) {
241 throw new XQException(
"Error creating QName: " + e.getLocalizedMessage());
243 XQItem colName = xqc.createItemFromString(
"col2", type);
244 colManager.createCollection(colName);
245 ZorbaXQCollection collection = colManager.getCollection(colName);
247 XQSequence data = xmlManager.parseXML(
"<books><book>Book 1</book><book>Book 2</book></books>");
248 collection.insertNodesLast(data);
253 public static void main ( String argv[] )
throws XQException
257 System.out.println (
"executing test 1" );
262 System.out.println (
"executing test 2" );
267 System.out.println (
"executing test 3a" );
268 res = example_3a ( );
272 System.out.println (
"executing test 3b" );
273 res = example_3b ( );
277 System.out.println (
"executing test 4" );
282 System.out.println (
"executing test 5" );
286 System.out.println (
"executing test 6a" );
287 res = example_6a ( );
291 System.out.println (
"executing test 6b" );
292 res = example_6b ( );
295 System.out.println (
"done." );