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.XQException;
31 import javax.xml.xquery.XQItem;
32 import javax.xml.xquery.XQItemType;
33 import org.w3c.dom.Node;
34 import org.xml.sax.ContentHandler;
35 import org.zorbaxquery.api.Item;
36 import org.zorbaxquery.api.ItemSequence;
79 private boolean closed =
false;
80 private boolean forwardOnly =
true;
81 private boolean currentItemGet =
false;
82 private Collection<XQItem> content =
new ArrayList<XQItem>();
83 private int position = 1;
85 private ItemSequence itemSequence = null;
89 while (sequence.next()) {
90 XQItem tmpItem = sequence.getItem();
92 if (tmpItem instanceof XQItem) {
93 item = ((org.zorbaxquery.api.xqj.ZorbaXQItem)tmpItem).getZorbaItem();
95 item = ((org.zorbaxquery.api.xqj.ZorbaXQResultItem)tmpItem).getZorbaItem();
97 content.add(
new org.zorbaxquery.api.xqj.ZorbaXQItem(item));
99 size = content.size();
100 }
catch (Exception e) {
101 throw new XQException(
"Error iterating from origin object" + e.getLocalizedMessage());
107 while (iterator.hasNext()) {
108 Object o = iterator.next();
109 if (o instanceof XQItem) {
110 content.add((XQItem)o);
113 size = content.size();
117 if (iterator.isOpen()) {
118 org.zorbaxquery.api.Item item =
new org.zorbaxquery.api.Item();
119 while (iterator.next(item)) {
120 XQItem xItem =
new org.zorbaxquery.api.xqj.ZorbaXQItem(item);
123 size = content.size();
128 XQItem xItem =
new org.zorbaxquery.api.xqj.ZorbaXQItem(item);
130 size = content.size();
135 org.zorbaxquery.api.Iterator iterator = seq.getIterator();
136 if (iterator.isOpen()) {
137 org.zorbaxquery.api.Item item = null;
138 while (iterator.next(item)) {
139 XQItem xItem =
new org.zorbaxquery.api.xqj.ZorbaXQItem(item);
142 size = content.size();
147 if (itemSequence==null) {
148 throw new XQException(
"This Sequence doesn't come from Zorba ItemSequence object");
171 public boolean absolute(
int i)
throws XQException {
172 isClosedXQException();
173 boolean result =
false;
199 isClosedXQException();
200 position = content.size()+1;
209 isClosedXQException();
220 public void close() throws XQException {
222 for (XQItem item: content) {
225 if (itemSequence!=null) {
226 itemSequence.delete();
245 public int count() throws XQException {
246 isClosedXQException();
260 isClosedXQException();
273 isClosedXQException();
274 return (position>0) && (position<(content.size()+1));
284 isClosedXQException();
296 public boolean first() throws XQException {
297 isClosedXQException();
298 boolean result =
false;
299 if (content.size()>0) {
317 isClosedXQException();
318 isItemGetXQException();
319 return (XQItem)content.toArray()[position-1];
331 isClosedXQException();
332 isItemGetXQException();
333 StringBuffer sb =
new StringBuffer();
334 for (XQItem item: content) {
335 sb.append(item.getItemAsString(null));
337 XMLInputFactory fac = XMLInputFactory.newInstance();
338 Reader read =
new StringReader(sb.toString());
339 XMLStreamReader result = null;
341 result = fac.createXMLStreamReader(read);
342 }
catch (XMLStreamException ex) {
343 throw new XQException(
"Problem reading the stream: " + sb +
" - with error: " + ex.getLocalizedMessage());
358 isClosedXQException();
359 isItemGetXQException();
360 StringBuffer sb =
new StringBuffer();
361 for (XQItem item: content) {
362 sb.append(item.getItemAsString(prprts));
364 return sb.toString();
376 isClosedXQException();
377 boolean result =
false;
379 result = position==size+1;
393 isClosedXQException();
394 boolean result =
false;
395 if (content.size()>0) {
396 result = position==0;
410 isClosedXQException();
411 boolean result =
false;
412 if (content.size()>0) {
413 result = position==1;
426 public boolean isLast() throws XQException {
427 isClosedXQException();
428 boolean result =
false;
430 result = position==size;
443 public boolean last() throws XQException {
444 isClosedXQException();
445 boolean result =
false;
461 public boolean next() throws XQException {
462 isClosedXQException();
463 boolean result =
false;
464 if ((position<=size) && (size>0)) {
465 result = (position<size);
480 isClosedXQException();
481 boolean result =
false;
482 if ((position>0) && (size>0)) {
483 result = (position>1);
499 public boolean relative(
int i)
throws XQException {
500 isClosedXQException();
501 boolean result =
false;
502 if ((i!=0) && (size>0)) {
503 position = position + i;
506 }
else if (position>size) {
524 public void writeSequence(OutputStream out, Properties prprts)
throws XQException {
525 isClosedXQException();
526 isItemGetXQException();
527 isNullXQException(out);
529 getItem().writeItem(out, prprts);
532 getItem().writeItem(out, prprts);
549 public void writeSequence(Writer writer, Properties prprts)
throws XQException {
550 isClosedXQException();
551 isItemGetXQException();
552 isNullXQException(writer);
554 getItem().writeItem(writer, prprts);
557 getItem().writeItem(writer, prprts);
570 isClosedXQException();
571 isItemGetXQException();
572 isNullXQException(ch);
594 isClosedXQException();
595 isItemGetXQException();
596 isNullXQException(result);
598 getItem().writeItemToResult(result);
601 getItem().writeItemToResult(result);
614 isClosedXQException();
627 isClosedXQException();
640 isClosedXQException();
653 isClosedXQException();
666 isClosedXQException();
679 isClosedXQException();
680 return getItem().getItemType();
692 isClosedXQException();
693 return getItem().getAtomicValue();
705 isClosedXQException();
718 isClosedXQException();
732 isClosedXQException();
745 isClosedXQException();
758 isClosedXQException();
759 return getItem().getItemAsStream();
772 isClosedXQException();
773 return getItem().getItemAsString(prprts);
785 isClosedXQException();
818 public boolean instanceOf(XQItemType xqit)
throws XQException {
819 isClosedXQException();
820 isNullXQException(xqit);
821 return getItem().instanceOf(xqit);
833 public void writeItem(OutputStream out, Properties prprts)
throws XQException {
834 isClosedXQException();
835 isNullXQException(out);
836 getItem().writeItem(out, prprts);
849 public void writeItem(Writer writer, Properties prprts)
throws XQException {
850 isClosedXQException();
851 isNullXQException(writer);
852 getItem().writeItem(writer, prprts);
864 isClosedXQException();
865 isNullXQException(ch);
882 isClosedXQException();
883 isNullXQException(result);
884 getItem().writeItemToResult(result);
887 private void isClosedXQException() throws XQException {
889 throw new XQException(
"This sequence is closed");
892 private void isItemGetXQException() throws XQException {
893 if (forwardOnly && currentItemGet) {
894 throw new XQException(
"Item already consumed on a forward-only sequence");
897 private void isNullXQException(Object value)
throws XQException {
899 throw new XQException(
"Parameter shouldn't be null");