16 package org.zorbaxquery.api.xqj;
19 import java.math.BigDecimal;
20 import java.math.BigInteger;
22 import java.util.Properties;
23 import javax.xml.datatype.DatatypeFactory;
24 import javax.xml.namespace.QName;
25 import javax.xml.parsers.DocumentBuilder;
26 import javax.xml.parsers.DocumentBuilderFactory;
27 import javax.xml.stream.XMLInputFactory;
28 import javax.xml.stream.XMLStreamReader;
29 import javax.xml.transform.Result;
30 import javax.xml.transform.Source;
31 import javax.xml.transform.Transformer;
32 import javax.xml.transform.TransformerFactory;
33 import javax.xml.transform.dom.DOMSource;
34 import javax.xml.xquery.XQException;
35 import javax.xml.xquery.XQItemType;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38 import org.w3c.dom.Node;
39 import org.xml.sax.ContentHandler;
40 import org.xml.sax.InputSource;
41 import org.xml.sax.helpers.AttributesImpl;
42 import org.zorbaxquery.api.*;
85 class ZorbaXQItem
implements javax.xml.xquery.XQItem {
88 private boolean closed=
false;
89 private XQItemType itemType;
91 protected Item getZorbaItem() {
95 public ZorbaXQItem(XQItemType itemType) {
96 this.itemType = itemType;
99 public ZorbaXQItem(Item item) {
100 this.item =
new Item(item);
104 public ZorbaXQItem(Item item, XQItemType itemType) {
105 this.item =
new Item(item);
106 this.itemType = itemType;
109 public ZorbaXQItem(javax.xml.xquery.XQItem item) throws XQException {
110 this.item =
new Item(((ZorbaXQItem)item).getZorbaItem());
111 this.itemType = item.getItemType();
121 public void close() throws XQException {
134 public boolean isClosed() {
146 public boolean getBoolean() throws XQException {
147 isClosedXQException();
148 if (itemType.getBaseType()!=XQItemType.XQBASETYPE_BOOLEAN) {
149 throw new XQException(
"Object is not boolean");
153 result = item.getBooleanValue();
154 }
catch (Exception e) {
155 throw new XQException(
"Error getting boolean value" + e.getLocalizedMessage());
168 public byte getByte() throws XQException {
169 isClosedXQException();
170 isDecimalXQException();
173 if (itemType.getBaseType() == XQItemType.XQBASETYPE_BYTE) {
174 result = (byte) item.getIntValue();
176 result = Byte.parseByte(item.getStringValue());
178 }
catch (Exception e) {
179 throw new XQException(
"Error getting byte value" + e.getLocalizedMessage());
192 public double getDouble() throws XQException {
193 isClosedXQException();
194 isFloatXQException();
197 if (itemType.getBaseType() == XQItemType.XQBASETYPE_DOUBLE) {
198 result = item.getDoubleValue();
200 result = Double.parseDouble(item.getStringValue());
202 }
catch (Exception e) {
203 throw new XQException(
"Error getting byte value" + e.getLocalizedMessage());
216 public float getFloat() throws XQException {
217 isClosedXQException();
218 isFloatXQException();
221 if (itemType.getBaseType() == XQItemType.XQBASETYPE_FLOAT) {
222 result = (float)item.getDoubleValue();
224 result = Float.parseFloat(item.getStringValue());
226 }
catch (Exception e) {
227 throw new XQException(
"Error getting byte value" + e.getLocalizedMessage());
240 public int getInt() throws XQException {
241 isClosedXQException();
242 isDecimalXQException();
245 if (itemType.getBaseType() == XQItemType.XQBASETYPE_INT) {
246 result = item.getIntValue();
248 result = Integer.parseInt(item.getStringValue());
250 }
catch (Exception e) {
251 throw new XQException(
"Error getting byte value" + e.getLocalizedMessage());
264 public XQItemType getItemType() throws XQException {
265 isClosedXQException();
277 public String getAtomicValue() throws XQException {
278 isClosedXQException();
279 if (!item.isAtomic()) {
280 throw new XQException(
"Object is not atomic");
282 org.zorbaxquery.api.Item lItem = null;
284 org.zorbaxquery.api.Iterator iterator = item.getAtomizationValue();
287 iterator.next(lItem);
289 }
catch (Exception e) {
290 throw new XQException(
"Error getting atomic value: " + e.getLocalizedMessage());
292 return lItem.getStringValue();
303 public long getLong() throws XQException {
304 isClosedXQException();
305 isDecimalXQException();
308 if (itemType.getBaseType() == XQItemType.XQBASETYPE_LONG) {
309 result = item.getLongValue();
311 result = Long.parseLong(item.getStringValue());
313 }
catch (Exception e) {
314 throw new XQException(
"Error getting byte value" + e.getLocalizedMessage());
327 public Node getNode() throws XQException {
328 isClosedXQException();
329 if (!item.isNode()) {
330 throw new XQException(
"Object is not a node");
333 InputStream inputStream =
new ByteArrayInputStream(item.toString().getBytes());
335 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
336 DocumentBuilder db = dbf.newDocumentBuilder();
337 Document doc = db.newDocument();
338 Item nodeName =
new Item();
339 item.getNodeName(nodeName);
340 switch (itemType.getItemKind()) {
341 case XQItemType.XQITEMKIND_ATTRIBUTE:
342 result = doc.createAttributeNS(
"", nodeName.getStringValue());
344 case XQItemType.XQITEMKIND_COMMENT:
345 result = doc.createComment(item.getStringValue());
347 case XQItemType.XQITEMKIND_DOCUMENT:
348 String tmpDoc = item.serialize();
349 if (tmpDoc.length()>0) {
350 result = db.parse(
new InputSource(
new StringReader(tmpDoc)));
355 case XQItemType.XQITEMKIND_ELEMENT:
356 Element e = doc.createElementNS(
"", nodeName.getStringValue());
357 Node text = doc.createTextNode(item.getStringValue());
361 case XQItemType.XQITEMKIND_PI:
362 String tmpPI = item.serialize().substring(2).split(
" ")[0];
363 result = doc.createProcessingInstruction(tmpPI, item.getStringValue());
365 case XQItemType.XQITEMKIND_TEXT:
366 result = doc.createTextNode(item.getStringValue());
369 }
catch (Exception ex) {
370 throw new XQException(
"Error converting Item to Node" + ex.getLocalizedMessage());
385 public URI getNodeUri() throws XQException {
386 isClosedXQException();
387 if (!item.isNode()) {
388 throw new XQException(
"Object is not a node");
391 String
namespace = "";
393 Item type =
new Item();
394 type = item.getType();
395 if (type.getStringValue().equalsIgnoreCase(
"xs:qname")) {
396 namespace = item.getNamespace();
398 StringPairVector bindings = item.getNamespaceBindings();
400 if (bindings.size()>0) {
401 StringPair pair = bindings.get(0);
402 namespace = pair.getFirst();
405 }
catch (Exception ex) {
406 throw new XQException(
"Error getting Node URI: " + ex.getLocalizedMessage());
409 result =
new URI(
namespace);
410 }
catch (Exception ex) {
411 throw new XQException(
"Error getting Node URI: " + ex.getLocalizedMessage());
424 public Object getObject() throws XQException {
425 isClosedXQException();
426 Object result = null;
428 if ((itemType.getItemKind()==XQItemType.XQITEMKIND_ATTRIBUTE) ||
429 (itemType.getItemKind()==XQItemType.XQITEMKIND_COMMENT) ||
430 (itemType.getItemKind()==XQItemType.XQITEMKIND_DOCUMENT) ||
431 (itemType.getItemKind()==XQItemType.XQITEMKIND_ELEMENT) ||
432 (itemType.getItemKind()==XQItemType.XQITEMKIND_PI) ||
433 (itemType.getItemKind()==XQItemType.XQITEMKIND_TEXT) ) {
434 result = this.getNode();
435 }
else if (itemType.getItemKind()==XQItemType.XQITEMKIND_ATOMIC) {
437 DatatypeFactory factory = DatatypeFactory.newInstance();
438 switch (itemType.getBaseType()) {
448 case XQItemType.XQBASETYPE_ANYURI:
449 result = item.getStringValue();
451 case XQItemType.XQBASETYPE_BASE64BINARY:
452 result = item.serialize().getBytes();
454 case XQItemType.XQBASETYPE_BOOLEAN:
455 result = item.getBooleanValue();
457 case XQItemType.XQBASETYPE_BYTE:
458 result =
new Byte(item.getStringValue());
460 case XQItemType.XQBASETYPE_DATE:
461 case XQItemType.XQBASETYPE_DATETIME:
462 case XQItemType.XQBASETYPE_GDAY:
463 case XQItemType.XQBASETYPE_GMONTH:
464 case XQItemType.XQBASETYPE_GMONTHDAY:
465 case XQItemType.XQBASETYPE_GYEAR:
466 case XQItemType.XQBASETYPE_GYEARMONTH:
467 case XQItemType.XQBASETYPE_TIME:
468 result = factory.newXMLGregorianCalendar(item.getStringValue());
470 case XQItemType.XQBASETYPE_DURATION:
471 result = factory.newDuration(item.getStringValue());
473 case XQItemType.XQBASETYPE_DAYTIMEDURATION:
474 String tmpString = item.getStringValue();
475 result = factory.newDurationDayTime(tmpString);
477 case XQItemType.XQBASETYPE_YEARMONTHDURATION:
478 result = factory.newDurationYearMonth(item.getStringValue());
480 case XQItemType.XQBASETYPE_DECIMAL:
481 result =
new BigDecimal(item.getStringValue());
483 case XQItemType.XQBASETYPE_DOUBLE:
484 result = item.getDoubleValue();
486 case XQItemType.XQBASETYPE_ENTITIES:
487 result = item.getStringValue();
489 case XQItemType.XQBASETYPE_ENTITY:
490 result = item.getStringValue();
492 case XQItemType.XQBASETYPE_FLOAT:
493 result = (float)(item.getDoubleValue());
495 case XQItemType.XQBASETYPE_HEXBINARY:
496 result = item.getStringValue().getBytes();
498 case XQItemType.XQBASETYPE_ID:
499 result = item.getStringValue();
501 case XQItemType.XQBASETYPE_IDREF:
502 result = item.getStringValue();
504 case XQItemType.XQBASETYPE_IDREFS:
505 result = item.getStringValue();
507 case XQItemType.XQBASETYPE_INT:
508 result = item.getIntValue();
510 case XQItemType.XQBASETYPE_LANGUAGE:
511 result = item.getStringValue();
513 case XQItemType.XQBASETYPE_LONG:
514 result = item.getLongValue();
516 case XQItemType.XQBASETYPE_NAME:
517 result = item.getStringValue();
519 case XQItemType.XQBASETYPE_NCNAME:
520 result = item.serialize();
522 case XQItemType.XQBASETYPE_NMTOKEN:
523 result = item.getStringValue();
525 case XQItemType.XQBASETYPE_NMTOKENS:
526 result = item.getStringValue();
528 case XQItemType.XQBASETYPE_INTEGER:
529 case XQItemType.XQBASETYPE_NEGATIVE_INTEGER:
530 case XQItemType.XQBASETYPE_POSITIVE_INTEGER:
531 case XQItemType.XQBASETYPE_NONNEGATIVE_INTEGER:
532 case XQItemType.XQBASETYPE_NONPOSITIVE_INTEGER:
533 case XQItemType.XQBASETYPE_UNSIGNED_LONG:
534 result =
new BigInteger(item.getStringValue());
536 case XQItemType.XQBASETYPE_NORMALIZED_STRING:
537 result = item.getStringValue();
539 case XQItemType.XQBASETYPE_NOTATION:
540 result = item.getStringValue();
542 case XQItemType.XQBASETYPE_QNAME:
543 result =
new QName(item.getNamespace(), item.getLocalName(), item.getPrefix()) ;
545 case XQItemType.XQBASETYPE_STRING:
546 result = item.getStringValue();
548 case XQItemType.XQBASETYPE_TOKEN:
549 result = item.getStringValue();
551 case XQItemType.XQBASETYPE_SHORT:
552 case XQItemType.XQBASETYPE_UNSIGNED_BYTE:
553 result =
new Short(item.getStringValue());
555 case XQItemType.XQBASETYPE_UNSIGNED_INT:
556 result =
new Long(item.getStringValue());
558 case XQItemType.XQBASETYPE_UNSIGNED_SHORT:
559 result =
new Integer(item.getStringValue());
561 case XQItemType.XQBASETYPE_UNTYPED:
562 result = item.getStringValue();
564 case XQItemType.XQBASETYPE_UNTYPEDATOMIC:
565 result = item.getStringValue();
570 }
catch (Exception e) {
571 throw new XQException(
"Error getting byte value: " + e.getLocalizedMessage());
584 public XMLStreamReader getItemAsStream() throws XQException {
585 isClosedXQException();
586 XMLInputFactory factory = XMLInputFactory.newInstance();
587 XMLStreamReader result = null;
589 result = factory.createXMLStreamReader(
new StringReader(item.serialize()));
590 }
catch (Exception ex) {
591 throw new XQException(
"Error creating XMLStreamReader: " + ex.getLocalizedMessage());
605 public String getItemAsString(Properties prprts)
throws XQException {
606 isClosedXQException();
607 String result = null;
609 SerializationOptions opts =
new SerializationOptions();
610 if ((prprts!=null) && prprts.size()>0) {
611 for(String key : prprts.stringPropertyNames()) {
612 String value = prprts.getProperty(key);
613 opts.setSerializerOption(key, value);
616 result = item.serialize(opts).replace(
">",
">").replace(
"<",
"<");
617 }
catch (Exception ex) {
618 throw new XQException(
"Error serializing item" + ex.getLocalizedMessage());
631 public short getShort() throws XQException {
632 isClosedXQException();
633 isDecimalXQException();
636 if (itemType.getBaseType() == XQItemType.XQBASETYPE_SHORT) {
637 result = (short) item.getIntValue();
639 result = Short.parseShort(item.getStringValue());
641 }
catch (Exception ex) {
642 throw new XQException(
"Error getting short value: " + ex.getLocalizedMessage());
676 public boolean instanceOf(XQItemType xqit)
throws XQException {
677 isClosedXQException();
678 return itemType.equals(xqit);
690 public void writeItem(OutputStream out, Properties prprts)
throws XQException {
691 isClosedXQException();
692 isNullXQException(out);
693 isNullXQException(prprts);
695 SerializationOptions opts =
new SerializationOptions();
696 if ((prprts!=null) && prprts.size()>0) {
697 for(String key : prprts.stringPropertyNames()) {
698 String value = prprts.getProperty(key);
699 opts.setSerializerOption(key, value);
702 ZorbaOutputWrapper OStream =
new ZorbaOutputWrapper(out);
703 item.serializeToStream(OStream);
704 }
catch (Exception ex) {
705 throw new XQException(
"Error writing on stream: " + ex.getLocalizedMessage());
719 public void writeItem(Writer writer, Properties prprts)
throws XQException {
720 isClosedXQException();
721 isNullXQException(writer);
722 isNullXQException(prprts);
724 SerializationOptions opts =
new SerializationOptions();
725 if ((prprts!=null) && prprts.size()>0) {
726 for(String key : prprts.stringPropertyNames()) {
727 String value = prprts.getProperty(key);
728 opts.setSerializerOption(key, value);
731 ZorbaWriterWrapper OStream =
new ZorbaWriterWrapper(writer);
732 item.serializeToStream(OStream);
733 writer.write(item.serialize(opts));
734 }
catch (Exception ex) {
735 throw new XQException(
"Error sending to writer: " + ex.getLocalizedMessage());
747 public void writeItemToSAX(ContentHandler ch)
throws XQException {
748 isClosedXQException();
749 isNullXQException(ch);
751 AttributesImpl attributes =
new AttributesImpl();
752 Iterator attr = item.getAttributes();
754 Item attribute =
new Item();
755 while (attr.next(attribute)) {
756 attributes.addAttribute(attribute.getNamespace(), attribute.getLocalName(),
"", attribute.getType().getStringValue(), attribute.getStringValue());
762 String
namespace = "";
763 if (itemType.getBaseType() != XQItemType.XQBASETYPE_ANYTYPE){
764 namespace = item.getNamespace();
766 Item name =
new Item();
767 item.getNodeName(name);
768 String nodeName = name.getStringValue();
771 char [] content = item.getStringValue().toCharArray();
773 ch.startElement(
namespace, nodeName ,
"", attributes);
774 ch.characters(content, 0, content.length);
775 ch.endElement(
namespace, nodeName,
"");
776 }
catch (Exception ex) {
777 throw new XQException(
"Error writing item to SAX: " + ex.getLocalizedMessage());
793 public void writeItemToResult(Result result)
throws XQException {
794 isClosedXQException();
795 isNullXQException(result);
797 Source xmlSource =
new DOMSource(getNode());
798 TransformerFactory transformerFactory = TransformerFactory.newInstance();
799 Transformer transformer;
800 transformer = transformerFactory.newTransformer();
801 transformer.setOutputProperty(
"indent",
"yes");
802 transformer.transform(xmlSource, result);
803 }
catch (Exception ex) {
804 throw new XQException (
"Error serializing to javax.xml.transform.Result");
808 private void isClosedXQException() throws XQException {
810 throw new XQException(
"This connection is closed");
814 private void isNullXQException(Object value)
throws XQException {
816 throw new XQException(
"Parameter shouldn't be null");
820 private void isDecimalXQException() throws XQException {
821 switch (itemType.getBaseType()) {
822 case XQItemType.XQBASETYPE_DECIMAL:
823 case XQItemType.XQBASETYPE_BYTE:
824 case XQItemType.XQBASETYPE_INT:
825 case XQItemType.XQBASETYPE_INTEGER:
826 case XQItemType.XQBASETYPE_LONG:
827 case XQItemType.XQBASETYPE_NEGATIVE_INTEGER:
828 case XQItemType.XQBASETYPE_NONNEGATIVE_INTEGER:
829 case XQItemType.XQBASETYPE_NONPOSITIVE_INTEGER:
830 case XQItemType.XQBASETYPE_POSITIVE_INTEGER:
831 case XQItemType.XQBASETYPE_SHORT:
832 case XQItemType.XQBASETYPE_UNSIGNED_LONG:
833 case XQItemType.XQBASETYPE_UNSIGNED_INT:
834 case XQItemType.XQBASETYPE_UNSIGNED_SHORT:
835 case XQItemType.XQBASETYPE_UNSIGNED_BYTE:
838 throw new XQException(
"Object is not xs:decimal or a derivate");
842 private void isFloatXQException() throws XQException {
843 if (! ((itemType.getBaseType()==XQItemType.XQBASETYPE_FLOAT) ||
844 (itemType.getBaseType()==XQItemType.XQBASETYPE_DOUBLE)) )
846 throw new XQException(
"Object is not xs:decimal or a derivate");
The ZorbaXQItemType class represents an item type as defined in XQuery 1.0: An XML Query language...