21 import java.net.URISyntaxException;
22 import java.util.Properties;
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 String getFile(String filename)
throws FileNotFoundException, IOException {
36 File file =
new File(filename);
37 FileInputStream fileStream =
new FileInputStream(file);
38 InputStreamReader reader =
new InputStreamReader( fileStream,
"UTF8" );
39 BufferedReader buffer =
new BufferedReader( reader );
41 StringBuilder builder =
new StringBuilder();
42 while ( (line = buffer.readLine()) != null )
46 return builder.toString();
49 static boolean checkResult(String expected, String result) {
50 System.out.println(
"Result:");
51 System.out.println(result);
52 System.out.println(
"Expected:");
53 System.out.println(expected);
54 return expected.equals(result);
57 static boolean test_1()
throws XQException
60 XQConnection xqc = xqds.getConnection();
61 XQExpression xqe = xqc.createExpression();
70 static boolean test_2 ()
throws XQException
72 StringBuilder strBuilder =
new StringBuilder();
74 BufferedReader in =
new BufferedReader(
new FileReader(
"managers/module1.xq"));
76 while ((str = in.readLine()) != null) {
77 strBuilder.append(str);
80 }
catch (Exception e) {
81 throw new XQException(
"Error reading file for test: " + e.getLocalizedMessage());
84 XQConnection xqc = xqds.getConnection();
85 XQExpression xqe = xqc.createExpression();
88 boolean resultAdding =
false;
89 boolean resultDeleting =
true;
92 XQItemType type = null;
94 uri =
new URI(
"http://www.mod2.com/");
95 qname =
new QName(
"http://www.mod2.com/",
"coll");
96 type = xqc.createAtomicType( XQItemType.XQBASETYPE_QNAME, qname, uri);
97 }
catch (URISyntaxException e) {
98 throw new XQException(
"Error creating QName: " + e.getLocalizedMessage());
100 XQItem colName = xqc.createItemFromString(
"coll", type);
101 colManager.createCollection(colName);
102 resultAdding = colManager.isAvailableCollection(colName);
103 colManager.deleteCollection(colName);
104 resultDeleting = !colManager.isAvailableCollection(colName);
106 return resultAdding && resultDeleting;
109 static boolean test_3a()
throws XQException
111 StringBuilder strBuilder =
new StringBuilder();
113 BufferedReader in =
new BufferedReader(
new FileReader(
"managers/module1.xq"));
115 while ((str = in.readLine()) != null) {
116 strBuilder.append(str);
119 }
catch (Exception e) {
120 throw new XQException(
"Error reading file for test: " + e.getLocalizedMessage());
124 XQExpression xqe = xqc.createExpression();
129 XQItemType type = null;
131 uri =
new URI(
"http://www.mod2.com/");
132 qname =
new QName(
"http://www.mod2.com/",
"coll");
133 type = xqc.createAtomicType( XQItemType.XQBASETYPE_QNAME, qname, uri);
134 }
catch (URISyntaxException e) {
135 throw new XQException(
"Error creating QName: " + e.getLocalizedMessage());
137 XQItem colName = xqc.createItemFromString(
"coll", type);
138 colManager.createCollection(colName);
142 XQSequence data = manager.parseXML(
"<books><book>Book 1</book><book>Book 2</book></books>");
143 collection.insertNodesFirst(data);
145 colManager.deleteCollection(colName);
146 boolean resultDeleting = !colManager.isAvailableCollection(colName);
148 return resultDeleting;
151 static boolean test_3b()
throws XQException
153 StringBuilder strBuilder =
new StringBuilder();
155 BufferedReader in =
new BufferedReader(
new FileReader(
"managers/module1.xq"));
157 while ((str = in.readLine()) != null) {
158 strBuilder.append(str);
161 }
catch (Exception e) {
162 throw new XQException(
"Error reading file for test: " + e.getLocalizedMessage());
164 InMemoryStore store = InMemoryStore.getInstance();
165 Zorba zorba = Zorba.getInstance ( store );
166 XQuery query = zorba.compileQuery(strBuilder.toString());
167 StaticCollectionManager manager = query.getStaticCollectionManager();
170 ItemFactory factory = zorba.getItemFactory();
171 Item name = factory.createQName(
"http://www.mod2.com/",
"coll");
173 manager.createCollection(name);
174 Collection collection = manager.getCollection(name);
176 XmlDataManager xmlManager = zorba.getXmlDataManager();
177 Item data = xmlManager.parseXMLtoItem(
"<books><book>Book 1</book><book>Book 2</book></books>");
178 ItemSequence sequence =
new ItemSequence(data);
179 collection.insertNodesLast(sequence);
182 InMemoryStore.shutdown ( store );
187 static boolean test_4()
throws XQException
191 XQExpression xqe = xqc.createExpression();
199 static boolean test_5()
throws XQException
205 boolean resultAdding =
false;
206 boolean resultDeleting =
true;
209 XQItemType type = null;
211 uri =
new URI(
"http://www.mod2.com/");
212 qname =
new QName(
"http://www.mod2.com/",
"col2");
213 type = xqc.createAtomicType( XQItemType.XQBASETYPE_QNAME, qname, uri);
214 }
catch (URISyntaxException e) {
215 throw new XQException(
"Error creating QName: " + e.getLocalizedMessage());
217 XQItem colName = xqc.createItemFromString(
"col2", type);
218 colManager.createCollection(colName);
219 resultAdding = colManager.isAvailableCollection(colName);
220 colManager.deleteCollection(colName);
221 resultDeleting = !colManager.isAvailableCollection(colName);
223 return resultAdding && resultDeleting;
225 static boolean test_6a()
throws XQException
227 InMemoryStore store = InMemoryStore.getInstance();
228 Zorba zorba = Zorba.getInstance ( store );
229 XmlDataManager xmlManager =
new XmlDataManager(zorba.getXmlDataManager());
230 CollectionManager manager =
new CollectionManager(xmlManager.getCollectionManager());
231 ItemFactory factory = zorba.getItemFactory();
232 Item name = factory.createQName(
"http://www.zorba-xquery.com/",
"aaa");
233 manager.createCollection(name);
234 boolean resultAdding = manager.isAvailableCollection(name);
235 Collection collection = null;
238 collection = manager.getCollection(name);
239 Item data = xmlManager.parseXMLtoItem(
"<books><book>Book 1</book><book>Book 2</book></books>");
240 collection.insertNodesLast(
new ItemSequence(data));
245 InMemoryStore.shutdown ( store );
249 static boolean test_6b()
throws XQException
257 XQItemType type = null;
259 uri =
new URI(
"http://www.mod2.com/");
260 qname =
new QName(
"http://www.mod2.com/",
"col2");
261 type = xqc.createAtomicType( XQItemType.XQBASETYPE_QNAME, qname, uri);
262 }
catch (URISyntaxException e) {
263 throw new XQException(
"Error creating QName: " + e.getLocalizedMessage());
265 XQItem colName = xqc.createItemFromString(
"col2", type);
266 colManager.createCollection(colName);
269 XQSequence data = xmlManager.parseXML(
"<books><book>Book 1</book><book>Book 2</book></books>");
270 collection.insertNodesLast(data);
275 static boolean test_7()
throws XQException
277 InMemoryStore store = InMemoryStore.getInstance();
278 Zorba zorba = Zorba.getInstance ( store );
279 String test =
"Hello world!";
280 ZorbaReaderWrapper stream =
new ZorbaReaderWrapper(
new StringReader(
"'"+test+
"'"));
281 XQuery query = zorba.compileQuery(stream);
282 SerializationOptions opts =
new SerializationOptions();
283 opts.setOmitXMLDeclaration(SerializationOptions.OmitXMLDeclaration.ZORBA_API_OMIT_XML_DECLARATION_YES);
284 String queryResult = query.execute(opts);
285 boolean result = checkResult(test, queryResult);
287 InMemoryStore.shutdown ( store );
291 static boolean test_8()
throws XQException, UnsupportedEncodingException
293 InMemoryStore store = InMemoryStore.getInstance();
294 Zorba zorba = Zorba.getInstance ( store );
295 StringBuilder test =
new StringBuilder();
296 for (
int i=0; i<20; i++) {
297 test.append(
"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
299 String queryString =
"'" + test.toString() +
"'";
300 ZorbaInputWrapper stream =
new ZorbaInputWrapper(
new ByteArrayInputStream(queryString.getBytes(
"UTF-8")));
301 XQuery query = zorba.compileQuery(stream);
302 SerializationOptions opts =
new SerializationOptions();
303 opts.setOmitXMLDeclaration(SerializationOptions.OmitXMLDeclaration.ZORBA_API_OMIT_XML_DECLARATION_YES);
304 String queryResult = query.execute(opts);
305 boolean result = checkResult(test.toString(), queryResult);
307 InMemoryStore.shutdown ( store );
311 static boolean test_9()
throws XQException, UnsupportedEncodingException
313 InMemoryStore store = InMemoryStore.getInstance();
314 Zorba zorba = Zorba.getInstance ( store );
315 String test =
"<Hello><ab/><ax/>World</Hello>";
316 ZorbaInputWrapper stream =
new ZorbaInputWrapper(
new ByteArrayInputStream(test.getBytes(
"UTF-8")));
317 SerializationOptions opts =
new SerializationOptions();
318 opts.setIndent(SerializationOptions.Indent.ZORBA_API_INDENT_NO);
319 opts.setOmitXMLDeclaration(SerializationOptions.OmitXMLDeclaration.ZORBA_API_OMIT_XML_DECLARATION_YES);
320 XQuery query = zorba.compileQuery(stream);
321 String queryResult = query.execute(opts);
322 boolean result = checkResult(test, queryResult);
325 InMemoryStore.shutdown ( store );
329 static boolean test_10()
throws XQException, UnsupportedEncodingException
331 InMemoryStore store = InMemoryStore.getInstance();
332 Zorba zorba = Zorba.getInstance ( store );
333 String test =
"<Hello><ab/><ax/>World</Hello>";
334 ZorbaInputWrapper stream =
new ZorbaInputWrapper(
new ByteArrayInputStream(test.getBytes(
"UTF-8")));
335 XQuery query = zorba.compileQuery(stream);
336 ByteArrayOutputStream out =
new ByteArrayOutputStream();
337 ZorbaOutputWrapper OStream =
new ZorbaOutputWrapper(out);
338 SerializationOptions opts =
new SerializationOptions();
339 opts.setIndent(SerializationOptions.Indent.ZORBA_API_INDENT_NO);
340 opts.setOmitXMLDeclaration(SerializationOptions.OmitXMLDeclaration.ZORBA_API_OMIT_XML_DECLARATION_YES);
341 query.execute(OStream, opts);
342 boolean result = checkResult(test, out.toString());
345 InMemoryStore.shutdown ( store );
349 static boolean test_11a()
throws XQException, FileNotFoundException, IOException
351 InMemoryStore store = InMemoryStore.getInstance();
352 Zorba zorba = Zorba.getInstance ( store );
353 String test = getFile(
"managers/utf.txt");
354 org.zorbaxquery.api.ZorbaReaderWrapper stream =
new org.zorbaxquery.api.ZorbaReaderWrapper(
new StringReader(
"'"+test+
"'"));
355 XQuery query = zorba.compileQuery(stream);
356 SerializationOptions opts =
new SerializationOptions();
357 opts.setOmitXMLDeclaration(SerializationOptions.OmitXMLDeclaration.ZORBA_API_OMIT_XML_DECLARATION_YES);
358 String queryResult = query.execute(opts);
359 boolean result = checkResult(test, queryResult);
361 InMemoryStore.shutdown ( store );
365 static boolean test_11b()
throws XQException, FileNotFoundException, IOException
367 String test = getFile(
"managers/utf.txt");
369 XQConnection xqc = xqds.getConnection();
370 XQExpression xqe = xqc.createExpression();
373 Properties prpts =
new Properties();
374 prpts.setProperty(
"omit-xml-declaration",
"yes");
375 String queryResult =xqs.getSequenceAsString(prpts);
376 boolean result = checkResult(test, queryResult);
An ZorbaXQDataSource is a factory for ZorbaXQConnection objects.
A connection (session) with a specific XQuery engine.
A Collection is a persistent sequence of node items.
This class defines a set of functions for managing persistent collections.
Using the ZorbaXQStaticCollectionManager one can retrieve information about statically declared colle...
ZorbaXQConnection getConnection()
Attempts to create a connection to an XML datasource.
This class represents a sequence of items obtained as a result of evaluation XQuery expressions...
Using the ZorbaXQXmlDataManager one can manage documents and collections.