Api_test.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 api_test;
17 
18 
19 import java.io.BufferedReader;
20 import java.io.FileReader;
21 import java.net.URI;
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.*;
27 
28 public class Api_test {
29 
30  static
31  {
32  System.loadLibrary ( "zorba_api" );
33  }
34 
35  static boolean example_1() throws XQException
36  {
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();
42  xqc.close();
43  xqc.close();
44  return true;
45  }
46 
47  static boolean example_2 () throws XQException
48  {
49  StringBuilder strBuilder = new StringBuilder();
50  try {
51  BufferedReader in = new BufferedReader(new FileReader("managers/module1.xq"));
52  String str;
53  while ((str = in.readLine()) != null) {
54  strBuilder.append(str);
55  }
56  in.close();
57  } catch (Exception e) {
58  throw new XQException("Error reading file for test: " + e.getLocalizedMessage());
59  }
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;
67  URI uri;
68  QName qname;
69  XQItemType type = null;
70  try {
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());
76  }
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);
82  xqc.close();
83  return resultAdding && resultDeleting;
84  }
85 
86  static boolean example_3a() throws XQException
87  {
88  StringBuilder strBuilder = new StringBuilder();
89  try {
90  BufferedReader in = new BufferedReader(new FileReader("managers/module1.xq"));
91  String str;
92  while ((str = in.readLine()) != null) {
93  strBuilder.append(str);
94  }
95  in.close();
96  } catch (Exception e) {
97  throw new XQException("Error reading file for test: " + e.getLocalizedMessage());
98  }
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();
104  URI uri;
105  QName qname;
106  XQItemType type = null;
107  try {
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());
113  }
114  XQItem colName = xqc.createItemFromString("coll", type);
115  colManager.createCollection(colName);
116  ZorbaXQCollection collection = colManager.getCollection(colName);
117 
118  ZorbaXQXmlDataManager manager = xqc.getXmlDataManager();
119  XQSequence data = manager.parseXML("<books><book>Book 1</book><book>Book 2</book></books>");
120  collection.insertNodesFirst(data);
121 
122  colManager.deleteCollection(colName);
123  boolean resultDeleting = !colManager.isAvailableCollection(colName);
124  xqc.close();
125  return resultDeleting;
126  }
127 
128  static boolean example_3b() throws XQException
129  {
130  StringBuilder strBuilder = new StringBuilder();
131  try {
132  BufferedReader in = new BufferedReader(new FileReader("managers/module1.xq"));
133  String str;
134  while ((str = in.readLine()) != null) {
135  strBuilder.append(str);
136  }
137  in.close();
138  } catch (Exception e) {
139  throw new XQException("Error reading file for test: " + e.getLocalizedMessage());
140  }
141  InMemoryStore store = InMemoryStore.getInstance();
142  Zorba zorba = Zorba.getInstance ( store );
143  XQuery query = zorba.compileQuery(strBuilder.toString());
144  StaticCollectionManager manager = query.getStaticCollectionManager();
145 
146 
147  ItemFactory factory = zorba.getItemFactory();
148  Item name = factory.createQName("http://www.mod2.com/", "coll");
149 
150  manager.createCollection(name);
151  Collection collection = manager.getCollection(name);
152 
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);
157 
158  zorba.shutdown();
159  InMemoryStore.shutdown ( store );
160 
161  return true;
162  }
163 
164  static boolean example_4() throws XQException
165  {
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();
171  xqc.close();
172  xqc.close();
173  return true;
174  }
175 
176  static boolean example_5() throws XQException
177  {
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;
184  URI uri;
185  QName qname;
186  XQItemType type = null;
187  try {
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());
193  }
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);
199  xqc.close();
200  return resultAdding && resultDeleting;
201  }
202  static boolean example_6a() throws XQException
203  {
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());
208 
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;
214  //Item data = new Item();
215  if (resultAdding) {
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));
219  }
220  collection.delete();
221 
222  zorba.shutdown();
223  InMemoryStore.shutdown ( store );
224  return true;
225  }
226 
227  static boolean example_6b() throws XQException
228  {
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();
233  URI uri;
234  QName qname;
235  XQItemType type = null;
236  try {
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());
242  }
243  XQItem colName = xqc.createItemFromString("col2", type);
244  colManager.createCollection(colName);
245  ZorbaXQCollection collection = colManager.getCollection(colName);
246  colName.close();
247  XQSequence data = xmlManager.parseXML("<books><book>Book 1</book><book>Book 2</book></books>");
248  collection.insertNodesLast(data);
249  xqc.close();
250  return true;
251  }
252 
253  public static void main ( String argv[] ) throws XQException
254  {
255  boolean res = false;
256 
257  System.out.println ("executing test 1" );
258  res = example_1( );
259  if ( !res )
260  System.exit ( 1 );
261 
262  System.out.println ( "executing test 2" );
263  res = example_2 ( );
264  if (!res)
265  System.exit ( 1 );
266 
267  System.out.println ( "executing test 3a" );
268  res = example_3a ( );
269  if (!res)
270  System.exit ( 1 );
271 
272  System.out.println ( "executing test 3b" );
273  res = example_3b ( );
274  if (!res)
275  System.exit ( 1 );
276 
277  System.out.println ( "executing test 4" );
278  res = example_4 ( );
279  if (!res)
280  System.exit ( 1 );
281 
282  System.out.println ( "executing test 5" );
283  res = example_5 ( );
284  if (!res)
285  System.exit ( 1 );
286  System.out.println ( "executing test 6a" );
287  res = example_6a ( );
288  if (!res)
289  System.exit ( 1 );
290 
291  System.out.println ( "executing test 6b" );
292  res = example_6b ( );
293  if (!res)
294  System.exit ( 1 );
295  System.out.println ( "done." );
296 
297  } // main
298 
299 } // class Test_Zorba
300 
blog comments powered by Disqus