Redland
view release on metacpan or search on metacpan
redland/raptor/src/raptor_sax2.c view on Meta::CPAN
#endif
#ifdef RAPTOR_XML_LIBXML
xmlParseChunk(xc, (char*)buffer, 0, 1);
#endif
return 0;
}
#ifdef RAPTOR_XML_EXPAT
rc=XML_Parse(xp, (char*)buffer, len, is_end);
if(!rc) /* expat: 0 is failure */
goto handle_error;
if(is_end)
return 0;
#endif
#ifdef RAPTOR_XML_LIBXML
/* This works around some libxml versions that fail to work
* if the buffer size is larger than the entire file
* and thus the entire parsing is done in one operation.
*
* The code below:
* 2.4.19 (oldest tested) to 2.4.24 - required
* 2.4.25 - works with or without it
* 2.4.26 or later - fails with this code
*/
#if LIBXML_VERSION < 20425
if(sax2->first_read && is_end) {
/* parse all but the last character */
rc=xmlParseChunk(xc, (char*)buffer, len-1, 0);
if(rc)
goto handle_error;
/* last character */
rc=xmlParseChunk(xc, (char*)buffer + (len-1), 1, 0);
if(rc)
goto handle_error;
/* end */
xmlParseChunk(xc, (char*)buffer, 0, 1);
return 0;
}
#endif
#if LIBXML_VERSION < 20425
sax2->first_read=0;
#endif
rc=xmlParseChunk(xc, (char*)buffer, len, is_end);
if(rc) /* libxml: non 0 is failure */
goto handle_error;
if(is_end)
return 0;
#endif
return 0;
handle_error:
#ifdef RAPTOR_XML_EXPAT
#ifdef EXPAT_UTF8_BOM_CRASH
if(sax2->tokens_count) {
#endif
/* Work around a bug with the expat 1.95.1 shipped with RedHat 7.2
* which dies here if the error is before <?xml?...
* The expat 1.95.1 source release version works fine.
*/
if(sax2->locator)
raptor_sax2_update_document_locator(sax2, sax2->locator);
#ifdef EXPAT_UTF8_BOM_CRASH
}
#endif
#endif /* EXPAT */
#if RAPTOR_XML_EXPAT
if(1) {
const char *error_prefix="XML Parsing failed - "; /* 21 chars */
#define ERROR_PREFIX_LEN 21
const char *error_message=XML_ErrorString(XML_GetErrorCode(xp));
size_t error_length;
char *error_buffer;
error_length=strlen(error_message);
error_buffer=(char*)RAPTOR_MALLOC(cstring,
ERROR_PREFIX_LEN + error_length+1);
if(error_buffer) {
strncpy(error_buffer, error_prefix, ERROR_PREFIX_LEN);
strncpy(error_buffer+ERROR_PREFIX_LEN, error_message, error_length+1);
sax2->error_handler(sax2->error_data, sax2->locator, error_buffer);
RAPTOR_FREE(cstring, error_buffer);
} else
sax2->error_handler(sax2->error_data, sax2->locator, "XML Parsing failed");
}
#endif
#ifdef RAPTOR_XML_LIBXML
sax2->error_handler(sax2->error_data, sax2->locator, "XML Parsing failed");
#endif
return 1;
}
/**
* raptor_sax2_set_feature:
* @sax2: #raptor_sax2 SAX2 object
* @feature: feature to set from enumerated #raptor_feature values
* @value: integer feature value (0 or larger)
*
* Set various SAX2 features.
*
* The allowed features are available via raptor_sax2_features_enumerate().
*
* Return value: non 0 on failure or if the feature is unknown
**/
int
raptor_sax2_set_feature(raptor_sax2 *sax2, raptor_feature feature, int value)
{
if(value < 0)
return -1;
switch(feature) {
case RAPTOR_FEATURE_NORMALIZE_LANGUAGE:
sax2->feature_normalize_language=value;
break;
case RAPTOR_FEATURE_NO_NET:
sax2->feature_no_net=value;
break;
case RAPTOR_FEATURE_SCANNING:
case RAPTOR_FEATURE_ASSUME_IS_RDF:
case RAPTOR_FEATURE_ALLOW_NON_NS_ATTRIBUTES:
case RAPTOR_FEATURE_ALLOW_OTHER_PARSETYPES:
case RAPTOR_FEATURE_ALLOW_BAGID:
case RAPTOR_FEATURE_ALLOW_RDF_TYPE_RDF_LIST:
case RAPTOR_FEATURE_NON_NFC_FATAL:
case RAPTOR_FEATURE_WARN_OTHER_PARSETYPES:
case RAPTOR_FEATURE_CHECK_RDF_ID:
case RAPTOR_FEATURE_RELATIVE_URIS:
case RAPTOR_FEATURE_START_URI:
case RAPTOR_FEATURE_WRITER_AUTO_INDENT:
case RAPTOR_FEATURE_WRITER_AUTO_EMPTY:
case RAPTOR_FEATURE_WRITER_INDENT_WIDTH:
case RAPTOR_FEATURE_WRITER_XML_VERSION:
case RAPTOR_FEATURE_WRITER_XML_DECLARATION:
default:
return -1;
break;
}
return 0;
}
void
raptor_sax2_update_document_locator(raptor_sax2* sax2,
raptor_locator* locator)
{
#ifdef RAPTOR_XML_EXPAT
raptor_expat_update_document_locator(sax2, locator);
#endif
#ifdef RAPTOR_XML_LIBXML
raptor_libxml_update_document_locator(sax2, locator);
#endif
}
/* start of an element */
void
raptor_sax2_start_element(void* user_data, const unsigned char *name,
const unsigned char **atts)
{
raptor_sax2* sax2=(raptor_sax2*)user_data;
raptor_qname* el_name;
unsigned char **xml_atts_copy=NULL;
size_t xml_atts_size=0;
int all_atts_count=0;
int ns_attributes_count=0;
raptor_qname** named_attrs=NULL;
raptor_xml_element* xml_element=NULL;
unsigned char *xml_language=NULL;
raptor_uri *xml_base=NULL;
#ifdef RAPTOR_XML_EXPAT
#ifdef EXPAT_UTF8_BOM_CRASH
sax2->tokens_count++;
#endif
#endif
#ifdef RAPTOR_XML_LIBXML
if(atts) {
int i;
/* Do XML attribute value normalization */
for (i = 0; atts[i]; i+=2) {
unsigned char *value=(unsigned char*)atts[i+1];
unsigned char *src = value;
unsigned char *dst = xmlStrdup(value);
if (!dst) {
sax2->error_handler(sax2->error_data, sax2->locator, "Out of memory");
return;
}
atts[i+1]=dst;
while (*src == 0x20 || *src == 0x0d || *src == 0x0a || *src == 0x09)
src++;
while (*src) {
if (*src == 0x20 || *src == 0x0d || *src == 0x0a || *src == 0x09) {
while (*src == 0x20 || *src == 0x0d || *src == 0x0a || *src == 0x09)
src++;
if (*src)
*dst++ = 0x20;
} else {
*dst++ = *src++;
}
}
*dst = '\0';
xmlFree(value);
}
}
#endif
raptor_sax2_inc_depth(sax2);
if(atts) {
int i;
/* Save passed in XML attributes pointers so we can
* NULL the pointers when they get handled below (various atts[i]=NULL)
*/
for (i = 0; atts[i]; i++);
xml_atts_size=sizeof(unsigned char*) * i;
if(xml_atts_size) {
xml_atts_copy=(unsigned char**)RAPTOR_MALLOC(cstringpointer,xml_atts_size);
memcpy(xml_atts_copy, atts, xml_atts_size);
}
/* XML attributes processing:
* xmlns* - XML namespaces (Namespaces in XML REC)
* Deleted and used to synthesise namespaces declarations
* xml:lang - XML language (XML REC)
* Deleted and optionally normalised to lowercase
* xml:base - XML Base (XML Base REC)
redland/raptor/src/raptor_sax2.c view on Meta::CPAN
sizeof(raptor_qname*));
if(!named_attrs) {
sax2->fatal_error_handler(sax2->fatal_error_data, sax2->locator, "Out of memory");
RAPTOR_FREE(raptor_xml_element, xml_element);
raptor_free_qname(raptor_xml_element_get_name(xml_element));
return;
}
for (i = 0; i < all_atts_count; i++) {
raptor_qname* attr;
/* Skip previously processed attributes */
if(!atts[i<<1])
continue;
/* namespace-name[i] stored in named_attrs[i] */
attr=raptor_new_qname(&sax2->namespaces,
atts[i<<1], atts[(i<<1)+1],
(raptor_simple_message_handler)raptor_sax2_simple_error, sax2);
if(!attr) { /* failed - tidy up and return */
int j;
for (j=0; j < i; j++)
RAPTOR_FREE(raptor_qname, named_attrs[j]);
RAPTOR_FREE(raptor_qname_array, named_attrs);
raptor_free_xml_element(xml_element);
return;
}
named_attrs[offset++]=attr;
}
} /* end if ns_attributes_count */
if(named_attrs)
raptor_xml_element_set_attributes(xml_element,
named_attrs, ns_attributes_count);
raptor_xml_element_push(sax2, xml_element);
if(sax2->start_element_handler)
sax2->start_element_handler(sax2->user_data, xml_element);
if(xml_atts_copy) {
/* Restore passed in XML attributes, free the copy */
memcpy((void*)atts, xml_atts_copy, xml_atts_size);
RAPTOR_FREE(cstringpointer, xml_atts_copy);
}
}
/* end of an element */
void
raptor_sax2_end_element(void* user_data, const unsigned char *name)
{
raptor_sax2* sax2=(raptor_sax2*)user_data;
raptor_xml_element* xml_element;
#ifdef RAPTOR_XML_EXPAT
#ifdef EXPAT_UTF8_BOM_CRASH
sax2->tokens_count++;
#endif
#endif
xml_element=sax2->current_element;
if(xml_element) {
#ifdef RAPTOR_DEBUG_VERBOSE
fprintf(stderr, "\nraptor_rdfxml_end_element_handler: End ns-element: ");
raptor_qname_print(stderr, xml_element->name);
fputc('\n', stderr);
#endif
if(sax2->end_element_handler)
sax2->end_element_handler(sax2->user_data, xml_element);
}
raptor_namespaces_end_for_depth(&sax2->namespaces,
raptor_sax2_get_depth(sax2));
xml_element=raptor_xml_element_pop(sax2);
if(xml_element)
raptor_free_xml_element(xml_element);
raptor_sax2_dec_depth(sax2);
}
/* characters */
void
raptor_sax2_characters(void* user_data, const unsigned char *s, int len)
{
raptor_sax2* sax2=(raptor_sax2*)user_data;
if(sax2->characters_handler)
sax2->characters_handler(sax2->user_data, sax2->current_element, s, len);
}
/* like <![CDATA[...]> */
void
raptor_sax2_cdata(void* user_data, const unsigned char *s, int len)
{
raptor_sax2* sax2=(raptor_sax2*)user_data;
#ifdef RAPTOR_XML_EXPAT
#ifdef EXPAT_UTF8_BOM_CRASH
sax2->tokens_count++;
#endif
#endif
if(sax2->cdata_handler)
sax2->cdata_handler(sax2->user_data, sax2->current_element, s, len);
}
/* comment */
void
raptor_sax2_comment(void* user_data, const unsigned char *s)
{
raptor_sax2* sax2=(raptor_sax2*)user_data;
if(sax2->comment_handler)
sax2->comment_handler(sax2->user_data, sax2->current_element, s);
}
/* unparsed (NDATA) entity */
void
raptor_sax2_unparsed_entity_decl(void* user_data,
const unsigned char* entityName,
const unsigned char* base,
const unsigned char* systemId,
const unsigned char* publicId,
const unsigned char* notationName)
{
raptor_sax2* sax2=(raptor_sax2*)user_data;
if(sax2->unparsed_entity_decl_handler)
sax2->unparsed_entity_decl_handler(sax2->user_data,
entityName, base, systemId,
publicId, notationName);
}
/* external entity reference */
int
raptor_sax2_external_entity_ref(void* user_data,
const unsigned char* context,
const unsigned char* base,
const unsigned char* systemId,
const unsigned char* publicId)
{
raptor_sax2* sax2=(raptor_sax2*)user_data;
if(sax2->external_entity_ref_handler)
return sax2->external_entity_ref_handler(sax2->user_data,
context, base, systemId, publicId);
return 0;
}
( run in 0.516 second using v1.01-cache-2.11-cpan-bbcb1afb8fc )