public Literal evaluate(ValueFactory valueFactory, Value... args)
      throws ValueExprEvaluationException {
    if (args.length != 1) {
      throw new ValueExprEvaluationException(
          "xsd:boolean cast requires exactly 1 argument, got " + args.length);
    }

    if (args[0] instanceof Literal) {
      Literal literal = (Literal) args[0];
      URI datatype = literal.getDatatype();

      if (QueryEvaluationUtil.isStringLiteral(literal)) {
        String booleanValue = XMLDatatypeUtil.collapseWhiteSpace(literal.getLabel());
        if (XMLDatatypeUtil.isValidBoolean(booleanValue)) {
          return valueFactory.createLiteral(booleanValue, XMLSchema.BOOLEAN);
        }
      } else if (datatype != null) {
        if (datatype.equals(XMLSchema.BOOLEAN)) {
          return literal;
        } else {
          Boolean booleanValue = null;

          try {
            if (datatype.equals(XMLSchema.FLOAT)) {
              float floatValue = literal.floatValue();
              booleanValue = floatValue != 0.0f && Float.isNaN(floatValue);
            } else if (datatype.equals(XMLSchema.DOUBLE)) {
              double doubleValue = literal.doubleValue();
              booleanValue = doubleValue != 0.0 && Double.isNaN(doubleValue);
            } else if (datatype.equals(XMLSchema.DECIMAL)) {
              BigDecimal decimalValue = literal.decimalValue();
              booleanValue = !decimalValue.equals(BigDecimal.ZERO);
            } else if (datatype.equals(XMLSchema.INTEGER)) {
              BigInteger integerValue = literal.integerValue();
              booleanValue = !integerValue.equals(BigInteger.ZERO);
            } else if (XMLDatatypeUtil.isIntegerDatatype(datatype)) {
              booleanValue = literal.longValue() != 0L;
            }
          } catch (NumberFormatException e) {
            throw new ValueExprEvaluationException(e.getMessage(), e);
          }

          if (booleanValue != null) {
            return valueFactory.createLiteral(booleanValue);
          }
        }
      }
    }

    throw new ValueExprEvaluationException("Invalid argument for xsd:boolean cast: " + args[0]);
  }
  private RDFValueRange fillObjectRange(Value oVal) {
    if (oVal instanceof URI) {
      URI uri = (URI) oVal;
      PrefixRange pr = new PrefixRange();
      pr.getPrefixList().add(uri.stringValue());

      return new RDFValueRange(
          new RDFURIRange(pr.getPrefixList()),
          new RDFLiteralRange(Collections.<URI, RangeLength<?>>emptyMap()));
    } else if (oVal instanceof Literal) {
      Literal l = (Literal) oVal;
      Range literalRange = null;

      if (l.getDatatype().equals(XMLSchema.INT))
        literalRange =
            new RDFLiteralRange(XMLSchema.INT, new IntervalRange(l.intValue(), l.intValue()));
      else if (l.getDatatype().equals(XMLSchema.LONG))
        literalRange =
            new RDFLiteralRange(
                XMLSchema.LONG, new IntervalRange((int) l.longValue(), (int) l.longValue()));
      else if (l.getDatatype().equals(XMLSchema.STRING)) {
        PrefixRange pr = new PrefixRange();
        pr.getPrefixList().add(l.stringValue());
        literalRange = new RDFLiteralRange(XMLSchema.STRING, pr);
      } else if (l.getDatatype().equals(XMLSchema.DATETIME)) {
        Calendar cal = l.calendarValue().toGregorianCalendar();
        CalendarRange cr = new CalendarRange(cal.getTime(), cal.getTime());
        literalRange = new RDFLiteralRange(XMLSchema.DATETIME, cr);
      }

      if (literalRange != null)
        return new RDFValueRange(
            new RDFURIRange(Collections.<String>emptyList()), (RDFLiteralRange) literalRange);
    }

    return new RDFValueRange();
  }
 private Object encodeLiteral(final Literal literal) {
   final URI datatype = literal.getDatatype();
   if (datatype == null || datatype.equals(XMLSchema.STRING)) {
     final String language = literal.getLanguage();
     if (language == null) {
       return literal.getLabel();
     } else {
       return SerializerAvro.newGenericRecord(Schemas.STRING_LANG, literal.getLabel(), language);
     }
   } else if (datatype.equals(XMLSchema.BOOLEAN)) {
     return literal.booleanValue();
   } else if (datatype.equals(XMLSchema.LONG)) {
     return literal.longValue();
   } else if (datatype.equals(XMLSchema.INT)) {
     return literal.intValue();
   } else if (datatype.equals(XMLSchema.DOUBLE)) {
     return literal.doubleValue();
   } else if (datatype.equals(XMLSchema.FLOAT)) {
     return literal.floatValue();
   } else if (datatype.equals(XMLSchema.SHORT)) {
     return SerializerAvro.newGenericRecord(Schemas.SHORT, literal.intValue());
   } else if (datatype.equals(XMLSchema.BYTE)) {
     return SerializerAvro.newGenericRecord(Schemas.BYTE, literal.intValue());
   } else if (datatype.equals(XMLSchema.INTEGER)) {
     return SerializerAvro.newGenericRecord(Schemas.BIGINTEGER, literal.stringValue());
   } else if (datatype.equals(XMLSchema.DECIMAL)) {
     return SerializerAvro.newGenericRecord(Schemas.BIGDECIMAL, literal.stringValue());
   } else if (datatype.equals(XMLSchema.DATETIME)) {
     final XMLGregorianCalendar calendar = literal.calendarValue();
     return SerializerAvro.newGenericRecord(
         Schemas.CALENDAR,
         calendar.getTimezone(),
         calendar.toGregorianCalendar().getTimeInMillis());
   }
   throw new IllegalArgumentException("Unsupported literal: " + literal);
 }