示例#1
0
static void
rasqal_sparql_xml_sax2_start_element_handler(void *user_data,
                                             raptor_xml_element *xml_element)
{
  rasqal_rowsource_sparql_xml_context* con;
  int i;
  raptor_qname* name;
  rasqal_sparql_xml_read_state state=STATE_unknown;
  int attr_count;

  con=(rasqal_rowsource_sparql_xml_context*)user_data;

  name=raptor_xml_element_get_name(xml_element);

  for(i=STATE_first; i <= STATE_last; i++) {
    if(!strcmp((const char*)raptor_qname_get_local_name(name),
               sparql_xml_element_names[i])) {
      state=(rasqal_sparql_xml_read_state)i;
      con->state=state;
    }
  }

  if(state == STATE_unknown) {
    fprintf(stderr, "UNKNOWN element %s\n", raptor_qname_get_local_name(name));
    con->failed++;
  }

#ifdef TRACE_XML
  if(con->trace) {
    pad(stderr, con->depth);
    fprintf(stderr, "Element %s (%d)\n", raptor_qname_get_local_name(name),
            state);
  }
#endif
  
  attr_count=raptor_xml_element_get_attributes_count(xml_element);
  con->name=NULL;
  con->datatype=NULL;
  con->language=NULL;
  
  if(attr_count > 0) {
    raptor_qname** attrs=raptor_xml_element_get_attributes(xml_element);
    for(i=0; i < attr_count; i++) {
#ifdef TRACE_XML
      if(con->trace) {
        pad(stderr, con->depth+1);
        fprintf(stderr, "Attribute %s='%s'\n",
                raptor_qname_get_local_name(attrs[i]),
                raptor_qname_get_value(attrs[i]));
      }
#endif
      if(!strcmp((const char*)raptor_qname_get_local_name(attrs[i]),
                 "name"))
        con->name=(const char*)raptor_qname_get_counted_value(attrs[i],
                                                             &con->name_length);
      else if(!strcmp((const char*)raptor_qname_get_local_name(attrs[i]),
                      "datatype"))
        con->datatype=(const char*)raptor_qname_get_value(attrs[i]);
    }
  }
  if(raptor_xml_element_get_language(xml_element)) {
    con->language=(const char*)raptor_xml_element_get_language(xml_element);
#ifdef TRACE_XML
    if(con->trace) {
      pad(stderr, con->depth+1);
      fprintf(stderr, "xml:lang '%s'\n", con->language);
    }
#endif
  }

  switch(state) {
    case STATE_variable:
      if(con->name) {
        unsigned char* var_name;
        rasqal_variable *v;
        
        var_name = (unsigned char*)RASQAL_MALLOC(cstring, con->name_length+1);
        memcpy(var_name, con->name, con->name_length + 1);

        v = rasqal_variables_table_add(con->vars_table,
                                       RASQAL_VARIABLE_TYPE_NORMAL,
                                       var_name, NULL);
        if(v)
          rasqal_rowsource_add_variable(con->rowsource, v);
      }
      break;
      
    case STATE_result:
      if(1) {
        con->row = rasqal_new_row(con->rowsource);
        RASQAL_DEBUG2("Made new row %d\n", con->offset);
        con->offset++;
      }
      break;
      
    case STATE_binding:
      con->result_offset = rasqal_rowsource_get_variable_offset_by_name(con->rowsource, (const unsigned char*)con->name);
      break;
      
    case STATE_sparql:
    case STATE_head:
    case STATE_results:
    case STATE_literal:
    case STATE_bnode:
    case STATE_uri:
    case STATE_unknown:
    default:
      break;
  }
  
  con->depth++;
}
示例#2
0
文件: rdfa.c 项目: Distrotech/raptor
static void raptor_rdfa_start_element(void *user_data,
                                      raptor_xml_element *xml_element)
{
  raptor_qname* qname = raptor_xml_element_get_name(xml_element);
  int nb_attributes = raptor_xml_element_get_attributes_count(xml_element);
  raptor_qname** attrs = raptor_xml_element_get_attributes(xml_element);
  unsigned char* localname = raptor_qname_to_counted_name(qname, NULL);
  const raptor_namespace* qname_ns = raptor_qname_get_namespace(qname);
  int nb_namespaces = 0;
  const char** namespaces = NULL;
  int nb_defaulted = 0;
  char** attr = NULL;
  int i;
  const char* ns_name = NULL;
  const char* ns_uri = NULL;

  if(nb_attributes > 0) {
    /* Everything written into 'attr' is a shared pointer into
     * xml_element or contained objects - qnames, namespaces, uris
     * and values
     */
    attr = (char**)malloc(sizeof(char*) * (1 + (nb_attributes * 5)));
    for(i = 0; i < nb_attributes; i++) {
      const raptor_namespace* attr_ns = attrs[i]->nspace;
      char** attri = &attr[5 * i];
      /* 5 tuple: (localname, prefix, URI, value, end) */
      attri[0] = (char*)attrs[i]->local_name;
      attri[1] = attr_ns ? (char*)attr_ns->prefix : NULL;
      attri[2] = attr_ns ? (char*)raptor_uri_as_string(attr_ns->uri) : NULL;
      attri[3] = (char*)attrs[i]->value;
      attri[4] = attri[3] + attrs[i]->value_length;
    }
    attr[5 * i] = NULL;
  }

/*
 * @ctx:  the user data (XML parser context)
 * @localname:  the local name of the element
 * @prefix:  the element namespace prefix if available
 * @URI:  the element namespace name if available
 * @nb_namespaces:  number of namespace definitions on that node
 * @namespaces:  pointer to the array of prefix/URI pairs namespace definitions
 * @nb_attributes:  the number of attributes on that node
 * @nb_defaulted:  the number of defaulted attributes. The defaulted
 *                  ones are at the end of the array
 * @attributes:  pointer to the array of (localname/prefix/URI/value/end)
 *               attribute values.
 */
  if(qname_ns) {
    ns_name = (const char*)raptor_namespace_get_prefix(qname_ns);
    ns_uri = (const char*)raptor_uri_as_string(qname_ns->uri);
  }

  start_element(user_data, (const char*)localname,
                ns_name,
                ns_uri,
                nb_namespaces,
                (const char**)namespaces,
                nb_attributes,
                nb_defaulted,
                (const char**)attr);
  if(attr)
    free(attr);
  raptor_free_memory(localname);
}