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  XQPreparedExpression expression;
337  if (lStaticContext == null) {
338  expression = new org.zorbaxquery.api.xqj.ZorbaXQPreparedExpression(this, reader);
339  } else {
340  expression = new org.zorbaxquery.api.xqj.ZorbaXQPreparedExpression(this, reader, lStaticContext);
341  }
342  cPreparedExpression.add(expression);
343  return expression;
344  }
345 
346  /** \brief Prepares an expression for execution.
347  *
348  * The properties of the connection's default ZorbaXQStaticContext are copied to the returned ZorbaXQPreparedExpression.
349  *
350  * @param reader - the XQuery expression as a Reader. Cannot be null
351  * @param xqsc - ZorbaXQStaticContext containing values of expression properties
352  * @return the prepared XQuery expression
353  * @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
354  */
355  @Override
356  public XQPreparedExpression prepareExpression(Reader reader, XQStaticContext xqsc) throws XQException {
357  isClosedXQException();
358  isNullXQException(reader);
359  isNullXQException(xqsc);
360 
361  XQPreparedExpression expression = new org.zorbaxquery.api.xqj.ZorbaXQPreparedExpression(this, reader, xqsc);
362 
363  cPreparedExpression.add(expression);
364  return expression;
365  }
366 
367  /** \brief Prepares an expression for execution.
368  *
369  * The properties of the connection's default ZorbaXQStaticContext are copied to the returned ZorbaXQPreparedExpression.
370  *
371  * @param in - the XQuery expression as an InputStream. Cannot be null
372  * @return the prepared XQuery expression
373  * @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
374  */
375  @Override
376  public XQPreparedExpression prepareExpression(InputStream in) throws XQException {
377  isClosedXQException();
378  isNullXQException(in);
379 
380  XQPreparedExpression expression;
381  if (lStaticContext == null) {
382  expression = new org.zorbaxquery.api.xqj.ZorbaXQPreparedExpression(this, in);
383  } else {
384  expression = new org.zorbaxquery.api.xqj.ZorbaXQPreparedExpression(this, in, lStaticContext);
385  }
386  try {
387  cPreparedExpression.add(expression);
388  } catch (Exception ex) {
389  throw new XQException("Error preparing expression" + ex.getLocalizedMessage());
390  }
391 
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  * @param xqsc - ZorbaXQStaticContext containing values of expression properties
401  * @return the prepared XQuery expression
402  * @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
403  */
404  @Override
405  public XQPreparedExpression prepareExpression(InputStream in, XQStaticContext xqsc) throws XQException {
406  isClosedXQException();
407  isNullXQException(in);
408  isNullXQException(xqsc);
409 
410  XQPreparedExpression expression = null;
411  try {
412  expression = new org.zorbaxquery.api.xqj.ZorbaXQPreparedExpression(this, in, xqsc);
413  cPreparedExpression.add(expression);
414  } catch (Exception ex) {
415  throw new XQException("Error preparing expression" + ex.getLocalizedMessage());
416  }
417  return expression;
418  }
419 
420  /** \brief Undoes all changes made in the current transaction and releases any locks held by the datasource.
421  *
422  * This method should be used only when auto-commit mode is disabled.
423  *
424  * @throw XQException - if the connection is in a closed state or this connection is operating in auto-commit mode
425  */
426  @Override
427  public void rollback() throws XQException {
428  throw new UnsupportedOperationException("Zorba does not support transactions... yet...");
429  }
430 
431  /** \brief Gets an ZorbaXQStaticContext representing the default values for all expression properties.
432  *
433  * 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.
434  *
435  * @return ZorbaXQStaticContext representing the default values for all expression properties
436  * @throw XQException - if the connection is in a closed state
437  */
438  @Override
439  public XQStaticContext getStaticContext() throws XQException {
440  isClosedXQException();
441  lStaticContext = new org.zorbaxquery.api.xqj.ZorbaXQStaticContext(zorba);
442  if (uriPaths!=null) {
443  ((org.zorbaxquery.api.xqj.ZorbaXQStaticContext)lStaticContext).setURIPaths(uriPaths);
444  }
445  if (libPaths!=null) {
446  ((org.zorbaxquery.api.xqj.ZorbaXQStaticContext)lStaticContext).setLIBPaths(libPaths);
447  }
448  if (modulePaths!=null) {
449  ((org.zorbaxquery.api.xqj.ZorbaXQStaticContext)lStaticContext).setMODPaths(modulePaths);
450  }
451  return lStaticContext;
452  }
453 
454  /** \brief Sets the default values for all expression properties.
455  *
456  * The implementation will read out all expression properties from the specified ZorbaXQStaticContext and update its private copy.
457  *
458  * @param xqsc - ZorbaXQStaticContext containing values of expression properties
459  * @throw XQException - if the connection is in a closed state
460  */
461  @Override
462  public void setStaticContext(XQStaticContext xqsc) throws XQException {
463  isClosedXQException();
464  isNullXQException(xqsc);
465  if ((lStaticContext!=null) && (lStaticContext!=xqsc)) { // delete previous static context
466  ((org.zorbaxquery.api.xqj.ZorbaXQStaticContext)lStaticContext).getZorbaStaticContext().delete();
467  }
468  lStaticContext = xqsc;
469  }
470 
471  /** \brief Creates an item from a given value.
472  *
473  * 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.
474  *
475  * @param value - the lexical string value of the type
476  * @param type - the item type
477  * @return ZorbaXQItem representing the resulting item
478  * @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
479  */
480  @Override
481  public XQItem createItemFromAtomicValue(String value, XQItemType type) throws XQException {
482  isClosedXQException();
483  isNullXQException(value);
484  isNullXQException(type);
485  if (type.getItemKind()!=XQItemType.XQITEMKIND_ATOMIC) {
486  throw new XQException("ItemType is not Atomic");
487  }
488  XQItem item = null;
489  try {
490  item = this.createItemFromString(value, type);
491  } catch (Exception e) {
492  throw new XQException("Error creating item of this type " + e.getLocalizedMessage());
493  }
494  return item;
495  }
496 
497  /** \brief Creates an item from a given value.
498  *
499  * 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.
500  * 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.
501  *
502  * @param value - the value to be converted, cannot be null
503  * @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
504  * @return ZorbaXQItem representing the resulting item
505  * @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
506  */
507  @Override
508  public XQItem createItemFromString(String value, XQItemType type) throws XQException {
509  isClosedXQException();
510  isNullXQException(value);
511  if (type==null) {
512  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_STRING);
513  }
514  ItemFactory itemFactory = zorba.getItemFactory();
515  XQItem item = null;
516  try {
517  switch (type.getBaseType()) {
518  case XQItemType.XQBASETYPE_BOOLEAN:
519  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createBoolean(Boolean.parseBoolean(value)), type);
520  break;
521  case XQItemType.XQBASETYPE_ANYURI:
522  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createAnyURI(value), type);
523  break;
524  case XQItemType.XQBASETYPE_BASE64BINARY:
525  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createBase64Binary(value, value.length()), type);
526  break;
527  case XQItemType.XQBASETYPE_BYTE:
528  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createByte(value.charAt(0)), type);
529  break;
530  case XQItemType.XQBASETYPE_DATE:
531  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDate(value), type);
532  break;
533  case XQItemType.XQBASETYPE_DATETIME:
534  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDateTime(value), type);
535  break;
536  case XQItemType.XQBASETYPE_DAYTIMEDURATION:
537  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDuration(value), type);
538  break;
539  case XQItemType.XQBASETYPE_DECIMAL:
540  BigDecimal dec = new BigDecimal(value);
541  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDecimal(value), type);
542  break;
543  case XQItemType.XQBASETYPE_DOUBLE:
544  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDouble(Double.parseDouble(value)), type);
545  break;
546  case XQItemType.XQBASETYPE_DURATION:
547  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDuration(value), type);
548  break;
549  case XQItemType.XQBASETYPE_FLOAT:
550  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createFloat(value), type);
551  break;
552  case XQItemType.XQBASETYPE_GDAY:
553  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGDay(value), type);
554  break;
555  case XQItemType.XQBASETYPE_GMONTH:
556  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGMonth(value), type);
557  break;
558  case XQItemType.XQBASETYPE_GMONTHDAY:
559  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGMonthDay(value), type);
560  break;
561  case XQItemType.XQBASETYPE_GYEAR:
562  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGYear(value), type);
563  break;
564  case XQItemType.XQBASETYPE_GYEARMONTH:
565  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGYearMonth(value), type);
566  break;
567  case XQItemType.XQBASETYPE_HEXBINARY:
568  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createHexBinary(value, value.length()), type);
569  break;
570  case XQItemType.XQBASETYPE_INT:
571  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createInt(Integer.parseInt(value)), type);
572  break;
573  case XQItemType.XQBASETYPE_INTEGER:
574  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createInteger(Integer.parseInt(value)), type);
575  break;
576  case XQItemType.XQBASETYPE_LONG:
577  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createLong(Long.parseLong(value)), type);
578  break;
579  case XQItemType.XQBASETYPE_NCNAME:
580  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNCName(value), type);
581  break;
582  case XQItemType.XQBASETYPE_NEGATIVE_INTEGER:
583  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNegativeInteger(Long.parseLong(value)), type);
584  break;
585  case XQItemType.XQBASETYPE_NONNEGATIVE_INTEGER:
586  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNonNegativeInteger(new BigInteger(value)), type);
587  break;
588  case XQItemType.XQBASETYPE_NONPOSITIVE_INTEGER:
589  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNonPositiveInteger(Long.parseLong(value)), type);
590  break;
591  case XQItemType.XQBASETYPE_POSITIVE_INTEGER:
592  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createPositiveInteger(new BigInteger(value)), type);
593  break;
594  case XQItemType.XQBASETYPE_QNAME:
595  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createQName(type.getSchemaURI().toString(), value), type);
596  break;
597  case XQItemType.XQBASETYPE_SHORT:
598  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createShort(Short.parseShort(value)), type);
599  break;
600  case XQItemType.XQBASETYPE_STRING:
601  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createString(value), type);
602  break;
603  case XQItemType.XQBASETYPE_TIME:
604  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createTime(value), type);
605  break;
606  case XQItemType.XQBASETYPE_UNSIGNED_BYTE:
607  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedByte(Short.parseShort(value)), type);
608  break;
609  case XQItemType.XQBASETYPE_UNSIGNED_INT:
610  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedInt(Long.parseLong(value)), type);
611  break;
612  case XQItemType.XQBASETYPE_UNSIGNED_LONG:
613  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedLong(new BigInteger(value)), type);
614  break;
615  case XQItemType.XQBASETYPE_UNSIGNED_SHORT:
616  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedShort(Integer.parseInt(value)), type);
617  break;
618  case XQItemType.XQBASETYPE_YEARMONTHDURATION:
619  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDuration(value), type);
620  break;
621  // TODO: revisit this posibilities
622  case XQItemType.XQBASETYPE_ANYATOMICTYPE:
623  case XQItemType.XQBASETYPE_ANYSIMPLETYPE:
624  case XQItemType.XQBASETYPE_ENTITY:
625  case XQItemType.XQBASETYPE_ENTITIES:
626  case XQItemType.XQBASETYPE_ANYTYPE:
627  case XQItemType.XQBASETYPE_LANGUAGE:
628  case XQItemType.XQBASETYPE_ID:
629  case XQItemType.XQBASETYPE_IDREF:
630  case XQItemType.XQBASETYPE_IDREFS:
631  case XQItemType.XQBASETYPE_NAME:
632  case XQItemType.XQBASETYPE_NMTOKEN:
633  case XQItemType.XQBASETYPE_NMTOKENS:
634  case XQItemType.XQBASETYPE_NORMALIZED_STRING:
635  case XQItemType.XQBASETYPE_NOTATION:
636  case XQItemType.XQBASETYPE_TOKEN:
637  case XQItemType.XQBASETYPE_UNTYPED:
638  case XQItemType.XQBASETYPE_UNTYPEDATOMIC:
639  throw new UnsupportedOperationException("Not supported yet.");
640  default:
641 
642  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createString(value), type);
643  break;
644  }
645  } catch (Exception e) {
646  throw new XQException ("Error parsing value" + e.getLocalizedMessage());
647  }
648  return item;
649  }
650 
651  /** \brief Creates an item from the given value.
652  *
653  * 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.
654  *
655  * 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.
656  *
657  * 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.
658  *
659  * @param value - the value to be converted, cannot be null
660  * @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.
661  * @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))
662  * @return ZorbaXQItem representing the resulting item
663  * @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
664  */
665  @Override
666  public XQItem createItemFromDocument(String value, String baseURI, XQItemType type) throws XQException {
667  isClosedXQException();
668  isNullXQException(value);
669  if (type!=null) {
670  if ( ! ((type.getItemKind()== XQItemType.XQITEMKIND_DOCUMENT_ELEMENT)||(type.getItemKind()== XQItemType.XQITEMKIND_DOCUMENT_SCHEMA_ELEMENT)) ) {
671  throw new XQException("Invalid type");
672  }
673  } else {
674  type = this.createElementType(null, XQItemType.XQBASETYPE_UNTYPED);
675  }
676 
677  XmlDataManager dm = null;
678  Item doc = null;
679  XQItem item = null;
680  try {
681  dm = zorba.getXmlDataManager();
682  doc = new Item();
683  doc = dm.parseXMLtoItem(value);
684  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(doc);
685  } catch (Exception e) {
686  throw new XQException("Error creating Item" + e.getLocalizedMessage());
687  }
688  return item;
689  }
690 
691  /** \brief Creates an item from the given value.
692  *
693  * 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.
694  *
695  * 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.
696  *
697  * 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.
698  *
699  * @param value - the value to be converted, cannot be null
700  * @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.
701  * @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))
702  * @return ZorbaXQItem representing the resulting item
703  * @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
704  */
705  @Override
706  public XQItem createItemFromDocument(Reader value, String baseURI, XQItemType type) throws XQException {
707  isClosedXQException();
708  isNullXQException(value);
709  if (type!=null) {
710  if ( ! ((type.getItemKind()== XQItemType.XQITEMKIND_DOCUMENT_ELEMENT)||(type.getItemKind()== XQItemType.XQITEMKIND_DOCUMENT_SCHEMA_ELEMENT)) ) {
711  throw new XQException("Invalid type");
712  }
713  } else {
714  type = this.createElementType(null, XQItemType.XQBASETYPE_UNTYPED);
715  }
716 
717  XmlDataManager dm = null;
718  Item doc = null;
719  XQItem item = null;
720  try {
721  dm = zorba.getXmlDataManager();
722  doc = new Item();
723  ZorbaReaderWrapper stream = new ZorbaReaderWrapper(value);
724  doc = dm.parseXMLtoItem(stream);
725  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(doc);
726  } catch (Exception e) {
727  throw new XQException("Error creating Item" + e.getLocalizedMessage());
728  }
729  return item;
730  }
731 
732  /** \brief Creates an item from the given value.
733  *
734  * 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.
735  *
736  * 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.
737  *
738  * 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.
739  *
740  * @param value - the value to be converted, cannot be null
741  * @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.
742  * @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))
743  * @return ZorbaXQItem representing the resulting item
744  * @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
745  */
746  @Override
747  public XQItem createItemFromDocument(InputStream value, String baseURI, XQItemType type) throws XQException {
748  isClosedXQException();
749  isNullXQException(value);
750  if (type!=null) {
751  if ( ! ((type.getItemKind()== XQItemType.XQITEMKIND_DOCUMENT_ELEMENT)||(type.getItemKind()== XQItemType.XQITEMKIND_DOCUMENT_SCHEMA_ELEMENT)) ) {
752  throw new XQException("Invalid type");
753  }
754  } else {
755  type = this.createElementType(null, XQItemType.XQBASETYPE_UNTYPED);
756  }
757 
758  XmlDataManager dm = null;
759  Item doc = null;
760  XQItem item = null;
761  try {
762  dm = zorba.getXmlDataManager();
763  doc = new Item();
764  ZorbaInputWrapper stream = new ZorbaInputWrapper(value);
765  doc = dm.parseXMLtoItem(stream);
766  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(doc);
767  } catch (Exception e) {
768  throw new XQException("Error creating Item" + e.getLocalizedMessage());
769  }
770  return item;
771  }
772 
773  /** \brief Creates an item from the given value.
774  *
775  * 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.
776  *
777  * 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.
778  *
779  * 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.
780  *
781  * @param value - the value to be converted, cannot be null
782  * @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))
783  * @return ZorbaXQItem representing the resulting item
784  * @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
785  */
786  @Override
787  public XQItem createItemFromDocument(XMLStreamReader value, XQItemType type) throws XQException {
788  isClosedXQException();
789  isNullXQException(value);
790  TransformerFactory tf = TransformerFactory.newInstance();
791  Transformer t;
792  StAXSource source;
793  StAXResult result;
794  XMLOutputFactory xof = XMLOutputFactory.newInstance();
795  Writer writer = new StringWriter();
796  try {
797  XMLStreamWriter xmlStreamWriter = xof.createXMLStreamWriter(writer);
798  t = tf.newTransformer();
799  source = new StAXSource(value);
800  result = new StAXResult(xmlStreamWriter);
801  t.transform(source, result);
802  } catch (Exception ex) {
803  throw new XQException("Error transforming xml expression" + ex.getLocalizedMessage());
804  }
805  XQItem item = createItemFromDocument(writer.toString(), "", type);
806  return item;
807  }
808 
809  private String nodeToString(Node node) {
810  StringWriter sw = new StringWriter();
811  try {
812  Transformer t = TransformerFactory.newInstance().newTransformer();
813  t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
814  t.transform(new DOMSource(node), new StreamResult(sw));
815  } catch (TransformerException te) {
816  System.out.println("nodeToString Transformer Exception" + te.getLocalizedMessage());
817  }
818  return sw.toString();
819  }
820 
821  /** \brief Creates an item from the given value.
822  *
823  * An XQJ implementation must at least support the following implementations:
824  * - javax.xml.transform.dom.DOMSource
825  * - javax.xml.transform.sax.SAXSource
826  * - javax.xml.transform.stream.StreamSource
827  *
828  * 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.
829  *
830  * 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.
831  *
832  * 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.
833  *
834  * @param value - the value to be converted, cannot be null
835  * @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))
836  * @return ZorbaXQItem representing the resulting item
837  * @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
838  */
839  @Override
840  public XQItem createItemFromDocument(Source value, XQItemType type) throws XQException {
841  isClosedXQException();
842  isNullXQException(value);
843  XQItem item = null;
844  if (value instanceof StreamSource) {
845  item = createItemFromDocument(((StreamSource)value).getReader(), "", type);
846  } else if (value instanceof SAXSource) {
847  item = createItemFromDocument(((SAXSource)value).getInputSource().getByteStream(), "", type);
848  } else if (value instanceof DOMSource) {
849  item = createItemFromDocument( nodeToString(((DOMSource)value).getNode()), "", type);
850  } else {
851  throw new UnsupportedOperationException("Not supported yet.");
852  }
853  return item;
854 
855  }
856 
857  /** \brief Creates an item from a given value.
858  *
859  * 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.
860  *
861  * @param value - the value to be converted
862  * @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
863  * @return ZorbaXQItem representing the resulting item
864  * @throw XQException - (1) the conversion of the value to an XDM instance failed, or (2) the underlying object implementing the interface is closed
865  */
866  @Override
867  public XQItem createItemFromObject(Object value, XQItemType type) throws XQException {
868  isClosedXQException();
869  isNullXQException(value);
870  if (value instanceof XQItem) {
871  return (XQItem)value;
872  }
873 
874  ItemFactory itemFactory = zorba.getItemFactory();
875  XQItem item = null;
876  if (type==null) {
877 
878  if (value instanceof Boolean) {
879  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_BOOLEAN);
880  } else if (value instanceof Byte) {
881  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_BYTE);
882  } else if (value instanceof Float) {
883  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_FLOAT);
884  } else if (value instanceof Double) {
885  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_DOUBLE);
886  } else if (value instanceof Integer) {
887  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_INT);
888  } else if (value instanceof Long) {
889  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_LONG);
890  } else if (value instanceof Short) {
891  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_SHORT);
892  } else if (value instanceof String) {
893  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_STRING);
894  } else if (value instanceof BigDecimal) {
895  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_DECIMAL);
896  } else if (value instanceof BigInteger) {
897  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_INTEGER);
898  } else if (value instanceof Duration) {
899  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_DURATION);
900  } else if (value instanceof XMLGregorianCalendar) {
901  QName schType = ((XMLGregorianCalendar)value).getXMLSchemaType();
902  if (schType.equals(DatatypeConstants.GDAY)) {
903  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_GDAY);
904  } else if (schType.equals(DatatypeConstants.GMONTHDAY)) {
905  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_GMONTHDAY);
906  } else if (schType.equals(DatatypeConstants.GMONTH)) {
907  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_GMONTH);
908  } else if (schType.equals(DatatypeConstants.GYEAR)) {
909  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_GYEAR);
910  } else if (schType.equals(DatatypeConstants.GYEARMONTH)) {
911  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_GYEARMONTH);
912  } else if (schType.equals(DatatypeConstants.DATETIME)) {
913  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_DATETIME);
914  } else if (schType.equals(DatatypeConstants.DATE)) {
915  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_DATE);
916  } else if (schType.equals(DatatypeConstants.TIME)) {
917  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_TIME);
918  }
919  } else if (value instanceof QName) {
920  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_QNAME);
921  } else if (value instanceof Document) {
922  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_DOCUMENT);
923  } else if (value instanceof DocumentFragment) {
924  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_DOCUMENT);
925  } else if (value instanceof Element){
926  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ELEMENT);
927  } else if (value instanceof Attr) {
928  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATTRIBUTE);
929  } else if (value instanceof Comment) {
930  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_COMMENT);
931  } else if (value instanceof ProcessingInstruction) {
932  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_PI);
933  } else if (value instanceof Text) {
934  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_TEXT);
935  } else {
936  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC);
937  }
938  }
939  try {
940  switch (type.getItemKind()) {
941  case XQItemType.XQITEMKIND_ATOMIC:
942  switch (type.getBaseType()) {
943  case XQItemType.XQBASETYPE_BOOLEAN:
944  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createBoolean((Boolean)value), type);
945  break;
946  case XQItemType.XQBASETYPE_ANYURI:
947  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createAnyURI(value.toString()), type);
948  break;
949  case XQItemType.XQBASETYPE_BASE64BINARY:
950  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createBase64Binary(value.toString(), (value.toString()).length()), type);
951  break;
952  case XQItemType.XQBASETYPE_BYTE:
953  if (value instanceof Byte) {
954  byte tmpByte = ((Byte)value).byteValue();
955  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createByte((char)tmpByte), type);
956  } else {
957  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createByte((value.toString()).charAt(0)), type);
958  }
959  break;
960  case XQItemType.XQBASETYPE_DATE:
961  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDate(value.toString()), type);
962  break;
963  case XQItemType.XQBASETYPE_DATETIME:
964  if (value.toString().contains(":")) {
965  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDateTime(value.toString()), type);
966  } else {
967  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDate(value.toString()), type);
968  }
969  break;
970  case XQItemType.XQBASETYPE_DAYTIMEDURATION:
971  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDuration(value.toString()), type);
972  break;
973  case XQItemType.XQBASETYPE_DECIMAL:
974  if (value instanceof BigDecimal) {
975  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDecimal(((BigDecimal)value).toPlainString()), type);
976  } else {
977  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDecimal(value.toString()), type);
978  }
979  break;
980  case XQItemType.XQBASETYPE_DOUBLE:
981  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDouble(value.toString()), type);
982  break;
983  case XQItemType.XQBASETYPE_DURATION:
984  if (value instanceof Duration) {
985  Duration duration = (Duration)value;
986  Boolean daytime = duration.isSet(DatatypeConstants.DAYS) || duration.isSet(DatatypeConstants.HOURS) ||
987  duration.isSet(DatatypeConstants.MINUTES) || duration.isSet(DatatypeConstants.SECONDS);
988  Boolean yearmonth = duration.isSet(DatatypeConstants.YEARS) || duration.isSet(DatatypeConstants.MONTHS);
989  if (daytime && yearmonth){
990  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDuration(value.toString()), type);
991  } else if (yearmonth) {
992  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createYearMonthDuration(value.toString()), type);
993  } else if (daytime) {
994  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDayTimeDuration(value.toString()), type);
995  }
996  } else {
997  boolean yearMonth = value.toString().contains("Y") || value.toString().contains("M");
998  boolean dayTime = value.toString().contains("D") || value.toString().contains("T");
999  if (yearMonth && dayTime) {
1000  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDuration(value.toString()), type);
1001  } else if (yearMonth) {
1002  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createYearMonthDuration(value.toString()), type);
1003  } else if (dayTime) {
1004  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDayTimeDuration(value.toString()), type);
1005  }
1006  }
1007  break;
1008  case XQItemType.XQBASETYPE_FLOAT:
1009  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createFloat((Float)value), type);
1010  break;
1011  case XQItemType.XQBASETYPE_GDAY:
1012  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGDay(value.toString()), type);
1013  break;
1014  case XQItemType.XQBASETYPE_GMONTH:
1015  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGMonth(value.toString()), type);
1016  break;
1017  case XQItemType.XQBASETYPE_GMONTHDAY:
1018  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGMonthDay(value.toString()), type);
1019  break;
1020  case XQItemType.XQBASETYPE_GYEAR:
1021  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGYear(value.toString()), type);
1022  break;
1023  case XQItemType.XQBASETYPE_GYEARMONTH:
1024  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createGYearMonth(value.toString()), type);
1025  break;
1026  case XQItemType.XQBASETYPE_HEXBINARY:
1027  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createHexBinary(value.toString(), (value.toString()).length()), type);
1028  break;
1029  case XQItemType.XQBASETYPE_INT:
1030  case XQItemType.XQBASETYPE_INTEGER:
1031  if (value instanceof BigInteger) {
1032  BigInteger val = (BigInteger)value;
1033  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createInteger(val.longValue()), type);
1034  } else if (value instanceof Integer) {
1035  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createInt((Integer)value), type);
1036  } else if (value instanceof String) {
1037  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createInteger(value.toString()), type);
1038  } else {
1039  throw new XQException ("Error parsing integer: " + value.toString());
1040  }
1041  break;
1042  case XQItemType.XQBASETYPE_LONG:
1043  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createLong((Long)value), type);
1044  break;
1045  case XQItemType.XQBASETYPE_NCNAME:
1046  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNCName(value.toString()), type);
1047  break;
1048  case XQItemType.XQBASETYPE_NEGATIVE_INTEGER:
1049  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNegativeInteger((Long)value), type);
1050  break;
1051  case XQItemType.XQBASETYPE_NONNEGATIVE_INTEGER:
1052  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNonNegativeInteger(new BigInteger(value.toString())), type);
1053  break;
1054  case XQItemType.XQBASETYPE_NONPOSITIVE_INTEGER:
1055  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNonPositiveInteger((Long)value), type);
1056  break;
1057  case XQItemType.XQBASETYPE_POSITIVE_INTEGER:
1058  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createPositiveInteger(new BigInteger(value.toString())), type);
1059  break;
1060  case XQItemType.XQBASETYPE_QNAME:
1061  if (value instanceof QName) {
1062  QName qname = (QName) value;
1063  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createQName(qname.getNamespaceURI(), qname.getPrefix(), qname.getLocalPart()), type);
1064  } else if (value.toString().contains("{")) {
1065  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createQName(value.toString()), type);
1066  } else {
1067  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createQName("", value.toString()), type);
1068  }
1069  break;
1070  case XQItemType.XQBASETYPE_SHORT:
1071  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createShort((Short)value), type);
1072  break;
1073  case XQItemType.XQBASETYPE_STRING:
1074  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createString(value.toString()), type);
1075  break;
1076  case XQItemType.XQBASETYPE_TIME:
1077  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createTime(value.toString()), type);
1078  break;
1079  case XQItemType.XQBASETYPE_TOKEN:
1080  break;
1081  case XQItemType.XQBASETYPE_UNSIGNED_BYTE:
1082  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedByte((Short)value), type);
1083  break;
1084  case XQItemType.XQBASETYPE_UNSIGNED_INT:
1085  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedInt((Long)value), type);
1086  break;
1087  case XQItemType.XQBASETYPE_UNSIGNED_LONG:
1088  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedLong(new BigInteger(value.toString())), type);
1089  break;
1090  case XQItemType.XQBASETYPE_UNSIGNED_SHORT:
1091  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedShort((Integer)value), type);
1092  break;
1093  case XQItemType.XQBASETYPE_YEARMONTHDURATION:
1094  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDuration(value.toString()), type);
1095  break;
1096  case XQItemType.XQBASETYPE_ANYATOMICTYPE:
1097  case XQItemType.XQBASETYPE_ANYSIMPLETYPE:
1098  case XQItemType.XQBASETYPE_ANYTYPE:
1099  case XQItemType.XQBASETYPE_ENTITIES:
1100  case XQItemType.XQBASETYPE_ENTITY:
1101  case XQItemType.XQBASETYPE_ID:
1102  case XQItemType.XQBASETYPE_IDREF:
1103  case XQItemType.XQBASETYPE_IDREFS:
1104  case XQItemType.XQBASETYPE_LANGUAGE:
1105  case XQItemType.XQBASETYPE_NAME:
1106  case XQItemType.XQBASETYPE_NMTOKEN:
1107  case XQItemType.XQBASETYPE_NMTOKENS:
1108  case XQItemType.XQBASETYPE_NORMALIZED_STRING:
1109  case XQItemType.XQBASETYPE_NOTATION:
1110  case XQItemType.XQBASETYPE_UNTYPED:
1111  case XQItemType.XQBASETYPE_UNTYPEDATOMIC:
1112  throw new XQException ("Base type not supported yet.");
1113  default:
1114  throw new XQException ("Unable to determine XQBASETYPE.");
1115  }
1116  break;
1117  case XQItemType.XQITEMKIND_ATTRIBUTE:
1118  if (value instanceof Attr){
1119  Attr attribute = (Attr) value;
1120  Item iEmpty = new Item();
1121  Item nodeName = itemFactory.createQName(attribute.getNamespaceURI()==null?"":attribute.getNamespaceURI(),
1122  attribute.getPrefix()==null?"":attribute.getPrefix(),
1123  attribute.getName()==null?"":attribute.getName());
1124  Item nodeValue = null;
1125  if (!attribute.getValue().isEmpty()) {
1126  nodeValue = itemFactory.createQName(attribute.getNamespaceURI()==null?"":attribute.getNamespaceURI(),
1127  attribute.getPrefix()==null?"":attribute.getPrefix(),
1128  attribute.getValue()==null?"":attribute.getValue());
1129  } else {
1130  nodeValue = new Item();
1131  }
1132  //TODO: Rodolfo: use centralized constants instead of strings
1133  Item iUntyped = itemFactory.createQName("http://www.w3.org/2001/XMLSchema", "xs", "untyped");
1134  Item attributeNode = itemFactory.createAttributeNode(iEmpty, nodeName, iUntyped, nodeValue);
1135  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(attributeNode, type);
1136  }
1137  break;
1138  case XQItemType.XQITEMKIND_COMMENT:
1139  if (value instanceof Comment){
1140  Comment comment = (Comment) value;
1141  Item iEmpty = new Item();
1142  Item elementNode = itemFactory.createCommentNode(iEmpty, comment.getTextContent());
1143  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(elementNode, type);
1144  } else {
1145  Item iEmpty = new Item();
1146  Item elementNode = itemFactory.createCommentNode(iEmpty, value.toString());
1147  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(elementNode, type);
1148  }
1149  break;
1150  case XQItemType.XQITEMKIND_DOCUMENT:
1151  if ((value instanceof Document) || (value instanceof DocumentFragment)){
1152  DOMSource source = null;
1153  if (value instanceof Document) {
1154  source = new DOMSource((Document)value);
1155  } else {
1156  source = new DOMSource((DocumentFragment)value);
1157  }
1158 
1159  Transformer transformer = TransformerFactory.newInstance().newTransformer();
1160  transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
1161  StreamResult result = new StreamResult(new StringWriter());
1162 
1163  transformer.transform(source, result);
1164  String xmlString = result.getWriter().toString();
1165  Item tmpItem = new Item();
1166  if (xmlString.length()>0) {
1167  XmlDataManager dataManager = zorba.getXmlDataManager();
1168  tmpItem = dataManager.parseXMLtoItem(xmlString);
1169  } else {
1170  tmpItem = itemFactory.createDocumentNode("", "");
1171  }
1172 
1173  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(tmpItem, type);
1174  }
1175 
1176  break;
1177  case XQItemType.XQITEMKIND_ELEMENT:
1178  if (value instanceof Element){
1179  Element element = (Element) value;
1180  Item iEmpty = new Item();
1181  Item nodeName = itemFactory.createQName(element.getNamespaceURI()==null?"":element.getNamespaceURI(),
1182  element.getPrefix()==null?"":element.getPrefix(),
1183  element.getNodeName()==null?"":element.getNodeName());
1184  Item iUntyped = itemFactory.createQName("http://www.w3.org/2001/XMLSchema", "xs", "untyped");
1185  Item elementNode = itemFactory.createElementNode(iEmpty, nodeName, iUntyped, false, false);
1186  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(elementNode, type);
1187  }
1188  break;
1189  case XQItemType.XQITEMKIND_PI:
1190  if (value instanceof ProcessingInstruction){
1191  ProcessingInstruction pi = (ProcessingInstruction) value;
1192  Item iEmpty = new Item();
1193  Item elementNode = itemFactory.createPiNode(iEmpty,
1194  pi.getTarget(),
1195  pi.getTextContent(),
1196  pi.getBaseURI()==null?"":pi.getBaseURI());
1197  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(elementNode, type);
1198  }
1199  break;
1200  case XQItemType.XQITEMKIND_TEXT:
1201  if (value instanceof Text){
1202  Text text = (Text) value;
1203  Item iEmpty = new Item();
1204  Item elementNode = itemFactory.createTextNode(iEmpty, text.getWholeText());
1205  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(elementNode, type);
1206  }
1207  break;
1208  case XQItemType.XQITEMKIND_DOCUMENT_ELEMENT:
1209  case XQItemType.XQITEMKIND_DOCUMENT_SCHEMA_ELEMENT:
1210  case XQItemType.XQITEMKIND_ITEM:
1211  case XQItemType.XQITEMKIND_NODE:
1212  case XQItemType.XQITEMKIND_SCHEMA_ATTRIBUTE:
1213  case XQItemType.XQITEMKIND_SCHEMA_ELEMENT:
1214  throw new XQException ("This item kind is currently unsupported.");
1215  default:
1216  throw new XQException ("Unable to determine XQITEMKIND.");
1217  }
1218  } catch (Exception e) {
1219  throw new XQException ("Error parsing value" + e.getLocalizedMessage());
1220  }
1221  return item;
1222  }
1223 
1224  @Override
1225  public XQItem createItemFromBoolean(boolean bln, XQItemType type) throws XQException {
1226  isClosedXQException();
1227  if (type == null) {
1228  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_BOOLEAN);
1229  }
1230  if (type.getBaseType() != XQItemType.XQBASETYPE_BOOLEAN) {
1231  throw new XQException("Type is not boolean, use proper method");
1232  }
1233 
1234  XQItem item = null;
1235  try {
1236  ItemFactory itemFactory = zorba.getItemFactory();
1237  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createBoolean(bln));
1238  } catch (Exception e) {
1239  throw new XQException("Error creating new item");
1240  }
1241  return item;
1242  }
1243 
1244  private XQItem createDecimal(BigDecimal value, XQItemType type) throws XQException {
1245  XQItem item = null;
1246  try {
1247  ItemFactory itemFactory = zorba.getItemFactory();
1248  switch (type.getBaseType()) {
1249  case XQItemType.XQBASETYPE_BYTE:
1250  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createByte((char)value.byteValue()), type);
1251  break;
1252  case XQItemType.XQBASETYPE_INTEGER:
1253  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createInteger(value.longValue()), type);
1254  break;
1255  case XQItemType.XQBASETYPE_DECIMAL:
1256  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDecimalFromLong(value.longValue()), type);
1257  break;
1258  case XQItemType.XQBASETYPE_INT:
1259  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createInt(value.intValue()), type);
1260  break;
1261  case XQItemType.XQBASETYPE_LONG:
1262  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createLong(value.longValue()), type);
1263  break;
1264  case XQItemType.XQBASETYPE_NEGATIVE_INTEGER:
1265  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNegativeInteger(value.longValue()), type);
1266  break;
1267  case XQItemType.XQBASETYPE_NONNEGATIVE_INTEGER:
1268  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNonNegativeInteger(value.toBigInteger()), type);
1269  break;
1270  case XQItemType.XQBASETYPE_NONPOSITIVE_INTEGER:
1271  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createNonPositiveInteger(value.longValue()), type);
1272  break;
1273  case XQItemType.XQBASETYPE_POSITIVE_INTEGER:
1274  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createPositiveInteger(value.toBigInteger()), type);
1275  break;
1276  case XQItemType.XQBASETYPE_SHORT:
1277  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createShort(value.shortValue()), type);
1278  break;
1279  case XQItemType.XQBASETYPE_UNSIGNED_BYTE:
1280  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedByte(value.shortValue()), type);
1281  break;
1282  case XQItemType.XQBASETYPE_UNSIGNED_INT:
1283  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedInt(value.longValue()), type);
1284  break;
1285  case XQItemType.XQBASETYPE_UNSIGNED_LONG:
1286  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createUnsignedLong(value.toBigInteger()), type);
1287  break;
1288  case XQItemType.XQBASETYPE_UNSIGNED_SHORT:
1289  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createShort(value.shortValue()), type);
1290  break;
1291  default:
1292  throw new XQException("Type is not xs:decimal or a derivate.");
1293  }
1294  } catch (Exception e) {
1295  throw new XQException("Error creating Item: " + e.getLocalizedMessage());
1296  }
1297  return item;
1298  }
1299 
1300  /** \brief Creates an item from a given value.
1301  *
1302  * 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.
1303  *
1304  * @param b - the value to be converted
1305  * @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
1306  * @return ZorbaXQItem representing the resulting item
1307  * @throw XQException - (1) the conversion of the value to an XDM instance failed, or (2) the underlying object implementing the interface is closed
1308  */
1309  @Override
1310  public XQItem createItemFromByte(byte b, XQItemType type) throws XQException {
1311  isClosedXQException();
1312  if (type == null) {
1313  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_BYTE);
1314  }
1315  return createDecimal(new BigDecimal(b), type);
1316  }
1317 
1318  /** \brief Creates an item from a given value.
1319  *
1320  * 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.
1321  *
1322  * @param value - the value to be converted
1323  * @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
1324  * @return ZorbaXQItem representing the resulting item
1325  * @throw XQException - (1) the conversion of the value to an XDM instance failed, or (2) the underlying object implementing the interface is closed
1326  */
1327  @Override
1328  public XQItem createItemFromDouble(double value, XQItemType type) throws XQException {
1329  isClosedXQException();
1330  if (type == null) {
1331  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_DOUBLE);
1332  }
1333  if (type.getBaseType() != XQItemType.XQBASETYPE_DOUBLE) {
1334  throw new XQException("Type is not double, use proper method");
1335  }
1336  XQItem item = null;
1337  try {
1338  ItemFactory itemFactory = zorba.getItemFactory();
1339  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createDouble(value), type);
1340  } catch (Exception e) {
1341  throw new XQException("Error Creating Item From Double" + e.getLocalizedMessage());
1342  }
1343  return item;
1344  }
1345 
1346  /** \brief Creates an item from a given value.
1347  *
1348  * 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.
1349  *
1350  * @param value - the value to be converted
1351  * @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
1352  * @return ZorbaXQItem representing the resulting item
1353  * @throw XQException - (1) the conversion of the value to an XDM instance failed, or (2) the underlying object implementing the interface is closed
1354  */
1355  @Override
1356  public XQItem createItemFromFloat(float value, XQItemType type) throws XQException {
1357  isClosedXQException();
1358  if (type == null) {
1359  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_FLOAT);
1360  }
1361  if (type.getBaseType() != XQItemType.XQBASETYPE_FLOAT) {
1362  throw new XQException("Type is not float, use proper method");
1363  }
1364  XQItem item = null;
1365  try {
1366  ItemFactory itemFactory = zorba.getItemFactory();
1367  item = new org.zorbaxquery.api.xqj.ZorbaXQItem(itemFactory.createFloat(value), type);
1368  } catch (Exception e) {
1369  throw new XQException("Error Creating Item From Float" + e.getLocalizedMessage());
1370  }
1371  return item;
1372  }
1373 
1374  /** \brief Creates an item from a given value.
1375  *
1376  * 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.
1377  *
1378  * @param value - the value to be converted
1379  * @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
1380  * @return ZorbaXQItem representing the resulting item
1381  * @throw XQException - (1) the conversion of the value to an XDM instance failed, or (2) the underlying object implementing the interface is closed
1382  */
1383  @Override
1384  public XQItem createItemFromInt(int value, XQItemType type) throws XQException {
1385  isClosedXQException();
1386  if (type == null) {
1387  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_INT);
1388  }
1389  switch (type.getBaseType()) {
1390  case XQItemType.XQBASETYPE_BYTE:
1391  case XQItemType.XQBASETYPE_SHORT:
1392  throw new XQException("Can't downgrade the Base Type from an Int number");
1393  default:
1394  break;
1395  }
1396  return createDecimal(new BigDecimal(value), type);
1397  }
1398 
1399  /** \brief Creates an item from a given value.
1400  *
1401  * 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.
1402  *
1403  * @param value - the value to be converted
1404  * @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
1405  * @return ZorbaXQItem representing the resulting item
1406  * @throw XQException - (1) the conversion of the value to an XDM instance failed, or (2) the underlying object implementing the interface is closed
1407  */
1408  @Override
1409  public XQItem createItemFromLong(long value, XQItemType type) throws XQException {
1410  isClosedXQException();
1411  if (type == null) {
1412  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_LONG);
1413  }
1414  switch (type.getBaseType()) {
1415  case XQItemType.XQBASETYPE_BYTE:
1416  case XQItemType.XQBASETYPE_SHORT:
1417  case XQItemType.XQBASETYPE_INT:
1418  throw new XQException("Can't downgrade the Base Type from an Long number");
1419  default:
1420  break;
1421  }
1422  return createDecimal(new BigDecimal(value), type);
1423  }
1424 
1425  /** \brief Creates an item from a given value.
1426  *
1427  * 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.
1428  *
1429  * @param value - the value to be converted
1430  * @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
1431  * @return ZorbaXQItem representing the resulting item
1432  * @throw XQException - (1) the conversion of the value to an XDM instance failed, or (2) the underlying object implementing the interface is closed
1433  */
1434  @Override
1435  public XQItem createItemFromNode(Node value, XQItemType type) throws XQException {
1436  isClosedXQException();
1437  isNullXQException(value);
1438 
1439  XQItem result = null;
1440  try {
1441  result = createItemFromString(nodeToString(value), type);
1442  } catch( Exception e ) {
1443  throw new XQException("Error Creating Item From Node" + e.getLocalizedMessage());
1444  }
1445  return result;
1446  }
1447 
1448  /** \brief Creates an item from a given value.
1449  *
1450  * 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.
1451  *
1452  * @param value - the value to be converted
1453  * @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
1454  * @return ZorbaXQItem representing the resulting item
1455  * @throw XQException - (1) the conversion of the value to an XDM instance failed, or (2) the underlying object implementing the interface is closed
1456  */
1457  @Override
1458  public XQItem createItemFromShort(short value, XQItemType type) throws XQException {
1459  isClosedXQException();
1460  if (type == null) {
1461  type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, XQItemType.XQBASETYPE_SHORT);
1462  }
1463  if (type.getBaseType()==XQItemType.XQBASETYPE_BYTE) {
1464  throw new XQException("Can't create a Byte from a Short number");
1465  }
1466  return createDecimal(new BigDecimal(value), type);
1467  }
1468 
1469  /** \brief Creates a copy of the specified ZorbaXQItem.
1470  *
1471  * This method can be used, for example, to copy an XQResultItem object so that the new item is not dependant on the connection.
1472  *
1473  * @param value - the ZorbaXQItem to copy
1474  * @return ZorbaXQItem independent of any underlying ZorbaXQConnection is created
1475  * @throw XQException - if (1) the specified item is null, (2) the underlying object implementing the interface is closed, (3) the specified item is closed
1476  */
1477  @Override
1478  public XQItem createItem(XQItem value) throws XQException {
1479  isClosedXQException();
1480  isNullXQException(value);
1481  if (value.isClosed()) {
1482  throw new XQException("Item is closed.");
1483  }
1484  XQItem result = new org.zorbaxquery.api.xqj.ZorbaXQItem(value);
1485  return result;
1486  }
1487 
1488  /** \brief Creates a copy of the specified ZorbaXQSequence.
1489  *
1490  * 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.
1491  *
1492  * @param value - input sequence
1493  * @return ZorbaXQSequence representing a copy of the input sequence
1494  * @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
1495  */
1496  @Override
1497  public XQSequence createSequence(XQSequence value) throws XQException {
1498  isClosedXQException();
1499  isNullXQException(value);
1500  if (value.isClosed()) {
1501  throw new XQException("Sequence is closed.");
1502  }
1503  XQSequence result = null;
1504  try {
1505  result = new org.zorbaxquery.api.xqj.ZorbaXQSequence(value);
1506  } catch (Exception ex) {
1507  throw new XQException("Error creating sequence: " + ex.getLocalizedMessage());
1508  }
1509  return result;
1510  }
1511 
1512  /** \brief Creates an ZorbaXQSequence, containing all the items from the iterator.
1513  *
1514  * 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.
1515  *
1516  * @param value - input sequence
1517  * @return ZorbaXQSequence representing a copy of the input sequence
1518  * @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
1519  */
1520  @Override
1521  public XQSequence createSequence(Iterator value) throws XQException {
1522  isClosedXQException();
1523  isNullXQException(value);
1524  XQSequence result = null;
1525  try {
1526  result = new org.zorbaxquery.api.xqj.ZorbaXQSequence(value);
1527  } catch (Exception ex) {
1528  throw new XQException("Error creating sequence: " + ex.getLocalizedMessage());
1529  }
1530  return result;
1531  }
1532 
1533  /** \brief Creates a new ZorbaXQItemType object representing an XQuery atomic type.
1534  *
1535  * The item kind of the item type will be ZorbaXQItemType.XQITEMKIND_ATOMIC.
1536  * Example -
1537  * \code{.java}
1538  * ZorbaXQConnection conn = ...;
1539  *
1540  * // to create xs:integer item type
1541  * conn.createAtomicType(ZorbaXQItemType.XQBASETYPE_INTEGER);
1542  * \endcode
1543  * @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 />
1544  * @return a new ZorbaXQItemType representing the atomic type
1545  * @throw XQException - if (1) an invalid basetype value is passed in, or (2) the underlying object implementing the interface is closed
1546  */
1547  @Override
1548  public XQItemType createAtomicType(int basetype) throws XQException {
1549  isClosedXQException();
1550  if ((basetype==XQItemType.XQBASETYPE_UNTYPED) ||
1551  (basetype==XQItemType.XQBASETYPE_ANYTYPE) ||
1552  (basetype==XQItemType.XQBASETYPE_IDREFS) ||
1553  (basetype==XQItemType.XQBASETYPE_NMTOKENS) ||
1554  (basetype==XQItemType.XQBASETYPE_ENTITIES) ||
1555  (basetype==XQItemType.XQBASETYPE_ANYSIMPLETYPE)) {
1556  throw new XQException("Invalid base type.");
1557  }
1558  XQItemType type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, basetype);
1559  return type;
1560  }
1561 
1562  /** \brief Creates a new ZorbaXQItemType object representing an XQuery atomic type.
1563  *
1564  * The item kind of the item type will be ZorbaXQItemType.XQITEMKIND_ATOMIC.
1565  * Example -
1566  * \code{.java}
1567  * ZorbaXQConnection conn = ...;
1568  *
1569  * // to create xs:integer item type
1570  * conn.createAtomicType(ZorbaXQItemType.XQBASETYPE_INTEGER);
1571  * \endcode
1572  * @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 />
1573  * @param qname - the QName of the type. If the QName refers to a predefinied type, it must match the basetype. Can be null
1574  * @param uri - the URI to the schema. Can be null. This can only be specified if the typename is also specified
1575  * @return a new ZorbaXQItemType representing the atomic type
1576  * @throw XQException - if (1) an invalid basetype value is passed in, or (2) the underlying object implementing the interface is closed
1577  */
1578  @Override
1579  public XQItemType createAtomicType(int basetype, QName qname, URI uri) throws XQException {
1580  if (closed) {
1581  throw new XQException("Object closed");
1582  }
1583  if ((basetype==XQItemType.XQBASETYPE_UNTYPED) ||
1584  (basetype==XQItemType.XQBASETYPE_ANYTYPE) ||
1585  (basetype==XQItemType.XQBASETYPE_IDREFS) ||
1586  (basetype==XQItemType.XQBASETYPE_NMTOKENS) ||
1587  (basetype==XQItemType.XQBASETYPE_ENTITIES) ||
1588  (basetype==XQItemType.XQBASETYPE_ANYSIMPLETYPE)) {
1589  throw new XQException("Invalid base type.");
1590  }
1591  XQItemType type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATOMIC, qname, basetype, uri);
1592  return type;
1593  }
1594 
1595  /** \brief Creates a new ZorbaXQItemType object representing the XQuery attribute(nodename, basetype) type with the given node name and base type.
1596  *
1597  * This method can be used to create item type for attributes with a pre-defined schema type.
1598  *
1599  * Example -
1600  * \code{.java}
1601  * ZorbaXQConnection conn = ..; // An XQuery connection
1602  *
1603  * - attribute() // no node name, pass null for the node name
1604  *
1605  * conn.createAttributeType(null, ZorbaXQItemType.XQBASETYPE_ANYSIMPLETYPE);
1606  *
1607  * - attribute (*) // equivalent to attribute()
1608  *
1609  * conn.createAttributeType(null, ZorbaXQItemType.XQBASETYPE_ANYSIMPLETYPE);
1610  *
1611  * - attribute (person) // attribute of name person and any simple type.
1612  *
1613  * conn.createAttributeType(new QName("person"), ZorbaXQItemType.XQBASETYPE_ANYSIMPLETYPE);
1614  *
1615  * - attribute(foo:bar) // node name foo:bar, type is any simple type
1616  *
1617  * conn.createAttributeType(new QName("http://www.foo.com", "bar","foo"),
1618  * ZorbaXQItemType.XQBASETYPE_ANYSIMPLETYPE);
1619  *
1620  * - attribute(foo:bar, xs:integer) // node name foo:bar, type is xs:integer
1621  *
1622  * conn.createAttributeType(new QName("http://www.foo.com", "bar","foo"),
1623  * ZorbaXQItemType.XQBASETYPE_INTEGER);
1624  * \endcode
1625  * @param nodename - specifies the name of the node.null indicates a wildcard for the node name
1626  * @param basetype - the base type of the attribute. One of the ZorbaXQItemType.XQBASETYPE_* other than ZorbaXQItemType.XQBASETYPE_UNTYPED or ZorbaXQItemType.XQBASETYPE_ANYTYPE
1627  * @return a new ZorbaXQItemType representing the XQuery attribute(nodename, basetype) type
1628  * @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
1629  */
1630  @Override
1631  public XQItemType createAttributeType(QName nodename, int basetype) throws XQException {
1632  isClosedXQException();
1633  if ((basetype==XQItemType.XQBASETYPE_UNTYPED)||(basetype==XQItemType.XQBASETYPE_ANYTYPE)) {
1634  throw new XQException("Base Type can't be XQItemType.XQBASETYPE_UNTYPED or XQItemType.XQBASETYPE_ANYTYPE");
1635  }
1636  return new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATTRIBUTE, nodename, basetype);
1637  }
1638 
1639  /** \brief Creates a new ZorbaXQItemType object representing the XQuery attribute(nodename, basetype) type with the given node name and base type.
1640  *
1641  * The type name can reference either pre-defined simple types or user-defined simple types.
1642  *
1643  * Example -
1644  * \code{.java}
1645  * ZorbaXQConnection conn = ..; // An XQuery connection
1646  *
1647  * - attribute (name, employeename) // attribute name of type employeename
1648  *
1649  * conn.createAttributeType(new QName("name"), ZorbaXQItemType.XQBASETYPE_ANYSIMPLETYPE,
1650  * new QName("employeename"), null);
1651  *
1652  * - attribute (foo:bar, po:city)
1653  * where the prefix foo refers to the namespace http://www.foo.com and the
1654  * prefix po refers to the namespace "http://www.address.com"
1655  *
1656  * conn.createAttributeType(new QName("http://www.foo.com", "bar","foo"),
1657  * ZorbaXQItemType.XQBASETYPE_ANYSIMPLETYPE,
1658  * new QName("http://address.com", "address","po"), null);
1659  *
1660  * - attribute (zip, zipcode) // attribute zip of type zipchode which derives from
1661  * // xs:string
1662  *
1663  * conn.createAttributeType(new QName("zip"), ZorbaXQItemType.XQBASETYPE_STRING,
1664  * new QName("zipcode"), null);
1665  *
1666  * - attribute(foo:bar, po:hatsize)
1667  * where the prefix foo refers to the namespace http://www.foo.com and the
1668  * prefix po refers to the namespace "http://www.hatsizes.com"
1669  * with schema URI "http://hatschema.com"
1670  *
1671  * conn.createAttributeType(new QName("http://www.foo.com", "bar","foo"),
1672  * ZorbaXQItemType.XQBASETYPE_INTEGER,
1673  * new QName("http://www.hatsizes.com", "hatsize","po"),
1674  * new QName("http://hatschema.com"));
1675  * \endcode
1676  * @param nodename - specifies the name of the node.null indicates a wildcard for the node name
1677  * @param basetype - the base type of the attribute. One of the XQItemTyupe.XQBASETYPE_* constants other than ZorbaXQItemType.XQBASETYPE_UNTYPED or ZorbaXQItemType.XQBASETYPE_ANYTYPE
1678  * @param typename - the QName of the type. If the QName refers to a predefinied type, it must match the basetype. Can be null.
1679  * @param schemaURI - the URI to the schema. Can be null. This can only be specified if the typename is also specified
1680  * @return a new ZorbaXQItemType representing the XQuery attribute(nodename,basetype, typename,schemaURI) type.
1681  * @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
1682  */
1683  @Override
1684  public XQItemType createAttributeType(QName nodename, int basetype, QName typename, URI schemaURI) throws XQException {
1685  isClosedXQException();
1686  if ((basetype==XQItemType.XQBASETYPE_UNTYPED)||(basetype==XQItemType.XQBASETYPE_ANYTYPE)) {
1687  throw new XQException("Base Type can't be XQItemType.XQBASETYPE_UNTYPED or XQItemType.XQBASETYPE_ANYTYPE");
1688  }
1689  if ((schemaURI!=null) && (typename==null)) {
1690  throw new XQException("If Schema URI is specified, Base Type must be also specified");
1691  }
1692  return new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ATTRIBUTE, nodename, basetype, typename, schemaURI, true);
1693  }
1694 
1695  /** \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.
1696  *
1697  * Example -
1698  * \code{.java}
1699  * ZorbaXQConnection conn = ..; // An XQuery connection
1700  *
1701  * - schema-attribute (name) // schema-attribute name, found in the schema
1702  * // available at http://customerschema.com
1703  *
1704  * conn.createSchemaAttributeType(new QName("name"),
1705  * ZorbaXQItemType.XQBASETYPE_STRING,
1706  * new URI(http://customerschema.com));
1707  * \endcode
1708  *
1709  * @param nodename - specifies the name of the node
1710  * @param basetype - the base type of the attribute. One of the XQItemTyupe.XQBASETYPE_* constants other than ZorbaXQItemType.XQBASETYPE_UNTYPED or ZorbaXQItemType.XQBASETYPE_ANYTYPE
1711  * @param uri - the URI to the schema. Can be null
1712  * @return a new ZorbaXQItemType representing the XQuery schema-attribute(nodename,basetype, schemaURI) type
1713  * @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
1714  */
1715  @Override
1716  public XQItemType createSchemaAttributeType(QName nodename, int basetype, URI uri) throws XQException {
1717  isClosedXQException();
1718  return new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_SCHEMA_ATTRIBUTE, nodename, basetype, uri);
1719  }
1720 
1721  /** \brief Creates a new ZorbaXQItemType object representing the XQuery comment() type.
1722  *
1723  * Creates a new ZorbaXQItemType object representing the XQuery comment() type. The ZorbaXQItemType object will have the item kind set to ZorbaXQItemType.XQITEMKIND_COMMENT.
1724  *
1725  * Example -
1726  * \code{.java}
1727  * ZorbaXQConnection conn = ..; // An XQuery connection
1728  * ZorbaXQItemType cmttype = conn.createCommentType();
1729  *
1730  * int itemkind = cmttype.getItemKind(); // will be ZorbaXQItemType.XQITEMKIND_COMMENT
1731  *
1732  * ZorbaXQExpression expr = conn.createExpression();
1733  * ZorbaXQSequence result = expr.executeQuery("<!-- comments -->");
1734  *
1735  * result.next();
1736  * boolean pi = result.instanceOf(cmttype); // will be true
1737  * \endcode
1738  *
1739  * @return a new ZorbaXQItemType representing the XQuery comment() type
1740  * @throw XQException - if the underlying object implementing the interface is closed
1741  */
1742  @Override
1743  public XQItemType createCommentType() throws XQException {
1744  isClosedXQException();
1745  return new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_COMMENT);
1746  }
1747 
1748  /** \brief Creates a new ZorbaXQItemType object representing the XQuery document-node(elementType) type containing a single element.
1749  *
1750  * 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.
1751  *
1752  * @param elementType - an ZorbaXQItemType object representing an XQuery element() type, cannot be null
1753  * @return a new ZorbaXQItemType representing the XQuery document-node(elementType) type containing a single element
1754  * @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
1755  */
1756  @Override
1757  public XQItemType createDocumentElementType(XQItemType elementType) throws XQException {
1758  isClosedXQException();
1759  isNullXQException(elementType);
1760  if (elementType.getItemKind()!=XQItemType.XQITEMKIND_ELEMENT) {
1761  throw new XQException("Item Kind must be XQItemType.XQITEMKIND_ELEMENT");
1762  }
1763  return new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_DOCUMENT_ELEMENT, elementType.getNodeName(), elementType.getBaseType(), elementType.getSchemaURI());
1764  }
1765 
1766  /** \brief Creates a new ZorbaXQItemType object representing the XQuery document-node(elementType) type containing a single schema-element(...).
1767  *
1768  * 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.
1769  *
1770  * @param elementType - an ZorbaXQItemType object representing an XQuery schema-element(...) type, cannot be null
1771  * @return a new ZorbaXQItemType representing the XQuery document-node(elementType) type containing a single schema-element(...) element
1772  * @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
1773  */
1774  @Override
1775  public XQItemType createDocumentSchemaElementType(XQItemType elementType) throws XQException {
1776  isClosedXQException();
1777  if (elementType.getItemKind()!=XQItemType.XQITEMKIND_DOCUMENT_SCHEMA_ELEMENT) {
1778  throw new XQException("Item Kind must be XQItemType.XQITEMKIND_DOCUMENT_SCHEMA_ELEMENT");
1779  }
1780  return new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_DOCUMENT_ELEMENT, elementType.getNodeName(), elementType.getBaseType(), elementType.getSchemaURI());
1781  }
1782 
1783  /** \brief Creates a new ZorbaXQItemType object representing the XQuery document-node() type.
1784  *
1785  * The ZorbaXQItemType object will have the item kind set to ZorbaXQItemType.XQITEMKIND_DOCUMENT.
1786  *
1787  * @return a new ZorbaXQItemType representing the XQuery document-node() type
1788  * @throw XQException - if the underlying object implementing the interface is closed
1789  */
1790  @Override
1791  public XQItemType createDocumentType() throws XQException {
1792  isClosedXQException();
1793  return new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_DOCUMENT);
1794  }
1795 
1796  /** \brief Creates a new ZorbaXQItemType object representing the XQuery element(nodename, basetype) type, with the given node name and base type.
1797  *
1798  * This method can be used to create item type for elements with a pre-defined schema type.
1799  *
1800  * Example -
1801  * \code{.java}
1802  * ZorbaXQConnection conn = ..; // An XQuery connection
1803  * - element() // no node name, pass null for the node name
1804  *
1805  * conn.createElementType(null, ZorbaXQItemType.XQBASETYPE_ANYTYPE);
1806  *
1807  * - element (*) // equivalent to element()
1808  *
1809  * conn.createElementType(null, ZorbaXQItemType.XQBASETYPE_ANYTYPE);
1810  *
1811  * - element(person) // element of name person and any type.
1812  *
1813  * conn.createElementType(new QName("person"), ZorbaXQItemType.XQBASETYPE_ANYTYPE);
1814  *
1815  * - element(foo:bar) // node name foo:bar, type is anytype
1816  *
1817  * conn.createElementType(new QName("http://www.foo.com", "bar","foo"),
1818  * ZorbaXQItemType.XQBASETYPE_ANYTYPE);
1819  *
1820  * - element(foo:bar, xs:integer) // node name foo:bar, type is xs:integer
1821  *
1822  * conn.createElementType(new QName("http://www.foo.com", "bar","foo"),
1823  * ZorbaXQItemType.XQBASETYPE_INTEGER);
1824  * \endcode
1825  * @param nodename - specifies the name of the node. null indicates a wildcard for the node name
1826  * @param baseType - the base type of the item. One of the ZorbaXQItemType.XQBASETYPE_* constants
1827  * @return a new ZorbaXQItemType representing the XQuery element(nodename, basetype) type
1828  * @throw XQException - if (1) the underlying object implementing the interface is closed
1829  */
1830  @Override
1831  public XQItemType createElementType(QName nodename, int baseType) throws XQException {
1832  isClosedXQException();
1833  XQItemType item = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ELEMENT, nodename, baseType);
1834  return item;
1835  }
1836 
1837  /** \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.
1838  *
1839  * The type name can reference either pre-defined schema types or user-defined types.
1840  *
1841  * Example -
1842  * \code{.java}
1843  * ZorbaXQConnection conn = ..; // An XQuery connection
1844  *
1845  * - element (person, employee) // element person of type employee
1846  *
1847  * conn.createElementType(new QName("person"), ZorbaXQItemType.XQBASETYPE_ANYTYPE,
1848  * new QName("employee"), null ,false);
1849  *
1850  * - element(person, employee ? ) // element person of type employee, whose nilled
1851  * // property may be true or false.
1852  *
1853  * conn.createElementType(new QName("person"), ZorbaXQItemType.XQBASETYPE_ANYTYPE,
1854  * new QName("employee"), null ,true);
1855  *
1856  * - element(foo:bar, po:address)
1857  * where the prefix foo refers to the namespace http://www.foo.com and the
1858  * prefix po refers to the namespace "http://www.address.com"
1859  *
1860  * conn.createElementType(new QName("http://www.foo.com", "bar","foo"),
1861  * ZorbaXQItemType.XQBASETYPE_ANYTYPE,
1862  * new QName("http://address.com", "address","po"), null, false);
1863  *
1864  * - element (zip, zipcode) // element zip of type zipchode which derives from
1865  * // xs:string
1866  *
1867  * conn.createElementType(new QName("zip"), ZorbaXQItemType.XQBASETYPE_STRING,
1868  * new QName("zipcode"), null, false);
1869  *
1870  * - element (*, xs:anyType ?)
1871  *
1872  * conn.createElementType(null, ZorbaXQItemType.XQBASETYPE_ANYTYPE, null, null, true);
1873  *
1874  * - element(foo:bar, po:hatsize)
1875  * where the prefix foo refers to the namespace http://www.foo.com and the
1876  * prefix po refers to the namespace "http://www.hatsizes.com"
1877  * with schema URI "http://hatschema.com"
1878  *
1879  * conn.createElementType(new QName("http://www.foo.com", "bar","foo"),
1880  * ZorbaXQItemType.XQBASETYPE_INTEGER,
1881  * new QName("http://www.hatsizes.com", "hatsize","po"),
1882  * new QName("http://hatschema.com"), false);
1883  *
1884  * \endcode
1885  *
1886  * @param nodename - specifies the name of the element. null indicates a wildcard for the node name
1887  * @param baseType - the base type of the item. One of the ZorbaXQItemType.XQBASETYPE_* constants
1888  * @param typename - the QName of the type. If the QName refers to a predefinied type, it must match the basetype. Can be null
1889  * @param schemaURI - the URI to the schema. Can be null. This can only be specified if the typename is also specified
1890  * @param allowNill - the nilled property of the element
1891  * @return a new ZorbaXQItemType representing the XQuery element(nodename,basetype, typename,schemaURI, allowNill) type
1892  * @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
1893  */
1894  @Override
1895  public XQItemType createElementType(QName nodename, int baseType, QName typename, URI schemaURI, boolean allowNill) throws XQException {
1896  isClosedXQException();
1897  XQItemType item = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ELEMENT, nodename, baseType, typename, schemaURI, allowNill);
1898  return item;
1899  }
1900 
1901  /** \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.
1902  *
1903  * Example -
1904  * \code{.java}
1905  * ZorbaXQConnection conn = ..; // An XQuery connection
1906  *
1907  * - schema-element (customer) // schema-element person, found in
1908  * // the schema available at http://customerschema.com
1909  *
1910  * conn.createElementType(new QName("customer"), ZorbaXQItemType.XQBASETYPE_ANYTYPE,
1911  * new URI("http://customerschema.com"));
1912  * \endcode
1913  * @param nodename - specifies the name of the element
1914  * @param baseType - the base type of the item. One of the ZorbaXQItemType.XQBASETYPE_* constants
1915  * @param schemaURI - the URI to the schema. Can be null
1916  * @return a new ZorbaXQItemType representing the XQuery schema-element(nodename,basetype, schemaURI) type
1917  * @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
1918  */
1919  @Override
1920  public XQItemType createSchemaElementType(QName nodename, int baseType, URI schemaURI) throws XQException {
1921  isClosedXQException();
1922  XQItemType item = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_SCHEMA_ELEMENT, nodename, baseType, schemaURI);
1923  return item;
1924  }
1925 
1926  /** \brief Creates a new ZorbaXQItemType object representing the XQuery item type.
1927  *
1928  * The ZorbaXQItemType object will have the item kind set to ZorbaXQItemType.XQITEMKIND_ITEM.
1929  *
1930  * Example -
1931  * \code{.java}
1932  * ZorbaXQConnection conn = ..; // An XQuery connection
1933  * ZorbaXQItemType typ = conn.createItemType(); // represents the XQuery item type "item()"
1934  * \endcode
1935  * @return a new ZorbaXQItemType representing the XQuery item type
1936  * @throw XQException - if the underlying object implementing the interface is closed
1937  */
1938  @Override
1939  public XQItemType createItemType() throws XQException {
1940  isClosedXQException();
1941  XQItemType type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_ITEM);
1942  return type;
1943  }
1944 
1945  /** \brief Creates a new ZorbaXQItemType object representing the XQuery node() type.
1946  *
1947  * The ZorbaXQItemType object will have the item kind set to ZorbaXQItemType.XQITEMKIND_NODE.
1948  *
1949  * @return a new ZorbaXQItemType representing the XQuery node() type
1950  * @throw XQException - if the underlying object implementing the interface is closed
1951  */
1952  @Override
1953  public XQItemType createNodeType() throws XQException {
1954  isClosedXQException();
1955  XQItemType type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_NODE);
1956  return type;
1957  }
1958 
1959  /** \brief Creates a new ZorbaXQItemType object representing the XQuery processing-instruction(piTarget) type.
1960  *
1961  * 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.
1962  *
1963  * Example -
1964  * \code{.java}
1965  * ZorbaXQConnection conn = ..; // An XQuery connection
1966  * ZorbaXQItemType anypi = conn.createProcessingInstructionType();
1967  * ZorbaXQItemType foopi = conn.createProcessingInstructionType("foo-format");
1968  *
1969  * ZorbaXQExpression expr = conn.createExpression();
1970  * ZorbaXQSequence result = expr.executeQuery("<?format role="output" ?>");
1971  *
1972  * result.next();
1973  * boolean pi = result.instanceOf(anypi); // will be true
1974  * pi = result.instanceOf(foopi); // will be false
1975  *
1976  * ZorbaXQExpression expr = conn.createExpression();
1977  * ZorbaXQSequence result = expr.executeQuery("<?foo-format role="output" ?>");
1978  *
1979  * result.next();
1980  * boolean pi = result.instanceOf(anypi); // will be true
1981  * pi = result.instanceOf(foopi); // will be true
1982  * \endcode
1983  *
1984  * @param piTarget - the string literal to match the processing instruction's PITarget. A null string value will match all processing instruction nodes
1985  * @return a new ZorbaXQItemType representing the XQuery processing-instruction(piTarget) type
1986  * @throw XQException - if the underlying object implementing the interface is closed
1987  */
1988  @Override
1989  public XQItemType createProcessingInstructionType(String piTarget) throws XQException {
1990  isClosedXQException();
1991  XQItemType type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_PI, piTarget);
1992  return type;
1993  }
1994 
1995  /** \brief Creates a new sequence type from an item type and occurence indicator.
1996  *
1997  * @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
1998  * @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
1999  * @return a new ZorbaXQSequenceType representing the type of a sequence
2000  * @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
2001  */
2002  @Override
2003  public XQSequenceType createSequenceType(XQItemType item, int occurence) throws XQException {
2004  isClosedXQException();
2005  if ( ((item==null) && (occurence != XQSequenceType.OCC_EMPTY)) || ((item!=null) && (occurence == XQSequenceType.OCC_EMPTY))) {
2006  throw new XQException("Item is null");
2007  }
2008  if (!((occurence == XQSequenceType.OCC_ZERO_OR_ONE) ||
2009  (occurence == XQSequenceType.OCC_EXACTLY_ONE) ||
2010  (occurence == XQSequenceType.OCC_ZERO_OR_MORE) ||
2011  (occurence == XQSequenceType.OCC_ONE_OR_MORE) ||
2012  (occurence == XQSequenceType.OCC_EMPTY))) {
2013  throw new XQException("Occurence must be from: OCC_ZERO_OR_ONE, OCC_EXACTLY_ONE, OCC_ZERO_OR_MORE, OCC_ONE_OR_MORE, OCC_EMPTY ");
2014  }
2015 
2016  XQSequenceType result = new org.zorbaxquery.api.xqj.ZorbaXQSequenceType(item, occurence);
2017  return result;
2018  }
2019 
2020  /** \brief Creates a new ZorbaXQItemType object representing the XQuery text() type.
2021  *
2022  * The ZorbaXQItemType object will have the item kind set to ZorbaXQItemType.XQITEMKIND_TEXT.
2023  *
2024  * @return a new ZorbaXQItemType representing the XQuery text() type
2025  * @throw XQException - if the underlying object implementing the interface is closed
2026  */
2027  @Override
2028  public XQItemType createTextType() throws XQException {
2029  isClosedXQException();
2030  XQItemType type = new org.zorbaxquery.api.xqj.ZorbaXQItemType(XQItemType.XQITEMKIND_TEXT);
2031  return type;
2032  }
2033 
2034  private void isClosedXQException() throws XQException {
2035  if (closed) {
2036  throw new XQException("This connection is closed");
2037  }
2038  }
2039  private void isNullXQException(Object value) throws XQException {
2040  if (value==null) {
2041  throw new XQException("Parameter shouldn't be null");
2042  }
2043  }
2044 
2045 }