Exemplo n.º 1
0
/*
 * rasqal_query_results_write_json1:
 * @iostr: #raptor_iostream to write the query to
 * @results: #rasqal_query_results query results format
 * @base_uri: #raptor_uri base URI of the output format
 *
 * Write a JSON version of the query results format to an
 * iostream in a format - INTERNAL.
 * 
 * If the writing succeeds, the query results will be exhausted.
 * 
 * Return value: non-0 on failure
 **/
static int
rasqal_query_results_write_json1(rasqal_query_results_formatter* formatter,
                                 raptor_iostream *iostr,
                                 rasqal_query_results* results,
                                 raptor_uri *base_uri)
{
  rasqal_world* world = rasqal_query_results_get_world(results);
  rasqal_query* query = rasqal_query_results_get_query(results);
  int i;
  int row_comma;
  int column_comma = 0;
  rasqal_query_results_type type;

  type = rasqal_query_results_get_type(results);

  if(type != RASQAL_QUERY_RESULTS_BINDINGS &&
     type != RASQAL_QUERY_RESULTS_BOOLEAN) {
    rasqal_log_error_simple(query->world, RAPTOR_LOG_LEVEL_ERROR,
                            &query->locator,
                            "Cannot write JSON for %s query result format",
                            rasqal_query_results_type_label(type));
    return 1;
  }

  raptor_iostream_counted_string_write("{\n", 2, iostr);
  
  /* Header */
  raptor_iostream_counted_string_write("  \"head\": {\n", 12, iostr);
  
  if(rasqal_query_results_is_bindings(results)) {
    raptor_iostream_counted_string_write("    \"vars\": [ ", 14, iostr);
    for(i = 0; 1; i++) {
      const unsigned char *name;
      
      name = rasqal_query_results_get_binding_name(results, i);
      if(!name)
        break;
      
      /*     'x', */
      if(i > 0)
        raptor_iostream_counted_string_write(", ", 2, iostr);
      raptor_iostream_write_byte('\"', iostr);
      raptor_iostream_string_write(name, iostr);
      raptor_iostream_write_byte('\"', iostr);
    }
    raptor_iostream_counted_string_write(" ]\n", 3, iostr);
  }

  /* FIXME - could add link inside 'head': */
    
  /*   End Header */
  raptor_iostream_counted_string_write("  },\n", 5, iostr);


  /* Boolean Results */
  if(rasqal_query_results_is_boolean(results)) {
    raptor_iostream_counted_string_write("  ", 2, iostr);
    rasqal_iostream_write_json_boolean(iostr, "boolean", 
                                       rasqal_query_results_get_boolean(results));
    goto results3done;
  }

  /* Variable Binding Results */
  raptor_iostream_counted_string_write("  \"results\": {\n", 15, iostr);

  if(query) {
    raptor_iostream_counted_string_write("    ", 4, iostr);
    rasqal_iostream_write_json_boolean(iostr, "ordered", 
                                       (rasqal_query_get_order_condition(query, 0) != NULL));
    raptor_iostream_counted_string_write(",\n", 2, iostr);

    raptor_iostream_counted_string_write("    ", 4, iostr);
    rasqal_iostream_write_json_boolean(iostr, "distinct", 
                                       rasqal_query_get_distinct(query));
    raptor_iostream_counted_string_write(",\n", 2, iostr);
  }
  
  raptor_iostream_counted_string_write("    \"bindings\" : [\n", 19, iostr);

  row_comma = 0;
  while(!rasqal_query_results_finished(results)) {
    if(row_comma)
      raptor_iostream_counted_string_write(",\n", 2, iostr);

    /* Result row */
    raptor_iostream_counted_string_write("      {\n", 8, iostr);

    column_comma = 0;
    for(i = 0; i<rasqal_query_results_get_bindings_count(results); i++) {
      const unsigned char *name = rasqal_query_results_get_binding_name(results, i);
      rasqal_literal *l = rasqal_query_results_get_binding_value(results, i);

      if(column_comma)
        raptor_iostream_counted_string_write(",\n", 2, iostr);

      /*       <binding> */
      raptor_iostream_counted_string_write("        \"", 9, iostr);
      raptor_iostream_string_write(name, iostr);
      raptor_iostream_counted_string_write("\" : { ", 6, iostr);

      if(!l) {
        raptor_iostream_string_write("\"type\": \"unbound\", \"value\": null", iostr);
      } else {
        const unsigned char* str;
        size_t len;

        switch(l->type) {
          case RASQAL_LITERAL_URI:
            raptor_iostream_string_write("\"type\": \"uri\", \"value\": \"", iostr);
            str = RASQAL_GOOD_CAST(const unsigned char*, raptor_uri_as_counted_string(l->value.uri, &len));
            raptor_string_ntriples_write(str, len, '"', iostr);
            raptor_iostream_write_byte('"', iostr);
            break;

          case RASQAL_LITERAL_BLANK:
            raptor_iostream_string_write("\"type\": \"bnode\", \"value\": \"", iostr);
            raptor_string_ntriples_write(l->string, l->string_len, '"', iostr);
            raptor_iostream_write_byte('"', iostr);
            break;

          case RASQAL_LITERAL_STRING:
            raptor_iostream_string_write("\"type\": \"literal\", \"value\": \"", iostr);
            raptor_string_ntriples_write(l->string, l->string_len, '"', iostr);
            raptor_iostream_write_byte('"', iostr);

            if(l->language) {
              raptor_iostream_string_write(",\n      \"xml:lang\" : \"", iostr);
              raptor_iostream_string_write(RASQAL_GOOD_CAST(const unsigned char*, l->language), iostr);
              raptor_iostream_write_byte('"', iostr);
            }

            if(l->datatype) {
              raptor_iostream_string_write(",\n      \"datatype\" : \"", iostr);
              str = RASQAL_GOOD_CAST(const unsigned char*, raptor_uri_as_counted_string(l->datatype, &len));
              raptor_string_ntriples_write(str, len, '"', iostr);
              raptor_iostream_write_byte('"', iostr);
            }

            break;

          case RASQAL_LITERAL_PATTERN:
          case RASQAL_LITERAL_QNAME:
          case RASQAL_LITERAL_INTEGER:
          case RASQAL_LITERAL_XSD_STRING:
          case RASQAL_LITERAL_BOOLEAN:
          case RASQAL_LITERAL_DOUBLE:
          case RASQAL_LITERAL_FLOAT:
          case RASQAL_LITERAL_VARIABLE:
          case RASQAL_LITERAL_DECIMAL:
          case RASQAL_LITERAL_DATE:
          case RASQAL_LITERAL_DATETIME:
          case RASQAL_LITERAL_UDT:
          case RASQAL_LITERAL_INTEGER_SUBTYPE:

          case RASQAL_LITERAL_UNKNOWN:
          default:
            rasqal_log_error_simple(world, RAPTOR_LOG_LEVEL_ERROR, NULL,
                                    "Cannot turn literal type %u into XML",
                                    l->type);
        }
      }

      /* End Binding */
      raptor_iostream_counted_string_write(" }", 2, iostr);
      column_comma = 1;
    }
Exemplo n.º 2
0
/*
 * rasqal_query_results_write_sv:
 * @iostr: #raptor_iostream to write the query to
 * @results: #rasqal_query_results query results format
 * @base_uri: #raptor_uri base URI of the output format
 * @label: name of this format for errors
 * @sep: column sep character
 *
 * INTERNAL - Write a @sep-separated values version of the query results format to an iostream.
 *
 * If the writing succeeds, the query results will be exhausted.
 *
 * Return value: non-0 on failure
 **/
static int
rasqal_query_results_write_sv(raptor_iostream *iostr,
                              rasqal_query_results* results,
                              raptor_uri *base_uri,
                              const char* label,
                              char sep)
{
    rasqal_query* query = rasqal_query_results_get_query(results);
    int i;
    int count = 1;
#define empty_value_str_len 0
    static const char empty_value_str[empty_value_str_len+1] = "";
#define nl_str_len 1
    static const char nl_str[nl_str_len+1] = "\n";
    int vars_count;

    if(!rasqal_query_results_is_bindings(results)) {
        rasqal_log_error_simple(query->world, RAPTOR_LOG_LEVEL_ERROR,
                                &query->locator,
                                "Can only write %s format for variable binding results",
                                label);
        return 1;
    }

    /* Header */
    raptor_iostream_counted_string_write("Result", 6, iostr);

    for(i = 0; 1; i++) {
        const unsigned char *name;

        name = rasqal_query_results_get_binding_name(results, i);
        if(!name)
            break;

        raptor_iostream_write_byte(sep, iostr);
        raptor_iostream_string_write(name, iostr);
    }
    raptor_iostream_counted_string_write(nl_str, nl_str_len, iostr);


    /* Variable Binding Results */
    vars_count = rasqal_query_results_get_bindings_count(results);
    while(!rasqal_query_results_finished(results)) {
        /* Result row */
        raptor_iostream_decimal_write(count++, iostr);

        for(i = 0; i < vars_count; i++) {
            rasqal_literal *l = rasqal_query_results_get_binding_value(results, i);

            raptor_iostream_write_byte(sep, iostr);

            if(!l) {
                if(empty_value_str_len)
                    raptor_iostream_counted_string_write(empty_value_str,
                                                         empty_value_str_len, iostr);
            } else switch(l->type) {
                    const unsigned char* str;
                    size_t len;

                case RASQAL_LITERAL_URI:
                    raptor_iostream_string_write("uri(", iostr);
                    str = (const unsigned char*)raptor_uri_as_counted_string(l->value.uri, &len);
                    raptor_string_ntriples_write(str, len, '"', iostr);
                    raptor_iostream_write_byte(')', iostr);
                    break;

                case RASQAL_LITERAL_BLANK:
                    raptor_iostream_string_write("blank(", iostr);
                    raptor_string_ntriples_write(l->string, l->string_len, '"', iostr);
                    raptor_iostream_write_byte(')', iostr);
                    break;

                case RASQAL_LITERAL_STRING:
                    if(l->datatype && l->valid) {
                        rasqal_literal_type ltype;
                        ltype = rasqal_xsd_datatype_uri_to_type(l->world, l->datatype);

                        if(ltype >= RASQAL_LITERAL_INTEGER &&
                                ltype <= RASQAL_LITERAL_DECIMAL) {
                            /* write integer, float, double and decimal XSD typed
                             * data without quotes, datatype or language
                             */
                            raptor_string_ntriples_write(l->string, l->string_len, '\0', iostr);
                            break;
                        }
                    }

                    raptor_iostream_write_byte('"', iostr);
                    raptor_string_ntriples_write(l->string, l->string_len, '"', iostr);
                    raptor_iostream_write_byte('"', iostr);

                    if(l->language) {
                        raptor_iostream_write_byte('@', iostr);
                        raptor_iostream_string_write((const unsigned char*)l->language,
                                                     iostr);
                    }

                    if(l->datatype) {
                        raptor_iostream_string_write("^^uri(", iostr);
                        str = (const unsigned char*)raptor_uri_as_counted_string(l->datatype, &len);
                        raptor_string_ntriples_write(str, len, '"', iostr);
                        raptor_iostream_write_byte(')', iostr);
                    }

                    break;

                case RASQAL_LITERAL_PATTERN:
                case RASQAL_LITERAL_QNAME:
                case RASQAL_LITERAL_INTEGER:
                case RASQAL_LITERAL_XSD_STRING:
                case RASQAL_LITERAL_BOOLEAN:
                case RASQAL_LITERAL_DOUBLE:
                case RASQAL_LITERAL_FLOAT:
                case RASQAL_LITERAL_VARIABLE:
                case RASQAL_LITERAL_DECIMAL:
                case RASQAL_LITERAL_DATETIME:
                case RASQAL_LITERAL_UDT:
                case RASQAL_LITERAL_INTEGER_SUBTYPE:

                case RASQAL_LITERAL_UNKNOWN:
                default:
                    rasqal_log_error_simple(query->world, RAPTOR_LOG_LEVEL_ERROR,
                                            &query->locator,
                                            "Cannot turn literal type %d into %s",
                                            l->type, label);
                }

            /* End Binding */
        }

        /* End Result Row */
        raptor_iostream_counted_string_write(nl_str, nl_str_len, iostr);

        rasqal_query_results_next(results);
    }

    /* end sparql */
    return 0;
}
Exemplo n.º 3
0
/*
 * Return value: non-0 if equal
 */
static int
compare_query_results_compare(compare_query_results* cqr)
{
  int differences = 0;
  int i;
  int rowi;
  int size1;
  int size2;
  int row_differences_count = 0;
  
  size1 = rasqal_query_results_get_bindings_count(cqr->qr1);
  size2 = rasqal_query_results_get_bindings_count(cqr->qr2);
  
  if(size1 != size2) {
    cqr->message.level = RAPTOR_LOG_LEVEL_ERROR;
    cqr->message.text = "Results have different numbers of bindings";
    if(cqr->log_handler)
      cqr->log_handler(cqr->log_user_data, &cqr->message);

    differences++;
    goto done;
  }
  
  
  /* check variables in each results project the same variables */
  for(i = 0; 1; i++) {
    const unsigned char* v1;
    const unsigned char* v2;
    
    v1 = rasqal_query_results_get_binding_name(cqr->qr1, i);
    v2 = rasqal_query_results_get_binding_name(cqr->qr2, i);
    if(!v1 && !v2)
      break;

    if(v1 && v2) {
      if(strcmp((const char*)v1, (const char*)v2)) {
        /* different names */
        differences++;
      }
    } else
      /* one is NULL, the other is a name */
      differences++;
  }

  if(differences) {
    cqr->message.level = RAPTOR_LOG_LEVEL_ERROR;
    cqr->message.text = "Results have different binding names";
    if(cqr->log_handler)
      cqr->log_handler(cqr->log_user_data, &cqr->message);

    goto done;
  }
  

  /* set results to be stored? */

  /* sort rows by something ?  As long as the sort is the same it
   * probably does not matter what the method is. */

  /* what to do about blank nodes? */

  /* for each row */
  for(rowi = 0; 1; rowi++) {
    int bindingi;
    rasqal_row* row1 = rasqal_query_results_get_row_by_offset(cqr->qr1, rowi);
    rasqal_row* row2 = rasqal_query_results_get_row_by_offset(cqr->qr2, rowi);
    int this_row_different = 0;
    
    if(!row1 && !row2)
      break;
    
    /* for each variable in row1 (== same variables in row2) */
    for(bindingi = 0; bindingi < size1; bindingi++) {
      /* we know the binding names are the same */
      const unsigned char* name;
      rasqal_literal *value1;
      rasqal_literal *value2;
      int error = 0;

      name = rasqal_query_results_get_binding_name(cqr->qr1, bindingi);

      value1 = rasqal_query_results_get_binding_value(cqr->qr1, bindingi);
      value2 = rasqal_query_results_get_binding_value(cqr->qr2, bindingi);

      /* should have compare as native flag? 
       * RASQAL_COMPARE_XQUERY doesn't compare all values
       */
      if(!rasqal_literal_equals_flags(value1, value2, RASQAL_COMPARE_XQUERY,
                                      &error)) {
        /* if different report it */
        raptor_world* raptor_world_ptr;
        void *string;
        size_t length;
        raptor_iostream* string_iostr;

        raptor_world_ptr = rasqal_world_get_raptor(cqr->world);

        string_iostr = raptor_new_iostream_to_string(raptor_world_ptr, 
                                                     &string, &length,
                                                     (raptor_data_malloc_handler)malloc);

        raptor_iostream_counted_string_write("Difference in row ", 18,
                                             string_iostr);
        raptor_iostream_decimal_write(rowi + 1,
                                      string_iostr);
        raptor_iostream_counted_string_write(" binding '", 10, 
                                             string_iostr);
        raptor_iostream_string_write(name,
                                     string_iostr);
        raptor_iostream_counted_string_write("' ", 2, 
                                             string_iostr);
        raptor_iostream_string_write(cqr->qr1_label, string_iostr);
        raptor_iostream_counted_string_write(" value ", 7,
                                             string_iostr);
        rasqal_literal_write(value1,
                             string_iostr);
        raptor_iostream_write_byte(' ',
                                   string_iostr);
        raptor_iostream_string_write(cqr->qr2_label,
                                     string_iostr);
        raptor_iostream_counted_string_write(" value ", 7,
                                             string_iostr);
        rasqal_literal_write(value2,
                             string_iostr);
        raptor_iostream_write_byte(' ',
                                   string_iostr);

        /* this allocates and copies result into 'string' */
        raptor_free_iostream(string_iostr);

        cqr->message.level = RAPTOR_LOG_LEVEL_ERROR;
        cqr->message.text = (const char*)string;
        if(cqr->log_handler)
          cqr->log_handler(cqr->log_user_data, &cqr->message);

        free(string);
        
        differences++;
        this_row_different = 1;
      }
    } /* end for each var */

    if(this_row_different)
      row_differences_count++;

    rasqal_query_results_next(cqr->qr1);
    rasqal_query_results_next(cqr->qr2);
  } /* end for each row */

  if(row_differences_count) {
    cqr->message.level = RAPTOR_LOG_LEVEL_ERROR;
    cqr->message.text = "Results have different values";
    if(cqr->log_handler)
      cqr->log_handler(cqr->log_user_data, &cqr->message);
  }

  done:
  return (differences == 0);
}
Exemplo n.º 4
0
/*
 * rasqal_query_results_write_sv:
 * @iostr: #raptor_iostream to write the query to
 * @results: #rasqal_query_results query results format
 * @base_uri: #raptor_uri base URI of the output format
 * @label: name of this format for errors
 * @sep: column sep character
 * @csv_escape: non-0 if values are written escaped with CSV rules, else turtle
 * @variable_prefix: char to print before a variable name or NUL
 * @eol_str: end of line string
 * @eol_str_len: length of @eol_str
 *
 * INTERNAL - Write a @sep-separated values version of the query results format to an iostream.
 * 
 * If the writing succeeds, the query results will be exhausted.
 * 
 * Return value: non-0 on failure
 **/
static int
rasqal_query_results_write_sv(raptor_iostream *iostr,
                              rasqal_query_results* results,
                              raptor_uri *base_uri,
                              const char* label,
                              const char sep,
                              int csv_escape,
                              const char variable_prefix,
                              const char* eol_str,
                              size_t eol_str_len)
{
  rasqal_query* query = rasqal_query_results_get_query(results);
  int i;
  int vars_count;
  
  if(!rasqal_query_results_is_bindings(results)) {
    rasqal_log_error_simple(query->world, RAPTOR_LOG_LEVEL_ERROR,
                            &query->locator,
                            "Can only write %s format for variable binding results",
                            label);
    return 1;
  }
  
  /* Header */
  for(i = 0; 1; i++) {
    const unsigned char *name;
    
    name = rasqal_query_results_get_binding_name(results, i);
    if(!name)
      break;

    if(i > 0)
      raptor_iostream_write_byte(sep, iostr);

    if(variable_prefix)
      raptor_iostream_write_byte(variable_prefix, iostr);
    raptor_iostream_string_write(name, iostr);
  }
  raptor_iostream_counted_string_write(eol_str, eol_str_len, iostr);


  /* Variable Binding Results */
  vars_count = rasqal_query_results_get_bindings_count(results);
  while(!rasqal_query_results_finished(results)) {
    /* Result row */
    for(i = 0; i < vars_count; i++) {
      rasqal_literal *l = rasqal_query_results_get_binding_value(results, i);

      if(i > 0)
        raptor_iostream_write_byte(sep, iostr);

      if(l) {
        const unsigned char* str;
        size_t len;
        
        switch(l->type) {
          case RASQAL_LITERAL_URI:
            str = RASQAL_GOOD_CAST(const unsigned char*, raptor_uri_as_counted_string(l->value.uri, &len));
            if(csv_escape)
              rasqal_iostream_write_csv_string(str, len, iostr);
            else {
              raptor_iostream_write_byte('<', iostr);
              if(str && len > 0)
                raptor_string_ntriples_write(str, len, '"', iostr);
              raptor_iostream_write_byte('>', iostr);
            }
            break;

          case RASQAL_LITERAL_BLANK:
            raptor_bnodeid_ntriples_write(l->string, l->string_len, iostr);
            break;

          case RASQAL_LITERAL_STRING:
            if(csv_escape) {
              rasqal_iostream_write_csv_string(l->string, l->string_len, iostr);
            } else {
              if(l->datatype && l->valid) {
                rasqal_literal_type ltype;
                ltype = rasqal_xsd_datatype_uri_to_type(l->world, l->datatype);

                if(ltype >= RASQAL_LITERAL_INTEGER &&
                   ltype <= RASQAL_LITERAL_DECIMAL) {
                  /* write integer, float, double and decimal XSD typed
                   * data without quotes, datatype or language 
                   */
                  raptor_string_ntriples_write(l->string, l->string_len, '\0', iostr);
                  break;
                }
              }

              raptor_iostream_write_byte('"', iostr);
              raptor_string_ntriples_write(l->string, l->string_len, '"', iostr);
              raptor_iostream_write_byte('"', iostr);

              if(l->language) {
                raptor_iostream_write_byte('@', iostr);
                raptor_iostream_string_write(RASQAL_GOOD_CAST(const unsigned char*, l->language), iostr);
              }

              if(l->datatype) {
                raptor_iostream_string_write("^^<", iostr);
                str = RASQAL_GOOD_CAST(const unsigned char*, raptor_uri_as_counted_string(l->datatype, &len));
                raptor_string_ntriples_write(str, len, '"', iostr);
                raptor_iostream_write_byte('>', iostr);
              }
            }

            break;

          case RASQAL_LITERAL_PATTERN:
          case RASQAL_LITERAL_QNAME:
          case RASQAL_LITERAL_INTEGER:
          case RASQAL_LITERAL_XSD_STRING:
          case RASQAL_LITERAL_BOOLEAN:
          case RASQAL_LITERAL_DOUBLE:
          case RASQAL_LITERAL_FLOAT:
          case RASQAL_LITERAL_VARIABLE:
          case RASQAL_LITERAL_DECIMAL:
          case RASQAL_LITERAL_DATE:
          case RASQAL_LITERAL_DATETIME:
          case RASQAL_LITERAL_UDT:
          case RASQAL_LITERAL_INTEGER_SUBTYPE:

          case RASQAL_LITERAL_UNKNOWN:
          default:
            rasqal_log_error_simple(query->world, RAPTOR_LOG_LEVEL_ERROR,
                                    &query->locator,
                                    "Cannot turn literal type %d into %s",
                                    l->type, label);
        }
      }

      /* End Binding */
    }
Exemplo n.º 5
0
/**
 * raptor_string_escaped_write:
 * @string: UTF-8 string to write
 * @len: length of UTF-8 string
 * @delim: Terminating delimiter character for string (such as " or >) or \0 for no escaping.
 * @flags: bit flags - see #raptor_escaped_write_bitflags
 * @iostr: #raptor_iostream to write to
 *
 * Write a UTF-8 string formatted using different escapes to a #raptor_iostream
 *
 * Supports writing escapes in the Python, N-Triples, Turtle, JSON, mKR,
 * SPARQL styles to an iostream.
 * 
 * Return value: non-0 on failure such as bad UTF-8 encoding.
 **/
int
raptor_string_escaped_write(const unsigned char *string,
                            size_t len,
                            const char delim,
                            unsigned int flags,
                            raptor_iostream *iostr)
{
  unsigned char c;
  int unichar_len;
  raptor_unichar unichar;

  if(!string)
    return 1;
  
  for(; (c=*string); string++, len--) {
    if((delim && c == delim && (delim == '\'' || delim == '"')) ||
       c == '\\') {
      raptor_iostream_write_byte('\\', iostr);
      raptor_iostream_write_byte(c, iostr);
      continue;
    }

    if(delim && c == delim) {
      raptor_iostream_counted_string_write("\\u", 2, iostr);
      raptor_iostream_hexadecimal_write(c, 4, iostr);
      continue;
    }
    
    if(flags & RAPTOR_ESCAPED_WRITE_BITFLAG_SPARQL_URI_ESCAPES) {
      /* Must escape #x00-#x20<>\"{}|^` */
      if(c <= 0x20 ||
         c == '<' || c == '>' || c == '\\' || c == '"' || 
         c == '{' || c == '}' || c == '|' || c == '^' || c == '`') {
        raptor_iostream_counted_string_write("\\u", 2, iostr);
        raptor_iostream_hexadecimal_write(c, 4, iostr);
        continue;
      } else if(c < 0x7f) {
        raptor_iostream_write_byte(c, iostr);
        continue;
      }
    }

    if(flags & RAPTOR_ESCAPED_WRITE_BITFLAG_BS_ESCAPES_TNRU) {
      if(c == 0x09) {
        raptor_iostream_counted_string_write("\\t", 2, iostr);
        continue;
      } else if(c == 0x0a) {
        raptor_iostream_counted_string_write("\\n", 2, iostr);
        continue;
      } else if(c == 0x0d) {
        raptor_iostream_counted_string_write("\\r", 2, iostr);
        continue;
      } else if(c < 0x20 || c == 0x7f) {
        raptor_iostream_counted_string_write("\\u", 2, iostr);
        raptor_iostream_hexadecimal_write(c, 4, iostr);
        continue;
      }
    }
    
    if(flags & RAPTOR_ESCAPED_WRITE_BITFLAG_BS_ESCAPES_BF) {
      if(c == 0x08) {
        /* JSON has \b for backspace */
        raptor_iostream_counted_string_write("\\b", 2, iostr);
        continue;
      } else if(c == 0x0b) {
        /* JSON has \f for formfeed */
        raptor_iostream_counted_string_write("\\f", 2, iostr);
        continue;
      }
    }

    /* Just format remaining characters */
    if(c < 0x7f) {
      raptor_iostream_write_byte(c, iostr);
      continue;
    } 
    
    /* It is unicode */    
    unichar_len = raptor_unicode_utf8_string_get_char(string, len, &unichar);
    if(unichar_len < 0 || RAPTOR_GOOD_CAST(size_t, unichar_len) > len)
      /* UTF-8 encoding had an error or ended in the middle of a string */
      return 1;

    if(flags & RAPTOR_ESCAPED_WRITE_BITFLAG_UTF8) {
      /* UTF-8 is allowed so no need to escape */
      raptor_iostream_counted_string_write(string, unichar_len, iostr);
    } else {
      if(unichar < 0x10000) {
        raptor_iostream_counted_string_write("\\u", 2, iostr);
        raptor_iostream_hexadecimal_write(RAPTOR_GOOD_CAST(unsigned int, unichar), 4, iostr);
      } else {
        raptor_iostream_counted_string_write("\\U", 2, iostr);
        raptor_iostream_hexadecimal_write(RAPTOR_GOOD_CAST(unsigned int, unichar), 8, iostr);
      }
    }
Exemplo n.º 6
0
/* return 0 to abort visit */
static int
raptor_json_serialize_avltree_visit(int depth, void* data, void *user_data)
{
  raptor_serializer* serializer = (raptor_serializer*)user_data;
  raptor_json_context* context = (raptor_json_context*)serializer->context;

  raptor_statement* statement = (raptor_statement*)data;
  raptor_statement* s1 = statement;
  raptor_statement* s2 = context->last_statement;
  int new_subject = 0;
  int new_predicate = 0;
  
  if(s2) {
    new_subject = !raptor_term_equals(s1->subject, s2->subject);

    if(new_subject) {
      /* end last predicate */
      raptor_json_writer_newline(context->json_writer);

      raptor_json_writer_end_block(context->json_writer, ']');
      raptor_json_writer_newline(context->json_writer);

      /* end last statement */
      raptor_json_writer_end_block(context->json_writer, '}');
      raptor_json_writer_newline(context->json_writer);

      context->need_subject_comma = 1;
      context->need_object_comma = 0;
    }
  } else
    new_subject = 1;
  
  if(new_subject)  {
    if(context->need_subject_comma) {
      raptor_iostream_write_byte(',', serializer->iostream);
      raptor_json_writer_newline(context->json_writer);
    }

    /* start triple */

    /* subject */
    switch(s1->subject->type) {
      case RAPTOR_TERM_TYPE_URI:
        raptor_json_writer_key_uri_value(context->json_writer, 
                                         NULL, 0,
                                         s1->subject->value.uri);
        break;
        
      case RAPTOR_TERM_TYPE_BLANK:
        raptor_iostream_counted_string_write("\"_:", 3, serializer->iostream);
        raptor_string_python_write(s1->subject->value.blank.string, 0,
                                   '"', 2,
                                   serializer->iostream);
        raptor_iostream_write_byte('"', serializer->iostream);
        break;
        
      case RAPTOR_TERM_TYPE_LITERAL:
      case RAPTOR_TERM_TYPE_UNKNOWN:
      default:
        raptor_log_error_formatted(serializer->world, RAPTOR_LOG_LEVEL_ERROR,
                                   NULL,
                                   "Triple has unsupported subject term type %d", 
                                   s1->subject->type);
        break;
    }

    raptor_iostream_counted_string_write(" : ", 3, serializer->iostream);
    raptor_json_writer_start_block(context->json_writer, '{');
  
    raptor_json_writer_newline(context->json_writer);
  }
  

  /* predicate */
  if(context->last_statement) {
    if(new_subject)
      new_predicate = 1;
    else {
      new_predicate = !raptor_uri_equals(s1->predicate->value.uri,
                                         s2->predicate->value.uri);
      if(new_predicate) {
        raptor_json_writer_newline(context->json_writer);
        raptor_json_writer_end_block(context->json_writer, ']');
        raptor_iostream_write_byte(',', serializer->iostream);
        raptor_json_writer_newline(context->json_writer);
      }
    }
  } else
    new_predicate = 1;

  if(new_predicate) {
    /* start predicate */

    raptor_json_writer_key_uri_value(context->json_writer, 
                                   NULL, 0,
                                   s1->predicate->value.uri);
    raptor_iostream_counted_string_write(" : ", 3, serializer->iostream);
    raptor_json_writer_start_block(context->json_writer, '[');
    raptor_iostream_write_byte(' ', serializer->iostream);

    context->need_object_comma = 0;
  }

  if(context->need_object_comma) {
    raptor_iostream_write_byte(',', serializer->iostream);
    raptor_json_writer_newline(context->json_writer);
  }
  
  /* object */
  switch(s1->object->type) {
    case RAPTOR_TERM_TYPE_URI:
      raptor_json_writer_uri_object(context->json_writer,
                                    s1->object->value.uri);
      raptor_json_writer_newline(context->json_writer);
      break;
          
    case RAPTOR_TERM_TYPE_LITERAL:
      raptor_json_writer_literal_object(context->json_writer,
                                        s1->object->value.literal.string,
                                        s1->object->value.literal.language, 
                                        s1->object->value.literal.datatype,
                                        "value", "type");
      break;

    case RAPTOR_TERM_TYPE_BLANK:
      raptor_json_writer_blank_object(context->json_writer, 
                                      s1->object->value.blank.string);
      raptor_json_writer_newline(context->json_writer);
      break;

    case RAPTOR_TERM_TYPE_UNKNOWN:
      default:
        raptor_log_error_formatted(serializer->world, RAPTOR_LOG_LEVEL_ERROR,
                                   NULL,
                                   "Triple has unsupported object term type %d", 
                                   s1->object->type);
        break;
  }

  /* end triple */

  context->need_object_comma = 1;
  context->last_statement = statement;

  return 1;
}
Exemplo n.º 7
0
static int
raptor_xml_writer_start_element_common(raptor_xml_writer* xml_writer,
                                       raptor_xml_element* element,
                                       int auto_empty)
{
  raptor_iostream* iostr = xml_writer->iostr;
  raptor_namespace_stack *nstack = xml_writer->nstack;
  int depth = xml_writer->depth;
  int auto_indent = XML_WRITER_AUTO_INDENT(xml_writer);
  int xml_version = XML_WRITER_XML_VERSION(xml_writer);
  struct nsd *nspace_declarations = NULL;
  size_t nspace_declarations_count = 0;  
  unsigned int i;

  /* max is 1 per element and 1 for each attribute + size of declared */
  if(nstack) {
    int nspace_max_count = element->attribute_count+1;
    if(element->declared_nspaces)
      nspace_max_count += raptor_sequence_size(element->declared_nspaces);
    
    nspace_declarations = (struct nsd*)RAPTOR_CALLOC(nsdarray, nspace_max_count,
                                                     sizeof(struct nsd));
    if(!nspace_declarations)
      return 1;
  }

  if(element->name->nspace) {
    if(nstack && !raptor_namespaces_namespace_in_scope(nstack, element->name->nspace)) {
      nspace_declarations[0].declaration=
        raptor_namespace_format_as_xml(element->name->nspace,
                                       &nspace_declarations[0].length);
      if(!nspace_declarations[0].declaration)
        goto error;
      nspace_declarations[0].nspace = element->name->nspace;
      nspace_declarations_count++;
    }
  }

  if(element->attributes) {
    for(i = 0; i < element->attribute_count; i++) {
      /* qname */
      if(element->attributes[i]->nspace) {
        if(nstack && 
           !raptor_namespaces_namespace_in_scope(nstack, element->attributes[i]->nspace) && element->attributes[i]->nspace != element->name->nspace) {
          /* not in scope and not same as element (so already going to be declared)*/
          unsigned int j;
          int declare_me = 1;
          
          /* check it wasn't an earlier declaration too */
          for(j = 0; j < nspace_declarations_count; j++)
            if(nspace_declarations[j].nspace == element->attributes[j]->nspace) {
              declare_me = 0;
              break;
            }
            
          if(declare_me) {
            nspace_declarations[nspace_declarations_count].declaration=
              raptor_namespace_format_as_xml(element->attributes[i]->nspace,
                                             &nspace_declarations[nspace_declarations_count].length);
            if(!nspace_declarations[nspace_declarations_count].declaration)
              goto error;
            nspace_declarations[nspace_declarations_count].nspace = element->attributes[i]->nspace;
            nspace_declarations_count++;
          }
        }
      }
    }
  }

  if(nstack && element->declared_nspaces &&
     raptor_sequence_size(element->declared_nspaces) > 0) {
    for(i = 0; i< (unsigned int)raptor_sequence_size(element->declared_nspaces); i++) {
      raptor_namespace* nspace = (raptor_namespace*)raptor_sequence_get_at(element->declared_nspaces, i);
      unsigned int j;
      int declare_me = 1;
      
      /* check it wasn't an earlier declaration too */
      for(j = 0; j < nspace_declarations_count; j++)
        if(nspace_declarations[j].nspace == nspace) {
          declare_me = 0;
          break;
        }
      
      if(declare_me) {
        nspace_declarations[nspace_declarations_count].declaration=
          raptor_namespace_format_as_xml(nspace,
                                         &nspace_declarations[nspace_declarations_count].length);
        if(!nspace_declarations[nspace_declarations_count].declaration)
          goto error;
        nspace_declarations[nspace_declarations_count].nspace = nspace;
        nspace_declarations_count++;
      }

    }
  }

  raptor_iostream_write_byte('<', iostr);

  if(element->name->nspace && element->name->nspace->prefix_length > 0) {
    raptor_iostream_counted_string_write((const char*)element->name->nspace->prefix, 
                                         element->name->nspace->prefix_length,
                                         iostr);
    raptor_iostream_write_byte(':', iostr);
  }
  raptor_iostream_counted_string_write((const char*)element->name->local_name,
                                       element->name->local_name_length,
                                       iostr);

  /* declare namespaces */
  if(nspace_declarations_count) {
    /* sort them into the canonical order */
    qsort((void*)nspace_declarations, 
          nspace_declarations_count, sizeof(struct nsd),
          raptor_xml_writer_nsd_compare);
    /* add them */
    for(i = 0; i < nspace_declarations_count; i++) {
      if(auto_indent && nspace_declarations_count > 1) {
        /* indent xmlns namespace attributes */
        raptor_xml_writer_newline(xml_writer);
        xml_writer->depth++;
        raptor_xml_writer_indent(xml_writer);
        xml_writer->depth--;
      }
      raptor_iostream_write_byte(' ', iostr);
      raptor_iostream_counted_string_write((const char*)nspace_declarations[i].declaration,
                                           nspace_declarations[i].length,
                                           iostr);
      RAPTOR_FREE(cstring, nspace_declarations[i].declaration);
      nspace_declarations[i].declaration = NULL;

      if(raptor_namespace_stack_start_namespace(nstack,
                                                (raptor_namespace*)nspace_declarations[i].nspace,
                                                depth))
        goto error;
    }
  }


  if(element->attributes) {
    for(i = 0; i < element->attribute_count; i++) {
      raptor_iostream_write_byte(' ', iostr);
      
      if(element->attributes[i]->nspace && 
         element->attributes[i]->nspace->prefix_length > 0) {
        raptor_iostream_counted_string_write((char*)element->attributes[i]->nspace->prefix,
                                             element->attributes[i]->nspace->prefix_length,
                                             iostr);
        raptor_iostream_write_byte(':', iostr);
      }

      raptor_iostream_counted_string_write((const char*)element->attributes[i]->local_name,
                                           element->attributes[i]->local_name_length,
                                           iostr);
      
      raptor_iostream_counted_string_write("=\"", 2, iostr);
      
      raptor_xml_escape_string_any_write(element->attributes[i]->value, 
                                          element->attributes[i]->value_length,
                                          '"',
                                          xml_version,
                                          iostr);
      raptor_iostream_write_byte('"', iostr);
    }
  }

  if(!auto_empty)
    raptor_iostream_write_byte('>', iostr);

  if(nstack)
    RAPTOR_FREE(stringarray, nspace_declarations);

  return 0;

  /* Clean up nspace_declarations on error */
  error:

  for(i = 0; i < nspace_declarations_count; i++) {
    if(nspace_declarations[i].declaration)
      RAPTOR_FREE(cstring, nspace_declarations[i].declaration);
  }

  if(nspace_declarations)
    RAPTOR_FREE(stringarray, nspace_declarations);

  return 1;
}
Exemplo n.º 8
0
static void
rasqal_query_write_sparql_graph_pattern(sparql_writer_context *wc,
                                        raptor_iostream* iostr,
                                        rasqal_graph_pattern *gp, 
                                        int gp_index, int indent)
{
  int triple_index = 0;
  rasqal_graph_pattern_operator op;
  raptor_sequence *seq;
  int filters_count = 0;
  int want_braces = 1;
  
  op = rasqal_graph_pattern_get_operator(gp);

  if(op == RASQAL_GRAPH_PATTERN_OPERATOR_SELECT) {
    raptor_sequence* vars_seq;
    rasqal_graph_pattern* where_gp;
    
    raptor_iostream_counted_string_write("SELECT", 6, iostr);
    vars_seq = rasqal_projection_get_variables_sequence(gp->projection);
    rasqal_query_write_sparql_select(wc, iostr, vars_seq);
    raptor_iostream_write_byte('\n', iostr);
    rasqal_query_write_indent(iostr, indent);
    raptor_iostream_counted_string_write("WHERE ", 6, iostr);
    where_gp = rasqal_graph_pattern_get_sub_graph_pattern(gp, 0);
    rasqal_query_write_sparql_graph_pattern(wc, iostr, where_gp, 0, indent);
    /* FIXME - not implemented: modifiers */
    return;
  }

  if(op == RASQAL_GRAPH_PATTERN_OPERATOR_LET) {
    /* LAQRS */
    raptor_iostream_counted_string_write("LET (", 5, iostr);
    rasqal_query_write_sparql_variable(wc, iostr, gp->var);
    raptor_iostream_counted_string_write(" := ", 4, iostr);
    rasqal_query_write_sparql_expression(wc, iostr, gp->filter_expression);
    raptor_iostream_counted_string_write(") .", 3, iostr);
    return;
  }
  
  if(op == RASQAL_GRAPH_PATTERN_OPERATOR_SERVICE) {
    rasqal_graph_pattern* service_gp;

    /* LAQRS */
    raptor_iostream_counted_string_write("SERVICE ", 8, iostr);
    rasqal_query_write_sparql_literal(wc, iostr, gp->origin);
    raptor_iostream_counted_string_write(" ", 1, iostr);
    service_gp = rasqal_graph_pattern_get_sub_graph_pattern(gp, 0);
    rasqal_query_write_sparql_graph_pattern(wc, iostr, service_gp, 0, indent);
    return;
  }
  
  if(op == RASQAL_GRAPH_PATTERN_OPERATOR_OPTIONAL ||
     op == RASQAL_GRAPH_PATTERN_OPERATOR_GRAPH) {
    /* prefix verbs */
    if(op == RASQAL_GRAPH_PATTERN_OPERATOR_OPTIONAL) 
      raptor_iostream_counted_string_write("OPTIONAL ", 9, iostr);
    else {
      raptor_iostream_counted_string_write("GRAPH ", 6, iostr);
      rasqal_query_write_sparql_literal(wc, iostr, gp->origin);
      raptor_iostream_write_byte(' ', iostr);
    }
  }

  if(gp->op == RASQAL_GRAPH_PATTERN_OPERATOR_FILTER)
    want_braces = 0;


  if(want_braces) {
    raptor_iostream_counted_string_write("{\n", 2, iostr);
    indent += 2;
  }

  /* look for triples */
  while(1) {
    rasqal_triple* t = rasqal_graph_pattern_get_triple(gp, triple_index);
    if(!t)
      break;
    
    rasqal_query_write_indent(iostr, indent);
    rasqal_query_write_sparql_triple(wc, iostr, t);
    raptor_iostream_write_byte('\n', iostr);

    triple_index++;
  }


  /* look for sub-graph patterns */
  seq = rasqal_graph_pattern_get_sub_graph_pattern_sequence(gp);
  if(seq && raptor_sequence_size(seq) > 0) {
    for(gp_index = 0; 1; gp_index++) {
      rasqal_graph_pattern* sgp;

      sgp = rasqal_graph_pattern_get_sub_graph_pattern(gp, gp_index);
      if(!sgp)
        break;

      if(sgp->op == RASQAL_GRAPH_PATTERN_OPERATOR_FILTER) {
        filters_count++;
        continue;
      }
      
      if(!gp_index)
        rasqal_query_write_indent(iostr, indent);
      else {
        if(op == RASQAL_GRAPH_PATTERN_OPERATOR_UNION)
          /* infix verb */
          raptor_iostream_counted_string_write(" UNION ", 7, iostr);
        else {
          /* must be prefix verb */
          raptor_iostream_write_byte('\n', iostr);
          rasqal_query_write_indent(iostr, indent);
        }
      }
      
      rasqal_query_write_sparql_graph_pattern(wc, iostr, sgp, gp_index, indent);
    }
    raptor_iostream_write_byte('\n', iostr);
  }
  

  /* look for constraints */
  if(filters_count > 0) {
    for(gp_index = 0; 1; gp_index++) {
      rasqal_graph_pattern* sgp;
      rasqal_expression* expr;

      sgp = rasqal_graph_pattern_get_sub_graph_pattern(gp, gp_index);
      if(!sgp)
        break;
      
      if(sgp->op != RASQAL_GRAPH_PATTERN_OPERATOR_FILTER)
        continue;

      expr = rasqal_graph_pattern_get_filter_expression(sgp);

      rasqal_query_write_indent(iostr, indent);
      raptor_iostream_counted_string_write("FILTER( ", 8, iostr);
      rasqal_query_write_sparql_expression(wc, iostr, expr);
      raptor_iostream_counted_string_write(" )\n", 3, iostr);
    }
  }
  

  if(want_braces) {
    indent -= 2;

    rasqal_query_write_indent(iostr, indent);
    raptor_iostream_write_byte('}', iostr);
  }

}
Exemplo n.º 9
0
int
rasqal_query_write_sparql_20060406(raptor_iostream *iostr, 
                                   rasqal_query* query, raptor_uri *base_uri)
{
  int i;
  sparql_writer_context wc;
  int limit, offset;
  rasqal_query_verb verb;

  wc.world = query->world;
  wc.base_uri = NULL;

  wc.type_uri = raptor_new_uri_for_rdf_concept(query->world->raptor_world_ptr,
                                               (const unsigned char*)"type");
  wc.nstack = raptor_new_namespaces(query->world->raptor_world_ptr, 1);

  if(base_uri) {
    raptor_iostream_counted_string_write("BASE ", 5, iostr);
    rasqal_query_write_sparql_uri(&wc, iostr, base_uri);
    raptor_iostream_write_byte('\n', iostr);

    /* from now on all URIs are relative to this */
    wc.base_uri = raptor_uri_copy(base_uri);
  }
  
  
  for(i = 0; 1 ; i++) {
    raptor_namespace *nspace;
    rasqal_prefix* p = rasqal_query_get_prefix(query, i);
    if(!p)
      break;
    
    raptor_iostream_counted_string_write("PREFIX ", 7, iostr);
    if(p->prefix)
      raptor_iostream_string_write(p->prefix, iostr);
    raptor_iostream_counted_string_write(": ", 2, iostr);
    rasqal_query_write_sparql_uri(&wc, iostr, p->uri);
    raptor_iostream_write_byte('\n', iostr);

    /* Use this constructor so we copy a URI directly */
    nspace = raptor_new_namespace_from_uri(wc.nstack, p->prefix, p->uri, i);
    raptor_namespaces_start_namespace(wc.nstack, nspace);
  }

  if(query->explain)
    raptor_iostream_counted_string_write("EXPLAIN ", 8, iostr);

  verb = query->verb;
  
  /* These terms are deprecated */
  if(query->verb == RASQAL_QUERY_VERB_INSERT ||
     query->verb == RASQAL_QUERY_VERB_DELETE) {
    verb = RASQAL_QUERY_VERB_UPDATE;
  }

  
  /* Write SPARQL 1.1 (Draft) Update forms */
  if(verb == RASQAL_QUERY_VERB_UPDATE) {
    rasqal_update_operation* update;
    /* Write SPARQL Update */

    for(i = 0; (update = rasqal_query_get_update_operation(query, i)); i++) {
      int is_always_2_args = (update->type >= RASQAL_UPDATE_TYPE_ADD &&
                              update->type <= RASQAL_UPDATE_TYPE_COPY);
      
      if(update->type == RASQAL_UPDATE_TYPE_UPDATE) {
        /* update operations:
         * WITH ... INSERT { template } DELETE { template } WHERE { template }
         * INSERT/DELETE { template } WHERE { template }
         * INSERT/DELETE DATA { triples } 
         */
        if(update->graph_uri) {
          raptor_iostream_counted_string_write("WITH ", 5, iostr);
          rasqal_query_write_sparql_uri(&wc, iostr, update->graph_uri);
          raptor_iostream_write_byte('\n', iostr);
        }
        if(update->delete_templates) {
          raptor_iostream_counted_string_write("DELETE ", 7, iostr);
          if(update->flags & RASQAL_UPDATE_FLAGS_DATA) 
            raptor_iostream_counted_string_write("DATA ", 5, iostr);
          rasqal_query_write_sparql_triple_data(&wc, iostr,
                                                update->delete_templates,
                                                0);
          raptor_iostream_write_byte('\n', iostr);
        }
        if(update->insert_templates) {
          raptor_iostream_counted_string_write("INSERT ", 7, iostr);
          if(update->flags & RASQAL_UPDATE_FLAGS_DATA) 
            raptor_iostream_counted_string_write("DATA ", 5, iostr);
          rasqal_query_write_sparql_triple_data(&wc, iostr,
                                                update->insert_templates,
                                                0);
          raptor_iostream_write_byte('\n', iostr);
        }
        if(update->where) {
          raptor_iostream_counted_string_write("WHERE ", 6, iostr);
          rasqal_query_write_sparql_graph_pattern(&wc, iostr,
                                                  update->where,
                                                  -1, 0);
          raptor_iostream_write_byte('\n', iostr);
        }
      } else {
        /* admin operations:
         * CLEAR GRAPH graph-uri | DEFAULT | NAMED | ALL
         * CREATE (SILENT) GRAPH graph-uri | DEFAULT | NAMED | ALL
         * DROP (SILENT) GRAPH graph-uri
         * LOAD (SILENT) doc-uri / LOAD (SILENT) doc-uri INTO GRAPH graph-uri
         * ADD (SILENT) GraphOrDefault TO GraphOrDefault
         * MOVE (SILENT) GraphOrDefault TO GraphOrDefault
         * COPY (SILENT) GraphOrDefault TO GraphOrDefault
         */
        raptor_iostream_string_write(rasqal_update_type_label(update->type),
                                     iostr);
        if(update->flags & RASQAL_UPDATE_FLAGS_SILENT)
          raptor_iostream_counted_string_write(" SILENT", 7, iostr);

        if(is_always_2_args) {
          /* ADD, MOVE, COPY are always 2-arg admin operations */
          rasqal_query_write_graphref(&wc, iostr, 
                                      update->graph_uri,
                                      RASQAL_UPDATE_GRAPH_ONE);

          raptor_iostream_counted_string_write(" TO", 3, iostr);
        
          rasqal_query_write_graphref(&wc, iostr, 
                                      update->document_uri,
                                      RASQAL_UPDATE_GRAPH_ONE);

        } else if(update->type == RASQAL_UPDATE_TYPE_LOAD) {
          /* LOAD is 1 or 2 URIs and first one never has a GRAPH prefix */

          raptor_iostream_write_byte(' ', iostr);

          rasqal_query_write_sparql_uri(&wc, iostr, update->document_uri);

          if(update->graph_uri) {
            raptor_iostream_counted_string_write(" INTO", 5, iostr);
            
            rasqal_query_write_graphref(&wc, iostr, 
                                        update->graph_uri,
                                        RASQAL_UPDATE_GRAPH_ONE);
          }
        } else {
          /* everything else is defined by update->applies; only
          * CLEAR and DROP may apply to >1 graph
          */
          rasqal_query_write_graphref(&wc, iostr, 
                                      update->graph_uri,
                                      update->applies);
        }
        
        raptor_iostream_write_byte('\n', iostr);

      }
    }

    goto tidy;
  }


  if(verb != RASQAL_QUERY_VERB_CONSTRUCT)
    raptor_iostream_string_write(rasqal_query_verb_as_string(query->verb),
                                 iostr);

  if(query->distinct) {
    if(query->distinct == 1)
      raptor_iostream_counted_string_write(" DISTINCT", 9, iostr);
    else
      raptor_iostream_counted_string_write(" REDUCED", 8, iostr);
  }

  if(query->wildcard)
    raptor_iostream_counted_string_write(" *", 2, iostr);
  else if(verb == RASQAL_QUERY_VERB_DESCRIBE) {
    raptor_sequence *lit_seq = query->describes;
    int count = raptor_sequence_size(lit_seq);

    for(i = 0; i < count; i++) {
      rasqal_literal* l = (rasqal_literal*)raptor_sequence_get_at(lit_seq, i);
      raptor_iostream_write_byte(' ', iostr);
      rasqal_query_write_sparql_literal(&wc, iostr, l);
    }
  } else if(verb == RASQAL_QUERY_VERB_SELECT) {
    rasqal_query_write_sparql_select(&wc, iostr, query->selects);
  }
  raptor_iostream_write_byte('\n', iostr);

  if(query->data_graphs) {
    for(i = 0; 1; i++) {
      rasqal_data_graph* dg = rasqal_query_get_data_graph(query, i);
      if(!dg)
        break;
      
      if(dg->flags & RASQAL_DATA_GRAPH_NAMED)
        continue;
      
      rasqal_query_write_data_format_comment(&wc, iostr, dg);
      raptor_iostream_counted_string_write("FROM ", 5, iostr);
      rasqal_query_write_sparql_uri(&wc, iostr, dg->uri);
      raptor_iostream_counted_string_write("\n", 1, iostr);
    }
    
    for(i = 0; 1; i++) {
      rasqal_data_graph* dg = rasqal_query_get_data_graph(query, i);
      if(!dg)
        break;

      if(!(dg->flags & RASQAL_DATA_GRAPH_NAMED))
        continue;
      
      rasqal_query_write_data_format_comment(&wc, iostr, dg);
      raptor_iostream_counted_string_write("FROM NAMED ", 11, iostr);
      rasqal_query_write_sparql_uri(&wc, iostr, dg->name_uri);
      raptor_iostream_write_byte('\n', iostr);
    }
    
  }

  if(query->constructs) {
    raptor_iostream_string_write("CONSTRUCT {\n", iostr);
    for(i = 0; 1; i++) {
      rasqal_triple* t = rasqal_query_get_construct_triple(query, i);
      if(!t)
        break;

      raptor_iostream_counted_string_write("  ", 2, iostr);
      rasqal_query_write_sparql_triple(&wc, iostr, t);
      raptor_iostream_write_byte('\n', iostr);
    }
    raptor_iostream_counted_string_write("}\n", 2, iostr);
  }
  if(query->query_graph_pattern) {
    raptor_iostream_counted_string_write("WHERE ", 6, iostr);
    rasqal_query_write_sparql_graph_pattern(&wc, iostr,
                                            query->query_graph_pattern, 
                                            -1, 0);
    raptor_iostream_write_byte('\n', iostr);
  }

  if(rasqal_query_get_group_conditions_sequence(query)) {
    raptor_iostream_counted_string_write("GROUP BY ", 9, iostr);
    for(i = 0; 1; i++) {
      rasqal_expression* expr = rasqal_query_get_group_condition(query, i);
      if(!expr)
        break;

      if(i > 0)
        raptor_iostream_write_byte(' ', iostr);
      rasqal_query_write_sparql_expression(&wc, iostr, expr);
    }
    raptor_iostream_write_byte('\n', iostr);
  }

  if(rasqal_query_get_having_conditions_sequence(query)) {
    raptor_iostream_counted_string_write("HAVING ", 7, iostr);
    for(i = 0; 1; i++) {
      rasqal_expression* expr = rasqal_query_get_having_condition(query, i);
      if(!expr)
        break;

      if(i > 0)
        raptor_iostream_write_byte(' ', iostr);
      rasqal_query_write_sparql_expression(&wc, iostr, expr);
    }
    raptor_iostream_write_byte('\n', iostr);
  }

  if(rasqal_query_get_order_conditions_sequence(query)) {
    raptor_iostream_counted_string_write("ORDER BY ", 9, iostr);
    for(i = 0; 1; i++) {
      rasqal_expression* expr = rasqal_query_get_order_condition(query, i);
      if(!expr)
        break;

      if(i > 0)
        raptor_iostream_write_byte(' ', iostr);
      rasqal_query_write_sparql_expression(&wc, iostr, expr);
    }
    raptor_iostream_write_byte('\n', iostr);
  }

  limit = rasqal_query_get_limit(query);
  offset = rasqal_query_get_offset(query);
  if(limit >= 0 || offset >= 0) {
    if(limit >= 0) {
      raptor_iostream_counted_string_write("LIMIT ", 7, iostr);
      raptor_iostream_decimal_write(limit, iostr);
    }
    if(offset >= 0) {
      if(limit)
        raptor_iostream_write_byte(' ', iostr);
      raptor_iostream_counted_string_write("OFFSET ", 8, iostr);
      raptor_iostream_decimal_write(offset, iostr);
    }
    raptor_iostream_write_byte('\n', iostr);
  }

  if(query->bindings)
    rasqal_write_sparql_bindings(&wc, iostr, query->bindings);

  tidy:
  raptor_free_uri(wc.type_uri);
  if(wc.base_uri)
    raptor_free_uri(wc.base_uri);
  raptor_free_namespaces(wc.nstack);

  return 0;
}
Exemplo n.º 10
0
static void
rasqal_query_write_sparql_literal(sparql_writer_context *wc,
                                  raptor_iostream* iostr, rasqal_literal* l)
{
  if(!l) {
    raptor_iostream_counted_string_write("null", 4, iostr);
    return;
  }

  switch(l->type) {
    case RASQAL_LITERAL_URI:
      rasqal_query_write_sparql_uri(wc, iostr, l->value.uri);
      break;
    case RASQAL_LITERAL_BLANK:
      raptor_iostream_counted_string_write("_:", 2, iostr);
      raptor_iostream_string_write(l->string, iostr);
      break;
    case RASQAL_LITERAL_STRING:
      raptor_iostream_write_byte('"', iostr);
      raptor_string_ntriples_write(l->string, l->string_len, '"', iostr);
      raptor_iostream_write_byte('"', iostr);
      if(l->language) {
        raptor_iostream_write_byte('@', iostr);
        raptor_iostream_string_write(l->language, iostr);
      }
      if(l->datatype) {
        raptor_iostream_counted_string_write("^^", 2, iostr);
        rasqal_query_write_sparql_uri(wc, iostr, l->datatype);
      }
      break;
    case RASQAL_LITERAL_QNAME:
      raptor_iostream_counted_string_write("QNAME(", 6, iostr);
      raptor_iostream_counted_string_write(l->string, l->string_len, iostr);
      raptor_iostream_write_byte(')', iostr);
      break;
    case RASQAL_LITERAL_INTEGER:
      raptor_iostream_decimal_write(l->value.integer, iostr);
      break;
    case RASQAL_LITERAL_BOOLEAN:
    case RASQAL_LITERAL_DOUBLE:
    case RASQAL_LITERAL_FLOAT:
    case RASQAL_LITERAL_DECIMAL:
      raptor_iostream_counted_string_write(l->string, l->string_len, iostr);
      break;
    case RASQAL_LITERAL_VARIABLE:
      rasqal_query_write_sparql_variable(wc, iostr, l->value.variable);
      break;
    case RASQAL_LITERAL_DATETIME:
    case RASQAL_LITERAL_XSD_STRING:
    case RASQAL_LITERAL_UDT:
    case RASQAL_LITERAL_INTEGER_SUBTYPE:
      if(1) {
        raptor_uri* dt_uri;
        
        raptor_iostream_write_byte('"', iostr);
        raptor_string_ntriples_write(l->string, l->string_len, '"', iostr);
        raptor_iostream_counted_string_write("\"^^", 3, iostr);
        if(l->type == RASQAL_LITERAL_UDT) 
          dt_uri = l->datatype;
        else
          dt_uri = rasqal_xsd_datatype_type_to_uri(l->world, l->type);
        rasqal_query_write_sparql_uri(wc, iostr, dt_uri);
      }
      break;

    case RASQAL_LITERAL_UNKNOWN:
    case RASQAL_LITERAL_PATTERN:
    default:
      RASQAL_FATAL2("Literal type %d cannot be written as a SPARQL literal", l->type);
  }
}
Exemplo n.º 11
0
static void
rasqal_query_write_sparql_expression(sparql_writer_context *wc,
                                     raptor_iostream* iostr, 
                                     rasqal_expression* e)
{
  int i;
  int count;

  switch(e->op) {
    case RASQAL_EXPR_CURRENT_DATETIME:
    case RASQAL_EXPR_NOW:
    case RASQAL_EXPR_RAND:
      rasqal_query_write_sparql_expression_op(wc, iostr, e);
      raptor_iostream_counted_string_write("()", 2, iostr);
      break;

    case RASQAL_EXPR_AND:
    case RASQAL_EXPR_OR:
    case RASQAL_EXPR_EQ:
    case RASQAL_EXPR_NEQ:
    case RASQAL_EXPR_LT:
    case RASQAL_EXPR_GT:
    case RASQAL_EXPR_LE:
    case RASQAL_EXPR_GE:
    case RASQAL_EXPR_PLUS:
    case RASQAL_EXPR_MINUS:
    case RASQAL_EXPR_STAR:
    case RASQAL_EXPR_SLASH:
    case RASQAL_EXPR_REM:
    case RASQAL_EXPR_STR_EQ:
    case RASQAL_EXPR_STR_NEQ:
    case RASQAL_EXPR_STRLANG:
    case RASQAL_EXPR_STRDT:
    case RASQAL_EXPR_STRSTARTS:
    case RASQAL_EXPR_STRENDS:
    case RASQAL_EXPR_CONTAINS:
      raptor_iostream_counted_string_write("( ", 2, iostr);
      rasqal_query_write_sparql_expression(wc, iostr, e->arg1);
      raptor_iostream_write_byte(' ', iostr);
      rasqal_query_write_sparql_expression_op(wc, iostr, e);
      raptor_iostream_write_byte(' ', iostr);
      rasqal_query_write_sparql_expression(wc, iostr, e->arg2);
      raptor_iostream_counted_string_write(" )", 2, iostr);
      break;

    case RASQAL_EXPR_BOUND:
    case RASQAL_EXPR_STR:
    case RASQAL_EXPR_LANG:
    case RASQAL_EXPR_DATATYPE:
    case RASQAL_EXPR_ISURI:
    case RASQAL_EXPR_ISBLANK:
    case RASQAL_EXPR_ISLITERAL:
    case RASQAL_EXPR_ORDER_COND_ASC:
    case RASQAL_EXPR_ORDER_COND_DESC:
    case RASQAL_EXPR_GROUP_COND_ASC:
    case RASQAL_EXPR_GROUP_COND_DESC:
    case RASQAL_EXPR_COUNT:
    case RASQAL_EXPR_SAMETERM:
    case RASQAL_EXPR_SUM:
    case RASQAL_EXPR_AVG:
    case RASQAL_EXPR_MIN:
    case RASQAL_EXPR_MAX:
    case RASQAL_EXPR_URI:
    case RASQAL_EXPR_IRI:
    case RASQAL_EXPR_BNODE:
    case RASQAL_EXPR_SAMPLE:
    case RASQAL_EXPR_ISNUMERIC:
    case RASQAL_EXPR_YEAR:
    case RASQAL_EXPR_MONTH:
    case RASQAL_EXPR_DAY:
    case RASQAL_EXPR_HOURS:
    case RASQAL_EXPR_MINUTES:
    case RASQAL_EXPR_SECONDS:
    case RASQAL_EXPR_TIMEZONE:
    case RASQAL_EXPR_FROM_UNIXTIME:
    case RASQAL_EXPR_TO_UNIXTIME:
    case RASQAL_EXPR_STRLEN:
    case RASQAL_EXPR_UCASE:
    case RASQAL_EXPR_LCASE:
    case RASQAL_EXPR_ENCODE_FOR_URI:
    case RASQAL_EXPR_TZ:
    case RASQAL_EXPR_ABS:
    case RASQAL_EXPR_ROUND:
    case RASQAL_EXPR_CEIL:
    case RASQAL_EXPR_FLOOR:
    case RASQAL_EXPR_MD5:
    case RASQAL_EXPR_SHA1:
    case RASQAL_EXPR_SHA224:
    case RASQAL_EXPR_SHA256:
    case RASQAL_EXPR_SHA384:
    case RASQAL_EXPR_SHA512:
      rasqal_query_write_sparql_expression_op(wc, iostr, e);
      raptor_iostream_counted_string_write("( ", 2, iostr);
      rasqal_query_write_sparql_expression(wc, iostr, e->arg1);
      raptor_iostream_counted_string_write(" )", 2, iostr);
      break;
      
    case RASQAL_EXPR_LANGMATCHES:
    case RASQAL_EXPR_REGEX:
    case RASQAL_EXPR_IF:
    case RASQAL_EXPR_SUBSTR:
      rasqal_query_write_sparql_expression_op(wc, iostr, e);
      raptor_iostream_counted_string_write("( ", 2, iostr);
      rasqal_query_write_sparql_expression(wc, iostr, e->arg1);
      raptor_iostream_counted_string_write(", ", 2, iostr);
      rasqal_query_write_sparql_expression(wc, iostr, e->arg2);
      if((e->op == RASQAL_EXPR_REGEX || e->op == RASQAL_EXPR_IF ||
          e->op == RASQAL_EXPR_SUBSTR) && e->arg3) {
        raptor_iostream_counted_string_write(", ", 2, iostr);
        rasqal_query_write_sparql_expression(wc, iostr, e->arg3);
      }
      raptor_iostream_counted_string_write(" )", 2, iostr);
      break;

    case RASQAL_EXPR_TILDE:
    case RASQAL_EXPR_BANG:
    case RASQAL_EXPR_UMINUS:
      rasqal_query_write_sparql_expression_op(wc, iostr, e);
      raptor_iostream_counted_string_write("( ", 2, iostr);
      rasqal_query_write_sparql_expression(wc, iostr, e->arg1);
      raptor_iostream_counted_string_write(" )", 2, iostr);
      break;

    case RASQAL_EXPR_LITERAL:
      rasqal_query_write_sparql_literal(wc, iostr, e->literal);
      break;

    case RASQAL_EXPR_FUNCTION:
      raptor_uri_write(e->name, iostr);
      raptor_iostream_counted_string_write("( ", 2, iostr);
      if(e->flags & RASQAL_EXPR_FLAG_DISTINCT)
        raptor_iostream_counted_string_write(" DISTINCT ", 10, iostr);
      count = raptor_sequence_size(e->args);
      for(i = 0; i < count ; i++) {
        rasqal_expression* arg;
        arg = (rasqal_expression*)raptor_sequence_get_at(e->args, i);
        if(i > 0)
          raptor_iostream_counted_string_write(", ", 2, iostr);
        rasqal_query_write_sparql_expression(wc, iostr, arg);
      }
      raptor_iostream_counted_string_write(" )", 2, iostr);
      break;

    case RASQAL_EXPR_CAST:
      raptor_uri_write(e->name, iostr);
      raptor_iostream_counted_string_write("( ", 2, iostr);
      rasqal_query_write_sparql_expression(wc, iostr, e->arg1);
      raptor_iostream_counted_string_write(" )", 2, iostr);
      break;

    case RASQAL_EXPR_VARSTAR:
      raptor_iostream_write_byte('*', iostr);
      break;
      
    case RASQAL_EXPR_COALESCE:
    case RASQAL_EXPR_CONCAT:
      rasqal_query_write_sparql_expression_op(wc, iostr, e);
      raptor_iostream_counted_string_write("( ", 2, iostr);
      count = raptor_sequence_size(e->args);
      for(i = 0; i < count ; i++) {
        rasqal_expression* e2;
        e2 = (rasqal_expression*)raptor_sequence_get_at(e->args, i);
        if(i > 0)
          raptor_iostream_counted_string_write(", ", 2, iostr);
        rasqal_query_write_sparql_expression(wc, iostr, e2);
      }
      raptor_iostream_counted_string_write(" )", 2, iostr);
      break;

    case RASQAL_EXPR_GROUP_CONCAT:
      raptor_iostream_counted_string_write("GROUP_CONCAT( ", 14, iostr);
      if(e->flags & RASQAL_EXPR_FLAG_DISTINCT)
        raptor_iostream_counted_string_write("DISTINCT ", 9, iostr);

      count = raptor_sequence_size(e->args);
      for(i = 0; i < count ; i++) {
        rasqal_expression* arg;
        arg = (rasqal_expression*)raptor_sequence_get_at(e->args, i);
        if(i > 0)
          raptor_iostream_counted_string_write(", ", 2, iostr);
        rasqal_query_write_sparql_expression(wc, iostr, arg);
      }

      if(e->literal) {
        raptor_iostream_counted_string_write(" ; SEPARATOR = ", 15, iostr);
        rasqal_query_write_sparql_literal(wc, iostr, e->literal);
      }
      
      raptor_iostream_counted_string_write(" )", 2, iostr);
      break;

    case RASQAL_EXPR_IN:
    case RASQAL_EXPR_NOT_IN:
      rasqal_query_write_sparql_expression(wc, iostr, e->arg1);
      raptor_iostream_write_byte(' ', iostr);
      rasqal_query_write_sparql_expression_op(wc, iostr, e);
      raptor_iostream_counted_string_write(" (", 2, iostr);
      count = raptor_sequence_size(e->args);
      for(i = 0; i < count ; i++) {
        rasqal_expression* e2;
        e2 = (rasqal_expression*)raptor_sequence_get_at(e->args, i);
        if(i > 0)
          raptor_iostream_counted_string_write(", ", 2, iostr);
        rasqal_query_write_sparql_expression(wc, iostr, e2);
      }
      raptor_iostream_counted_string_write(" )", 2, iostr);
      break;

    case RASQAL_EXPR_UNKNOWN:
    case RASQAL_EXPR_STR_MATCH:
    case RASQAL_EXPR_STR_NMATCH:
    default:
      RASQAL_FATAL2("Expression op %d cannot be written as a SPARQL expresson", e->op);
  }
}
Exemplo n.º 12
0
static int
rasqal_iostream_write_html_literal(rasqal_world* world,
                                   raptor_iostream *iostr,
                                   rasqal_literal* l)
{
  if(!l) {
    raptor_iostream_counted_string_write("<span class=\"unbound\">", 22, iostr);
    raptor_iostream_counted_string_write("unbound", 7, iostr);
  } else {
    const unsigned char* str;
    size_t len;

    switch(l->type) {
      case RASQAL_LITERAL_URI:
        str = RASQAL_GOOD_CAST(const unsigned char*, raptor_uri_as_counted_string(l->value.uri, &len));
        raptor_iostream_counted_string_write("<span class=\"uri\">", 18, iostr);
        raptor_iostream_counted_string_write("<a href=\"", 9, iostr);
        raptor_xml_escape_string_write(str, len, '"', iostr);
        raptor_iostream_counted_string_write("\">", 2, iostr);
        raptor_xml_escape_string_write(str, len, 0, iostr);
        raptor_iostream_counted_string_write("</a>", 4, iostr);
        break;

      case RASQAL_LITERAL_BLANK:
        raptor_iostream_counted_string_write("<span class=\"blank\">", 20, iostr);
        raptor_xml_escape_string_write(l->string, l->string_len, 0, iostr);
        break;

      case RASQAL_LITERAL_XSD_STRING:
      case RASQAL_LITERAL_BOOLEAN:
      case RASQAL_LITERAL_INTEGER:
      case RASQAL_LITERAL_DOUBLE:
      case RASQAL_LITERAL_STRING:
      case RASQAL_LITERAL_PATTERN:
      case RASQAL_LITERAL_QNAME:
      case RASQAL_LITERAL_FLOAT:
      case RASQAL_LITERAL_DECIMAL:
      case RASQAL_LITERAL_DATE:
      case RASQAL_LITERAL_DATETIME:
      case RASQAL_LITERAL_UDT:
      case RASQAL_LITERAL_INTEGER_SUBTYPE:
        raptor_iostream_counted_string_write("<span class=\"literal\">", 22, iostr);
        raptor_iostream_counted_string_write("<span class=\"value\"", 19, iostr);
        if(l->language) {
          str = RASQAL_GOOD_CAST(const unsigned char*, l->language);
          raptor_iostream_counted_string_write(" xml:lang=\"", 11, iostr);
          raptor_xml_escape_string_write(str, strlen(l->language), '"', iostr);
          raptor_iostream_write_byte('"', iostr);
        }
        raptor_iostream_write_byte('>', iostr);
        raptor_xml_escape_string_write(l->string, l->string_len, 0, iostr);
        raptor_iostream_counted_string_write("</span>", 7, iostr);

        if(l->datatype) {
          raptor_iostream_counted_string_write("^^&lt;<span class=\"datatype\">", 29, iostr);
          str = RASQAL_GOOD_CAST(const unsigned char*, raptor_uri_as_counted_string(l->datatype, &len));
          raptor_xml_escape_string_write(str, len, 0, iostr);
          raptor_iostream_counted_string_write("</span>&gt;", 11, iostr);
        }
        break;

      case RASQAL_LITERAL_VARIABLE:
        return rasqal_iostream_write_html_literal(world, iostr, l->value.variable->value);

      case RASQAL_LITERAL_UNKNOWN:
      default:
        rasqal_log_error_simple(world, RAPTOR_LOG_LEVEL_ERROR, NULL,
                                "Cannot turn literal type %d into HTML", 
                                l->type);
        return 1;
    }
Exemplo n.º 13
0
/*
 * rasqal_query_results_write_turtle:
 * @iostr: #raptor_iostream to write the query to
 * @results: #rasqal_query_results query results format
 * @base_uri: #raptor_uri base URI of the output format
 *
 * Write a Turtle version of the query results format to an
 * iostream in a format - INTERNAL.
 * 
 * If the writing succeeds, the query results will be exhausted.
 * 
 * Return value: non-0 on failure
 **/
static int
rasqal_query_results_write_turtle(raptor_iostream *iostr,
                                  rasqal_query_results* results,
                                  raptor_uri *base_uri)
{
  rasqal_world* world = rasqal_query_results_get_world(results);
  int i;
  int row_semicolon;
  int column_semicolon = 0;
  int size;
  
  if(!rasqal_query_results_is_bindings(results)) {
    rasqal_log_error_simple(world, RAPTOR_LOG_LEVEL_ERROR, NULL,
                            "Can only write Turtle format for variable binding results");
    return 1;
  }
  
  
  raptor_iostream_string_write("@prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .\n", iostr);
  raptor_iostream_string_write("@prefix rs:      <http://www.w3.org/2001/sw/DataAccess/tests/result-set#> .\n", iostr);
  raptor_iostream_string_write("@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n", iostr);
  raptor_iostream_write_byte('\n', iostr);

  raptor_iostream_counted_string_write("[]    rdf:type      rs:ResultSet ;\n",
                                       35, iostr);


  /* Variable Binding Results */

  for(i = 0; 1; i++) {
    const unsigned char *name;
    
    name = rasqal_query_results_get_binding_name(results, i);
    if(!name)
      break;
      
    raptor_iostream_counted_string_write("      rs:resultVariable  \"",
                                         26, iostr);
    raptor_iostream_string_write(name, iostr);
    raptor_iostream_counted_string_write("\" ;\n", 4, iostr);
  }

  size = rasqal_query_results_get_bindings_count(results);
  row_semicolon = 0;

  while(!rasqal_query_results_finished(results)) {
    if(row_semicolon)
      raptor_iostream_counted_string_write(" ;\n", 3, iostr);

    /* Result row */
    raptor_iostream_counted_string_write("      rs:solution   [ ", 22, iostr);

    column_semicolon = 0;
    for(i = 0; i < size; i++) {
      const unsigned char *name;
      rasqal_literal *l;

      name = rasqal_query_results_get_binding_name(results, i);
      l = rasqal_query_results_get_binding_value(results, i);

      if(column_semicolon)
        raptor_iostream_counted_string_write("; \n                      ",
                                             25, iostr);

      /* binding */
      raptor_iostream_counted_string_write("rs:binding    [ ", 16, iostr);

      /* only emit rs:value and rs:variable triples if there is a value */
      if(l) {
        raptor_iostream_counted_string_write("rs:variable   \"", 15, iostr);
        raptor_iostream_string_write(name, iostr);
        raptor_iostream_counted_string_write("\" ;\n                                      rs:value      ", 56, iostr);
        rasqal_literal_write_turtle(l, iostr);
        raptor_iostream_counted_string_write("\n                                    ] ", 39, iostr);
        column_semicolon = 1;
      }

    }

    /* End Result Row */
    raptor_iostream_counted_string_write("\n      ]", 8, iostr);
    row_semicolon = 1;
    
    rasqal_query_results_next(results);
  }

  raptor_iostream_counted_string_write(" .\n", 3, iostr);

  return 0;
}
Exemplo n.º 14
0
/**
 * raptor_string_python_write:
 * @string: UTF-8 string to write
 * @len: length of UTF-8 string
 * @delim: Terminating delimiter character for string (such as " or >)
 * or \0 for no escaping.
 * @flags: flags 0=N-Triples mode, 1=Turtle (allow raw UTF-8), 2=Turtle long string (allow raw UTF-8), 3=JSON
 * @iostr: #raptor_iostream to write to
 *
 * Write a UTF-8 string using Python-style escapes (N-Triples, Turtle, JSON) to an iostream.
 * 
 * Return value: non-0 on failure such as bad UTF-8 encoding.
 **/
int
raptor_string_python_write(const unsigned char *string,
                           size_t len,
                           const char delim,
                           int flags,
                           raptor_iostream *iostr)
{
  unsigned char c;
  int unichar_len;
  raptor_unichar unichar;

  if(flags < 0 || flags > 3)
    return 1;
  
  for(; (c=*string); string++, len--) {
    if((delim && c == delim && (delim == '\'' || delim == '"')) ||
       c == '\\') {
      raptor_iostream_write_byte('\\', iostr);
      raptor_iostream_write_byte(c, iostr);
      continue;
    }
    if(delim && c == delim) {
      raptor_iostream_counted_string_write("\\u", 2, iostr);
      raptor_iostream_hexadecimal_write(c, 4, iostr);
      continue;
    }
    
    if(flags != 2) {
      /* N-Triples, Turtle or JSON */

      /* Note: NTriples is ASCII */
      if(c == 0x09) {
        raptor_iostream_counted_string_write("\\t", 2, iostr);
        continue;
      } else if((flags == 3) && c == 0x08) {
        /* JSON has \b for backspace */
        raptor_iostream_counted_string_write("\\b", 2, iostr);
        continue;
      } else if(c == 0x0a) {
        raptor_iostream_counted_string_write("\\n", 2, iostr);
        continue;
      } else if((flags == 3) && c == 0x0b) {
        /* JSON has \f for formfeed */
        raptor_iostream_counted_string_write("\\f", 2, iostr);
        continue;
      } else if(c == 0x0d) {
        raptor_iostream_counted_string_write("\\r", 2, iostr);
        continue;
      } else if(c < 0x20|| c == 0x7f) {
        raptor_iostream_counted_string_write("\\u", 2, iostr);
        raptor_iostream_hexadecimal_write(c, 4, iostr);
        continue;
      } else if(c < 0x80) {
        raptor_iostream_write_byte(c, iostr);
        continue;
      }
    } else if(c < 0x80) {
      /* Turtle long string has no escapes except delim */
      raptor_iostream_write_byte(c, iostr);
      continue;
    } 
    
    /* It is unicode */
    
    unichar_len = raptor_unicode_utf8_string_get_char(string, len, NULL);
    if(unichar_len < 0 || unichar_len > (int)len)
      /* UTF-8 encoding had an error or ended in the middle of a string */
      return 1;

    if(flags >= 1 && flags <= 3) {
      /* Turtle and JSON are UTF-8 - no need to escape */
      raptor_iostream_counted_string_write(string, unichar_len, iostr);
    } else {
      unichar_len = raptor_unicode_utf8_string_get_char(string, len, &unichar);

      if(unichar < 0x10000) {
        raptor_iostream_counted_string_write("\\u", 2, iostr);
        raptor_iostream_hexadecimal_write(unichar, 4, iostr);
      } else {
        raptor_iostream_counted_string_write("\\U", 2, iostr);
        raptor_iostream_hexadecimal_write(unichar, 8, iostr);
      }
    }
    
    unichar_len--; /* since loop does len-- */
    string += unichar_len; len -= unichar_len;

  }

  return 0;
}
Exemplo n.º 15
0
/**
 * raptor_turtle_writer_raw_counted:
 * @turtle_writer: Turtle writer object
 * @s: raw string to write
 * @len: length of string
 *
 * Write a counted string to the Turtle writer verbatim.
 *
 **/
void
raptor_turtle_writer_raw_counted(raptor_turtle_writer* turtle_writer,
                                 const unsigned char *s, unsigned int len)
{
  raptor_iostream_counted_string_write(s, len, turtle_writer->iostr);
}
Exemplo n.º 16
0
/**
 * raptor_xml_element_write:
 * @element: XML element to format
 * @nstack: Namespace stack context to use in formatting
 * @is_empty: non-0 if element is empty
 * @is_end: non-0 if this is an end element (else is a start element)
 * @depth: XML element depth
 * @iostr: iostream object
 *
 * Write a formatted XML element to a #raptor_iostream
 *
 * Return value: non-0 on failure
*/
int
raptor_xml_element_write(raptor_xml_element *element,
                         raptor_namespace_stack *nstack,
                         int is_empty,
                         int is_end,
                         int depth,
                         raptor_iostream* iostr)
{
  struct nsd *nspace_declarations = NULL;
  size_t nspace_declarations_count = 0;  
  unsigned int i;

  /* max is 1 per element and 1 for each attribute + size of declared */
  if(nstack) {
    int nspace_max_count = element->attribute_count+1;
    if(element->declared_nspaces)
      nspace_max_count += raptor_sequence_size(element->declared_nspaces);
    
    nspace_declarations = RAPTOR_CALLOC(struct nsd*, nspace_max_count,
                                        sizeof(struct nsd));
  }

  if(element->name->nspace) {
    if(!is_end && nstack &&
       !raptor_namespaces_namespace_in_scope(nstack, element->name->nspace)) {
      nspace_declarations[0].declaration=
        raptor_namespace_format_as_xml(element->name->nspace,
                                       &nspace_declarations[0].length);
      nspace_declarations[0].nspace = element->name->nspace;
      nspace_declarations_count++;
    }
  }

  if(!is_end && element->attributes) {
    for(i = 0; i < element->attribute_count; i++) {
      /* qname */
      if(element->attributes[i]->nspace) {
        if(nstack && 
           !raptor_namespaces_namespace_in_scope(nstack, element->attributes[i]->nspace) && element->attributes[i]->nspace != element->name->nspace) {
          /* not in scope and not same as element (so already going to be declared)*/
          unsigned int j;
          int declare_me = 1;
          
          /* check it wasn't an earlier declaration too */
          for(j = 0; j < nspace_declarations_count; j++)
            if(nspace_declarations[j].nspace == element->attributes[j]->nspace) {
              declare_me = 0;
              break;
            }
            
          if(declare_me) {
            nspace_declarations[nspace_declarations_count].declaration=
              raptor_namespace_format_as_xml(element->attributes[i]->nspace,
                                             &nspace_declarations[nspace_declarations_count].length);
            nspace_declarations[nspace_declarations_count].nspace = element->attributes[i]->nspace;
            nspace_declarations_count++;
          }
        }

      }
    }
  }
  

  if(!is_end && nstack && element->declared_nspaces &&
     raptor_sequence_size(element->declared_nspaces) > 0) {
    for(i = 0; i< (unsigned int)raptor_sequence_size(element->declared_nspaces); i++) {
      raptor_namespace* nspace = (raptor_namespace*)raptor_sequence_get_at(element->declared_nspaces, i);
      unsigned int j;
      int declare_me = 1;
      
      /* check it wasn't an earlier declaration too */
      for(j = 0; j < nspace_declarations_count; j++)
        if(nspace_declarations[j].nspace == nspace) {
          declare_me = 0;
          break;
        }
      
      if(declare_me) {
        nspace_declarations[nspace_declarations_count].declaration=
          raptor_namespace_format_as_xml(nspace,
                                         &nspace_declarations[nspace_declarations_count].length);
        nspace_declarations[nspace_declarations_count].nspace = nspace;
        nspace_declarations_count++;
      }

    }
  }



  raptor_iostream_write_byte('<', iostr);
  if(is_end)
    raptor_iostream_write_byte('/', iostr);

  if(element->name->nspace && element->name->nspace->prefix_length > 0) {
    raptor_iostream_counted_string_write((const char*)element->name->nspace->prefix, 
                                         element->name->nspace->prefix_length,
                                         iostr);
    raptor_iostream_write_byte(':', iostr);
  }
  raptor_iostream_counted_string_write((const char*)element->name->local_name,
                                       element->name->local_name_length,
                                       iostr);

  /* declare namespaces */
  if(nspace_declarations_count) {
    /* sort them into the canonical order */
    qsort((void*)nspace_declarations, 
          nspace_declarations_count, sizeof(struct nsd),
          raptor_nsd_compare);
    /* add them */
    for(i = 0; i < nspace_declarations_count; i++) {
      raptor_iostream_write_byte(' ', iostr);
      raptor_iostream_counted_string_write((const char*)nspace_declarations[i].declaration,
                                           nspace_declarations[i].length,
                                           iostr);
      RAPTOR_FREE(char*, nspace_declarations[i].declaration);
      nspace_declarations[i].declaration = NULL;

      raptor_namespace_stack_start_namespace(nstack,
                                             (raptor_namespace*)nspace_declarations[i].nspace,
                                             depth);
    }
  }


  if(!is_end && element->attributes) {
    for(i = 0; i < element->attribute_count; i++) {
      raptor_iostream_write_byte(' ', iostr);
      
      if(element->attributes[i]->nspace && 
         element->attributes[i]->nspace->prefix_length > 0) {
        raptor_iostream_counted_string_write((char*)element->attributes[i]->nspace->prefix,
                                             element->attributes[i]->nspace->prefix_length,
                                             iostr);
        raptor_iostream_write_byte(':', iostr);
      }

      raptor_iostream_counted_string_write((const char*)element->attributes[i]->local_name,
                                           element->attributes[i]->local_name_length,
                                           iostr);
      
      raptor_iostream_counted_string_write("=\"", 2, iostr);
      
      raptor_xml_escape_string_write(element->attributes[i]->value, 
                                      element->attributes[i]->value_length,
                                      '"',
                                      iostr);
      raptor_iostream_write_byte('"', iostr);
    }
  }
  
  if(is_empty)
    raptor_iostream_write_byte('/', iostr);

  raptor_iostream_write_byte('>', iostr);

  if(nstack)
    RAPTOR_FREE(stringarray, nspace_declarations);

  return 0;
}