@Override
    public NodeValue exec(NodeValue v) {
      // http://www.w3.org/TR/xpath-functions/#casting
      String s = null;
      Node n = v.asNode();

      if (n.isBlank()) throw new ExprEvalException("CastXSD: Can't cast blank nodes: " + v);

      if (n.isURI()) {
        if (castType.equals(XSDDatatype.XSDstring)) s = n.getURI();
        else
          throw new ExprEvalException(
              "CastXSD: Can't cast node: " + v + " to " + castType.getURI());
      } else if (n.isLiteral())
        // What if there is a lang tag?
        s = n.getLiteralLexicalForm();
      else
        throw new ExprEvalException(
            "CastXSD: Can't cast node: " + v + "(not a literal, not URI to string)");

      if (s == null && v.isString()) s = v.getString();

      if (s == null)
        throw new ExprEvalException("CastXSD: Can't cast: " + v + "(has no string appearance)");

      //        // Special case - non-normalised xsd:booleans use 0 and 1.
      //        if ( v.isBoolean() )
      //        {
      //            if ( s.equals("0") ) s = "false" ;
      //            if ( s.equals("1") ) s = "true" ;
      //        }

      NodeValue r = cast(s, v, castType);
      return r;
    }
Example #2
0
    protected NodeValue cast(String s, NodeValue nv, XSDDatatype castType2) {
      if (whitespaceSurroundAllowed)
        // Maybe more convenient, but is not strictly correct.
        s = s.trim();
      else {
        // Correct mode.  No white space allowed around values types numeric, boolean, dateTime, ...
        // See also "JenaParameters.enableWhitespaceCheckingOfTypedLiterals" which defaults to false
        // (accept surrounding whitespace)
        // This CastXSD - not need to check it is an XSD datatype.
        // if ( castType.getURI().startsWith(XSDDatatype.XSD) &&

        if (castType instanceof XSDBaseNumericType
            || castType.equals(XSDDatatype.XSDfloat)
            || castType.equals(XSDDatatype.XSDdouble)
            || castType.equals(XSDDatatype.XSDboolean)
            || castType instanceof XSDAbstractDateTimeType) // Includes durations, and Gregorian
        {
          if (s.startsWith(" ") || s.endsWith(" "))
            throw new ExprEvalException(
                "CastXSD: Not a valid literal form (has whitespace): '" + s + "'");
        }
      }

      // Plain cast.
      try {
        if (!castType.isValid(s))
          throw new ExprEvalException("CastXSD: Not a valid literal form: '" + s + "'");
        // Unfortunately, validity testing happens in NodeValue.makeNode as well.
        // but better error messages this way.
        return NodeValue.makeNode(s, castType);
      } catch (RuntimeException ex) {
        throw new ExprEvalException("CastXSD: Not a strictly valid literal form: '" + s + "'");
      }
    }
  public void nullSafeSet(PreparedStatement statement, Object value, int index)
      throws HibernateException, SQLException {

    if (value == null) {
      statement.setNull(index, Types.VARCHAR);
    } else {
      XSDDatatype valueXsd = (XSDDatatype) value;
      String xsdUri = valueXsd.getURI();
      xsdUri = xsdUri.replaceAll("http://www.w3.org/2001/XMLSchema#", "");
      statement.setString(index, xsdUri);
    }
  }
 protected NodeValue cast(String s, NodeValue nv, XSDDatatype castType2) {
   // Plain cast.
   if (!castType.isValid(s))
     throw new ExprEvalException("CastXSD: Not a valid literal form: " + s);
   // Unfortunately, validity testing happens in NodeValue.makeNode as well.
   return NodeValue.makeNode(s, castType);
 }
Example #5
0
 /** Static initializer. Adds builtin datatypes to the mapper. */
 static {
   theTypeMap = new TypeMapper();
   theTypeMap.registerDatatype(XMLLiteralType.theXMLLiteralType);
   XSDDatatype.loadXSDSimpleTypes(theTypeMap);
 }