Exemple #1
0
  /** Evaluate in a context where a string is wanted */
  public CharSequence evaluateAsString(XPathContext context) throws XPathException {

    int numArgs = argument.length;
    Controller ctrl = context.getController();

    DecimalSymbols dfs = decimalFormatSymbols;

    AtomicValue av0 = (AtomicValue) argument[0].evaluateItem(context);
    if (av0 == null) {
      av0 = DoubleValue.NaN;
    }
    NumericValue number = (NumericValue) av0;

    if (dfs == null) {
      // the decimal-format name was not resolved statically
      if (requireFixup) {
        // we registered for a fixup, but none came
        dynamicError("Unknown decimal format name", "XTDE1280", context);
        return null;
      }
      DecimalFormatManager dfm = ctrl.getExecutable().getDecimalFormatManager();
      if (numArgs == 2) {
        dfs = dfm.getDefaultDecimalFormat();
      } else {
        // the decimal-format name was given as a run-time expression
        String lexicalName = argument[2].evaluateItem(context).getStringValue();
        StructuredQName qName = null;
        try {
          qName =
              StructuredQName.fromLexicalQName(
                  lexicalName, false, context.getConfiguration().getNameChecker(), nsContext);
        } catch (XPathException e) {
          dynamicError("Invalid decimal format name. " + e.getMessage(), "XTDE1280", context);
        }

        dfs = dfm.getNamedDecimalFormat(qName);
        if (dfs == null) {
          dynamicError(
              "format-number function: decimal-format '" + lexicalName + "' is not defined",
              "XTDE1280",
              context);
          return null;
        }
      }
    }
    SubPicture[] pics = subPictures;
    if (pics == null) {
      String format = argument[1].evaluateItem(context).getStringValue();
      pics = getSubPictures(format, dfs);
    }
    return formatNumber(number, pics, dfs).toString();
  }
Exemple #2
0
 public TailCall processLeavingTail(XPathContext context) throws XPathException {
   // TODO: we're always constructing the document in memory. Sometimes we could push it out
   // directly.
   Item item = evaluateItem(context);
   if (item != null) {
     SequenceReceiver out = context.getReceiver();
     out.append(item, locationId, NodeInfo.ALL_NAMESPACES);
   }
   return null;
 }
  @Override
  public SequenceIterator iterate(XPathContext xpathContext) throws XPathException {

    final XPathContextMajor newXPathContext = xpathContext.newCleanContext();

    final Expression avtExpression;
    {
      // Prepare expression
      final PooledXPathExpression xpathExpression =
          prepareExpression(xpathContext, argument[0], true);
      avtExpression = xpathExpression.prepareExpression(newXPathContext);
    }

    return avtExpression.iterate(newXPathContext);
  }
  public Item evaluateItem(XPathContext context) throws XPathException {
    final String arg = argument[0].evaluateAsString(context);
    try {
      final DateTimeValue value = new DateTimeValue(arg);

      // "the return value is equal to the number of seconds difference between the specified
      // dateTime
      // (normalized to UTC) and 1970-01-01T00:00:00Z"
      return new IntegerValue(
          value
              .normalize(context.getConfiguration())
              .toJulianInstant()
              .subtract(BASELINE)
              .longValue());

    } catch (XPathException e) {
      return NAN;
    }
  }
  public Item evaluateItem(XPathContext xpathContext) throws XPathException {

    // Get parameters
    final Item item = argument[0].evaluateItem(xpathContext);
    final String excludeResultPrefixes =
        argument.length >= 2 ? argument[1].evaluateAsString(xpathContext).toString() : null;
    final boolean readonly =
        argument.length >= 3
            && ExpressionTool.effectiveBooleanValue(argument[2].iterate(xpathContext));

    // Make sure it is a NodeInfo
    if (!(item instanceof NodeInfo)) {
      return null;
    }

    // Get Element
    final Element rootElement;
    if (item instanceof NodeWrapper) {
      final Object node = ((NodeWrapper) item).getUnderlyingNode();
      rootElement = (Element) node;
    } else {
      final NodeInfo nodeInfo = (NodeInfo) item;
      final Document document = TransformerUtils.tinyTreeToDom4j2(nodeInfo);
      rootElement = document.getRootElement();
    }

    // Convert to Document or DocumentInfo
    final Object result =
        extractDocument(
            xpathContext.getConfiguration(), rootElement, excludeResultPrefixes, readonly);

    // Return DocumentInfo
    if (result instanceof Document)
      return new DocumentWrapper(
          (Document) result,
          null,
          getContainingDocument(xpathContext).getStaticState().getXPathConfiguration());
    else return (DocumentInfo) result;
  }
Exemple #6
0
  /** Evaluate as an expression. */
  public Item evaluateItem(XPathContext context) throws XPathException {
    if (isLazyConstruction()
        && (context.getConfiguration().areAllNodesUntyped()
            || (validation == Validation.PRESERVE && getSchemaType() == null))) {
      return new UnconstructedDocument(this, context);
    } else {
      Controller controller = context.getController();
      DocumentInfo root;
      if (textOnly) {
        CharSequence textValue;
        if (constantText != null) {
          textValue = constantText;
        } else {
          FastStringBuffer sb = new FastStringBuffer(100);
          SequenceIterator iter = content.iterate(context);
          while (true) {
            Item item = iter.next();
            if (item == null) break;
            sb.append(item.getStringValueCS());
          }
          textValue = sb.condense();
        }
        root = new TextFragmentValue(textValue, getBaseURI());
        ((TextFragmentValue) root).setConfiguration(controller.getConfiguration());
      } else {
        try {
          XPathContext c2 = context.newMinorContext();
          c2.setOrigin(this);

          Builder builder = controller.makeBuilder();
          // builder.setSizeParameters(treeSizeParameters);
          builder.setLineNumbering(controller.getConfiguration().isLineNumbering());

          // receiver.setSystemId(getBaseURI());
          builder.setBaseURI(getBaseURI());
          builder.setTiming(false);

          PipelineConfiguration pipe = controller.makePipelineConfiguration();
          pipe.setHostLanguage(getHostLanguage());
          // pipe.setBaseURI(baseURI);
          builder.setPipelineConfiguration(pipe);

          c2.changeOutputDestination(
              null, builder, false, getHostLanguage(), validation, getSchemaType());
          Receiver out = c2.getReceiver();
          out.open();
          out.startDocument(0);

          content.process(c2);

          out.endDocument();
          out.close();

          root = (DocumentInfo) builder.getCurrentRoot();
        } catch (XPathException e) {
          e.maybeSetLocation(this);
          e.maybeSetContext(context);
          throw e;
        }
      }
      return root;
    }
  }