ZorbaXQConnection.java
Go to the documentation of this file.
1 /*
2  * Copyright 2006-2012 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.zorbaxquery.api.xqj;
17 
18 import java.io.*;
19 import java.math.BigDecimal;
20 import java.math.BigInteger;
21 import java.net.URI;
22 import java.nio.CharBuffer;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Iterator;
26 import java.util.Properties;
27 import javax.xml.datatype.DatatypeConstants;
28 import javax.xml.datatype.Duration;
29 import javax.xml.datatype.XMLGregorianCalendar;
30 import javax.xml.namespace.QName;
31 import javax.xml.stream.XMLOutputFactory;
32 import javax.xml.stream.XMLStreamReader;
33 import javax.xml.stream.XMLStreamWriter;
34 import javax.xml.transform.*;
35 import javax.xml.transform.dom.DOMSource;
36 import javax.xml.transform.sax.SAXSource;
37 import javax.xml.transform.stax.StAXResult;
38 import javax.xml.transform.stax.StAXSource;
39 import javax.xml.transform.stream.StreamResult;
40 import javax.xml.transform.stream.StreamSource;
41 import javax.xml.xquery.*;
42 import org.w3c.dom.*;
43 import org.zorbaxquery.api.*;
44 
45  /**
46  *
47  * A connection (session) with a specific XQuery engine. Connections are obtained through an ZorbaXQDataSource object.
48  *
49  * XQuery expressions are executed and results are returned within the context of a connection. They are either executed through ZorbaXQExpression or ZorbaXQPreparedExpression objects.
50  *
51  * ZorbaXQDataSource ds;// obtain the XQuery datasource
52  * ...
53  * ZorbaXQConnection conn = ds.getConnection();
54  *
55  * ZorbaXQPreparedExpression expr = conn.prepareExpression("for $i in ...");
56  * XQResultSequence result = expr.executeQuery();
57  * // - or -
58  * ZorbaXQExpression expr = conn.createExpression();
59  * ZorbaXQSequence result = expr.executeQuery("for $i in..");
60  *
61  * // The sequence can now be iterated
62  * while (result.next())
63  * {
64  * String str = result.getItemAsString();
65  * System.out.println(" output "+ str);
66  * }
67  * result.close();
68  * expr.close();
69  * conn.close(); // close the connection and free all resources..
70  *
71  *
72  * A connection holds also default values for ZorbaXQExpression and ZorbaXQPreparedExpression properties. An application can override these defaults by passing an ZorbaXQStaticContext object to the setStaticContext() method.
73  *
74  */
75 public class ZorbaXQConnection implements javax.xml.xquery.XQConnection {
76  private InMemoryStore store;
77  private Zorba zorba;
78  private boolean autocommit;
79  private boolean closed;
80  private Collection<XQExpression> cExpression;
81  private Collection<XQPreparedExpression> cPreparedExpression;
82  private Collection<XQMetaData> cMetadata;
83  private XQStaticContext lStaticContext;
84  private ZorbaXQXmlDataManager lXmlDataManager;
85  private Properties properties;
86  private StringVector uriPaths;
87  private StringVector libPaths;
88  private StringVector modulePaths;
89 
90  protected Zorba getZorbaInstance() throws XQException {
91  if (zorba!=null) {
92  return zorba;
93  } else {
94  throw new XQException("Error returning Zorba Instance");
95  }
96  }
97  public ZorbaXQConnection() {
98  cExpression = new ArrayList<XQExpression>();
99  cPreparedExpression = new ArrayList<XQPreparedExpression>();
100  store = InMemoryStore.getInstance();
101  zorba = Zorba.getInstance ( store );
102  autocommit = true;
103  closed = false;
104  }
105  public ZorbaXQConnection(Properties aProperties) {
106  cExpression = new ArrayList<XQExpression>();
107  cPreparedExpression = new ArrayList<XQPreparedExpression>();
108  store = InMemoryStore.getInstance();
109  zorba = Zorba.getInstance ( store );
110  autocommit = true;
111  closed = false;
112  properties = aProperties;
113  for (String prop :properties.stringPropertyNames()) {
114  if (prop.equalsIgnoreCase(ZorbaXQDataSource.ZORBA_PROPERTIES_URI_PATHS)) {
115  String[] paths = properties.getProperty(prop).split("[;,]");
116  uriPaths = new StringVector();
117  for (String path: paths) {
118  uriPaths.add(path);
119  }
120  }
121  if (prop.equalsIgnoreCase(ZorbaXQDataSource.ZORBA_PROPERTIES_LIB_PATHS)) {
122  String[] paths = properties.getProperty(prop).split("[;,]");
123  libPaths = new StringVector();
124  for (String path: paths) {
125  libPaths.add(path);
126  }
127  }
128  if (prop.equalsIgnoreCase(ZorbaXQDataSource.ZORBA_PROPERTIES_MODULE_PATHS)) {
129  String[] paths = properties.getProperty(prop).split("[;,]");
130  modulePaths = new StringVector();
131  for (String path: paths) {
132  modulePaths.add(path);
133  }
134  }
135  }
136  lStaticContext = new org.zorbaxquery.api.xqj.ZorbaXQStaticContext(zorba);
137  if (uriPaths!=null) {
138  ((org.zorbaxquery.api.xqj.ZorbaXQStaticContext)lStaticContext).setURIPaths(uriPaths);
139  }
140  if (libPaths!=null) {
141  ((org.zorbaxquery.api.xqj.ZorbaXQStaticContext)lStaticContext).setLIBPaths(libPaths);
142  }
143  if (modulePaths!=null) {
144  ((org.zorbaxquery.api.xqj.ZorbaXQStaticContext)lStaticContext).setMODPaths(modulePaths);
145  }
146 
147  }
148 
149  /** \brief Closes the connection.
150  *
151  * Closes the connection. This also closes any ZorbaXQExpression and ZorbaXQPreparedExpression obtained from this connection. Once the connection is closed, no method other than close or the isClosed method may be called on the connection object. Calling close on an ZorbaXQConnection object that is already closed has no effect. Note that an XQJ driver is not required to provide finalizer methods for the connection and other objects. Hence it is strongly recommended that users call this method explicitly to free any resources. It is also recommended that they do so under a final block to ensure that the object is closed even when there are exceptions.
152  *
153  * @throw XQException - if there is an error during closing the connection.
154  */
155  @Override
156  public void close() throws XQException {
157  if (closed) return; // already closed
158  try {
159  for (XQExpression exp : cExpression ){
160  exp.close(); // Notify the dependents objects to close
161  }
162  for (XQPreparedExpression exp : cPreparedExpression ){
163  exp.close(); // Notify the dependents objects to close
164  }
165  } catch (XQException e) {
166  throw new XQException("Error closing connection: " + e.getLocalizedMessage());
167  }
168 
169  if (lStaticContext != null ) {
170  ((org.zorbaxquery.api.xqj.ZorbaXQStaticContext)lStaticContext).getZorbaStaticContext().destroy();
171  }
172  if (lXmlDataManager != null) {
173  lXmlDataManager.close();
174  }
175  zorba.shutdown();
176  InMemoryStore.shutdown ( store );
177  closed = true;
178  }
179 
181  if (lXmlDataManager==null) {
182  lXmlDataManager = new ZorbaXQXmlDataManager(zorba);
183  }
184  return lXmlDataManager;
185  }
186 
187  @Override
188  public void setAutoCommit(boolean bln) throws XQException {
189  isClosedXQException();
190  autocommit = bln;
191  }
192 
193  /** \brief Gets the auto-commit attribute of this connection
194  *
195  * @return the auto-commit attribute of this connection. true if the connection operates in auto-commit mode; otherwise false
196  * @throw XQException - if the connection is in a closed state
197  */
198  @Override
199  public boolean getAutoCommit() throws XQException {
200  isClosedXQException();
201  return autocommit;
202  }
203 
204  /** \brief Makes all changes made in the current transaction permanent and releases any locks held by the datasource.
205  *
206  * This method should be used only when auto-commit mode is disabled. Any XQResultSequence, or XQResultItem may be implicitly closed upon commit, if the holdability property of the sequence is set to XQConstants.HOLDTYPE_CLOSE_CURSORS_AT_COMMIT.
207  *
208  * @throw XQException - if the connection is in a closed state or this connection is operating in auto-commit mode
209  */
210  @Override
211  public void commit() throws XQException {
212  isClosedXQException();
213  throw new UnsupportedOperationException("Zorba does not support transactions... yet...");
214  }
215 
216  /** \brief Creates a new ZorbaXQExpression object that can be used to perform execute immediate operations with XQuery expressions.
217  *
218  * The properties of the connection's default ZorbaXQStaticContext are copied to the returned ZorbaXQExpression.
219  *
220  * @return ZorbaXQExpression that can be used to execute multiple expressions
221  * @throw XQException - if the connection is in a closed state
222  */
223  @Override
224  public XQExpression createExpression() throws XQException {
225  isClosedXQException();
226  XQExpression expression;
227  if (lStaticContext == null) {
228  expression = new org.zorbaxquery.api.xqj.ZorbaXQExpression(this);
229  } else {
230  expression = new org.zorbaxquery.api.xqj.ZorbaXQExpression(this, lStaticContext);
231  }
232  cExpression.add(expression);
233  return expression;
234  }
235 
236  /** \brief Creates a new ZorbaXQExpression object that can be used to perform execute immediate operations with XQuery expressions.
237  *
238  * The properties of the specified ZorbaXQStaticContext values are copied to the returned ZorbaXQExpression.
239  *
240  * @param value - ZorbaXQStaticContext containing values of expression properties
241  * @return ZorbaXQExpression that can be used to execute multiple expressions
242  * @throw XQException - if (1) the connection is in a closed state, or (2) the specified argument is null
243  */
244  @Override
245  public XQExpression createExpression(XQStaticContext value) throws XQException {
246  isClosedXQException();
247  isNullXQException(value);
248  XQExpression expression = new org.zorbaxquery.api.xqj.ZorbaXQExpression(this, value);
249  cExpression.add(expression);
250  return expression;
251  }
252 
253  /** \brief Gets the metadata for this connection.
254  *
255  * @return XQMetadata representing the metadata of this connection
256  * @throw XQException - if the connection is in a closed state
257  */
258  @Override
259  public XQMetaData getMetaData() throws XQException {
260  isClosedXQException();
261  return new org.zorbaxquery.api.xqj.ZorbaXQMetaData(this);
262  }
263 
264  /** \brief Checks if the connection is closed.
265  *
266  * @return true if the connection is in a closed state, false otherwise
267  */
268  @Override
269  public boolean isClosed() {
270  return closed;
271  }
272 
273  /** \brief Prepares an expression for execution.
274  *
275  * The properties of the connection's default ZorbaXQStaticContext are copied to the returned ZorbaXQPreparedExpression.
276  *
277  * @param value - the XQuery expression as a String. Cannot be null
278  * @return the prepared XQuery expression
279  * @throw XQException - if (1) the connection is in a closed state, (2) there are errors preparing the expression, or (3) the xquery parameter is null
280  */
281  @Override
282  public XQPreparedExpression prepareExpression(String value) throws XQException {
283  isClosedXQException();
284  isNullXQException(value);
285  XQPreparedExpression expression;
286  try {
287  if (lStaticContext == null) {
288  expression = new org.zorbaxquery.api.xqj.ZorbaXQPreparedExpression(this, value);
289  } else {
290  expression = new org.zorbaxquery.api.xqj.ZorbaXQPreparedExpression(this, value, lStaticContext);
291  }
292  cPreparedExpression.add(expression);
293  } catch (Exception e) {
294  throw new XQException("Error preparing expression: "+e.getLocalizedMessage());
295  }
296  return expression;
297  }
298 
299  /** \brief Prepares an expression for execution.
300  *
301  * The properties of the connection's default ZorbaXQStaticContext are copied to the returned ZorbaXQPreparedExpression.
302  *
303  * @param string - the XQuery expression as a String. Cannot be null
304  * @param xqsc - ZorbaXQStaticContext containing values of expression properties.
305  * @return the prepared XQuery expression
306  * @throw XQException - if (1) the connection is in a closed state, (2) there are errors preparing the expression, or (3) the xquery parameter is null
307  */
308  @Override
309  public XQPreparedExpression prepareExpression(String string, XQStaticContext xqsc) throws XQException {
310  isClosedXQException();
311  isNullXQException(string);
312  isNullXQException(xqsc);
313  XQPreparedExpression expression = null;
314  try {
315  expression = new org.zorbaxquery.api.xqj.ZorbaXQPreparedExpression(this, string, xqsc);
316  cPreparedExpression.add(expression);
317  } catch (Exception e) {
318  throw new XQException("Error preparing expression");
319  }
320  return expression;
321  }
322 
323  /** \brief Prepares an expression for execution.
324  *
325  * The properties of the connection's default ZorbaXQStaticContext are copied to the returned ZorbaXQPreparedExpression.
326  *
327  * @param reader - the XQuery expression as a Reader. Cannot be null
328  * @return the prepared XQuery expression
329  * @throw XQException - if (1) the connection is in a closed state, (2) there are errors preparing the expression, or (3) the xquery parameter is null
330  */
331  @Override
332  public XQPreparedExpression prepareExpression(Reader reader) throws XQException {
333  isClosedXQException();
334  isNullXQException(reader);
335 
336  StringBuffer string = new StringBuffer();
337  CharBuffer buffer = CharBuffer.allocate(1024);
338  Writer writer = new StringWriter();
339 
340  try {
341  while( reader.read(buffer) >= 0 ) {
342  buffer.flip();
343  writer.append(buffer);
344  buffer.clear();
345  }
346  reader.close();
347  } catch (Exception ex) {
348  throw new XQException("Error preparing expression" + ex.getLocalizedMessage());
349  }
350 
351  XQPreparedExpression expression;
352  if (lStaticContext == null) {
353  expression = new org.zorbaxquery.api.xqj.ZorbaXQPreparedExpression(this, writer.toString());
354  } else {
355  expression = new org.zorbaxquery.api.xqj.ZorbaXQPreparedExpression(this, writer.toString(), lStaticContext);
356  }
357  cPreparedExpression.add(expression);
358  return expression;
359  }
360 
361  /** \brief Prepares an expression for execution.
362  *
363  * The properties of the connection's default ZorbaXQStaticContext are copied to the returned ZorbaXQPreparedExpression.
364  *
365  * @param reader - the XQuery expression as a Reader. Cannot be null
366  * @param xqsc - ZorbaXQStaticContext containing values of expression properties
367  * @return the prepared XQuery expression
368  * @throw XQException - if (1) the connection is in a closed state, (2) there are errors preparing the expression, or (3) the xquery parameter is null
369  */
370  @Override
371  public XQPreparedExpression prepareExpression(Reader reader, XQStaticContext xqsc) throws XQException {
372  isClosedXQException();
373  isNullXQException(reader);
374  isNullXQException(xqsc);
375  StringBuffer string = new StringBuffer();
376  CharBuffer buffer = CharBuffer.allocate(1024);
377  Writer writer = new StringWriter();
378  try {
379  while( reader.read(buffer) >= 0 ) {
380  buffer.flip();
381  writer.append(buffer);
382  buffer.clear();
383  }
384  reader.close();
385  } catch (Exception ex) {
386  throw new XQException("Error preparing expression" + ex.getLocalizedMessage());
387  }
388 
389  XQPreparedExpression expression = new org.zorbaxquery.api.xqj.ZorbaXQPreparedExpression(this, writer.toString(), xqsc);
390 
391  cPreparedExpression.add(expression);
392  return expression;
393  }
394 
395  /** \brief Prepares an expression for execution.
396  *
397  * The properties of the connection's default ZorbaXQStaticContext are copied to the returned ZorbaXQPreparedExpression.
398  *
399  * @param in - the XQuery expression as an InputStream. Cannot be null
400  * @return the prepared XQuery expression
401  * @throw XQException - if (1) the connection is in a closed state, (2) there are errors preparing the expression, or (3) the xquery parameter is null
402  */
403  @Override
404  public XQPreparedExpression prepareExpression(InputStream in) throws XQException {
405  isClosedXQException();
406  isNullXQException(in);
407  StringBuffer out = new StringBuffer ();
408  try {
409  byte[] b = new byte[4096];
410  for (int n; (n = in.read(b)) != -1;) {
411  out.append(new String(b, 0, n));
412  }
413  } catch (Exception ex) {
414  throw new XQException("Error preparing expression" + ex.getLocalizedMessage());
415  }
416 
417  XQPreparedExpression expression;
418  if (lStaticContext == null) {
419  expression = new org.zorbaxquery.api.xqj.ZorbaXQPreparedExpression(this, out.toString());
420  } else {
421  expression = new org.zorbaxquery.api.xqj.ZorbaXQPreparedExpression(this, out.toString(), lStaticContext);
422  }
423  try {
424  cPreparedExpression.add(expression);
425  } catch (Exception ex) {
426  throw new XQException("Error preparing expression" + ex.getLocalizedMessage());
427  }
428 
429  return expression;
430  }
431 
432  /** \brief Prepares an expression for execution.
433  *
434  * The properties of the connection's default ZorbaXQStaticContext are copied to the returned ZorbaXQPreparedExpression.
435  *
436  * @param in - the XQuery expression as an InputStream. Cannot be null
437  * @param xqsc - ZorbaXQStaticContext containing values of expression properties
438  * @return the prepared XQuery expression
439  * @throw XQException - if (1) the connection is in a closed state, (2) there are errors preparing the expression, or (3) the xquery parameter is null
440  */
441  @Override
442  public XQPreparedExpression prepareExpression(InputStream in, XQStaticContext xqsc) throws XQException {
443  isClosedXQException();
444  isNullXQException(in);
445  isNullXQException(xqsc);
446  StringBuffer out = new StringBuffer ();
447  try {
448  byte[] b = new byte[4096];
449  for (int n; (n = in.read(b)) != -1;) {
450  out.append(new String(b, 0, n));
451  }
452  } catch (IOException ex) {
453  throw new XQException("Error preparing expression" + ex.getLocalizedMessage());
454  }
455 
456  XQPreparedExpression expression = null;
457  try {
458  expression = new org.zorbaxquery.api.xqj.ZorbaXQPreparedExpression(this, out.toString(), xqsc);
459  cPreparedExpression.add(expression);
460  } catch (Exception ex) {
461  throw new XQException("Error preparing expression" + ex.getLocalizedMessage());
462  }
463  return expression;
464  }
465 
466  /** \brief Undoes all changes made in the current transaction and releases any locks held by the datasource.
467  *
468  * This method should be used only when auto-commit mode is disabled.
469  *
470  * @throw XQException - if the connection is in a closed state or this connection is operating in auto-commit mode
471  */
472  @Override
473  public void rollback() throws XQException {
474  throw new UnsupportedOperationException("Zorba does not support transactions... yet...");
475  }
476 
477  /** \brief Gets an ZorbaXQStaticContext representing the default values for all expression properties.
478  *
479  * In order to modify the defaults, it is not sufficient to modify the values in the returned ZorbaXQStaticContext object; in addition setStaticContext should be called to make those new values effective.
480  *
481  * @return ZorbaXQStaticContext representing the default values for all expression properties
482  * @throw XQException - if the connection is in a closed state
483  */
484  @Override
485  public XQStaticContext getStaticContext() throws XQException {
486  isClosedXQException();
487  lStaticContext = new org.zorbaxquery.api.xqj.ZorbaXQStaticContext(zorba);
488  if (uriPaths!=null) {
489  ((org.zorbaxquery.api.xqj.ZorbaXQStaticContext)lStaticContext).setURIPaths(uriPaths);
490  }
491  if (libPaths!=null) {
492  ((org.zorbaxquery.api.xqj.ZorbaXQStaticContext)lStaticContext).setLIBPaths(libPaths);
493  }
494  if (modulePaths!=null) {
495  ((org.zorbaxquery.api.xqj.ZorbaXQStaticContext)lStaticContext).setMODPaths(modulePaths);
496  }
497  return lStaticContext;
498  }
499 
500  /** \brief Sets the default values for all expression properties.
501  *
502  * The implementation will read out all expression properties from the specified ZorbaXQStaticContext and update its private copy.
503  *
504  * @param xqsc - ZorbaXQStaticContext containing values of expression properties
505  * @throw XQException - if the connection is in a closed state
506  */
507  @Override
508  public void setStaticContext(XQStaticContext xqsc) throws XQException {
509  isClosedXQException();
510  isNullXQException(xqsc);
511  if ((lStaticContext!=null) && (lStaticContext!=xqsc)) { // delete previous static context
512  ((org.zorbaxquery.api.xqj.ZorbaXQStaticContext)lStaticContext).getZorbaStaticContext().delete();
513  }
514  lStaticContext = xqsc;
515  }
516 
517  /** \brief Creates an item from a given value.
518  *
519  * The value is converted into an instance of the specified type according to the casting from xs:string rules outlined in 17.1.1 Casting from xs:string and xs:untypedAtomic, XQuery 1.0 and XPath 2.0 Functions and Operators. If the cast fails an XQException is thrown.
520  *
521  * @param value - the lexical string value of the type
522  * @param type - the item type
523  * @return ZorbaXQItem representing the resulting item
524  * @throw XQException - if (1) any of the arguments are null, (2) given type is not an atomic type, (3) the conversion of the value to an XDM instance failed, or (4) the underlying object implementing the interface is closed
525  */
526  @Override
527  public XQItem createItemFromAtomicValue(String value, XQItemType type) throws XQException {
528  isClosedXQException();
529  isNullXQException(value);
530  isNullXQException(type);
531  if (type.getItemKind()!=XQItemType.XQITEMKIND_ATOMIC) {
532  throw new XQException("ItemType is not Atomic");
533  }
534  XQItem item = null;
535  try {
536  item = this.createItemFromString(value, type);
537  } catch (Exception e) {
538  throw new XQException("Error creating item of this type " + e.getLocalizedMessage());
539  }
540  return item;
541  }
542 
543  /** \brief Creates an item from a given value.
544  *
545  * The value is converted into an instance of the specified type, which must represent an xs:string or a type derived by restriction from xs:string. If the specified type is null, it defaults to xs:string.
546  * Subsequently the value is converted into an instance of the specified type according to the rule defined in 14.2 Mapping a Java Data Type to an XQuery Data Type, XQuery API for Java (XQJ) 1.0. If the conversion fails, an XQException will be thrown.
547  *
548  * @param value - the value to be converted, cannot be null
549  * @param type - the type of the value to be bound to the external variable. The default type, xs:string, is used in case null is specified
550  * @return ZorbaXQItem representing the resulting item
551  * @throw XQException - if (1) the value argument is null, (2) the conversion of the value to an XDM instance failed, or (3) the underlying object implementing the interface is closed
552  */
553  @Override
554  public XQItem createItemFromString(String value, XQItemType type) throws XQException {
555  isClosedXQException();
556  isNullXQException(value);
557  if (type==null) {
558  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_STRING);
559  }
560  ItemFactory itemFactory = zorba.getItemFactory();
561  XQItem item = null;
562  try {
563  switch (type.getBaseType()) {
564  case XQItemType.XQBASETYPE_BOOLEAN:
565  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createBoolean(Boolean.parseBoolean(value)), type);
566  break;
567  case XQItemType.XQBASETYPE_ANYURI:
568  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createAnyURI(value), type);
569  break;
570  case XQItemType.XQBASETYPE_BASE64BINARY:
571  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createBase64Binary(value, value.length()), type);
572  break;
573  case XQItemType.XQBASETYPE_BYTE:
574  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createByte(value.charAt(0)), type);
575  break;
576  case XQItemType.XQBASETYPE_DATE:
577  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDate(value), type);
578  break;
579  case XQItemType.XQBASETYPE_DATETIME:
580  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDateTime(value), type);
581  break;
582  case XQItemType.XQBASETYPE_DAYTIMEDURATION:
583  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDuration(value), type);
584  break;
585  case XQItemType.XQBASETYPE_DECIMAL:
586  BigDecimal dec = new BigDecimal(value);
587  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDecimal(value), type);
588  break;
589  case XQItemType.XQBASETYPE_DOUBLE:
590  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDouble(Double.parseDouble(value)), type);
591  break;
592  case XQItemType.XQBASETYPE_DURATION:
593  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDuration(value), type);
594  break;
595  case XQItemType.XQBASETYPE_FLOAT:
596  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createFloat(value), type);
597  break;
598  case XQItemType.XQBASETYPE_GDAY:
599  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGDay(value), type);
600  break;
601  case XQItemType.XQBASETYPE_GMONTH:
602  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGMonth(value), type);
603  break;
604  case XQItemType.XQBASETYPE_GMONTHDAY:
605  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGMonthDay(value), type);
606  break;
607  case XQItemType.XQBASETYPE_GYEAR:
608  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGYear(value), type);
609  break;
610  case XQItemType.XQBASETYPE_GYEARMONTH:
611  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGYearMonth(value), type);
612  break;
613  case XQItemType.XQBASETYPE_HEXBINARY:
614  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createHexBinary(value, value.length()), type);
615  break;
616  case XQItemType.XQBASETYPE_INT:
617  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createInt(Integer.parseInt(value)), type);
618  break;
619  case XQItemType.XQBASETYPE_INTEGER:
620  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createInteger(Integer.parseInt(value)), type);
621  break;
622  case XQItemType.XQBASETYPE_LONG:
623  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createLong(Long.parseLong(value)), type);
624  break;
625  case XQItemType.XQBASETYPE_NCNAME:
626  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNCName(value), type);
627  break;
628  case XQItemType.XQBASETYPE_NEGATIVE_INTEGER:
629  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNegativeInteger(Long.parseLong(value)), type);
630  break;
631  case XQItemType.XQBASETYPE_NONNEGATIVE_INTEGER:
632  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNonNegativeInteger(new BigInteger(value)), type);
633  break;
634  case XQItemType.XQBASETYPE_NONPOSITIVE_INTEGER:
635  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNonPositiveInteger(Long.parseLong(value)), type);
636  break;
637  case XQItemType.XQBASETYPE_POSITIVE_INTEGER:
638  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createPositiveInteger(new BigInteger(value)), type);
639  break;
640  case XQItemType.XQBASETYPE_QNAME:
641  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createQName(type.getSchemaURI().toString(), value), type);
642  break;
643  case XQItemType.XQBASETYPE_SHORT:
644  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createShort(Short.parseShort(value)), type);
645  break;
646  case XQItemType.XQBASETYPE_STRING:
647  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createString(value), type);
648  break;
649  case XQItemType.XQBASETYPE_TIME:
650  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createTime(value), type);
651  break;
652  case XQItemType.XQBASETYPE_UNSIGNED_BYTE:
653  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedByte(Short.parseShort(value)), type);
654  break;
655  case XQItemType.XQBASETYPE_UNSIGNED_INT:
656  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedInt(Long.parseLong(value)), type);
657  break;
658  case XQItemType.XQBASETYPE_UNSIGNED_LONG:
659  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedLong(new BigInteger(value)), type);
660  break;
661  case XQItemType.XQBASETYPE_UNSIGNED_SHORT:
662  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedShort(Integer.parseInt(value)), type);
663  break;
664  case XQItemType.XQBASETYPE_YEARMONTHDURATION:
665  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDuration(value), type);
666  break;
667  // TODO: revisit this posibilities
668  case XQItemType.XQBASETYPE_ANYATOMICTYPE:
669  case XQItemType.XQBASETYPE_ANYSIMPLETYPE:
670  case XQItemType.XQBASETYPE_ENTITY:
671  case XQItemType.XQBASETYPE_ENTITIES:
672  case XQItemType.XQBASETYPE_ANYTYPE:
673  case XQItemType.XQBASETYPE_LANGUAGE:
674  case XQItemType.XQBASETYPE_ID:
675  case XQItemType.XQBASETYPE_IDREF:
676  case XQItemType.XQBASETYPE_IDREFS:
677  case XQItemType.XQBASETYPE_NAME:
678  case XQItemType.XQBASETYPE_NMTOKEN:
679  case XQItemType.XQBASETYPE_NMTOKENS:
680  case XQItemType.XQBASETYPE_NORMALIZED_STRING:
681  case XQItemType.XQBASETYPE_NOTATION:
682  case XQItemType.XQBASETYPE_TOKEN:
683  case XQItemType.XQBASETYPE_UNTYPED:
684  case XQItemType.XQBASETYPE_UNTYPEDATOMIC:
685  throw new UnsupportedOperationException("Not supported yet.");
686  default:
687 
688  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createString(value), type);
689  break;
690  }
691  } catch (Exception e) {
692  throw new XQException ("Error parsing value" + e.getLocalizedMessage());
693  }
694  return item;
695  }
696 
697  /** \brief Creates an item from the given value.
698  *
699  * If the value represents a well-formed XML document, it will be parsed and results in a document node. The kind of the input type must be null, XQITEMKIND_DOCUMENT_ELEMENT, or XQITEMKIND_DOCUMENT_SCHEMA_ELEMENT.
700  *
701  * The value is converted into an instance of the specified type according to the rules defined in 14.3 Mapping a Java XML document to an XQuery document node, XQuery API for Java (XQJ) 1.0.
702  *
703  * If the value is not well formed, or if a kind of the input type other than the values list above is specified, behavior is implementation defined and may raise an exception.
704  *
705  * @param value - the value to be converted, cannot be null
706  * @param baseURI - an optional base URI, can be null. It can be used, for example, to resolve relative URIs and to include in error messages.
707  * @param type - the type of the value for the created document node. If null is specified, it behaves as if XQDataFactory.createDocumentElementType( XQDataFactory.createElementType(null, ZorbaXQItemType.XQBASETYPE_XS_UNTYPED)) were passed in as the type parameter. That is, the type represents the XQuery sequence type document-node(element(*, xs:untyped))
708  * @return ZorbaXQItem representing the resulting item
709  * @throw XQException - if (1) the value argument is null, (2) the conversion of the value to an XDM instance failed, or (3) the underlying object implementing the interface is closed
710  */
711  @Override
712  public XQItem createItemFromDocument(String value, String baseURI, XQItemType type) throws XQException {
713  isClosedXQException();
714  isNullXQException(value);
715  if (type!=null) {
716  if ( ! ((type.getItemKind()== XQItemType.XQITEMKIND_DOCUMENT_ELEMENT)||(type.getItemKind()== XQItemType.XQITEMKIND_DOCUMENT_SCHEMA_ELEMENT)) ) {
717  throw new XQException("Invalid type");
718  }
719  } else {
720  type = this.createElementType(null, XQItemType.XQBASETYPE_UNTYPED);
721  }
722 
723  XmlDataManager dm = null;
724  Item doc = null;
725  XQItem item = null;
726  try {
727  dm = zorba.getXmlDataManager();
728  doc = new Item();
729  org.zorbaxquery.api.Iterator iterator = dm.parseXML(value);
730  iterator.open();
731  iterator.next(doc);
732  iterator.close();
733  iterator.delete();
734  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(doc);
735  } catch (Exception e) {
736  throw new XQException("Error creating Item" + e.getLocalizedMessage());
737  }
738  return item;
739  }
740 
741  /** \brief Creates an item from the given value.
742  *
743  * If the value represents a well-formed XML document, it will be parsed and results in a document node. The kind of the input type must be null, XQITEMKIND_DOCUMENT_ELEMENT, or XQITEMKIND_DOCUMENT_SCHEMA_ELEMENT.
744  *
745  * The value is converted into an instance of the specified type according to the rules defined in 14.3 Mapping a Java XML document to an XQuery document node, XQuery API for Java (XQJ) 1.0.
746  *
747  * If the value is not well formed, or if a kind of the input type other than the values list above is specified, behavior is implementation defined and may raise an exception.
748  *
749  * @param value - the value to be converted, cannot be null
750  * @param baseURI - an optional base URI, can be null. It can be used, for example, to resolve relative URIs and to include in error messages.
751  * @param type - the type of the value for the created document node. If null is specified, it behaves as if XQDataFactory.createDocumentElementType( XQDataFactory.createElementType(null, ZorbaXQItemType.XQBASETYPE_XS_UNTYPED)) were passed in as the type parameter. That is, the type represents the XQuery sequence type document-node(element(*, xs:untyped))
752  * @return ZorbaXQItem representing the resulting item
753  * @throw XQException - if (1) the value argument is null, (2) the conversion of the value to an XDM instance failed, or (3) the underlying object implementing the interface is closed
754  */
755  @Override
756  public XQItem createItemFromDocument(Reader value, String baseURI, XQItemType type) throws XQException {
757  isClosedXQException();
758  isNullXQException(value);
759 
760  StringBuffer string = new StringBuffer();
761  CharBuffer buffer = CharBuffer.allocate(1024);
762  Writer writer = new StringWriter();
763 
764  try {
765  while( value.read(buffer) >= 0 ) {
766  buffer.flip();
767  writer.append(buffer);
768  buffer.clear();
769  }
770  value.close();
771  } catch (Exception ex) {
772  throw new XQException("Error preparing expression" + ex.getLocalizedMessage());
773  }
774 
775  XQItem item = createItemFromDocument(writer.toString(), baseURI, type);
776  return item;
777  }
778 
779  /** \brief Creates an item from the given value.
780  *
781  * If the value represents a well-formed XML document, it will be parsed and results in a document node. The kind of the input type must be null, XQITEMKIND_DOCUMENT_ELEMENT, or XQITEMKIND_DOCUMENT_SCHEMA_ELEMENT.
782  *
783  * The value is converted into an instance of the specified type according to the rules defined in 14.3 Mapping a Java XML document to an XQuery document node, XQuery API for Java (XQJ) 1.0.
784  *
785  * If the value is not well formed, or if a kind of the input type other than the values list above is specified, behavior is implementation defined and may raise an exception.
786  *
787  * @param value - the value to be converted, cannot be null
788  * @param baseURI - an optional base URI, can be null. It can be used, for example, to resolve relative URIs and to include in error messages.
789  * @param type - the type of the value for the created document node. If null is specified, it behaves as if XQDataFactory.createDocumentElementType( XQDataFactory.createElementType(null, ZorbaXQItemType.XQBASETYPE_XS_UNTYPED)) were passed in as the type parameter. That is, the type represents the XQuery sequence type document-node(element(*, xs:untyped))
790  * @return ZorbaXQItem representing the resulting item
791  * @throw XQException - if (1) the value argument is null, (2) the conversion of the value to an XDM instance failed, or (3) the underlying object implementing the interface is closed
792  */
793  @Override
794  public XQItem createItemFromDocument(InputStream value, String baseURI, XQItemType type) throws XQException {
795  isClosedXQException();
796  isNullXQException(value);
797  // TODO: Rodolfo: optimize this, not good to have a string
798  StringBuffer out = new StringBuffer ();
799  try {
800  byte[] b = new byte[4096];
801  for (int n; (n = value.read(b)) != -1;) {
802  out.append(new String(b, 0, n));
803  }
804  } catch (Exception ex) {
805  throw new XQException("Error preparing expression" + ex.getLocalizedMessage());
806  }
807  XQItem item = createItemFromDocument(out.toString(), baseURI, type);
808  return item;
809  }
810 
811  /** \brief Creates an item from the given value.
812  *
813  * If the value represents a well-formed XML document, it will be parsed and results in a document node. The kind of the input type must be null, XQITEMKIND_DOCUMENT_ELEMENT, or XQITEMKIND_DOCUMENT_SCHEMA_ELEMENT.
814  *
815  * The value is converted into an instance of the specified type according to the rules defined in 14.3 Mapping a Java XML document to an XQuery document node, XQuery API for Java (XQJ) 1.0.
816  *
817  * If the value is not well formed, or if a kind of the input type other than the values list above is specified, behavior is implementation defined and may raise an exception.
818  *
819  * @param value - the value to be converted, cannot be null
820  * @param type - the type of the value for the created document node. If null is specified, it behaves as if XQDataFactory.createDocumentElementType( XQDataFactory.createElementType(null, ZorbaXQItemType.XQBASETYPE_XS_UNTYPED)) were passed in as the type parameter. That is, the type represents the XQuery sequence type document-node(element(*, xs:untyped))
821  * @return ZorbaXQItem representing the resulting item
822  * @throw XQException - if (1) the value argument is null, (2) the conversion of the value to an XDM instance failed, or (3) the underlying object implementing the interface is closed
823  */
824  @Override
825  public XQItem createItemFromDocument(XMLStreamReader value, XQItemType type) throws XQException {
826  isClosedXQException();
827  isNullXQException(value);
828  TransformerFactory tf = TransformerFactory.newInstance();
829  Transformer t;
830  StAXSource source;
831  StAXResult result;
832  XMLOutputFactory xof = XMLOutputFactory.newInstance();
833  Writer writer = new StringWriter();
834  try {
835  XMLStreamWriter xmlStreamWriter = xof.createXMLStreamWriter(writer);
836  t = tf.newTransformer();
837  source = new StAXSource(value);
838  result = new StAXResult(xmlStreamWriter);
839  t.transform(source, result);
840  } catch (Exception ex) {
841  throw new XQException("Error transforming xml expression" + ex.getLocalizedMessage());
842  }
843  XQItem item = createItemFromDocument(writer.toString(), "", type);
844  return item;
845  }
846 
847  private String nodeToString(Node node) {
848  StringWriter sw = new StringWriter();
849  try {
850  Transformer t = TransformerFactory.newInstance().newTransformer();
851  t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
852  t.transform(new DOMSource(node), new StreamResult(sw));
853  } catch (TransformerException te) {
854  System.out.println("nodeToString Transformer Exception" + te.getLocalizedMessage());
855  }
856  return sw.toString();
857  }
858 
859  /** \brief Creates an item from the given value.
860  *
861  * An XQJ implementation must at least support the following implementations:
862  * - javax.xml.transform.dom.DOMSource
863  * - javax.xml.transform.sax.SAXSource
864  * - javax.xml.transform.stream.StreamSource
865  *
866  * If the value represents a well-formed XML document, it will result in a document node. The kind of the input type must be null, XQITEMKIND_DOCUMENT_ELEMENT, or XQITEMKIND_DOCUMENT_SCHEMA_ELEMENT.
867  *
868  * The value is converted into an instance of the specified type according to the rules defined in 14.3 Mapping a Java XML document to an XQuery document node, XQuery API for Java (XQJ) 1.0.
869  *
870  * If the value is not well formed, or if a kind of the input type other than the values list above is specified, behavior is implementation defined and may raise an exception.
871  *
872  * @param value - the value to be converted, cannot be null
873  * @param type - the type of the value for the created document node. If null is specified, it behaves as if XQDataFactory.createDocumentElementType( XQDataFactory.createElementType(null, ZorbaXQItemType.XQBASETYPE_XS_UNTYPED)) were passed in as the type parameter. That is, the type represents the XQuery sequence type document-node(element(*, xs:untyped))
874  * @return ZorbaXQItem representing the resulting item
875  * @throw XQException - if (1) the value argument is null, (2) the conversion of the value to an XDM instance failed, or (3) the underlying object implementing the interface is closed
876  */
877  @Override
878  public XQItem createItemFromDocument(Source value, XQItemType type) throws XQException {
879  isClosedXQException();
880  isNullXQException(value);
881  XQItem item = null;
882  if (value instanceof StreamSource) {
883  item = createItemFromDocument(((StreamSource)value).getReader(), "", type);
884  } else if (value instanceof SAXSource) {
885  item = createItemFromDocument(((SAXSource)value).getInputSource().getByteStream(), "", type);
886  } else if (value instanceof DOMSource) {
887  item = createItemFromDocument( nodeToString(((DOMSource)value).getNode()), "", type);
888  } else {
889  throw new UnsupportedOperationException("Not supported yet.");
890  }
891  return item;
892 
893  }
894 
895  /** \brief Creates an item from a given value.
896  *
897  * The value is converted into an instance of the specified type according to the rule defined in 14.2 Mapping a Java Data Type to an XQuery Data Type, XQuery API for Java (XQJ) 1.0. If the converstion fails, an XQException will be thrown.
898  *
899  * @param value - the value to be converted
900  * @param type - the type of the value to be bound to the external variable. The default type of the value is used in case null is specified
901  * @return ZorbaXQItem representing the resulting item
902  * @throw XQException - (1) the conversion of the value to an XDM instance failed, or (2) the underlying object implementing the interface is closed
903  */
904  @Override
905  public XQItem createItemFromObject(Object value, XQItemType type) throws XQException {
906  isClosedXQException();
907  isNullXQException(value);
908  if (value instanceof XQItem) {
909  return (XQItem)value;
910  }
911 
912  ItemFactory itemFactory = zorba.getItemFactory();
913  XQItem item = null;
914  if (type==null) {
915 
916  if (value instanceof Boolean) {
917  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_BOOLEAN);
918  } else if (value instanceof Byte) {
919  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_BYTE);
920  } else if (value instanceof Float) {
921  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_FLOAT);
922  } else if (value instanceof Double) {
923  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_DOUBLE);
924  } else if (value instanceof Integer) {
925  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_INT);
926  } else if (value instanceof Long) {
927  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_LONG);
928  } else if (value instanceof Short) {
929  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_SHORT);
930  } else if (value instanceof String) {
931  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_STRING);
932  } else if (value instanceof BigDecimal) {
933  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_DECIMAL);
934  } else if (value instanceof BigInteger) {
935  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_INTEGER);
936  } else if (value instanceof Duration) {
937  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_DURATION);
938  } else if (value instanceof XMLGregorianCalendar) {
939  QName schType = ((XMLGregorianCalendar)value).getXMLSchemaType();
940  if (schType.equals(DatatypeConstants.GDAY)) {
941  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_GDAY);
942  } else if (schType.equals(DatatypeConstants.GMONTHDAY)) {
943  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_GMONTHDAY);
944  } else if (schType.equals(DatatypeConstants.GMONTH)) {
945  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_GMONTH);
946  } else if (schType.equals(DatatypeConstants.GYEAR)) {
947  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_GYEAR);
948  } else if (schType.equals(DatatypeConstants.GYEARMONTH)) {
949  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_GYEARMONTH);
950  } else if (schType.equals(DatatypeConstants.DATETIME)) {
951  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_DATETIME);
952  } else if (schType.equals(DatatypeConstants.DATE)) {
953  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_DATE);
954  } else if (schType.equals(DatatypeConstants.TIME)) {
955  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_TIME);
956  }
957  } else if (value instanceof QName) {
958  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_QNAME);
959  } else if (value instanceof Document) {
960  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_DOCUMENT);
961  } else if (value instanceof DocumentFragment) {
962  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_DOCUMENT);
963  } else if (value instanceof Element){
964  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ELEMENT);
965  } else if (value instanceof Attr) {
966  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATTRIBUTE);
967  } else if (value instanceof Comment) {
968  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_COMMENT);
969  } else if (value instanceof ProcessingInstruction) {
970  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_PI);
971  } else if (value instanceof Text) {
972  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_TEXT);
973  } else {
974  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC);
975  }
976  }
977  try {
978  switch (type.getItemKind()) {
979  case XQItemType.XQITEMKIND_ATOMIC:
980  switch (type.getBaseType()) {
981  case XQItemType.XQBASETYPE_BOOLEAN:
982  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createBoolean((Boolean)value), type);
983  break;
984  case XQItemType.XQBASETYPE_ANYURI:
985  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createAnyURI(value.toString()), type);
986  break;
987  case XQItemType.XQBASETYPE_BASE64BINARY:
988  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createBase64Binary(value.toString(), (value.toString()).length()), type);
989  break;
990  case XQItemType.XQBASETYPE_BYTE:
991  if (value instanceof Byte) {
992  byte tmpByte = ((Byte)value).byteValue();
993  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createByte((char)tmpByte), type);
994  } else {
995  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createByte((value.toString()).charAt(0)), type);
996  }
997  break;
998  case XQItemType.XQBASETYPE_DATE:
999  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDate(value.toString()), type);
1000  break;
1001  case XQItemType.XQBASETYPE_DATETIME:
1002  if (value.toString().contains(":")) {
1003  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDateTime(value.toString()), type);
1004  } else {
1005  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDate(value.toString()), type);
1006  }
1007  break;
1008  case XQItemType.XQBASETYPE_DAYTIMEDURATION:
1009  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDuration(value.toString()), type);
1010  break;
1011  case XQItemType.XQBASETYPE_DECIMAL:
1012  if (value instanceof BigDecimal) {
1013  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDecimal(((BigDecimal)value).toPlainString()), type);
1014  } else {
1015  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDecimal(value.toString()), type);
1016  }
1017  break;
1018  case XQItemType.XQBASETYPE_DOUBLE:
1019  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDouble(value.toString()), type);
1020  break;
1021  case XQItemType.XQBASETYPE_DURATION:
1022  if (value instanceof Duration) {
1023  Duration duration = (Duration)value;
1024  Boolean daytime = duration.isSet(DatatypeConstants.DAYS) || duration.isSet(DatatypeConstants.HOURS) ||
1025  duration.isSet(DatatypeConstants.MINUTES) || duration.isSet(DatatypeConstants.SECONDS);
1026  Boolean yearmonth = duration.isSet(DatatypeConstants.YEARS) || duration.isSet(DatatypeConstants.MONTHS);
1027  if (daytime && yearmonth){
1028  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDuration(value.toString()), type);
1029  } else if (yearmonth) {
1030  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createYearMonthDuration(value.toString()), type);
1031  } else if (daytime) {
1032  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDayTimeDuration(value.toString()), type);
1033  }
1034  } else {
1035  boolean yearMonth = value.toString().contains("Y") || value.toString().contains("M");
1036  boolean dayTime = value.toString().contains("D") || value.toString().contains("T");
1037  if (yearMonth && dayTime) {
1038  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDuration(value.toString()), type);
1039  } else if (yearMonth) {
1040  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createYearMonthDuration(value.toString()), type);
1041  } else if (dayTime) {
1042  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDayTimeDuration(value.toString()), type);
1043  }
1044  }
1045  break;
1046  case XQItemType.XQBASETYPE_FLOAT:
1047  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createFloat((Float)value), type);
1048  break;
1049  case XQItemType.XQBASETYPE_GDAY:
1050  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGDay(value.toString()), type);
1051  break;
1052  case XQItemType.XQBASETYPE_GMONTH:
1053  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGMonth(value.toString()), type);
1054  break;
1055  case XQItemType.XQBASETYPE_GMONTHDAY:
1056  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGMonthDay(value.toString()), type);
1057  break;
1058  case XQItemType.XQBASETYPE_GYEAR:
1059  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGYear(value.toString()), type);
1060  break;
1061  case XQItemType.XQBASETYPE_GYEARMONTH:
1062  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGYearMonth(value.toString()), type);
1063  break;
1064  case XQItemType.XQBASETYPE_HEXBINARY:
1065  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createHexBinary(value.toString(), (value.toString()).length()), type);
1066  break;
1067  case XQItemType.XQBASETYPE_INT:
1068  case XQItemType.XQBASETYPE_INTEGER:
1069  if (value instanceof BigInteger) {
1070  BigInteger val = (BigInteger)value;
1071  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createInteger(val.longValue()), type);
1072  } else if (value instanceof Integer) {
1073  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createInt((Integer)value), type);
1074  } else if (value instanceof String) {
1075  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createInteger(value.toString()), type);
1076  } else {
1077  throw new XQException ("Error parsing integer: " + value.toString());
1078  }
1079  break;
1080  case XQItemType.XQBASETYPE_LONG:
1081  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createLong((Long)value), type);
1082  break;
1083  case XQItemType.XQBASETYPE_NCNAME:
1084  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNCName(value.toString()), type);
1085  break;
1086  case XQItemType.XQBASETYPE_NEGATIVE_INTEGER:
1087  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNegativeInteger((Long)value), type);
1088  break;
1089  case XQItemType.XQBASETYPE_NONNEGATIVE_INTEGER:
1090  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNonNegativeInteger(new BigInteger(value.toString())), type);
1091  break;
1092  case XQItemType.XQBASETYPE_NONPOSITIVE_INTEGER:
1093  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNonPositiveInteger((Long)value), type);
1094  break;
1095  case XQItemType.XQBASETYPE_POSITIVE_INTEGER:
1096  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createPositiveInteger(new BigInteger(value.toString())), type);
1097  break;
1098  case XQItemType.XQBASETYPE_QNAME:
1099  if (value instanceof QName) {
1100  QName qname = (QName) value;
1101  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createQName(qname.getNamespaceURI(), qname.getPrefix(), qname.getLocalPart()), type);
1102  } else if (value.toString().contains("{")) {
1103  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createQName(value.toString()), type);
1104  } else {
1105  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createQName("", value.toString()), type);
1106  }
1107  break;
1108  case XQItemType.XQBASETYPE_SHORT:
1109  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createShort((Short)value), type);
1110  break;
1111  case XQItemType.XQBASETYPE_STRING:
1112  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createString(value.toString()), type);
1113  break;
1114  case XQItemType.XQBASETYPE_TIME:
1115  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createTime(value.toString()), type);
1116  break;
1117  case XQItemType.XQBASETYPE_TOKEN:
1118  break;
1119  case XQItemType.XQBASETYPE_UNSIGNED_BYTE:
1120  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedByte((Short)value), type);
1121  break;
1122  case XQItemType.XQBASETYPE_UNSIGNED_INT:
1123  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedInt((Long)value), type);
1124  break;
1125  case XQItemType.XQBASETYPE_UNSIGNED_LONG:
1126  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedLong(new BigInteger(value.toString())), type);
1127  break;
1128  case XQItemType.XQBASETYPE_UNSIGNED_SHORT:
1129  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedShort((Integer)value), type);
1130  break;
1131  case XQItemType.XQBASETYPE_YEARMONTHDURATION:
1132  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDuration(value.toString()), type);
1133  break;
1134  case XQItemType.XQBASETYPE_ANYATOMICTYPE:
1135  case XQItemType.XQBASETYPE_ANYSIMPLETYPE:
1136  case XQItemType.XQBASETYPE_ANYTYPE:
1137  case XQItemType.XQBASETYPE_ENTITIES:
1138  case XQItemType.XQBASETYPE_ENTITY:
1139  case XQItemType.XQBASETYPE_ID:
1140  case XQItemType.XQBASETYPE_IDREF:
1141  case XQItemType.XQBASETYPE_IDREFS:
1142  case XQItemType.XQBASETYPE_LANGUAGE:
1143  case XQItemType.XQBASETYPE_NAME:
1144  case XQItemType.XQBASETYPE_NMTOKEN:
1145  case XQItemType.XQBASETYPE_NMTOKENS:
1146  case XQItemType.XQBASETYPE_NORMALIZED_STRING:
1147  case XQItemType.XQBASETYPE_NOTATION:
1148  case XQItemType.XQBASETYPE_UNTYPED:
1149  case XQItemType.XQBASETYPE_UNTYPEDATOMIC:
1150  throw new XQException ("Base type not supported yet.");
1151  default:
1152  throw new XQException ("Unable to determine XQBASETYPE.");
1153  }
1154  break;
1155  case XQItemType.XQITEMKIND_ATTRIBUTE:
1156  if (value instanceof Attr){
1157  Attr attribute = (Attr) value;
1158  Item iEmpty = new Item();
1159  Item nodeName = itemFactory.createQName(attribute.getNamespaceURI()==null?"":attribute.getNamespaceURI(),
1160  attribute.getPrefix()==null?"":attribute.getPrefix(),
1161  attribute.getName()==null?"":attribute.getName());
1162  Item nodeValue = null;
1163  if (!attribute.getValue().isEmpty()) {
1164  nodeValue = itemFactory.createQName(attribute.getNamespaceURI()==null?"":attribute.getNamespaceURI(),
1165  attribute.getPrefix()==null?"":attribute.getPrefix(),
1166  attribute.getValue()==null?"":attribute.getValue());
1167  } else {
1168  nodeValue = new Item();
1169  }
1170  //TODO: Rodolfo: use centralized constants instead of strings
1171  Item iUntyped = itemFactory.createQName("http://www.w3.org/2001/XMLSchema", "xs", "untyped");
1172  Item attributeNode = itemFactory.createAttributeNode(iEmpty, nodeName, iUntyped, nodeValue);
1173  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(attributeNode, type);
1174  }
1175  break;
1176  case XQItemType.XQITEMKIND_COMMENT:
1177  if (value instanceof Comment){
1178  Comment comment = (Comment) value;
1179  Item iEmpty = new Item();
1180  Item elementNode = itemFactory.createCommentNode(iEmpty, comment.getTextContent());
1181  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(elementNode, type);
1182  } else {
1183  Item iEmpty = new Item();
1184  Item elementNode = itemFactory.createCommentNode(iEmpty, value.toString());
1185  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(elementNode, type);
1186  }
1187  break;
1188  case XQItemType.XQITEMKIND_DOCUMENT:
1189  if ((value instanceof Document) || (value instanceof DocumentFragment)){
1190  DOMSource source = null;
1191  if (value instanceof Document) {
1192  source = new DOMSource((Document)value);
1193  } else {
1194  source = new DOMSource((DocumentFragment)value);
1195  }
1196 
1197  Transformer transformer = TransformerFactory.newInstance().newTransformer();
1198  transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
1199  StreamResult result = new StreamResult(new StringWriter());
1200 
1201  transformer.transform(source, result);
1202  String xmlString = result.getWriter().toString();
1203  Item tmpItem = new Item();
1204  if (xmlString.length()>0) {
1205  XmlDataManager dataManager = zorba.getXmlDataManager();
1206  org.zorbaxquery.api.Iterator iter = dataManager.parseXML(xmlString);
1207  iter.open();
1208  iter.next(tmpItem);
1209  iter.close();
1210  iter.delete();
1211  } else {
1212  tmpItem = itemFactory.createDocumentNode("", "");
1213  }
1214 
1215  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(tmpItem, type);
1216  }
1217 
1218  break;
1219  case XQItemType.XQITEMKIND_ELEMENT:
1220  if (value instanceof Element){
1221  Element element = (Element) value;
1222  Item iEmpty = new Item();
1223  Item nodeName = itemFactory.createQName(element.getNamespaceURI()==null?"":element.getNamespaceURI(),
1224  element.getPrefix()==null?"":element.getPrefix(),
1225  element.getNodeName()==null?"":element.getNodeName());
1226  Item iUntyped = itemFactory.createQName("http://www.w3.org/2001/XMLSchema", "xs", "untyped");
1227  Item elementNode = itemFactory.createElementNode(iEmpty, nodeName, iUntyped, false, false);
1228  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(elementNode, type);
1229  }
1230  break;
1231  case XQItemType.XQITEMKIND_PI:
1232  if (value instanceof ProcessingInstruction){
1233  ProcessingInstruction pi = (ProcessingInstruction) value;
1234  Item iEmpty = new Item();
1235  Item elementNode = itemFactory.createPiNode(iEmpty,
1236  pi.getTarget(),
1237  pi.getTextContent(),
1238  pi.getBaseURI()==null?"":pi.getBaseURI());
1239  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(elementNode, type);
1240  }
1241  break;
1242  case XQItemType.XQITEMKIND_TEXT:
1243  if (value instanceof Text){
1244  Text text = (Text) value;
1245  Item iEmpty = new Item();
1246  Item elementNode = itemFactory.createTextNode(iEmpty, text.getWholeText());
1247  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(elementNode, type);
1248  }
1249  break;
1250  case XQItemType.XQITEMKIND_DOCUMENT_ELEMENT:
1251  case XQItemType.XQITEMKIND_DOCUMENT_SCHEMA_ELEMENT:
1252  case XQItemType.XQITEMKIND_ITEM:
1253  case XQItemType.XQITEMKIND_NODE:
1254  case XQItemType.XQITEMKIND_SCHEMA_ATTRIBUTE:
1255  case XQItemType.XQITEMKIND_SCHEMA_ELEMENT:
1256  throw new XQException ("This item kind is currently unsupported.");
1257  default:
1258  throw new XQException ("Unable to determine XQITEMKIND.");
1259  }
1260  } catch (Exception e) {
1261  throw new XQException ("Error parsing value" + e.getLocalizedMessage());
1262  }
1263  return item;
1264  }
1265 
1266  @Override
1267  public XQItem createItemFromBoolean(boolean bln, XQItemType type) throws XQException {
1268  isClosedXQException();
1269  if (type == null) {
1270  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_BOOLEAN);
1271  }
1272  if (type.getBaseType() != XQItemType.XQBASETYPE_BOOLEAN) {
1273  throw new XQException("Type is not boolean, use proper method");
1274  }
1275 
1276  XQItem item = null;
1277  try {
1278  ItemFactory itemFactory = zorba.getItemFactory();
1279  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createBoolean(bln));
1280  } catch (Exception e) {
1281  throw new XQException("Error creating new item");
1282  }
1283  return item;
1284  }
1285 
1286  private XQItem createDecimal(BigDecimal value, XQItemType type) throws XQException {
1287  XQItem item = null;
1288  try {
1289  ItemFactory itemFactory = zorba.getItemFactory();
1290  switch (type.getBaseType()) {
1291  case XQItemType.XQBASETYPE_BYTE:
1292  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createByte((char)value.byteValue()), type);
1293  break;
1294  case XQItemType.XQBASETYPE_INTEGER:
1295  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createInteger(value.longValue()), type);
1296  break;
1297  case XQItemType.XQBASETYPE_DECIMAL:
1298  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDecimalFromLong(value.longValue()), type);
1299  break;
1300  case XQItemType.XQBASETYPE_INT:
1301  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createInt(value.intValue()), type);
1302  break;
1303  case XQItemType.XQBASETYPE_LONG:
1304  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createLong(value.longValue()), type);
1305  break;
1306  case XQItemType.XQBASETYPE_NEGATIVE_INTEGER:
1307  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNegativeInteger(value.longValue()), type);
1308  break;
1309  case XQItemType.XQBASETYPE_NONNEGATIVE_INTEGER:
1310  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNonNegativeInteger(value.toBigInteger()), type);
1311  break;
1312  case XQItemType.XQBASETYPE_NONPOSITIVE_INTEGER:
1313  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNonPositiveInteger(value.longValue()), type);
1314  break;
1315  case XQItemType.XQBASETYPE_POSITIVE_INTEGER:
1316  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createPositiveInteger(value.toBigInteger()), type);
1317  break;
1318  case XQItemType.XQBASETYPE_SHORT:
1319  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createShort(value.shortValue()), type);
1320  break;
1321  case XQItemType.XQBASETYPE_UNSIGNED_BYTE:
1322  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedByte(value.shortValue()), type);
1323  break;
1324  case XQItemType.XQBASETYPE_UNSIGNED_INT:
1325  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedInt(value.longValue()), type);
1326  break;
1327  case XQItemType.XQBASETYPE_UNSIGNED_LONG:
1328  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedLong(value.toBigInteger()), type);
1329  break;
1330  case XQItemType.XQBASETYPE_UNSIGNED_SHORT:
1331  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createShort(value.shortValue()), type);
1332  break;
1333  default:
1334  throw new XQException("Type is not xs:decimal or a derivate.");
1335  }
1336  } catch (Exception e) {
1337  throw new XQException("Error creating Item: " + e.getLocalizedMessage());
1338  }
1339  return item;
1340  }
1341 
1342  /** \brief Creates an item from a given value.
1343  *
1344  * The value is converted into an instance of the specified type according to the rule defined in 14.2 Mapping a Java Data Type to an XQuery Data Type, XQuery API for Java (XQJ) 1.0. If the converstion fails, an XQException will be thrown.
1345  *
1346  * @param b - the value to be converted
1347  * @param type - the type of the value to be bound to the external variable. The default type of the value is used in case null is specified
1348  * @return ZorbaXQItem representing the resulting item
1349  * @throw XQException - (1) the conversion of the value to an XDM instance failed, or (2) the underlying object implementing the interface is closed
1350  */
1351  @Override
1352  public XQItem createItemFromByte(byte b, XQItemType type) throws XQException {
1353  isClosedXQException();
1354  if (type == null) {
1355  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_BYTE);
1356  }
1357  return createDecimal(new BigDecimal(b), type);
1358  }
1359 
1360  /** \brief Creates an item from a given value.
1361  *
1362  * The value is converted into an instance of the specified type according to the rule defined in 14.2 Mapping a Java Data Type to an XQuery Data Type, XQuery API for Java (XQJ) 1.0. If the converstion fails, an XQException will be thrown.
1363  *
1364  * @param value - the value to be converted
1365  * @param type - the type of the value to be bound to the external variable. The default type of the value is used in case null is specified
1366  * @return ZorbaXQItem representing the resulting item
1367  * @throw XQException - (1) the conversion of the value to an XDM instance failed, or (2) the underlying object implementing the interface is closed
1368  */
1369  @Override
1370  public XQItem createItemFromDouble(double value, XQItemType type) throws XQException {
1371  isClosedXQException();
1372  if (type == null) {
1373  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_DOUBLE);
1374  }
1375  if (type.getBaseType() != XQItemType.XQBASETYPE_DOUBLE) {
1376  throw new XQException("Type is not double, use proper method");
1377  }
1378  XQItem item = null;
1379  try {
1380  ItemFactory itemFactory = zorba.getItemFactory();
1381  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDouble(value), type);
1382  } catch (Exception e) {
1383  throw new XQException("Error Creating Item From Double" + e.getLocalizedMessage());
1384  }
1385  return item;
1386  }
1387 
1388  /** \brief Creates an item from a given value.
1389  *
1390  * The value is converted into an instance of the specified type according to the rule defined in 14.2 Mapping a Java Data Type to an XQuery Data Type, XQuery API for Java (XQJ) 1.0. If the converstion fails, an XQException will be thrown.
1391  *
1392  * @param value - the value to be converted
1393  * @param type - the type of the value to be bound to the external variable. The default type of the value is used in case null is specified
1394  * @return ZorbaXQItem representing the resulting item
1395  * @throw XQException - (1) the conversion of the value to an XDM instance failed, or (2) the underlying object implementing the interface is closed
1396  */
1397  @Override
1398  public XQItem createItemFromFloat(float value, XQItemType type) throws XQException {
1399  isClosedXQException();
1400  if (type == null) {
1401  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_FLOAT);
1402  }
1403  if (type.getBaseType() != XQItemType.XQBASETYPE_FLOAT) {
1404  throw new XQException("Type is not float, use proper method");
1405  }
1406  XQItem item = null;
1407  try {
1408  ItemFactory itemFactory = zorba.getItemFactory();
1409  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createFloat(value), type);
1410  } catch (Exception e) {
1411  throw new XQException("Error Creating Item From Float" + e.getLocalizedMessage());
1412  }
1413  return item;
1414  }
1415 
1416  /** \brief Creates an item from a given value.
1417  *
1418  * The value is converted into an instance of the specified type according to the rule defined in 14.2 Mapping a Java Data Type to an XQuery Data Type, XQuery API for Java (XQJ) 1.0. If the converstion fails, an XQException will be thrown.
1419  *
1420  * @param value - the value to be converted
1421  * @param type - the type of the value to be bound to the external variable. The default type of the value is used in case null is specified
1422  * @return ZorbaXQItem representing the resulting item
1423  * @throw XQException - (1) the conversion of the value to an XDM instance failed, or (2) the underlying object implementing the interface is closed
1424  */
1425  @Override
1426  public XQItem createItemFromInt(int value, XQItemType type) throws XQException {
1427  isClosedXQException();
1428  if (type == null) {
1429  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_INT);
1430  }
1431  switch (type.getBaseType()) {
1432  case XQItemType.XQBASETYPE_BYTE:
1433  case XQItemType.XQBASETYPE_SHORT:
1434  throw new XQException("Can't downgrade the Base Type from an Int number");
1435  default:
1436  break;
1437  }
1438  return createDecimal(new BigDecimal(value), type);
1439  }
1440 
1441  /** \brief Creates an item from a given value.
1442  *
1443  * The value is converted into an instance of the specified type according to the rule defined in 14.2 Mapping a Java Data Type to an XQuery Data Type, XQuery API for Java (XQJ) 1.0. If the converstion fails, an XQException will be thrown.
1444  *
1445  * @param value - the value to be converted
1446  * @param type - the type of the value to be bound to the external variable. The default type of the value is used in case null is specified
1447  * @return ZorbaXQItem representing the resulting item
1448  * @throw XQException - (1) the conversion of the value to an XDM instance failed, or (2) the underlying object implementing the interface is closed
1449  */
1450  @Override
1451  public XQItem createItemFromLong(long value, XQItemType type) throws XQException {
1452  isClosedXQException();
1453  if (type == null) {
1454  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_LONG);
1455  }
1456  switch (type.getBaseType()) {
1457  case XQItemType.XQBASETYPE_BYTE:
1458  case XQItemType.XQBASETYPE_SHORT:
1459  case XQItemType.XQBASETYPE_INT:
1460  throw new XQException("Can't downgrade the Base Type from an Long number");
1461  default:
1462  break;
1463  }
1464  return createDecimal(new BigDecimal(value), type);
1465  }
1466 
1467  /** \brief Creates an item from a given value.
1468  *
1469  * The value is converted into an instance of the specified type according to the rule defined in 14.2 Mapping a Java Data Type to an XQuery Data Type, XQuery API for Java (XQJ) 1.0. If the converstion fails, an XQException will be thrown.
1470  *
1471  * @param value - the value to be converted
1472  * @param type - the type of the value to be bound to the external variable. The default type of the value is used in case null is specified
1473  * @return ZorbaXQItem representing the resulting item
1474  * @throw XQException - (1) the conversion of the value to an XDM instance failed, or (2) the underlying object implementing the interface is closed
1475  */
1476  @Override
1477  public XQItem createItemFromNode(Node value, XQItemType type) throws XQException {
1478  isClosedXQException();
1479  isNullXQException(value);
1480 
1481  XQItem result = null;
1482  try {
1483  result = createItemFromString(nodeToString(value), type);
1484  } catch( Exception e ) {
1485  throw new XQException("Error Creating Item From Node" + e.getLocalizedMessage());
1486  }
1487  return result;
1488  }
1489 
1490  /** \brief Creates an item from a given value.
1491  *
1492  * The value is converted into an instance of the specified type according to the rule defined in 14.2 Mapping a Java Data Type to an XQuery Data Type, XQuery API for Java (XQJ) 1.0. If the converstion fails, an XQException will be thrown.
1493  *
1494  * @param value - the value to be converted
1495  * @param type - the type of the value to be bound to the external variable. The default type of the value is used in case null is specified
1496  * @return ZorbaXQItem representing the resulting item
1497  * @throw XQException - (1) the conversion of the value to an XDM instance failed, or (2) the underlying object implementing the interface is closed
1498  */
1499  @Override
1500  public XQItem createItemFromShort(short value, XQItemType type) throws XQException {
1501  isClosedXQException();
1502  if (type == null) {
1503  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_SHORT);
1504  }
1505  if (type.getBaseType()==XQItemType.XQBASETYPE_BYTE) {
1506  throw new XQException("Can't create a Byte from a Short number");
1507  }
1508  return createDecimal(new BigDecimal(value), type);
1509  }
1510 
1511  /** \brief Creates a copy of the specified ZorbaXQItem.
1512  *
1513  * This method can be used, for example, to copy an XQResultItem object so that the new item is not dependant on the connection.
1514  *
1515  * @param value - the ZorbaXQItem to copy
1516  * @return ZorbaXQItem independent of any underlying ZorbaXQConnection is created
1517  * @throw XQException - if (1) the specified item is null, (2) the underlying object implementing the interface is closed, (3) the specified item is closed
1518  */
1519  @Override
1520  public XQItem createItem(XQItem value) throws XQException {
1521  isClosedXQException();
1522  isNullXQException(value);
1523  if (value.isClosed()) {
1524  throw new XQException("Item is closed.");
1525  }
1526  XQItem result = new org.zorbaxquery.api.xqj.ZorbaXQItem(value);
1527  return result;
1528  }
1529 
1530  /** \brief Creates a copy of the specified ZorbaXQSequence.
1531  *
1532  * Creates a copy of the specified ZorbaXQSequence. The newly created ZorbaXQSequence is scrollable and independent of any underlying ZorbaXQConnection. The new ZorbaXQSequence will contain all items from the specified ZorbaXQSequence starting from its current position. The copy process will implicitly perform next operations on the specified sequence to read the items. All items are consumed, the current position of the cursor is set to point after the last item.
1533  *
1534  * @param value - input sequence
1535  * @return ZorbaXQSequence representing a copy of the input sequence
1536  * @throw XQException - if (1) there are errors accessing the items in the specified sequence, (2) the specified sequence is closed, (3) in the case of a forward only sequence, a get or write method has already been invoked on the current item, (4) the s parameter is null, or (5) the underlying object implementing the interface is closed
1537  */
1538  @Override
1539  public XQSequence createSequence(XQSequence value) throws XQException {
1540  isClosedXQException();
1541  isNullXQException(value);
1542  if (value.isClosed()) {
1543  throw new XQException("Sequence is closed.");
1544  }
1545  XQSequence result = null;
1546  try {
1547  result = new org.zorbaxquery.api.xqj.ZorbaXQSequence(value);
1548  } catch (Exception ex) {
1549  throw new XQException("Error creating sequence: " + ex.getLocalizedMessage());
1550  }
1551  return result;
1552  }
1553 
1554  /** \brief Creates an ZorbaXQSequence, containing all the items from the iterator.
1555  *
1556  * The newly created ZorbaXQSequence is scrollable and independent of any underlying ZorbaXQConnection. If the iterator returns an ZorbaXQItem, it is added to the sequence. If the iterator returns any other object, an item is added to the sequence following the rules from 14.2 Mapping a Java Data Type to an XQuery Data Type, XQuery API for Java (XQJ) 1.0. If the iterator does not return any items, then an empty sequence is created.
1557  *
1558  * @param value - input sequence
1559  * @return ZorbaXQSequence representing a copy of the input sequence
1560  * @throw XQException - if (1) there are errors accessing the items in the specified sequence, (2) the specified sequence is closed, (3) in the case of a forward only sequence, a get or write method has already been invoked on the current item, (4) the s parameter is null, or (5) the underlying object implementing the interface is closed
1561  */
1562  @Override
1563  public XQSequence createSequence(Iterator value) throws XQException {
1564  isClosedXQException();
1565  isNullXQException(value);
1566  XQSequence result = null;
1567  try {
1568  result = new org.zorbaxquery.api.xqj.ZorbaXQSequence(value);
1569  } catch (Exception ex) {
1570  throw new XQException("Error creating sequence: " + ex.getLocalizedMessage());
1571  }
1572  return result;
1573  }
1574 
1575  /** \brief Creates a new ZorbaXQItemType object representing an XQuery atomic type.
1576  *
1577  * The item kind of the item type will be ZorbaXQItemType.XQITEMKIND_ATOMIC.
1578  * Example -
1579  * \code{.java}
1580  * ZorbaXQConnection conn = ...;
1581  *
1582  * // to create xs:integer item type
1583  * conn.createAtomicType(ZorbaXQItemType.XQBASETYPE_INTEGER);
1584  * \endcode
1585  * @param basetype - one of the ZorbaXQItemType.XQBASETYPE_* constants. All basetype constants except the following are valid:<br />ZorbaXQItemType.XQBASETYPE_UNTYPED<br />ZorbaXQItemType.XQBASETYPE_ANYTYPE<br />ZorbaXQItemType.XQBASETYPE_IDREFS<br />ZorbaXQItemType.XQBASETYPE_NMTOKENS<br />ZorbaXQItemType.XQBASETYPE_ENTITIES<br />ZorbaXQItemType.XQBASETYPE_ANYSIMPLETYPE<br />
1586  * @return a new ZorbaXQItemType representing the atomic type
1587  * @throw XQException - if (1) an invalid basetype value is passed in, or (2) the underlying object implementing the interface is closed
1588  */
1589  @Override
1590  public XQItemType createAtomicType(int basetype) throws XQException {
1591  isClosedXQException();
1592  if ((basetype==XQItemType.XQBASETYPE_UNTYPED) ||
1593  (basetype==XQItemType.XQBASETYPE_ANYTYPE) ||
1594  (basetype==XQItemType.XQBASETYPE_IDREFS) ||
1595  (basetype==XQItemType.XQBASETYPE_NMTOKENS) ||
1596  (basetype==XQItemType.XQBASETYPE_ENTITIES) ||
1597  (basetype==XQItemType.XQBASETYPE_ANYSIMPLETYPE)) {
1598  throw new XQException("Invalid base type.");
1599  }
1600  XQItemType type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, basetype);
1601  return type;
1602  }
1603 
1604  /** \brief Creates a new ZorbaXQItemType object representing an XQuery atomic type.
1605  *
1606  * The item kind of the item type will be ZorbaXQItemType.XQITEMKIND_ATOMIC.
1607  * Example -
1608  * \code{.java}
1609  * ZorbaXQConnection conn = ...;
1610  *
1611  * // to create xs:integer item type
1612  * conn.createAtomicType(ZorbaXQItemType.XQBASETYPE_INTEGER);
1613  * \endcode
1614  * @param basetype - one of the ZorbaXQItemType.XQBASETYPE_* constants. All basetype constants except the following are valid:<br />ZorbaXQItemType.XQBASETYPE_UNTYPED<br />ZorbaXQItemType.XQBASETYPE_ANYTYPE<br />ZorbaXQItemType.XQBASETYPE_IDREFS<br />ZorbaXQItemType.XQBASETYPE_NMTOKENS<br />ZorbaXQItemType.XQBASETYPE_ENTITIES<br />ZorbaXQItemType.XQBASETYPE_ANYSIMPLETYPE<br />
1615  * @param qname - the QName of the type. If the QName refers to a predefinied type, it must match the basetype. Can be null
1616  * @param uri - the URI to the schema. Can be null. This can only be specified if the typename is also specified
1617  * @return a new ZorbaXQItemType representing the atomic type
1618  * @throw XQException - if (1) an invalid basetype value is passed in, or (2) the underlying object implementing the interface is closed
1619  */
1620  @Override
1621  public XQItemType createAtomicType(int basetype, QName qname, URI uri) throws XQException {
1622  if (closed) {
1623  throw new XQException("Object closed");
1624  }
1625  if ((basetype==XQItemType.XQBASETYPE_UNTYPED) ||
1626  (basetype==XQItemType.XQBASETYPE_ANYTYPE) ||
1627  (basetype==XQItemType.XQBASETYPE_IDREFS) ||
1628  (basetype==XQItemType.XQBASETYPE_NMTOKENS) ||
1629  (basetype==XQItemType.XQBASETYPE_ENTITIES) ||
1630  (basetype==XQItemType.XQBASETYPE_ANYSIMPLETYPE)) {
1631  throw new XQException("Invalid base type.");
1632  }
1633  XQItemType type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, qname, basetype, uri);
1634  return type;
1635  }
1636 
1637  /** \brief Creates a new ZorbaXQItemType object representing the XQuery attribute(nodename, basetype) type with the given node name and base type.
1638  *
1639  * This method can be used to create item type for attributes with a pre-defined schema type.
1640  *
1641  * Example -
1642  * \code{.java}
1643  * ZorbaXQConnection conn = ..; // An XQuery connection
1644  *
1645  * - attribute() // no node name, pass null for the node name
1646  *
1647  * conn.createAttributeType(null, ZorbaXQItemType.XQBASETYPE_ANYSIMPLETYPE);
1648  *
1649  * - attribute (*) // equivalent to attribute()
1650  *
1651  * conn.createAttributeType(null, ZorbaXQItemType.XQBASETYPE_ANYSIMPLETYPE);
1652  *
1653  * - attribute (person) // attribute of name person and any simple type.
1654  *
1655  * conn.createAttributeType(new QName("person"), ZorbaXQItemType.XQBASETYPE_ANYSIMPLETYPE);
1656  *
1657  * - attribute(foo:bar) // node name foo:bar, type is any simple type
1658  *
1659  * conn.createAttributeType(new QName("http://www.foo.com", "bar","foo"),
1660  * ZorbaXQItemType.XQBASETYPE_ANYSIMPLETYPE);
1661  *
1662  * - attribute(foo:bar, xs:integer) // node name foo:bar, type is xs:integer
1663  *
1664  * conn.createAttributeType(new QName("http://www.foo.com", "bar","foo"),
1665  * ZorbaXQItemType.XQBASETYPE_INTEGER);
1666  * \endcode
1667  * @param nodename - specifies the name of the node.null indicates a wildcard for the node name
1668  * @param basetype - the base type of the attribute. One of the ZorbaXQItemType.XQBASETYPE_* other than ZorbaXQItemType.XQBASETYPE_UNTYPED or ZorbaXQItemType.XQBASETYPE_ANYTYPE
1669  * @return a new ZorbaXQItemType representing the XQuery attribute(nodename, basetype) type
1670  * @throw XQException - if (1) the underlying object implementing the interface is closed or (2) if the base type is one of: ZorbaXQItemType.XQBASETYPE_UNTYPED or ZorbaXQItemType.XQBASETYPE_ANYTYPE
1671  */
1672  @Override
1673  public XQItemType createAttributeType(QName nodename, int basetype) throws XQException {
1674  isClosedXQException();
1675  if ((basetype==XQItemType.XQBASETYPE_UNTYPED)||(basetype==XQItemType.XQBASETYPE_ANYTYPE)) {
1676  throw new XQException("Base Type can't be XQItemType.XQBASETYPE_UNTYPED or XQItemType.XQBASETYPE_ANYTYPE");
1677  }
1678  return new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATTRIBUTE, nodename, basetype);
1679  }
1680 
1681  /** \brief Creates a new ZorbaXQItemType object representing the XQuery attribute(nodename, basetype) type with the given node name and base type.
1682  *
1683  * The type name can reference either pre-defined simple types or user-defined simple types.
1684  *
1685  * Example -
1686  * \code{.java}
1687  * ZorbaXQConnection conn = ..; // An XQuery connection
1688  *
1689  * - attribute (name, employeename) // attribute name of type employeename
1690  *
1691  * conn.createAttributeType(new QName("name"), ZorbaXQItemType.XQBASETYPE_ANYSIMPLETYPE,
1692  * new QName("employeename"), null);
1693  *
1694  * - attribute (foo:bar, po:city)
1695  * where the prefix foo refers to the namespace http://www.foo.com and the
1696  * prefix po refers to the namespace "http://www.address.com"
1697  *
1698  * conn.createAttributeType(new QName("http://www.foo.com", "bar","foo"),
1699  * ZorbaXQItemType.XQBASETYPE_ANYSIMPLETYPE,
1700  * new QName("http://address.com", "address","po"), null);
1701  *
1702  * - attribute (zip, zipcode) // attribute zip of type zipchode which derives from
1703  * // xs:string
1704  *
1705  * conn.createAttributeType(new QName("zip"), ZorbaXQItemType.XQBASETYPE_STRING,
1706  * new QName("zipcode"), null);
1707  *
1708  * - attribute(foo:bar, po:hatsize)
1709  * where the prefix foo refers to the namespace http://www.foo.com and the
1710  * prefix po refers to the namespace "http://www.hatsizes.com"
1711  * with schema URI "http://hatschema.com"
1712  *
1713  * conn.createAttributeType(new QName("http://www.foo.com", "bar","foo"),
1714  * ZorbaXQItemType.XQBASETYPE_INTEGER,
1715  * new QName("http://www.hatsizes.com", "hatsize","po"),
1716  * new QName("http://hatschema.com"));
1717  * \endcode
1718  * @param nodename - specifies the name of the node.null indicates a wildcard for the node name
1719  * @param basetype - the base type of the attribute. One of the XQItemTyupe.XQBASETYPE_* constants other than ZorbaXQItemType.XQBASETYPE_UNTYPED or ZorbaXQItemType.XQBASETYPE_ANYTYPE
1720  * @param typename - the QName of the type. If the QName refers to a predefinied type, it must match the basetype. Can be null.
1721  * @param schemaURI - the URI to the schema. Can be null. This can only be specified if the typename is also specified
1722  * @return a new ZorbaXQItemType representing the XQuery attribute(nodename,basetype, typename,schemaURI) type.
1723  * @throw XQException - if (1) the underlying object implementing the interface is closed, (2) if the base type is one of: ZorbaXQItemType.XQBASETYPE_UNTYPED or ZorbaXQItemType.XQBASETYPE_ANYTYPE, (3) the schema URI is specified and the typename is not specified, (4) the implementation does not support user-defined XML schema types, or (5) if the typename refers to a predefinied type and does not match basetype
1724  */
1725  @Override
1726  public XQItemType createAttributeType(QName nodename, int basetype, QName typename, URI schemaURI) throws XQException {
1727  isClosedXQException();
1728  if ((basetype==XQItemType.XQBASETYPE_UNTYPED)||(basetype==XQItemType.XQBASETYPE_ANYTYPE)) {
1729  throw new XQException("Base Type can't be XQItemType.XQBASETYPE_UNTYPED or XQItemType.XQBASETYPE_ANYTYPE");
1730  }
1731  if ((schemaURI!=null) && (typename==null)) {
1732  throw new XQException("If Schema URI is specified, Base Type must be also specified");
1733  }
1734  return new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATTRIBUTE, nodename, basetype, typename, schemaURI, true);
1735  }
1736 
1737  /** \brief Creates a new ZorbaXQItemType object representing the XQuery schema-attribute(nodename,basetype,schemaURI) type, with the given node name, base type, and schema URI.
1738  *
1739  * Example -
1740  * \code{.java}
1741  * ZorbaXQConnection conn = ..; // An XQuery connection
1742  *
1743  * - schema-attribute (name) // schema-attribute name, found in the schema
1744  * // available at http://customerschema.com
1745  *
1746  * conn.createSchemaAttributeType(new QName("name"),
1747  * ZorbaXQItemType.XQBASETYPE_STRING,
1748  * new URI(http://customerschema.com));
1749  * \endcode
1750  *
1751  * @param nodename - specifies the name of the node
1752  * @param basetype - the base type of the attribute. One of the XQItemTyupe.XQBASETYPE_* constants other than ZorbaXQItemType.XQBASETYPE_UNTYPED or ZorbaXQItemType.XQBASETYPE_ANYTYPE
1753  * @param uri - the URI to the schema. Can be null
1754  * @return a new ZorbaXQItemType representing the XQuery schema-attribute(nodename,basetype, schemaURI) type
1755  * @throw XQException - if (1) the node name is null, (2) if the base type is one of: ZorbaXQItemType.XQBASETYPE_UNTYPED or ZorbaXQItemType.XQBASETYPE_ANYTYPE, (3) the underlying object implementing the interface is closed, or (4) the implementation does not support user-defined XML schema types
1756  */
1757  @Override
1758  public XQItemType createSchemaAttributeType(QName nodename, int basetype, URI uri) throws XQException {
1759  isClosedXQException();
1760  return new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_SCHEMA_ATTRIBUTE, nodename, basetype, uri);
1761  }
1762 
1763  /** \brief Creates a new ZorbaXQItemType object representing the XQuery comment() type.
1764  *
1765  * Creates a new ZorbaXQItemType object representing the XQuery comment() type. The ZorbaXQItemType object will have the item kind set to ZorbaXQItemType.XQITEMKIND_COMMENT.
1766  *
1767  * Example -
1768  * \code{.java}
1769  * ZorbaXQConnection conn = ..; // An XQuery connection
1770  * ZorbaXQItemType cmttype = conn.createCommentType();
1771  *
1772  * int itemkind = cmttype.getItemKind(); // will be ZorbaXQItemType.XQITEMKIND_COMMENT
1773  *
1774  * ZorbaXQExpression expr = conn.createExpression();
1775  * ZorbaXQSequence result = expr.executeQuery("<!-- comments -->");
1776  *
1777  * result.next();
1778  * boolean pi = result.instanceOf(cmttype); // will be true
1779  * \endcode
1780  *
1781  * @return a new ZorbaXQItemType representing the XQuery comment() type
1782  * @throw XQException - if the underlying object implementing the interface is closed
1783  */
1784  @Override
1785  public XQItemType createCommentType() throws XQException {
1786  isClosedXQException();
1787  return new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_COMMENT);
1788  }
1789 
1790  /** \brief Creates a new ZorbaXQItemType object representing the XQuery document-node(elementType) type containing a single element.
1791  *
1792  * Creates a new ZorbaXQItemType object representing the XQuery document-node(elementType) type containing a single element. The ZorbaXQItemType object will have the item kind set to ZorbaXQItemType.XQITEMKIND_DOCUMENT_ELEMENT and the base type set to the item type of the input elementType.
1793  *
1794  * @param elementType - an ZorbaXQItemType object representing an XQuery element() type, cannot be null
1795  * @return a new ZorbaXQItemType representing the XQuery document-node(elementType) type containing a single element
1796  * @throw XQException - if (1) the underlying object implementing the interface is closed or (2) the elementType has an item kind different from ZorbaXQItemType.XQITEMKIND_ELEMENT, (3) the elementType argument is null, or (4) the implementation does not support user-defined XML schema types
1797  */
1798  @Override
1799  public XQItemType createDocumentElementType(XQItemType elementType) throws XQException {
1800  isClosedXQException();
1801  isNullXQException(elementType);
1802  if (elementType.getItemKind()!=XQItemType.XQITEMKIND_ELEMENT) {
1803  throw new XQException("Item Kind must be XQItemType.XQITEMKIND_ELEMENT");
1804  }
1805  return new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_DOCUMENT_ELEMENT, elementType.getNodeName(), elementType.getBaseType(), elementType.getSchemaURI());
1806  }
1807 
1808  /** \brief Creates a new ZorbaXQItemType object representing the XQuery document-node(elementType) type containing a single schema-element(...).
1809  *
1810  * The ZorbaXQItemType object will have the item kind set to ZorbaXQItemType.XQITEMKIND_DOCUMENT_SCHEMA_ELEMENT and the base type set to the item type of the input elementType.
1811  *
1812  * @param elementType - an ZorbaXQItemType object representing an XQuery schema-element(...) type, cannot be null
1813  * @return a new ZorbaXQItemType representing the XQuery document-node(elementType) type containing a single schema-element(...) element
1814  * @throw XQException - if (1) the underlying object implementing the interface is closed or (2) the elementType has an item kind different from ZorbaXQItemType.XQITEMKIND_SCHEMA_ELEMENT, (3) the elementType argument is null, (4) the implementation does not support user-defined XML schema types
1815  */
1816  @Override
1817  public XQItemType createDocumentSchemaElementType(XQItemType elementType) throws XQException {
1818  isClosedXQException();
1819  if (elementType.getItemKind()!=XQItemType.XQITEMKIND_DOCUMENT_SCHEMA_ELEMENT) {
1820  throw new XQException("Item Kind must be XQItemType.XQITEMKIND_DOCUMENT_SCHEMA_ELEMENT");
1821  }
1822  return new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_DOCUMENT_ELEMENT, elementType.getNodeName(), elementType.getBaseType(), elementType.getSchemaURI());
1823  }
1824 
1825  /** \brief Creates a new ZorbaXQItemType object representing the XQuery document-node() type.
1826  *
1827  * The ZorbaXQItemType object will have the item kind set to ZorbaXQItemType.XQITEMKIND_DOCUMENT.
1828  *
1829  * @return a new ZorbaXQItemType representing the XQuery document-node() type
1830  * @throw XQException - if the underlying object implementing the interface is closed
1831  */
1832  @Override
1833  public XQItemType createDocumentType() throws XQException {
1834  isClosedXQException();
1835  return new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_DOCUMENT);
1836  }
1837 
1838  /** \brief Creates a new ZorbaXQItemType object representing the XQuery element(nodename, basetype) type, with the given node name and base type.
1839  *
1840  * This method can be used to create item type for elements with a pre-defined schema type.
1841  *
1842  * Example -
1843  * \code{.java}
1844  * ZorbaXQConnection conn = ..; // An XQuery connection
1845  * - element() // no node name, pass null for the node name
1846  *
1847  * conn.createElementType(null, ZorbaXQItemType.XQBASETYPE_ANYTYPE);
1848  *
1849  * - element (*) // equivalent to element()
1850  *
1851  * conn.createElementType(null, ZorbaXQItemType.XQBASETYPE_ANYTYPE);
1852  *
1853  * - element(person) // element of name person and any type.
1854  *
1855  * conn.createElementType(new QName("person"), ZorbaXQItemType.XQBASETYPE_ANYTYPE);
1856  *
1857  * - element(foo:bar) // node name foo:bar, type is anytype
1858  *
1859  * conn.createElementType(new QName("http://www.foo.com", "bar","foo"),
1860  * ZorbaXQItemType.XQBASETYPE_ANYTYPE);
1861  *
1862  * - element(foo:bar, xs:integer) // node name foo:bar, type is xs:integer
1863  *
1864  * conn.createElementType(new QName("http://www.foo.com", "bar","foo"),
1865  * ZorbaXQItemType.XQBASETYPE_INTEGER);
1866  * \endcode
1867  * @param nodename - specifies the name of the node. null indicates a wildcard for the node name
1868  * @param baseType - the base type of the item. One of the ZorbaXQItemType.XQBASETYPE_* constants
1869  * @return a new ZorbaXQItemType representing the XQuery element(nodename, basetype) type
1870  * @throw XQException - if (1) the underlying object implementing the interface is closed
1871  */
1872  @Override
1873  public XQItemType createElementType(QName nodename, int baseType) throws XQException {
1874  isClosedXQException();
1875  XQItemType item = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ELEMENT, nodename, baseType);
1876  return item;
1877  }
1878 
1879  /** \brief Creates a new ZorbaXQItemType object representing the XQuery element(nodename,basetype,typename,schemaURI, allowNill) type, given the node name, base type, schema type name, schema URI, and nilled check.
1880  *
1881  * The type name can reference either pre-defined schema types or user-defined types.
1882  *
1883  * Example -
1884  * \code{.java}
1885  * ZorbaXQConnection conn = ..; // An XQuery connection
1886  *
1887  * - element (person, employee) // element person of type employee
1888  *
1889  * conn.createElementType(new QName("person"), ZorbaXQItemType.XQBASETYPE_ANYTYPE,
1890  * new QName("employee"), null ,false);
1891  *
1892  * - element(person, employee ? ) // element person of type employee, whose nilled
1893  * // property may be true or false.
1894  *
1895  * conn.createElementType(new QName("person"), ZorbaXQItemType.XQBASETYPE_ANYTYPE,
1896  * new QName("employee"), null ,true);
1897  *
1898  * - element(foo:bar, po:address)
1899  * where the prefix foo refers to the namespace http://www.foo.com and the
1900  * prefix po refers to the namespace "http://www.address.com"
1901  *
1902  * conn.createElementType(new QName("http://www.foo.com", "bar","foo"),
1903  * ZorbaXQItemType.XQBASETYPE_ANYTYPE,
1904  * new QName("http://address.com", "address","po"), null, false);
1905  *
1906  * - element (zip, zipcode) // element zip of type zipchode which derives from
1907  * // xs:string
1908  *
1909  * conn.createElementType(new QName("zip"), ZorbaXQItemType.XQBASETYPE_STRING,
1910  * new QName("zipcode"), null, false);
1911  *
1912  * - element (*, xs:anyType ?)
1913  *
1914  * conn.createElementType(null, ZorbaXQItemType.XQBASETYPE_ANYTYPE, null, null, true);
1915  *
1916  * - element(foo:bar, po:hatsize)
1917  * where the prefix foo refers to the namespace http://www.foo.com and the
1918  * prefix po refers to the namespace "http://www.hatsizes.com"
1919  * with schema URI "http://hatschema.com"
1920  *
1921  * conn.createElementType(new QName("http://www.foo.com", "bar","foo"),
1922  * ZorbaXQItemType.XQBASETYPE_INTEGER,
1923  * new QName("http://www.hatsizes.com", "hatsize","po"),
1924  * new QName("http://hatschema.com"), false);
1925  *
1926  * \endcode
1927  *
1928  * @param nodename - specifies the name of the element. null indicates a wildcard for the node name
1929  * @param baseType - the base type of the item. One of the ZorbaXQItemType.XQBASETYPE_* constants
1930  * @param typename - the QName of the type. If the QName refers to a predefinied type, it must match the basetype. Can be null
1931  * @param schemaURI - the URI to the schema. Can be null. This can only be specified if the typename is also specified
1932  * @param allowNill - the nilled property of the element
1933  * @return a new ZorbaXQItemType representing the XQuery element(nodename,basetype, typename,schemaURI, allowNill) type
1934  * @throw XQException - if (1) schemaURI is specified but the typename is not specified, (2) the underlying object implementing the interface is closed, (3) the implementation does not support user-defined XML schema types, or (4) if the typename refers to a predefinied type and does not match basetype
1935  */
1936  @Override
1937  public XQItemType createElementType(QName nodename, int baseType, QName typename, URI schemaURI, boolean allowNill) throws XQException {
1938  isClosedXQException();
1939  XQItemType item = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ELEMENT, nodename, baseType, typename, schemaURI, allowNill);
1940  return item;
1941  }
1942 
1943  /** \brief Creates a new ZorbaXQItemType object representing the XQuery schema-element(nodename,basetype,schemaURI) type, given the node name, base type, and the schema URI.
1944  *
1945  * Example -
1946  * \code{.java}
1947  * ZorbaXQConnection conn = ..; // An XQuery connection
1948  *
1949  * - schema-element (customer) // schema-element person, found in
1950  * // the schema available at http://customerschema.com
1951  *
1952  * conn.createElementType(new QName("customer"), ZorbaXQItemType.XQBASETYPE_ANYTYPE,
1953  * new URI("http://customerschema.com"));
1954  * \endcode
1955  * @param nodename - specifies the name of the element
1956  * @param baseType - the base type of the item. One of the ZorbaXQItemType.XQBASETYPE_* constants
1957  * @param schemaURI - the URI to the schema. Can be null
1958  * @return a new ZorbaXQItemType representing the XQuery schema-element(nodename,basetype, schemaURI) type
1959  * @throw XQException - if (1) the node name is null, (2) the underlying object implementing the interface is closed, or (3) the implementation does not support user-defined XML schema types
1960  */
1961  @Override
1962  public XQItemType createSchemaElementType(QName nodename, int baseType, URI schemaURI) throws XQException {
1963  isClosedXQException();
1964  XQItemType item = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_SCHEMA_ELEMENT, nodename, baseType, schemaURI);
1965  return item;
1966  }
1967 
1968  /** \brief Creates a new ZorbaXQItemType object representing the XQuery item type.
1969  *
1970  * The ZorbaXQItemType object will have the item kind set to ZorbaXQItemType.XQITEMKIND_ITEM.
1971  *
1972  * Example -
1973  * \code{.java}
1974  * ZorbaXQConnection conn = ..; // An XQuery connection
1975  * ZorbaXQItemType typ = conn.createItemType(); // represents the XQuery item type "item()"
1976  * \endcode
1977  * @return a new ZorbaXQItemType representing the XQuery item type
1978  * @throw XQException - if the underlying object implementing the interface is closed
1979  */
1980  @Override
1981  public XQItemType createItemType() throws XQException {
1982  isClosedXQException();
1983  XQItemType type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ITEM);
1984  return type;
1985  }
1986 
1987  /** \brief Creates a new ZorbaXQItemType object representing the XQuery node() type.
1988  *
1989  * The ZorbaXQItemType object will have the item kind set to ZorbaXQItemType.XQITEMKIND_NODE.
1990  *
1991  * @return a new ZorbaXQItemType representing the XQuery node() type
1992  * @throw XQException - if the underlying object implementing the interface is closed
1993  */
1994  @Override
1995  public XQItemType createNodeType() throws XQException {
1996  isClosedXQException();
1997  XQItemType type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_NODE);
1998  return type;
1999  }
2000 
2001  /** \brief Creates a new ZorbaXQItemType object representing the XQuery processing-instruction(piTarget) type.
2002  *
2003  * The ZorbaXQItemType object will have the item kind set to ZorbaXQItemType.XQITEMKIND_PI. A string literal can be passed to match the PITarget of the processing instruction as described in 2.5.4.2 Matching an Item Type and an Item, XQuery 1.0: An XML Query Language.
2004  *
2005  * Example -
2006  * \code{.java}
2007  * ZorbaXQConnection conn = ..; // An XQuery connection
2008  * ZorbaXQItemType anypi = conn.createProcessingInstructionType();
2009  * ZorbaXQItemType foopi = conn.createProcessingInstructionType("foo-format");
2010  *
2011  * ZorbaXQExpression expr = conn.createExpression();
2012  * ZorbaXQSequence result = expr.executeQuery("<?format role="output" ?>");
2013  *
2014  * result.next();
2015  * boolean pi = result.instanceOf(anypi); // will be true
2016  * pi = result.instanceOf(foopi); // will be false
2017  *
2018  * ZorbaXQExpression expr = conn.createExpression();
2019  * ZorbaXQSequence result = expr.executeQuery("<?foo-format role="output" ?>");
2020  *
2021  * result.next();
2022  * boolean pi = result.instanceOf(anypi); // will be true
2023  * pi = result.instanceOf(foopi); // will be true
2024  * \endcode
2025  *
2026  * @param piTarget - the string literal to match the processing instruction's PITarget. A null string value will match all processing instruction nodes
2027  * @return a new ZorbaXQItemType representing the XQuery processing-instruction(piTarget) type
2028  * @throw XQException - if the underlying object implementing the interface is closed
2029  */
2030  @Override
2031  public XQItemType createProcessingInstructionType(String piTarget) throws XQException {
2032  isClosedXQException();
2033  XQItemType type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_PI, piTarget);
2034  return type;
2035  }
2036 
2037  /** \brief Creates a new sequence type from an item type and occurence indicator.
2038  *
2039  * @param item - the item type. This parameter must be null if the occurance is ZorbaXQSequenceType.OCC_EMPTY, and cannot be null for any other occurance indicator
2040  * @param occurence - The occurence of the item type, must be one of ZorbaXQSequenceType.OCC_ZERO_OR_ONE, ZorbaXQSequenceType.OCC_EXACTLY_ONE, ZorbaXQSequenceType.OCC_ZERO_OR_MORE, ZorbaXQSequenceType.OCC_ONE_OR_MORE, ZorbaXQSequenceType.OCC_EMPTY
2041  * @return a new ZorbaXQSequenceType representing the type of a sequence
2042  * @throw XQException - if (1) the item is null and the occurance is not ZorbaXQSequenceType.OCC_EMPTY, (2) the item is not null and the occurance is ZorbaXQSequenceType.OCC_EMPTY, (3) the occurence is not one of: ZorbaXQSequenceType.OCC_ZERO_OR_ONE, ZorbaXQSequenceType.OCC_EXACTLY_ONE, ZorbaXQSequenceType.OCC_ZERO_OR_MORE, ZorbaXQSequenceType.OCC_ONE_OR_MORE, ZorbaXQSequenceType.OCC_EMPTY or (4) the underlying object implementing the interface is closed
2043  */
2044  @Override
2045  public XQSequenceType createSequenceType(XQItemType item, int occurence) throws XQException {
2046  isClosedXQException();
2047  if ( ((item==null) && (occurence != XQSequenceType.OCC_EMPTY)) || ((item!=null) && (occurence == XQSequenceType.OCC_EMPTY))) {
2048  throw new XQException("Item is null");
2049  }
2050  if (!((occurence == XQSequenceType.OCC_ZERO_OR_ONE) ||
2051  (occurence == XQSequenceType.OCC_EXACTLY_ONE) ||
2052  (occurence == XQSequenceType.OCC_ZERO_OR_MORE) ||
2053  (occurence == XQSequenceType.OCC_ONE_OR_MORE) ||
2054  (occurence == XQSequenceType.OCC_EMPTY))) {
2055  throw new XQException("Occurence must be from: OCC_ZERO_OR_ONE, OCC_EXACTLY_ONE, OCC_ZERO_OR_MORE, OCC_ONE_OR_MORE, OCC_EMPTY ");
2056  }
2057 
2058  XQSequenceType result = new org.zorbaxquery.api.xqj.ZorbaXQSequenceType(item, occurence);
2059  return result;
2060  }
2061 
2062  /** \brief Creates a new ZorbaXQItemType object representing the XQuery text() type.
2063  *
2064  * The ZorbaXQItemType object will have the item kind set to ZorbaXQItemType.XQITEMKIND_TEXT.
2065  *
2066  * @return a new ZorbaXQItemType representing the XQuery text() type
2067  * @throw XQException - if the underlying object implementing the interface is closed
2068  */
2069  @Override
2070  public XQItemType createTextType() throws XQException {
2071  isClosedXQException();
2072  XQItemType type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_TEXT);
2073  return type;
2074  }
2075 
2076  private void isClosedXQException() throws XQException {
2077  if (closed) {
2078  throw new XQException("This connection is closed");
2079  }
2080  }
2081  private void isNullXQException(Object value) throws XQException {
2082  if (value==null) {
2083  throw new XQException("Parameter shouldn't be null");
2084  }
2085  }
2086 
2087 }
blog comments powered by Disqus