Exemplo n.º 1
0
char* rdfa_resolve_curie(
   rdfacontext* context, const char* uri, curieparse_t mode)
{
   char* rval = NULL;
   curie_t ctype = rdfa_get_curie_type(uri);

   if(!uri)
      return NULL;

   if(ctype == CURIE_TYPE_INVALID)
   {
      rval = NULL;
   }
   else if((ctype == CURIE_TYPE_IRI_OR_UNSAFE) &&
           ((mode == CURIE_PARSE_HREF_SRC) ||
            (context->rdfa_version == RDFA_VERSION_1_0 &&
               mode == CURIE_PARSE_ABOUT_RESOURCE)))
   {
      /* If we are parsing something that can take either a CURIE or a
       * URI, and the type is either IRI or UNSAFE, assume that it is
       * an IRI */
      rval = rdfa_resolve_uri(context, uri);
   }

   /*
    * Check to see if the value is a term.
    */
   if(ctype == CURIE_TYPE_IRI_OR_UNSAFE && mode == CURIE_PARSE_PROPERTY)
   {
      const char* term_iri;
      term_iri = (const char*)rdfa_get_mapping(context->term_mappings, uri);
      if(term_iri != NULL)
      {
         rval = strdup(term_iri);
      }
      else if(context->default_vocabulary == NULL && strstr(uri, ":") == NULL)
      {
         /* Generate the processor warning if this is a missing term */
#define FORMAT_1 "The use of the '%s' term was unrecognized by the RDFa processor because it is not a valid term for the current Host Language."

#ifdef LIBRDFA_IN_RAPTOR
         raptor_parser_warning((raptor_parser*)context->callback_data, 
                               FORMAT_1, uri);
#else
         char msg[1024];
         snprintf(msg, 1024, FORMAT_1, uri);

         rdfa_processor_triples(context, RDFA_PROCESSOR_WARNING, msg);
#endif
      }
   }

   /* if we are processing a safe CURIE OR
    * if we are parsing an unsafe CURIE that is an @type_of,
    * @datatype, @property, @rel, or @rev attribute, treat the curie
    * as not an IRI, but an unsafe CURIE */
   if(rval == NULL && ((ctype == CURIE_TYPE_SAFE) ||
         ((ctype == CURIE_TYPE_IRI_OR_UNSAFE) &&
          ((mode == CURIE_PARSE_INSTANCEOF_DATATYPE) ||
           (mode == CURIE_PARSE_PROPERTY) ||
           (mode == CURIE_PARSE_RELREV) ||
           (context->rdfa_version == RDFA_VERSION_1_1 &&
              mode == CURIE_PARSE_ABOUT_RESOURCE)))))
   {
      char* working_copy = NULL;
      char* wcptr = NULL;
      char* prefix = NULL;
      char* curie_reference = NULL;
      const char* expanded_prefix = NULL;
      size_t uri_len = strlen(uri);
      working_copy = (char*)malloc(uri_len + 1);
      memcpy(working_copy, uri, uri_len + 1);/*rdfa_replace_string(working_copy, uri);*/

      /* if this is a safe CURIE, chop off the beginning and the end */
      if(ctype == CURIE_TYPE_SAFE)
      {
         prefix = strtok_r(working_copy, "[:]", &wcptr);
         if(wcptr)
            curie_reference = strtok_r(NULL, "[]", &wcptr);
      }
      else if(ctype == CURIE_TYPE_IRI_OR_UNSAFE)
      {
         prefix = strtok_r(working_copy, ":", &wcptr);
         if(wcptr)
            curie_reference = strtok_r(NULL, "", &wcptr);
      }

      /* fully resolve the prefix and get its length */

      /* if a colon was found, but no prefix, use the XHTML vocabulary URI
       * as the expanded prefix */
      if((uri[0] == ':') || (strcmp(uri, "[:]") == 0))
      {
         expanded_prefix = XHTML_VOCAB_URI;
         curie_reference = prefix;
         prefix = NULL;
      }
      else if(uri[0] == ':')
      {
         /* FIXME: This looks like a bug - don't know why this code is
          * in here. I think it's for the case where ":next" is
          * specified, but the code's not checking that -- manu */
         expanded_prefix = context->base;
         curie_reference = prefix;
         prefix = NULL;
      }
      else if(prefix != NULL)
      {
         if((mode != CURIE_PARSE_PROPERTY) &&
            (mode != CURIE_PARSE_RELREV) &&
            strcmp(prefix, "_") == 0)
         {
            /* if the prefix specifies this as a blank node, then we
             * use the blank node prefix */
            expanded_prefix = "_";
         }
         else
         {
            /* if the prefix was defined, get it from the set of URI mappings. */
#ifdef LIBRDFA_IN_RAPTOR
            if(!strcmp(prefix, "xml"))
            {
               expanded_prefix = RAPTOR_GOOD_CAST(const char*, raptor_xml_namespace_uri);
            }
            else
            {
               raptor_namespace *nspace;
               raptor_uri* ns_uri;
               nspace = raptor_namespaces_find_namespace(&context->sax2->namespaces,
                                                         (const unsigned char*)prefix,
                                                         (int)strlen(prefix));
               if(nspace) {
                  ns_uri = raptor_namespace_get_uri(nspace);
                  if(ns_uri)
                     expanded_prefix = (const char*)raptor_uri_as_string(ns_uri);
               }
            }
#else
            expanded_prefix =
               rdfa_get_mapping(context->uri_mappings, prefix);

            /* Generate the processor warning if the prefix was not found */
            if(expanded_prefix == NULL && strstr(uri, ":") != NULL &&
               strstr(uri, "://") == NULL)
            {
#define FORMAT_2 "The '%s' prefix was not found. You may want to check that it is declared before it is used, or that it is a valid prefix string."
#ifdef LIBRDFA_IN_RAPTOR
              raptor_parser_warning((raptor_parser*)context->callback_data, 
                                    FORMAT_2, prefix);
#else
              char msg[1024];
              snprintf(msg, 1024, FORMAT_2, prefix);

               rdfa_processor_triples(context, RDFA_PROCESSOR_WARNING, msg);
#endif
            }
#endif
         }
Exemplo n.º 2
0
char* rdfa_resolve_curie(
   rdfacontext* context, const char* uri, curieparse_t mode)
{
   char* rval = NULL;
   curie_t ctype = rdfa_get_curie_type(uri);

   if(ctype == CURIE_TYPE_INVALID)
   {
      rval = NULL;
   }
   else if((ctype == CURIE_TYPE_IRI_OR_UNSAFE) &&
           ((mode == CURIE_PARSE_HREF_SRC) ||
            (mode == CURIE_PARSE_ABOUT_RESOURCE)))
   {
      // If we are parsing something that can take either a CURIE or a
      // URI, and the type is either IRI or UNSAFE, assume that it is
      // an IRI
      rval = rdfa_resolve_uri(context, uri);
   }

   // if we are processing a safe CURIE OR
   // if we are parsing an unsafe CURIE that is an @type_of,
   // @datatype, @property, @rel, or @rev attribute, treat the curie
   // as not an IRI, but an unsafe CURIE
   if((ctype == CURIE_TYPE_SAFE) ||
         ((ctype == CURIE_TYPE_IRI_OR_UNSAFE) &&
          ((mode == CURIE_PARSE_INSTANCEOF_DATATYPE) ||
           (mode == CURIE_PARSE_PROPERTY) ||
           (mode == CURIE_PARSE_RELREV))))
   {
      char* working_copy = NULL;
      char* wcptr = NULL;
      char* prefix = NULL;
      char* curie_reference = NULL;
      const char* expanded_prefix = NULL;

      working_copy = (char*)malloc(strlen(uri) + 1);
      strcpy(working_copy, uri);//rdfa_replace_string(working_copy, uri);

      // if this is a safe CURIE, chop off the beginning and the end
      if(ctype == CURIE_TYPE_SAFE)
      {
         prefix = strtok_r(working_copy, "[:]", &wcptr);
         if(wcptr)
            curie_reference = strtok_r(NULL, "[:]", &wcptr);
      }
      else if(ctype == CURIE_TYPE_IRI_OR_UNSAFE)
      {
         prefix = strtok_r(working_copy, ":", &wcptr);
         if(wcptr)
            curie_reference = strtok_r(NULL, ":", &wcptr);
      }

      // fully resolve the prefix and get it's length

      // if a colon was found, but no prefix, use the XHTML vocabulary URI
      // as the expanded prefix 
      if((uri[0] == ':') || (strcmp(uri, "[:]") == 0))
      {
         expanded_prefix = XHTML_VOCAB_URI;
         curie_reference = prefix;
         prefix = NULL;
      }
      else if(uri[0] == ':')
      {
         // FIXME: This looks like a bug - don't know why this code is
         // in here. I think it's for the case where ":next" is
         // specified, but the code's not checking that -- manu
         expanded_prefix = context->base;
         curie_reference = prefix;
         prefix = NULL;
      }
      else if(prefix != NULL)
      {
         if(strcmp(prefix, "_") == 0)
         {
            // if the prefix specifies this as a blank node, then we
            // use the blank node prefix
            expanded_prefix = "_";
         }
         //else if(strcmp(prefix, "rdf") == 0)
         //{
         //   expanded_prefix = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
         //}
         else
         {
            // if the prefix was defined, get it from the set of URI mappings.
#ifdef LIBRDFA_IN_RAPTOR
            raptor_namespace *nspace;
            raptor_uri* ns_uri;
            nspace = raptor_namespaces_find_namespace(&context->sax2->namespaces,
                                                      (const unsigned char*)prefix, strlen(prefix));
            if(nspace) {
               ns_uri = raptor_namespace_get_uri(nspace);
               if(ns_uri)
                  expanded_prefix = (const char*)raptor_uri_as_string(ns_uri);
            }
#else
            expanded_prefix =
               rdfa_get_mapping(context->uri_mappings, prefix);
#endif
         }
      }

      if((expanded_prefix != NULL) && (curie_reference != NULL))
      {
         // if the expanded prefix and the reference exist, generate the
         // full IRI.
         if(strcmp(expanded_prefix, "_") == 0)
         {
            rval = rdfa_join_string("_:", curie_reference);
         }
         else
         {
            rval = rdfa_join_string(expanded_prefix, curie_reference);
         }
      }
      else if((expanded_prefix != NULL) && (expanded_prefix[0] != '_') && 
         (curie_reference == NULL))
      {
         // if the expanded prefix exists, but the reference is null, 
	 // generate the CURIE because a reference-less CURIE is still
         // valid
 	 rval = rdfa_join_string(expanded_prefix, "");
      }

      free(working_copy);
   }

   // if we're NULL at this point, the CURIE might be the special
   // unnamed bnode specified by _:
   if((rval == NULL) &&
      ((strcmp(uri, "[_:]") == 0) ||
       (strcmp(uri, "_:") == 0)))
   {
      if(context->underscore_colon_bnode_name == NULL)
      {
         context->underscore_colon_bnode_name = rdfa_create_bnode(context);
      }
      rval = rdfa_replace_string(rval, context->underscore_colon_bnode_name);
   }
   
   // even though a reference-only CURIE is valid, it does not
   // generate a triple in XHTML+RDFa. If we're NULL at this point,
   // the given value wasn't valid in XHTML+RDFa.
   
   return rval;
}