Пример #1
0
/**
 * raptor_turtle_writer_quoted_counted_string:
 * @turtle_writer: Turtle writer object
 * @s: string to write
 * @len: string length
 *
 * Write a Turtle escaped-string inside double quotes to the writer.
 *
 * Return value: non-0 on failure
 **/
int
raptor_turtle_writer_quoted_counted_string(raptor_turtle_writer* turtle_writer,
                                           const unsigned char *s, size_t len)
{
  const unsigned char *quotes = (const unsigned char *)"\"\"\"\"";
  const unsigned char *q;
  size_t q_len;
  int flags;
  int rc = 0;

  if(!s)
    return 1;
  
  /* Turtle """longstring""" (2) or "string" (1) */
  flags = raptor_turtle_writer_contains_newline(s) ? 2 : 1;
  q = (flags == 2) ? quotes : quotes + 2;
  q_len = (q == quotes) ? 3 : 1;
  raptor_iostream_counted_string_write(q, q_len, turtle_writer->iostr);
  rc = raptor_string_python_write(s, strlen((const char*)s), '"', flags,
                                  turtle_writer->iostr);
  raptor_iostream_counted_string_write(q, q_len, turtle_writer->iostr);

  return rc;
}
Пример #2
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;
}