Package org.exist.xquery.value

Examples of org.exist.xquery.value.Item


       
        // Iterate through each item in the right-hand sequence
        for( final SequenceIterator itRightSeq = rightSeq.iterate(); itRightSeq.hasNext(); ) {

            //Get the index key
            Item key = itRightSeq.nextItem().atomize();

            //if key has truncation, convert it to string
            if( truncation != Constants.TRUNC_NONE ) {

                if( !Type.subTypeOf( key.getType(), Type.STRING ) ) {
                    LOG.info( "Truncated key. Converted from " + Type.getTypeName( key.getType() ) + " to xs:string" );

                    //truncation is only possible on strings
                    key = key.convertTo( Type.STRING );
                }
            }
            //else if key is not the same type as the index
            //TODO : use Type.isSubType() ??? -pb
            else if( key.getType() != indexType ) {

                //try to convert the key to the index type
                try {
                    key = key.convertTo( indexType );
                }
                catch( final XPathException xpe ) {

                    if( LOG.isTraceEnabled() ) {
                        LOG.trace( "Cannot convert key: " + Type.getTypeName( key.getType() ) + " to required index type: " + Type.getTypeName( indexType ) );
                    }

                    throw( new XPathException( this, "Cannot convert key to required index type" ) );
                }
            }

            // If key implements org.exist.storage.Indexable, we can use the index
            if( key instanceof Indexable ) {

                if( LOG.isTraceEnabled() ) {
                    LOG.trace( "Using QName range index for key: " + key.getStringValue() );
                }

                NodeSet  temp;
                final NodeSet  contextSet = useContext ? contextSequence.toNodeSet() : null;
                final Collator collator   = ( ( collationArg != null ) ? getCollator( contextSequence ) : null );

                if( truncation == Constants.TRUNC_NONE ) {
                    temp         = context.getBroker().getValueIndex().find(context.getWatchDog(), relation, contextSequence.getDocumentSet(), contextSet, NodeSet.DESCENDANT, contextQName, ( Indexable )key, collator );
                    hasUsedIndex = true;
                } else {

                    try {
                        final String matchString = key.getStringValue();
                        final int    matchType   = getMatchType( truncation );

                        temp         = context.getBroker().getValueIndex().match(context.getWatchDog(), contextSequence.getDocumentSet(), contextSet, NodeSet.DESCENDANT, matchString, contextQName, matchType, collator, truncation );

                        hasUsedIndex = true;
View Full Code Here


            //Iterate through the right hand sequence
            for( final SequenceIterator itRightSeq = rightSeq.iterate(); itRightSeq.hasNext(); ) {

                //Get the index key
                Item key = itRightSeq.nextItem().atomize();

                //if key has truncation, convert it to string
                if( truncation != Constants.TRUNC_NONE ) {

                    if( !Type.subTypeOf( key.getType(), Type.STRING ) ) {
                        LOG.info( "Truncated key. Converted from " + Type.getTypeName( key.getType() ) + " to xs:string" );

                        //truncation is only possible on strings
                        key = key.convertTo( Type.STRING );
                    }
                }
                //else if key is not the same type as the index
                //TODO : use Type.isSubType() ??? -pb
                else if( key.getType() != indexType ) {

                    //try to convert the key to the index type
                    try {
                        key = key.convertTo( indexType );
                    }
                    catch( final XPathException xpe ) {
                        //TODO : rethrow the exception ? -pb

                        //Could not convert the key to a suitable type for the index, fallback to nodeSetCompare()
                        if( context.getProfiler().isEnabled() ) {
                            context.getProfiler().message( this, Profiler.OPTIMIZATION_FLAGS, "OPTIMIZATION FALLBACK", "Falling back to nodeSetCompare (" + xpe.getMessage() + ")" );
                        }

                        if( LOG.isTraceEnabled() ) {
                            LOG.trace( "Cannot convert key: " + Type.getTypeName( key.getType() ) + " to required index type: " + Type.getTypeName( indexType ) );
                        }

                        return( nodeSetCompare( nodes, contextSequence ) );
                    }
                }

                // If key implements org.exist.storage.Indexable, we can use the index
                if( key instanceof Indexable ) {

                    if( LOG.isTraceEnabled() ) {
                        LOG.trace( "Checking if range index can be used for key: " + key.getStringValue() );
                    }

                    final Collator collator = ( ( collationArg != null ) ? getCollator( contextSequence ) : null );

                    if( Type.subTypeOf( key.getType(), indexType ) ) {

                        if( truncation == Constants.TRUNC_NONE ) {

                            if( LOG.isTraceEnabled() ) {
                                LOG.trace( "Using range index for key: " + key.getStringValue() );
                            }

                            //key without truncation, find key
                            context.getProfiler().message( this, Profiler.OPTIMIZATIONS, "OPTIMIZATION", "Using value index '" + context.getBroker().getValueIndex().toString() + "' to find key '" + Type.getTypeName( key.getType() ) + "(" + key.getStringValue() + ")'" );

                            NodeSet ns;

                            if( indexScan ) {
                                ns = context.getBroker().getValueIndex().findAll( context.getWatchDog(), relation, docs, nodes, NodeSet.ANCESTOR, ( Indexable )key, collator );
                            } else {
                                ns = context.getBroker().getValueIndex().find( context.getWatchDog(), relation, docs, nodes, NodeSet.ANCESTOR, myContextQName,
                                        ( Indexable )key, collator, indexMixed );
                            }
                            hasUsedIndex = true;

                            if( result == null ) {
                                result = ns;
                            } else {
                                result = result.union( ns );
                            }

                        } else {

                            //key with truncation, match key
                            if( LOG.isTraceEnabled() ) {
                                context.getProfiler().message( this, Profiler.OPTIMIZATIONS, "OPTIMIZATION", "Using value index '" + context.getBroker().getValueIndex().toString() + "' to match key '" + Type.getTypeName( key.getType() ) + "(" + key.getStringValue() + ")'" );
                            }

                            if( LOG.isTraceEnabled() ) {
                                LOG.trace( "Using range index for key: " + key.getStringValue() );
                            }

                            try {
                                NodeSet ns;

                                final String  matchString = key.getStringValue();
                                final int     matchType   = getMatchType( truncation );

                                if( indexScan ) {
                                    ns = context.getBroker().getValueIndex().matchAll( context.getWatchDog(), docs, nodes, NodeSet.ANCESTOR, matchString, matchType, 0, true, collator, truncation );
                                } else {
                                    ns = context.getBroker().getValueIndex().match( context.getWatchDog(), docs, nodes, NodeSet.ANCESTOR, matchString, myContextQName, matchType, collator, truncation );
                                }

                                hasUsedIndex = true;

                                if( result == null ) {
                                    result = ns;
                                } else {
                                    result = result.union( ns );
                                }

                            }
                            catch( final EXistException e ) {
                                throw( new XPathException( this, e ) );
                            }
                        }
                    } else {

                        //our key does is not of the correct type
                        if( context.getProfiler().isEnabled() ) {
                            context.getProfiler().message( this, Profiler.OPTIMIZATION_FLAGS, "OPTIMIZATION FALLBACK", "Falling back to nodeSetCompare (key is of type: " + Type.getTypeName( key.getType() ) + ") whereas index is of type '" + Type.getTypeName( indexType ) + "'" );
                        }

                        if( LOG.isTraceEnabled() ) {
                            LOG.trace( "Cannot use range index: key is of type: " + Type.getTypeName( key.getType() ) + ") whereas index is of type '" + Type.getTypeName( indexType ) );
                        }

                        return( nodeSetCompare( nodes, contextSequence ) );
                    }
                } else {

                    //our key does not implement org.exist.storage.Indexable
                    if( context.getProfiler().isEnabled() ) {
                        context.getProfiler().message( this, Profiler.OPTIMIZATION_FLAGS, "OPTIMIZATION FALLBACK", "Falling back to nodeSetCompare (key is not an indexable type: " + key.getClass().getName() );
                    }

                    if( LOG.isTraceEnabled() ) {
                        LOG.trace( "Cannot use key which is of type '" + key.getClass().getName() );
                    }

                    return( nodeSetCompare( nodes, contextSequence ) );

                }
View Full Code Here

                final MemTreeBuilder builder = context.getDocumentBuilder();
                context.proceed(this, builder);
                final StringBuilder buf = new StringBuilder();
                for(final SequenceIterator i = contentSeq.iterate(); i.hasNext(); ) {
                    context.proceed(this, builder);
                    final Item next = i.nextItem();
                    if(buf.length() > 0)
                        {buf.append(' ');}
                    buf.append(next.toString());
                }
                //It is possible for a text node constructor to construct a text node containing a zero-length string.
                //However, if used in the content of a constructed element or document node,
                //such a text node will be deleted or merged with another text node.
                if (!newDocumentContext && buf.length() == 0)
View Full Code Here

          );}
    }
   
    private Sequence convert(Sequence seq) throws XPathException {
        final ValueSequence result = new ValueSequence();
        Item item;
        for(final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
            item = i.nextItem();
            result.add(item.convertTo(type.getPrimaryType()));
        }
        return result;
    }
View Full Code Here

    boolean canDecide = (mode == EVERY) ? true : false;

    for (final SequenceIterator i = inSeq.iterate(); i.hasNext(); ) {
      canDecide = true;
     
      final Item item = i.nextItem();     
      // set variable value to current item
            var.setValue(item.toSequence());
            if (sequenceType == null)
                {var.checkType();} //... because is makes some conversions
     
            Sequence satisfiesSeq = null;
           
            //Binds the variable : now in scope
        final LocalVariable mark = context.markLocalVariables(false);
        try {
          context.declareVariableBinding(var);
          //Evaluate the return clause for the current value of the variable
          satisfiesSeq = returnExpr.eval(contextSequence, contextItem);
        } finally {
          //Unbind the variable until the next iteration : now out of scope
          context.popLocalVariables(mark, satisfiesSeq);
        }
           
            if (sequenceType != null) {
            //TODO : ignore nodes right now ; they are returned as xs:untypedAtomicType
            if (!Type.subTypeOf(sequenceType.getPrimaryType(), Type.NODE)) {
                if (!Type.subTypeOf(item.toSequence().getItemType(), sequenceType.getPrimaryType()))
              {throw new XPathException(this, ErrorCodes.XPTY0004, "Invalid type for variable $" + varName +
                  ". Expected " +
                  Type.getTypeName(sequenceType.getPrimaryType()) +
                  ", got " +Type.getTypeName(contextItem.toSequence().getItemType()), inSeq);}
              } else if (!Type.subTypeOf(item.getType(), Type.NODE))
            {throw new XPathException(this, ErrorCodes.XPTY0004, "Invalid type for variable $" + varName +
                ". Expected " +
                Type.getTypeName(Type.NODE) +
                " (or more specific), got " + Type.getTypeName(item.getType()), inSeq);}
            //trigger the old behaviour
            else {var.checkType();}
            } 
           
      found = satisfiesSeq.effectiveBooleanValue();
View Full Code Here

            final Sequence nameSeq = qnameExpr.eval(contextSequence, contextItem);
            if(!nameSeq.hasOne())
              {throw new XPathException(this, "The name expression should evaluate to a single value");}

            final Item qnItem = nameSeq.itemAt(0);
            QName qn;
            if (qnItem.getType() == Type.QNAME)
                {qn = ((QNameValue) qnItem).getQName();}
            else
              try {
                qn = QName.parse(context, nameSeq.getStringValue(), null);
          } catch (final IllegalArgumentException e) {
          throw new XPathException(this, ErrorCodes.XPTY0004, "'" + nameSeq.getStringValue() + "' is not a valid attribute name");
        }

            //Not in the specs but... makes sense
            if(!XMLChar.isValidName(qn.getLocalName()))
              {throw new XPathException(this, ErrorCodes.XPTY0004, "'" + qn.getLocalName() + "' is not a valid attribute name");}
           
            if ("xmlns".equals(qn.getLocalName()) && qn.getNamespaceURI().isEmpty())
              {throw new XPathException(this, ErrorCodes.XQDY0044, "'" + qn.getLocalName() + "' is not a valid attribute name");}

            String value;
            final Sequence valueSeq = valueExpr.eval(contextSequence, contextItem);
            if(valueSeq.isEmpty())
              {value = "";}
            else {
                final StringBuilder buf = new StringBuilder();
                for(final SequenceIterator i = valueSeq.iterate(); i.hasNext(); ) {
                    final Item next = i.nextItem();
                    buf.append(next.getStringValue());
                    if(i.hasNext())
                        {buf.append(' ');}
                }
                value = buf.toString();
            }
View Full Code Here

        if (rseq.isEmpty())
            {result = Sequence.EMPTY_SEQUENCE;}
        else if (lseq.isEmpty())
            {result = Sequence.EMPTY_SEQUENCE;}
        else {
            Item lvalue = lseq.itemAt(0);
            Item rvalue = rseq.itemAt(0);
            try {
                if (lvalue.getType() == Type.UNTYPED_ATOMIC || lvalue.getType() == Type.ATOMIC)
                    {lvalue = lvalue.convertTo(Type.NUMBER);}
                if (rvalue.getType() == Type.UNTYPED_ATOMIC || rvalue.getType() == Type.ATOMIC)
                    {rvalue = rvalue.convertTo(Type.NUMBER);}
                if (!(lvalue instanceof ComputableValue))
                    {throw new XPathException(this, ErrorCodes.XPTY0004, "'" +
                        Type.getTypeName(lvalue.getType()) + "(" + lvalue + ")' can not be an operand for " +
                        Constants.OPS[operator]);}
                if (!(rvalue instanceof ComputableValue))
                    {throw new XPathException(this, ErrorCodes.XPTY0004, "'" +
                        Type.getTypeName(rvalue.getType()) + "(" + rvalue + ")' can not be an operand for " +
                        Constants.OPS[operator]);}
                //TODO : move to implementations
                if (operator == Constants.IDIV) {
                    if (!Type.subTypeOf(lvalue.getType(), Type.NUMBER))
                        {throw new XPathException(this, ErrorCodes.XPTY0004, "'" +
                            Type.getTypeName(lvalue.getType()) + "(" + lvalue + ")' can not be an operand for " + Constants.OPS[operator]);}
                    if (!Type.subTypeOf(rvalue.getType(), Type.NUMBER))
                        {throw new XPathException(this, ErrorCodes.XPTY0004, "'" +
                            Type.getTypeName(rvalue.getType()) + "(" + rvalue + ")' can not be an operand for " + Constants.OPS[operator]);}
                    //If the divisor is (positive or negative) zero, then an error is raised [err:FOAR0001]
                    if (((NumericValue)rvalue).isZero())
                        {throw new XPathException(this, ErrorCodes.FOAR0001, "Division by zero");}
                    //If either operand is NaN then an error is raised [err:FOAR0002].
                    if (((NumericValue)lvalue).isNaN())
                        {throw new XPathException(this, ErrorCodes.FOAR0002, "Division of " +
                            Type.getTypeName(lvalue.getType()) + "(" + lvalue + ")'");}
                    //If either operand is NaN then an error is raised [err:FOAR0002].
                    if (((NumericValue)rvalue).isNaN())
                        {throw new XPathException(this, ErrorCodes.FOAR0002, "Division of " +
                            Type.getTypeName(rvalue.getType()) + "(" + rvalue + ")'");}
                    //If $arg1 is INF or -INF then an error is raised [err:FOAR0002].
                    if (((NumericValue)lvalue).isInfinite())
                        {throw new XPathException(this, ErrorCodes.FOAR0002, "Division of " +
                            Type.getTypeName(lvalue.getType()) + "(" + lvalue + ")'");}
                    result = ((NumericValue) lvalue).idiv((NumericValue) rvalue);
View Full Code Here

    public static Sequence atomize(Sequence input) throws XPathException {
        if (input.isEmpty())
            {return Sequence.EMPTY_SEQUENCE;}
        if (input.hasOne())
            {return input.itemAt(0).atomize();}
        Item next;
        final ValueSequence result = new ValueSequence();
        for(final SequenceIterator i = input.iterate(); i.hasNext(); ) {
            next = i.nextItem();
            result.add(next.atomize());
        }
        return result;
    }
View Full Code Here

                result = new ValueSequence();
                final Set<Item> set = new TreeSet<Item>();
                for (final SequenceIterator i = rval.unorderedIterator(); i.hasNext(); )
                    set.add(i.nextItem());
                for (final SequenceIterator i = lval.unorderedIterator(); i.hasNext(); ) {
                    final Item next = i.nextItem();
                    if (!set.contains(next))
                        {result.add(next);}
                }
                result.removeDuplicates();
            }
View Full Code Here

            // create the element
            final Sequence qnameSeq = qnameExpr.eval(contextSequence, contextItem);
            if(!qnameSeq.hasOne())
              {throw new XPathException(this, ErrorCodes.XPTY0004, "Type error: the node name should evaluate to a single item");}
            final Item qnitem = qnameSeq.itemAt(0);
            QName qn;
            if (qnitem instanceof QNameValue) {
                qn = ((QNameValue)qnitem).getQName();
            } else {
                //Do we have the same result than Atomize there ? -pb
              try {
                qn = QName.parse(context, qnitem.getStringValue());
              } catch (final IllegalArgumentException e) {
              throw new XPathException(this, ErrorCodes.XPTY0004, "" + qnitem.getStringValue() + "' is not a valid element name");
              } catch (final XPathException e) {
                e.setLocation(getLine(), getColumn(), getSource());
                throw e;
        }
             
                //Use the default namespace if specified
                /*
                 if (qn.getPrefix() == null && context.inScopeNamespaces.get("xmlns") != null) {
                     qn.setNamespaceURI((String)context.inScopeNamespaces.get("xmlns"));
                 }
                 */
                if (qn.getPrefix() == null && context.getInScopeNamespace("") != null) {
                     qn.setNamespaceURI(context.getInScopeNamespace(""));
                }
             }

            //Not in the specs but... makes sense
            if(!XMLChar.isValidName(qn.getLocalName()))
              {throw new XPathException(this, ErrorCodes.XPTY0004, "'" + qnitem.getStringValue() + "' is not a valid element name");}

            // add namespace declaration nodes
            final int nodeNr = builder.startElement(qn, attrs);
            if(namespaceDecls != null) {
                for(int i = 0; i < namespaceDecls.length; i++) {
View Full Code Here

TOP

Related Classes of org.exist.xquery.value.Item

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.