/*
   * See http://www.w3.org/TR/rdf-sparql-query paragraph 11.4.7
   *
   * "(DATATYPE) Returns the datatype IRI of typedLit; returns xsd:string if the parameter is a simple literal."
   *
   * @see http://www.w3.org/TR/rdf-sparql-query
   */
  private void convertDataType(E_Datatype expr) {
    logger.debug("convertDataType " + expr.toString());

    expr.getArg().visit(this);

    Expression arg = expression.pop();

    if (arg instanceof AttributeExprEx) {
      AttributeExprEx variable = (AttributeExprEx) arg;
      NodeMaker nm = variable.getNodeMaker();
      DetermineNodeType filter = new DetermineNodeType();
      nm.describeSelf(filter);
      if (!filter.isLimittedToLiterals()) {
        // type error, return false?
        logger.warn("type error: " + variable + " is not a literal, returning FALSE");
        expression.push(Expression.FALSE);
        return;
      }
      RDFDatatype datatype = filter.getDatatype();
      logger.debug("datatype " + datatype);

      Node node =
          Node.createURI((datatype != null) ? datatype.getURI() : XSDDatatype.XSDstring.getURI());

      ConstantEx constantEx = new ConstantEx(NodeValue.makeNode(node).asString(), node);
      logger.debug("pushing " + constantEx);
      expression.push(constantEx);
    } else if (arg instanceof ConstantEx) {
      ConstantEx constant = (ConstantEx) arg;
      Node node = constant.getNode();
      if (!node.isLiteral()) {
        // type error, return false?
        logger.warn("type error: " + node + " is not a literal, returning FALSE");
        expression.push(Expression.FALSE);
        return;
      }
      RDFDatatype datatype = node.getLiteralDatatype();
      logger.debug("datatype " + datatype);
      node =
          Node.createURI((datatype != null) ? datatype.getURI() : XSDDatatype.XSDstring.getURI());
      ConstantEx constantEx = new ConstantEx(NodeValue.makeNode(node).asString(), node);
      logger.debug("pushing " + constantEx);
      expression.push(constantEx);
    } else {
      conversionFailed(expr);
    }
  }
  /*
   * Convert an individual Hudson parameter definition to an OSLC Property.
   */
  private Property toProperty(StaplerRequest request, ParameterDefinition def)
      throws URISyntaxException {
    Property prop = new Property();
    prop.setName(def.getName());
    prop.setDescription(def.getDescription());

    if (def instanceof BooleanParameterDefinition) {
      prop.setValueType(new URI(XSDDatatype.XSDboolean.getURI()));
    } else if (def instanceof StringParameterDefinition
        || def instanceof PasswordParameterDefinition) {
      prop.setValueType(new URI(XSDDatatype.XSDstring.getURI()));
    } else if (def instanceof ChoiceParameterDefinition) {
      prop.setValueType(new URI(XSDDatatype.XSDstring.getURI()));
      ChoiceParameterDefinition choices = (ChoiceParameterDefinition) def;
      prop.setAllowedValuesCollection(choices.getChoices());
    }
    // TODO: Other types?

    ParameterValue defaultValue = def.getDefaultParameterValue();
    if (defaultValue == null) {
      prop.setOccurs(Occurs.ExactlyOne);
    } else {
      prop.setOccurs(Occurs.ZeroOrOne);
      if (!defaultValue.isSensitive()) {
        if (defaultValue instanceof BooleanParameterValue) {
          BooleanParameterValue bool = (BooleanParameterValue) defaultValue;
          prop.setDefaultValue(bool.value);
        } else if (defaultValue instanceof StringParameterValue) {
          StringParameterValue str = (StringParameterValue) defaultValue;
          prop.setDefaultValue(str.value);
        }
        // TODO: Other types?
      }
    }

    return prop;
  }