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;
38 import org.zorbaxquery.api.SerializationOptions;
79 private boolean closed =
false;
80 private boolean delivered =
false;
82 private boolean consumedItem =
false;
83 private boolean iterDeleted =
false;
85 private XQConnection connection = null;
86 private Iterator iter = null;
88 private Collection<ZorbaXQResultItem> items =
new ArrayList<ZorbaXQResultItem>();
89 private org.zorbaxquery.api.XQuery lQuery=null;
90 private boolean preparedExpression;
95 iter = query.iterator();
98 preparedExpression = prepared;
108 isClosedXQException();
130 public boolean absolute(
int i)
throws XQException {
131 isClosedXQException();
132 throw new XQException(
"This sequence is forward-only");
141 isClosedXQException();
142 throw new XQException(
"This sequence is forward-only");
151 isClosedXQException();
152 throw new XQException(
"This sequence is forward-only");
162 public void close() throws XQException {
168 items.removeAll(items);
174 if (!preparedExpression) {
178 if (lStaticCollectionManager != null) {
179 lStaticCollectionManager.
close();
199 public int count() throws XQException {
200 isClosedXQException();
201 throw new XQException(
"This sequence is forward-only");
214 isClosedXQException();
215 throw new XQException(
"This sequence is forward-only");
227 isClosedXQException();
228 return (current!=null);
238 isClosedXQException();
250 public boolean first() throws XQException {
251 isClosedXQException();
252 throw new XQException(
"This sequence is forward-only");
266 isClosedXQException();
267 isItemDeliveredXQException();
268 isOnItemXQException();
284 isClosedXQException();
285 isConsumedXQException();
286 Properties properties = null;
288 XMLInputFactory fac = XMLInputFactory.newInstance();
289 Reader read =
new StringReader(str);
290 XMLStreamReader result = null;
292 result = fac.createXMLStreamReader(read);
293 }
catch (XMLStreamException ex) {
294 throw new XQException(
"Problem reading the stream: " + str +
" - with error: " + ex.getLocalizedMessage());
309 isClosedXQException();
310 isConsumedXQException();
311 String result = null;
313 SerializationOptions opts =
new SerializationOptions();
314 if ((prprts!=null) && prprts.size()>0) {
315 for(String key : prprts.stringPropertyNames()) {
316 String value = prprts.getProperty(key);
317 opts.setSerializerOption(key, value);
326 result = lQuery.execute(opts).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");