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");
String getSequenceAsString(Properties prprts)
Serializes the sequence starting from the current position to a String.
int getPosition()
Gets the current cursor position.
URI getNodeUri()
Returns the URI for this item.
XQConnection getConnection()
Gets the XQuery connection associated with this result sequence.
boolean isAfterLast()
Checks if the current position is after the last item in the sequence.
boolean isOnItem()
Check if the sequence is positioned on an item or not.
boolean relative(int i)
Moves the cursor a relative number of items.
boolean isScrollable()
Checks if the sequence is scrollable.
long getLong()
Gets the current item as a long.
ZorbaXQResultSequence(XQConnection conn, org.zorbaxquery.api.XQuery query, boolean prepared)
void writeSequence(Writer writer, Properties prprts)
Serializes the sequence starting from the current position to a Writer.
boolean previous()
Moves to the previous item in the sequence.
void writeItem(Writer writer, Properties prprts)
Serializes the current item to a Writer.
int count()
Returns a number indicating the number of items in the sequence.
void writeItemToResult(Result result)
Writes the current item to a Result.
boolean absolute(int i)
Moves the XQSequence's position to the given item number in this object.
boolean next()
Moves to the next item in the sequence.
Node getNode()
Gets the item as a DOM node.
boolean last()
Moves to the last item in the sequence.
void writeSequenceToSAX(ContentHandler ch)
Writes the entire sequence starting from the current position to a SAX handler.
XQItem getItem()
Get the current item as an immutable XQItem object.
void close()
Closes the sequence and frees all resources associated with this sequence.
boolean isClosed()
Checks if the sequence is closed.
void writeSequenceToResult(Result result)
Writes the entire sequence starting from the current position to a Result.
XQItemType getItemType()
Gets the type of the item.
Using the ZorbaXQStaticCollectionManager one can retrieve information about statically declared colle...
String getItemAsString(Properties prprts)
Serializes the current item according to the XSLT 2.0 and XQuery 1.0 serialization.
boolean isLast()
Checks if the current position at the last item in the sequence.
boolean first()
Moves to the first item in the sequence.
short getShort()
Gets the current item as a short.
boolean instanceOf(XQItemType xqit)
Checks if the item "matches" an item type.
void writeItem(OutputStream out, Properties prprts)
Serializes the current item to a Writer.
boolean isBeforeFirst()
Checks if the current position before the first item in the sequence.
float getFloat()
Gets the current item as a float.
This class represents a sequence of items obtained as a result of evaluation XQuery expressions...
void afterLast()
Move to the position after the last item.
Object getObject()
Gets the current item as an Object.
void writeItemToSAX(ContentHandler ch)
Writes the current item to a SAX handler.
boolean isFirst()
Checks if the current position at the first item in the sequence.
XMLStreamReader getSequenceAsStream()
Read the entire sequence starting from the current position as an XMLStreamReader object...
void writeSequence(OutputStream out, Properties prprts)
Serializes the sequence starting from the current position to an OutputStream.
byte getByte()
Gets the current item as a byte.
String getAtomicValue()
Gets the current item as a Java String.
boolean getBoolean()
Gets the current item as a boolean.
double getDouble()
Gets the current item as a double.
XMLStreamReader getItemAsStream()
Read the current item as an XMLStreamReader object.
This class represents an immutable item object obtained from an XQResultSequence using the getItem me...
ZorbaXQStaticCollectionManager getStaticCollectionManager()
Returns a StaticCollectionManager.
void beforeFirst()
Moves to the position before the first item.
int getInt()
Gets the current item as an int.