16 package org.zorbaxquery.api.xqj;
18 import java.io.OutputStream;
19 import java.io.Reader;
20 import java.io.StringReader;
21 import java.io.Writer;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Properties;
26 import javax.xml.stream.XMLInputFactory;
27 import javax.xml.stream.XMLStreamException;
28 import javax.xml.stream.XMLStreamReader;
29 import javax.xml.transform.Result;
30 import javax.xml.xquery.XQConnection;
31 import javax.xml.xquery.XQException;
32 import javax.xml.xquery.XQItem;
33 import javax.xml.xquery.XQItemType;
34 import org.w3c.dom.Node;
35 import org.xml.sax.ContentHandler;
36 import org.zorbaxquery.api.Item;
37 import org.zorbaxquery.api.Iterator;
78 private boolean closed =
false;
79 private boolean delivered =
false;
81 private boolean consumedItem =
false;
82 private boolean iterDeleted =
false;
84 private XQConnection connection = null;
85 private Iterator iter = null;
87 private Collection<ZorbaXQResultItem> items =
new ArrayList<ZorbaXQResultItem>();
88 private org.zorbaxquery.api.XQuery lQuery=null;
89 private boolean preparedExpression;
94 iter = query.iterator();
97 preparedExpression = prepared;
107 isClosedXQException();
129 public boolean absolute(
int i)
throws XQException {
130 isClosedXQException();
131 throw new XQException(
"This sequence is forward-only");
140 isClosedXQException();
141 throw new XQException(
"This sequence is forward-only");
150 isClosedXQException();
151 throw new XQException(
"This sequence is forward-only");
161 public void close() throws XQException {
167 items.removeAll(items);
173 if (!preparedExpression) {
177 if (lStaticCollectionManager != null) {
178 lStaticCollectionManager.
close();
198 public int count() throws XQException {
199 isClosedXQException();
200 throw new XQException(
"This sequence is forward-only");
213 isClosedXQException();
214 throw new XQException(
"This sequence is forward-only");
226 isClosedXQException();
227 return (current!=null);
237 isClosedXQException();
249 public boolean first() throws XQException {
250 isClosedXQException();
251 throw new XQException(
"This sequence is forward-only");
265 isClosedXQException();
266 isItemDeliveredXQException();
267 isOnItemXQException();
283 isClosedXQException();
284 isConsumedXQException();
285 Properties properties = null;
287 XMLInputFactory fac = XMLInputFactory.newInstance();
288 Reader read =
new StringReader(str);
289 XMLStreamReader result = null;
291 result = fac.createXMLStreamReader(read);
292 }
catch (XMLStreamException ex) {
293 throw new XQException(
"Problem reading the stream: " + str +
" - with error: " + ex.getLocalizedMessage());
308 isClosedXQException();
309 isConsumedXQException();
310 String result = null;
326 result = lQuery.execute().replace(
">",
">").replace(
"<",
"<");
327 }
catch (Exception e) {
328 throw new XQException(
"Error getting stream: " + e.getLocalizedMessage());
330 if (!preparedExpression) {
346 isClosedXQException();
347 throw new XQException(
"This sequence is forward-only");
359 isClosedXQException();
360 throw new XQException(
"This sequence is forward-only");
372 isClosedXQException();
373 throw new XQException(
"This sequence is forward-only");
384 public boolean isLast() throws XQException {
385 isClosedXQException();
386 throw new XQException(
"This sequence is forward-only");
397 public boolean last() throws XQException {
398 isClosedXQException();
399 throw new XQException(
"This sequence is forward-only");
410 public boolean next() throws XQException {
411 isClosedXQException();
412 Item lItem =
new Item();
414 if (iter.next(lItem)) {
420 return (current != null);
432 isClosedXQException();
433 throw new XQException(
"This sequence is forward-only");
446 public boolean relative(
int i)
throws XQException {
447 isClosedXQException();
448 throw new XQException(
"This sequence is forward-only");
460 public void writeSequence(OutputStream out, Properties prprts)
throws XQException {
461 isClosedXQException();
462 isNullXQException(out);
464 getItem().writeItem(out, prprts);
467 getItem().writeItem(out, prprts);
484 public void writeSequence(Writer writer, Properties prprts)
throws XQException {
485 isClosedXQException();
486 isNullXQException(writer);
488 getItem().writeItem(writer, prprts);
491 getItem().writeItem(writer, prprts);
504 isClosedXQException();
505 isNullXQException(ch);
527 isClosedXQException();
528 isNullXQException(result);
530 getItem().writeItemToResult(result);
533 getItem().writeItemToResult(result);
546 isClosedXQException();
547 isOnItemXQException();
560 isClosedXQException();
561 isOnItemXQException();
574 isClosedXQException();
575 isOnItemXQException();
588 isClosedXQException();
589 isOnItemXQException();
602 isClosedXQException();
603 isOnItemXQException();
616 isClosedXQException();
617 isOnItemXQException();
618 return current.getItemType();
630 isClosedXQException();
631 isOnItemXQException();
632 return getItem().getAtomicValue();
644 isClosedXQException();
645 isOnItemXQException();
658 isClosedXQException();
659 isOnItemXQException();
673 isClosedXQException();
674 isOnItemXQException();
675 return current.getNodeUri();
687 isClosedXQException();
688 isOnItemXQException();
701 isClosedXQException();
702 isOnItemXQException();
703 return getItem().getItemAsStream();
716 isClosedXQException();
717 return getItem().getItemAsString(prprts);
729 isClosedXQException();
762 public boolean instanceOf(XQItemType xqit)
throws XQException {
763 isClosedXQException();
764 isOnItemXQException();
765 isNullXQException(xqit);
766 return current.instanceOf(xqit);
778 public void writeItem(OutputStream out, Properties prprts)
throws XQException {
779 isClosedXQException();
780 isNullXQException(out);
781 isNullXQException(prprts);
782 getItem().writeItem(out, prprts);
795 public void writeItem(Writer writer, Properties prprts)
throws XQException {
796 isClosedXQException();
797 isNullXQException(writer);
798 isNullXQException(prprts);
799 getItem().writeItem(writer, prprts);
811 isClosedXQException();
812 isNullXQException(ch);
829 isClosedXQException();
830 isNullXQException(result);
831 getItem().writeItemToResult(result);
843 isClosedXQException();
844 if (lStaticCollectionManager==null) {
847 return lStaticCollectionManager;
850 private void isClosedXQException() throws XQException {
852 throw new XQException(
"This sequence is closed");
855 private void isConsumedXQException() throws XQException {
857 throw new XQException(
"Items already consumed");
860 private void isItemDeliveredXQException() throws XQException {
862 throw new XQException(
"Item already consumed");
865 private void isOnItemXQException() throws XQException {
867 throw new XQException(
"There are no more items to consume");
871 private void isNullXQException(Object value)
throws XQException {
873 throw new XQException(
"Parameter shouldn't be null");