Package org.exist.xquery.value

Examples of org.exist.xquery.value.ValueSequence


            }
        };
    }
   
    private org.exist.xquery.value.Sequence convertToExistSequence(final XQueryContext xqueryContext, final TypedArgumentValue argument, final int fnParameterType) throws RestXqServiceException, XPathException  {
        final org.exist.xquery.value.Sequence sequence = new ValueSequence();
       
        for(final TypedValue value : (Sequence<Object>)argument.getTypedValue()) {
           
            final org.exquery.xquery.Type destinationType = TypeAdapter.toExQueryType(fnParameterType);
           
            final Class destinationClass;
           
            switch(fnParameterType) {
               
                case Type.ITEM:
                    destinationClass = Item.class;
                    break;
                   
                case Type.DOCUMENT:
                    destinationClass = DocumentImpl.class//TODO test this
                    break;
               
                case Type.STRING:
                    destinationClass = StringValue.class;
                    break;
                   
                case Type.INT:
                case Type.INTEGER:
                    destinationClass = IntegerValue.class;
                    break;
                   
                case Type.FLOAT:
                    destinationClass = FloatValue.class;
                    break;
                   
                case Type.DOUBLE:
                    destinationClass = DoubleValue.class;
                    break;   
               
                case Type.DECIMAL:
                    destinationClass = DecimalValue.class;
                    break;
               
                case Type.DATE:
                    destinationClass = DateValue.class;
                    break;
                   
                case Type.DATE_TIME:
                    destinationClass = DateTimeValue.class;
                    break;
                   
                case Type.TIME:
                    destinationClass = TimeValue.class;
                    break;
                   
                case Type.QNAME:
                    destinationClass = QNameValue.class;
                    break;
               
                case Type.ANY_URI:
                    destinationClass = AnyURIValue.class;
                    break;
               
                case Type.BOOLEAN:
                    destinationClass = BooleanValue.class;
                    break;
               
                default:
                    destinationClass = Item.class;
            }
           
            final TypedValue<? extends Item> val = convertToType(xqueryContext, argument.getArgumentName(), value, destinationType, destinationClass);
           
            sequence.add(val.getValue());
        }
       
        return sequence;
    }
View Full Code Here


    if (args[0].isEmpty()) {
      return Sequence.EMPTY_SEQUENCE;
    }
   
    // iterate through the argument sequence and echo each item
    ValueSequence result = new ValueSequence();
    for (SequenceIterator i = args[0].iterate(); i.hasNext();) {
      String str = i.nextItem().getStringValue();
      result.add(new StringValue("echo: " + str));
    }
    return result;
  }
View Full Code Here

    super(context, signature);
   }

  public Sequence eval(Sequence[] args, Sequence contextSequence)
    throws XPathException {
        ValueSequence result = new ValueSequence();
        try {
            ExistRepository repo = getContext().getRepository();
            Repository parent_repo = repo.getParentRepo();
            for ( Packages pkg :  parent_repo.listPackages() ) {
                String name = pkg.name();
                result.add(new StringValue(name));
            }
        } catch (Exception ex ) {
            throw new XPathException("Problem listing packages in expath repository ", ex);
        }
        return result;
View Full Code Here

   *      org.exist.xquery.value.Sequence)
   */
  public Sequence eval(Sequence[] args, Sequence contextSequence)
      throws XPathException {
   
    ValueSequence result = new ValueSequence();
    // boolean isDatabasePath = false;
    boolean isSaveToDataBase = false;

    if (args[0].isEmpty()) {
      return Sequence.EMPTY_SEQUENCE;
    }

    AnyURIValue picturePath = (AnyURIValue) args[0].itemAt(0);
    if (picturePath.getStringValue().startsWith("xmldb:exist://")) {
      picturePath = new AnyURIValue(picturePath.getStringValue()
          .substring(14));
    }

    AnyURIValue thumbPath = null;
    if (args[1].isEmpty()) {
      thumbPath = new AnyURIValue(picturePath.toXmldbURI().append(
          THUMBPATH));
      isSaveToDataBase = true;
    } else {
      thumbPath = (AnyURIValue) args[1].itemAt(0);
      if (thumbPath.getStringValue().startsWith("file:")) {
        isSaveToDataBase = false;
        thumbPath = new AnyURIValue(thumbPath.getStringValue().substring(5));
      } else {
        isSaveToDataBase = true;
        try {
          XmldbURI thumbsURI = XmldbURI.xmldbUriFor(thumbPath.getStringValue());
          if (!thumbsURI.isAbsolute())
            thumbsURI = picturePath.toXmldbURI().append(thumbPath.toString());
          thumbPath = new AnyURIValue(thumbsURI.toString());
        } catch (URISyntaxException e) {
          throw new XPathException(this, e.getMessage());
        }
      }
    }

    // result.add(new StringValue(picturePath.getStringValue()));
    // result.add(new StringValue(thumbPath.getStringValue() + " isDB?= "
    // + isSaveToDataBase));

    int maxThumbHeight = MAXTHUMBHEIGHT;
    int maxThumbWidth = MAXTHUMBWIDTH;

    if (!args[2].isEmpty()) {
      maxThumbHeight = ((IntegerValue) args[2].itemAt(0)).getInt();
      if (args[2].hasMany())
        maxThumbWidth = ((IntegerValue) args[2].itemAt(1)).getInt();
    }

    String prefix = THUMBPREFIX;

    if (!args[3].isEmpty()) {
      prefix = args[3].itemAt(0).getStringValue();

    }

    // result.add(new StringValue("maxThumbHeight = " + maxThumbHeight
    // + ", maxThumbWidth = " + maxThumbWidth));

    DBBroker dbbroker = context.getBroker();

    BrokerPool pool = null;
    try {
      pool = BrokerPool.getInstance();
    } catch (Exception e) {
      result.add(new StringValue(e.getMessage()));
      return result;
    }
    TransactionManager transact = pool.getTransactionManager();

    // Start transaction
    Txn transaction = transact.beginTransaction();

    Collection thumbCollection = null;
    File thumbDir = null;
    if (isSaveToDataBase) {
      try {
        thumbCollection = dbbroker.getOrCreateCollection(transaction,
            thumbPath.toXmldbURI());
        dbbroker.saveCollection(transaction, thumbCollection);
      } catch (Exception e) {
        throw new XPathException(this, e.getMessage());
      }
    } else {
      thumbDir = new File(thumbPath.toString());
      if (!thumbDir.isDirectory())
        try {
          thumbDir.mkdirs();
        } catch (Exception e) {
          throw new XPathException(this, e.getMessage());
        }

    }

                Collection allPictures = null;
                Collection existingThumbsCol = null;
                File[] existingThumbsArray = null;
                try {
                    allPictures = dbbroker.getCollection(picturePath.toXmldbURI());

                    if (allPictures == null) {
                            return Sequence.EMPTY_SEQUENCE;
                    }

                    if (isSaveToDataBase) {
                            existingThumbsCol = dbbroker.getCollection(thumbPath.toXmldbURI());
                    } else {
                            existingThumbsArray = thumbDir.listFiles(new FilenameFilter() {
                                    public boolean accept(File dir, String name) {
                                            return (name.endsWith(".jpeg") || name.endsWith(".jpg"));
                                    }
                            });
                    }
                } catch (PermissionDeniedException pde) {
                    throw new XPathException(pde.getMessage(), pde);
                }

    DocumentImpl docImage = null;
    BinaryDocument binImage = null;
    @SuppressWarnings("unused")
    BinaryDocument doc = null;
    BufferedImage bImage = null;
    @SuppressWarnings("unused")
    byte[] imgData = null;
    Image image = null;
    ByteArrayOutputStream os = null;

                try {
    Iterator<DocumentImpl> i = allPictures.iterator(dbbroker);

    while (i.hasNext()) {
      docImage = (DocumentImpl) i.next();
      // is not already existing??
      if (!((fileExist(context.getBroker(), existingThumbsCol, docImage, prefix)) || (fileExist(
          existingThumbsArray, docImage, prefix)))) {
        if (docImage.getResourceType() == DocumentImpl.BINARY_FILE)
          // TODO maybe extends for gifs too.
          if (docImage.getMetadata().getMimeType().startsWith(
              "image/jpeg")) {

            binImage = (BinaryDocument) docImage;

            // get a byte array representing the image

            try {
                                                   InputStream is = dbbroker.getBinaryResource(binImage);
              image = ImageIO.read(is);
            } catch (IOException ioe) {
              throw new XPathException(this,ioe.getMessage());
            }

            try {
              bImage = ImageModule.createThumb(image, maxThumbHeight,
                  maxThumbWidth);
            } catch (Exception e) {
              throw new XPathException(this, e.getMessage());
            }

            if (isSaveToDataBase) {
              os = new ByteArrayOutputStream();
              try {
                ImageIO.write(bImage, "jpg", os);
              } catch (Exception e) {
                throw new XPathException(this, e.getMessage());
              }
              try {
                doc = thumbCollection.addBinaryResource(
                    transaction, dbbroker,
                    XmldbURI.create(prefix
                        + docImage.getFileURI()), os
                        .toByteArray(), "image/jpeg");
              } catch (Exception e) {
                throw new XPathException(this, e.getMessage());
              }
              // result.add(new
              // StringValue(""+docImage.getFileURI()+"|"+thumbCollection.getURI()+THUMBPREFIX
              // + docImage.getFileURI()));
            } else {
              try {
                ImageIO
                    .write(
                        bImage,
                        "jpg",
                        new File(thumbPath.toString()
                            + "/" + prefix
                            + docImage.getFileURI()));
              } catch (Exception e) {
                throw new XPathException(this, e.getMessage());
              }
              // result.add(new StringValue(
              // thumbPath.toString() + "/"
              // + THUMBPREFIX
              // + docImage.getFileURI()));
            }
          }
      } else {

        // result.add(new StringValue(""+docImage.getURI()+"|"
        // + ((existingThumbsCol != null) ? ""
        // + existingThumbsCol.getURI() : thumbDir
        // .toString()) + "/" + prefix
        // + docImage.getFileURI()));

        result.add(new StringValue(docImage.getFileURI().toString()));
      }
    }
                } catch(PermissionDeniedException pde) {
                    throw new XPathException(this, pde.getMessage(), pde);
                }
View Full Code Here

        }
        Sequence result = null;
        if (steps.size() == 0) {
            result = Sequence.EMPTY_SEQUENCE;
        } else {
            result = new ValueSequence();
            Sequence currentContext;
            if (contextItem != null)
                currentContext = contextItem.toSequence();
            else
                currentContext = contextSequence;
View Full Code Here

//      }
////    }
//  }
 
  public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    Sequence result = new ValueSequence();
     
      Sequence selected = select.eval(contextSequence, contextItem);
     
      if (sort != null)
        selected = sort.eval(selected, null);
     
//      for (Item each : selected) {
        for (SequenceIterator iterInner = selected.iterate(); iterInner.hasNext();) {
            Item each = iterInner.nextItem();  

            //Sequence seq = childNodes.eval(contextSequence, each);
        Sequence answer = super.eval(contextSequence, each);
        result.addAll(answer);
      }
     
      return result;
  }
View Full Code Here

      }
    }
 
  public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {

    Sequence result = new ValueSequence();
     
        context.pushDocumentContext();

        LocalVariable mark = context.markLocalVariables(false);
        try {
      Sequence selected;
        if (select == null) {
          selected = anyChild.eval(contextSequence, contextItem);
        } else {
          selected = select.eval(contextSequence, contextItem);
        }
       
        for (Expression expr : steps) {
          if (expr instanceof Sort) {
          Sort sort = (Sort) expr;
          selected = sort.eval(selected, null);
          } else if (expr instanceof TextConstructor) {
            //ignore text elements
          } else if (expr instanceof WithParam) {
            WithParam param = (WithParam)expr;
            context.declareVariable(param.getName(), param);
        } else {
          throw new XPathException("not suported "+expr);//TODO: error?
        }
        }
         
        XSLStylesheet xslt = getXSLContext().getXSLStylesheet();
 
      int pos = 0;
  //      for (Item item : selected) {
          for (SequenceIterator iterInner = selected.iterate(); iterInner.hasNext();) {
              Item item = iterInner.nextItem();  
         
            context.setContextSequencePosition(pos, selected);
          Sequence res = xslt.templates(selected, item);
          if (res == null) {
                if (item instanceof Text) {
                      MemTreeBuilder builder = context.getDocumentBuilder();
                  builder.characters(item.getStringValue());
                  result.add(item);
                } else if (item instanceof Node || item instanceof NodeValue) {
                  res = eval(selected, item); //item.toSequence();//
                } else
                  throw new XPathException("not supported item type");
          }
          result.addAll(res);
          pos++;
        }
       
        return result;
        } finally {
View Full Code Here

                        {resultSequence = new OrderedValueSequence(orderSpecs, in.getItemCount());}
                }
                if (groupedSequence==null){
                    if (returnExpr instanceof BindingExpression) {
                      if (resultSequence == null) {
                          resultSequence = new ValueSequence();
                          ((ValueSequence)resultSequence).keepUnOrdered(unordered);
                      }
                      ((BindingExpression)returnExpr).eval(contextSequence, null, resultSequence,null);
                    } else {
                        in = returnExpr.eval(contextSequence);
                        if (resultSequence == null)
                            {resultSequence = in;}
                        else
                            {resultSequence.addAll(in);}
                    }
                }
                else{
                    /* bv : special processing for groupby :
                    if returnExpr is a Binding expression, pass the groupedSequence.
                    Else, add item to groupedSequence and don't evaluate here !
                    */
                    if (returnExpr instanceof BindingExpression) {
                        if (resultSequence == null) {
                            resultSequence = new ValueSequence();
                            ((ValueSequence)resultSequence).keepUnOrdered(unordered);
                        }
                        ((BindingExpression)returnExpr).eval(contextSequence, null, resultSequence, groupedSequence);
                    } else{
                      final Sequence toGroupSequence = context.resolveVariable(groupedSequence.getToGroupVarName()).getValue();
View Full Code Here

        } catch (final Exception ex) {
            dependantModule = location;
        }
       
        if(e == null) {
            return new XPathException(ErrorCodes.XQST0059, message, new ValueSequence(new StringValue(moduleLocation), new StringValue(dependantModule)));
        } else {
            return new XPathException(ErrorCodes.XQST0059, message, new ValueSequence(new StringValue(moduleLocation), new StringValue(dependantModule)), e);
        }
    }
View Full Code Here

            nextNode = document.next[nextNode];
        }
    }

    public NodeImpl getFirstChild(NodeTest test) throws XPathException {
      final ValueSequence seq = new ValueSequence();
      selectChildren(test, seq);
      return seq.isEmpty() ? null : seq.get(0);
    }
View Full Code Here

TOP

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

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.