/* end a serialize */
static int
raptor_dot_serializer_end(raptor_serializer* serializer)
{
  raptor_dot_context* context=(raptor_dot_context*)serializer->context;
  raptor_dot_serializer_node* node;
  int i;

  /* Print our nodes. */
  raptor_iostream_write_string(serializer->iostream,
                               (const unsigned char *)"\n\t// Resources\n");
  for( i = 0 ; i < raptor_sequence_size(context->resources) ; i++ ) {
    node = (raptor_dot_serializer_node*)raptor_sequence_get_at(context->resources, i);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\t\"R");
    raptor_dot_serializer_write_node(serializer, node->value.resource.uri,
				     RAPTOR_IDENTIFIER_TYPE_RESOURCE, NULL, NULL);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\" [ label=\"");
    raptor_dot_serializer_write_node(serializer, node->value.resource.uri,
				     RAPTOR_IDENTIFIER_TYPE_RESOURCE, NULL, NULL);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\", shape=ellipse");
    raptor_dot_serializer_write_colors(serializer,
                                       RAPTOR_IDENTIFIER_TYPE_RESOURCE);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)" ];\n");
    
  }
  raptor_free_sequence(context->resources);

  raptor_iostream_write_string(serializer->iostream,
                               (const unsigned char *)"\n\t// Anonymous nodes\n");
  for( i = 0 ; i < raptor_sequence_size(context->bnodes) ; i++ ) {
    node = (raptor_dot_serializer_node *)raptor_sequence_get_at(context->bnodes, i);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\t\"B");
    raptor_dot_serializer_write_node(serializer, node->value.resource.uri,
				   RAPTOR_IDENTIFIER_TYPE_ANONYMOUS, NULL, NULL);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\" [ label=\"");
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\", shape=circle");
    raptor_dot_serializer_write_colors(serializer,
                                       RAPTOR_IDENTIFIER_TYPE_ANONYMOUS);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)" ];\n");
  }
  raptor_free_sequence(context->bnodes);

  raptor_iostream_write_string(serializer->iostream,
                               (const unsigned char *)"\n\t// Literals\n");
  for( i = 0 ; i < raptor_sequence_size(context->literals) ; i++ ) {
    node = (raptor_dot_serializer_node *)raptor_sequence_get_at(context->literals, i);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\t\"L");
    raptor_dot_serializer_write_node(serializer, node->value.literal.string,
				     RAPTOR_IDENTIFIER_TYPE_LITERAL,
                                     node->value.literal.datatype,
				     node->value.literal.language);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\" [ label=\"");
    raptor_dot_serializer_write_node(serializer, node->value.literal.string,
				     RAPTOR_IDENTIFIER_TYPE_LITERAL,
                                     node->value.literal.datatype,
				     node->value.literal.language);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\", shape=record");
    raptor_dot_serializer_write_colors(serializer,
                                       RAPTOR_IDENTIFIER_TYPE_LITERAL);
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)" ];\n");
  }
  raptor_free_sequence(context->literals);

  raptor_iostream_write_string(serializer->iostream,
                               (const unsigned char *)"\n\tlabel=\"\\n\\nModel:\\n");
  if(serializer->base_uri)
    raptor_iostream_write_string(serializer->iostream,
                                 raptor_uri_as_string(serializer->base_uri));
  else
    raptor_iostream_write_string(serializer->iostream, "(Unknown)");

  if(raptor_sequence_size(context->namespaces)) {
    raptor_iostream_write_string(serializer->iostream,
                                 (const unsigned char *)"\\n\\nNamespaces:\\n");

    for( i = 0 ; i < raptor_sequence_size(context->namespaces) ; i++ ) {
      raptor_namespace* ns =
	(raptor_namespace*)raptor_sequence_get_at(context->namespaces, i);
      const unsigned char* prefix = raptor_namespace_get_prefix(ns);

      if(prefix) {
	raptor_iostream_write_string(serializer->iostream,
				     (const unsigned char *)ns->prefix);
	raptor_iostream_write_string(serializer->iostream,
				     (const unsigned char *)": ");
      }
      raptor_iostream_write_string(serializer->iostream,
				   raptor_uri_as_string(ns->uri));
      raptor_iostream_write_string(serializer->iostream,
				   (const unsigned char *)"\\n");
    }

    raptor_free_sequence(context->namespaces);
  }

  raptor_iostream_write_string(serializer->iostream,
                               (const unsigned char *)"\";\n");

  raptor_iostream_write_string(serializer->iostream,
                               (const unsigned char *) "}\n");

  return 0;
}
/* end a serialize */
static int
raptor_dot_serializer_end(raptor_serializer* serializer)
{
  raptor_dot_context* context = (raptor_dot_context*)serializer->context;
  raptor_term* node;
  int i;

  /* Print our nodes. */
  raptor_iostream_string_write((const unsigned char*)"\n\t// Resources\n",
                               serializer->iostream);
  for(i = 0 ; i < raptor_sequence_size(context->resources) ; i++ ) {
    node = (raptor_term*)raptor_sequence_get_at(context->resources, i);
    raptor_iostream_string_write((const unsigned char*)"\t\"R",
                                 serializer->iostream);
    raptor_dot_serializer_write_term(serializer, node);
    raptor_iostream_string_write((const unsigned char*)"\" [ label=\"",
                                 serializer->iostream);
    raptor_dot_serializer_write_term(serializer, node);
    raptor_iostream_string_write((const unsigned char*)"\", shape = ellipse",
                                 serializer->iostream);
    raptor_dot_serializer_write_colors(serializer, RAPTOR_TERM_TYPE_URI);
    raptor_iostream_string_write((const unsigned char*)" ];\n",
                                 serializer->iostream);
    
  }
  raptor_free_sequence(context->resources);

  raptor_iostream_string_write((const unsigned char*)"\n\t// Anonymous nodes\n",
                               serializer->iostream);
  for(i = 0 ; i < raptor_sequence_size(context->bnodes) ; i++ ) {
    node = (raptor_term *)raptor_sequence_get_at(context->bnodes, i);
    raptor_iostream_string_write((const unsigned char*)"\t\"B",
                                 serializer->iostream);
    raptor_dot_serializer_write_term(serializer, node);
    raptor_iostream_string_write((const unsigned char*)"\" [ label=\"",
                                 serializer->iostream);
    raptor_iostream_string_write((const unsigned char*)"\", shape = circle",
                                 serializer->iostream);
    raptor_dot_serializer_write_colors(serializer, RAPTOR_TERM_TYPE_BLANK);
    raptor_iostream_string_write((const unsigned char*)" ];\n",
                                 serializer->iostream);
  }
  raptor_free_sequence(context->bnodes);

  raptor_iostream_string_write((const unsigned char*)"\n\t// Literals\n",
                               serializer->iostream);
  for(i = 0 ; i < raptor_sequence_size(context->literals) ; i++ ) {
    node = (raptor_term*)raptor_sequence_get_at(context->literals, i);
    raptor_iostream_string_write((const unsigned char*)"\t\"L",
                                 serializer->iostream);
    raptor_dot_serializer_write_term(serializer, node);
    raptor_iostream_string_write((const unsigned char*)"\" [ label=\"",
                                 serializer->iostream);
    raptor_dot_serializer_write_term(serializer, node);
    raptor_iostream_string_write((const unsigned char*)"\", shape = record",
                                 serializer->iostream);
    raptor_dot_serializer_write_colors(serializer, RAPTOR_TERM_TYPE_LITERAL);
    raptor_iostream_string_write((const unsigned char*)" ];\n",
                                 serializer->iostream);
  }
  raptor_free_sequence(context->literals);

  raptor_iostream_string_write((const unsigned char*)"\n\tlabel=\"\\n\\nModel:\\n",
                               serializer->iostream);
  if(serializer->base_uri)
    raptor_iostream_string_write(raptor_uri_as_string(serializer->base_uri),
                                 serializer->iostream);
  else
    raptor_iostream_string_write("(Unknown)", serializer->iostream);

  if(raptor_sequence_size(context->namespaces)) {
    raptor_iostream_string_write((const unsigned char*)"\\n\\nNamespaces:\\n",
                                 serializer->iostream);

    for(i = 0 ; i < raptor_sequence_size(context->namespaces) ; i++ ) {
      raptor_namespace* ns;
      const unsigned char* prefix;

      ns = (raptor_namespace*)raptor_sequence_get_at(context->namespaces, i);

      prefix = raptor_namespace_get_prefix(ns);
      if(prefix) {
        raptor_iostream_string_write((const unsigned char*)ns->prefix,
                                     serializer->iostream);
        raptor_iostream_string_write((const unsigned char*)": ",
                                     serializer->iostream);
      }
      raptor_iostream_string_write(raptor_uri_as_string(ns->uri),
                                   serializer->iostream);
      raptor_iostream_string_write((const unsigned char*)"\\n",
                                   serializer->iostream);
    }

    raptor_free_sequence(context->namespaces);
  }

  raptor_iostream_string_write((const unsigned char*)"\";\n",
                               serializer->iostream);

  raptor_iostream_string_write((const unsigned char*) "}\n",
                               serializer->iostream);

  return 0;
}