Ejemplo n.º 1
0
result_t XmlParser::parse(XmlDocument* doc, exlib::string source)
{
    XmlParser parser(doc, true);

    parser.m_now = doc;
    parser.m_list.push_back(doc);

    XML_Parser xml_parser = XML_ParserCreate(NULL);

    XML_SetParamEntityParsing(xml_parser, XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
    XML_SetUserData(xml_parser, &parser);

    XML_SetXmlDeclHandler(xml_parser, XmlDeclHandler);
    XML_SetElementHandler(xml_parser, StartElementHandler, EndElementHandler);
    XML_SetCharacterDataHandler(xml_parser, CharacterDataHandler);
    XML_SetProcessingInstructionHandler(xml_parser, ProcessingInstructionHandler);
    XML_SetCommentHandler(xml_parser, CommentHandler);
    XML_SetCdataSectionHandler(xml_parser, StartCdataSectionHandler, EndCdataSectionHandler);
    XML_SetStartDoctypeDeclHandler(xml_parser, StartDoctypeDeclHandler);

    if (XML_Parse(xml_parser, source.c_str(), (int32_t)source.length(), true) != XML_STATUS_OK) {
        char msg[128];
        sprintf(msg, "XmlParser: error on line %lu at column %lu: %s", XML_GetCurrentLineNumber(xml_parser),
            XML_GetCurrentColumnNumber(xml_parser) + 1,
            XML_ErrorString(XML_GetErrorCode(xml_parser)));

        XML_ParserFree(xml_parser);
        return CHECK_ERROR(Runtime::setError(msg));
    }

    XML_ParserFree(xml_parser);

    return 0;
}
Ejemplo n.º 2
0
END_TEST

/* Regression test for SF bug #483514. */
START_TEST(test_dtd_default_handling)
{
    char *text =
        "<!DOCTYPE doc [\n"
        "<!ENTITY e SYSTEM 'http://xml.libexpat.org/e'>\n"
        "<!NOTATION n SYSTEM 'http://xml.libexpat.org/n'>\n"
        "<!ELEMENT doc EMPTY>\n"
        "<!ATTLIST doc a CDATA #IMPLIED>\n"
        "<?pi in dtd?>\n"
        "<!--comment in dtd-->\n"
        "]><doc/>";

    XML_SetDefaultHandler(parser, accumulate_characters);
    XML_SetDoctypeDeclHandler(parser,
                              dummy_start_doctype_handler,
                              dummy_end_doctype_handler);
    XML_SetEntityDeclHandler(parser, dummy_entity_decl_handler);
    XML_SetNotationDeclHandler(parser, dummy_notation_decl_handler);
    XML_SetElementDeclHandler(parser, dummy_element_decl_handler);
    XML_SetAttlistDeclHandler(parser, dummy_attlist_decl_handler);
    XML_SetProcessingInstructionHandler(parser, dummy_pi_handler);
    XML_SetCommentHandler(parser, dummy_comment_handler);
    run_character_check(text, "\n\n\n\n\n\n\n<doc/>");
}
Ejemplo n.º 3
0
Archivo: ext_xml.cpp Proyecto: 2bj/hhvm
bool f_xml_set_processing_instruction_handler(const Resource& parser, const Variant& handler) {
  XmlParser * p = parser.getTyped<XmlParser>();
  xml_set_handler(&p->processingInstructionHandler, handler);
  XML_SetProcessingInstructionHandler(p->parser,
                                      _xml_processingInstructionHandler);
  return true;
}
Ejemplo n.º 4
0
bool f_xml_set_processing_instruction_handler(CObjRef parser, CVarRef handler){
  XmlParser * p = parser.getTyped<XmlParser>();
  xml_set_handler(&p->processingInstructionHandler, handler);
  XML_SetProcessingInstructionHandler(p->parser,
                                      _xml_processingInstructionHandler);
  return true;
}
Ejemplo n.º 5
0
ExpatAdapter::ExpatAdapter() : parser(0)
{

    #if XMP_DebugBuild
        this->elemNesting = 0;
        #if DumpXMLParseEvents
            if ( this->parseLog == 0 ) this->parseLog = stdout;
        #endif
    #endif

    this->parser = XML_ParserCreateNS ( 0, FullNameSeparator );
    if ( this->parser == 0 ) XMP_Throw ( "Failure creating Expat parser", kXMPErr_ExternalFailure );

    XML_SetUserData ( this->parser, this );

    XML_SetNamespaceDeclHandler ( this->parser, StartNamespaceDeclHandler, EndNamespaceDeclHandler );
    XML_SetElementHandler ( this->parser, StartElementHandler, EndElementHandler );

    XML_SetCharacterDataHandler ( this->parser, CharacterDataHandler );
    XML_SetCdataSectionHandler ( this->parser, StartCdataSectionHandler, EndCdataSectionHandler );

    XML_SetProcessingInstructionHandler ( this->parser, ProcessingInstructionHandler );
    XML_SetCommentHandler ( this->parser, CommentHandler );

    #if BanAllEntityUsage
        XML_SetStartDoctypeDeclHandler ( this->parser, StartDoctypeDeclHandler );
        isAborted = false;
    #endif

    this->parseStack.push_back ( &this->tree );	// Push the XML root node.

}	// ExpatAdapter::ExpatAdapter
Ejemplo n.º 6
0
TinyDomElement* TinyDom::parse(std::istream& istr) {

  XML_Parser parser = XML_ParserCreate(0);
  TinyDomParser tdparser;

  XML_SetUserData(parser, &tdparser);
  XML_SetCharacterDataHandler(parser, characterDataHandler);
  XML_SetElementHandler(parser, startElementHandler, endElementHandler);
  XML_SetCommentHandler(parser, commentHandler);
  XML_SetProcessingInstructionHandler(parser, processingInstructionHandler);
  XML_SetXmlDeclHandler(parser, xmlDeclHandler);
  XML_SetDefaultHandler(parser, defaultHandler);
  XML_SetDoctypeDeclHandler(parser, startDoctypeDeclHandler, endDoctypeDeclHandler);

  try {

    while (1) {
      void* buf = XML_GetBuffer(parser, 1024);
      if (!buf) {
	throw std::runtime_error("out of memory!");
      }
      istr.read((char*)buf, 1024);
	  std::streamsize len = istr.gcount();
      if (istr.fail() && !istr.eof()) {
	throw std::runtime_error("failed IO");
      }
      bool isFinal = (istr.eof() || len < 1024);
      if (! XML_ParseBuffer(parser, len, isFinal)) {
	std::ostringstream ostr;
	ostr << "parse error at line " << XML_GetErrorLineNumber(parser)
	     << ", column " << XML_GetErrorColumnNumber(parser) << ": "
	     << XML_ErrorString(XML_GetErrorCode(parser));
	throw std::runtime_error(ostr.str());
				 
      }
      if (isFinal) {
	break;
      }
    }
	
    XML_ParserFree(parser);
	
  } catch (...) {

    //std::cerr << "Got exception: " << e.what() << "\n";

    if (parser) { XML_ParserFree(parser); }
    delete tdparser.rootElement;
    throw;

  }

  return tdparser.rootElement;

}
Ejemplo n.º 7
0
PRIVATE void HTXML_setHandlers (XML_Parser me)
{
    XML_SetElementHandler(me, XML_startElement, XML_endElement);
    XML_SetCharacterDataHandler(me, XML_characterData);
    XML_SetProcessingInstructionHandler(me, XML_processingInstruction);
    XML_SetDefaultHandler(me, XML_default);
    XML_SetUnparsedEntityDeclHandler(me, XML_unparsedEntityDecl);
    XML_SetNotationDeclHandler(me, XML_notationDecl);
    XML_SetExternalEntityRefHandler(me, XML_externalEntityRef);
    XML_SetUnknownEncodingHandler(me, XML_unknownEncoding, NULL);
}
Ejemplo n.º 8
0
int main(int argc, char **argv) {

//	int res = isatty(fileno( stdin));
//
//	if(!res){
//		fprintf(stderr, "Must provide input document on stdin\n");
//		exit(1);
//	}

	XML_Parser p = XML_ParserCreate(NULL);
	if (!p) {
		fprintf(stderr, "Couldn't allocate memory for parser\n");
		exit(-1);
	}




	XML_UseParserAsHandlerArg(p);
	XML_SetElementHandler(p, start_hndl, end_hndl);
	XML_SetCharacterDataHandler(p, char_hndl);
	XML_SetProcessingInstructionHandler(p, proc_hndl);

	/* Notice that the default handler is not set at this point */

	for (;;) {
		int done;
		int len;
		fgets(Buff, sizeof(Buff), stdin);





		len = strlen(Buff);
		if (ferror(stdin)) {
			fprintf(stderr, "Read error\n");
			exit(-1);
		}
		done = feof(stdin);
		if (XML_Parse(p, Buff, len, done) != XML_STATUS_OK) {
			fprintf(stderr, "Parse error at line %lu :\n %s \n",
					XML_GetCurrentLineNumber(p), XML_ErrorString(
							XML_GetErrorCode(p)));
			exit(-1);
		}

		if (done)
			break;
	}
	printf("\n");
	return 0;
} /* End main */
Ejemplo n.º 9
0
JSBool
xmljs_parse(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
    XMLCallback *cb;
    XML_Parser xml;
    JSString *str;
    JSBool ok;

    cb = (XMLCallback *)JS_GetPrivate(cx, obj);
    if (!cb) {
	JS_ReportError(cx, "XMLParser object has no parser!");
	return JS_FALSE;
    }
    if (cb->preParse &&
	!cb->preParse(cx, obj, argc, argv, rval))
	return JS_FALSE;
    if (argc < 1) {
	*rval = JSVAL_TRUE;
	return JS_TRUE;
    }

    xml = XML_ParserCreate(NULL);
    if (!xml)
	return JS_FALSE;	/* XXX report error */
    /* after this point, have to leave via out: */

    XML_SetElementHandler(xml, cb->start, cb->end);
    XML_SetCharacterDataHandler(xml, cb->cdata);
    XML_SetProcessingInstructionHandler(xml, cb->processing);
    XML_SetUserData(xml, (void *)cb);
    cb->xml = xml;
    str = JS_ValueToString(cx, argv[0]);
    if (!XML_Parse(xml, JS_GetStringBytes(str), JS_GetStringLength(str), 1)) {
	str = JS_NewStringCopyZ(cx, XML_ErrorString(XML_GetErrorCode(xml)));
	if (!str) {
	    ok = JS_FALSE;
	    goto out;
	}
	if (!JS_DefineProperty(cx, obj, "error", STRING_TO_JSVAL(str),
			       NULL, NULL, JSPROP_ENUMERATE)) {
	    ok = JS_FALSE;
	    goto out;
	}
	*rval = JSVAL_FALSE;
    } else {
	*rval = JSVAL_TRUE;
    }
 out:
    XML_ParserFree(xml);
    cb->xml = NULL;
    return ok;
}
Ejemplo n.º 10
0
XmlTextInfo::XmlTextInfo
	( const wxString &path
	, const char *encoding /*= NULL*/
	)
	: WrapExpat ( encoding )
{
	XML_SetUserData ( p, this );
	XML_SetBase ( p, path.utf8_str() );
	XML_SetDoctypeDeclHandler ( p, startdoctypehandler, NULL );
	XML_SetProcessingInstructionHandler ( p, processinghandler );
	XML_SetElementHandler ( p, start, NULL );
	XML_SetDefaultHandlerExpand ( p, defaulthandler );
}
Ejemplo n.º 11
0
void ParserEngine::init()
{
	if (_parser)
		XML_ParserFree(_parser);

	if (!_pBuffer)
		_pBuffer  = new char[PARSE_BUFFER_SIZE];

	if (dynamic_cast<NoNamespacePrefixesStrategy*>(_pNamespaceStrategy))
	{
		_parser = XML_ParserCreateNS(_encodingSpecified ? _encoding.c_str() : 0, '\t');
		if (_parser)
		{
			XML_SetNamespaceDeclHandler(_parser, handleStartNamespaceDecl, handleEndNamespaceDecl);
		}
	}
	else if (dynamic_cast<NamespacePrefixesStrategy*>(_pNamespaceStrategy))
	{
		_parser = XML_ParserCreateNS(_encodingSpecified ? _encoding.c_str() : 0, '\t');
		if (_parser)
		{
			XML_SetReturnNSTriplet(_parser, 1);
			XML_SetNamespaceDeclHandler(_parser, handleStartNamespaceDecl, handleEndNamespaceDecl);
		}
	}
	else
	{
		_parser = XML_ParserCreate(_encodingSpecified ? _encoding.c_str() : 0);
	}

	if (!_parser) throw XMLException("Cannot create Expat parser");

	XML_SetUserData(_parser, this);
	XML_SetElementHandler(_parser, handleStartElement, handleEndElement);
	XML_SetCharacterDataHandler(_parser, handleCharacterData);
	XML_SetProcessingInstructionHandler(_parser, handleProcessingInstruction);
	if (_expandInternalEntities)
		XML_SetDefaultHandlerExpand(_parser, handleDefault);
	else
		XML_SetDefaultHandler(_parser, handleDefault);
	XML_SetUnparsedEntityDeclHandler(_parser, handleUnparsedEntityDecl);
	XML_SetNotationDeclHandler(_parser, handleNotationDecl);
	XML_SetExternalEntityRefHandler(_parser, handleExternalEntityRef);
	XML_SetCommentHandler(_parser, handleComment);
	XML_SetCdataSectionHandler(_parser, handleStartCdataSection, handleEndCdataSection);
	XML_SetDoctypeDeclHandler(_parser, handleStartDoctypeDecl, handleEndDoctypeDecl);
	XML_SetEntityDeclHandler(_parser, handleEntityDecl);
	XML_SetSkippedEntityHandler(_parser, handleSkippedEntity);
	XML_SetParamEntityParsing(_parser, _externalParameterEntities ? XML_PARAM_ENTITY_PARSING_ALWAYS : XML_PARAM_ENTITY_PARSING_NEVER);
	XML_SetUnknownEncodingHandler(_parser, handleUnknownEncoding, this);
}
Ejemplo n.º 12
0
void 
RS_XML(initParser)(XML_Parser parser, RS_XMLParserData *parserData)
{
  XML_SetUserData(parser, parserData);
  XML_SetElementHandler(parser, RS_XML(startElement), RS_XML(endElement));
  XML_SetCommentHandler(parser, RS_XML(commentHandler));
  XML_SetExternalEntityRefHandler(parser, RS_XML(externalEntityHandler));
  XML_SetUnparsedEntityDeclHandler(parser, RS_XML(entityDeclarationHandler));
  XML_SetCharacterDataHandler(parser, RS_XML(textHandler));
  XML_SetProcessingInstructionHandler(parser, RS_XML(processingInstructionHandler));
  XML_SetCdataSectionHandler(parser, RS_XML(startCdataSectionHandler), RS_XML(endCdataSectionHandler));
  XML_SetBase(parser, parserData->fileName);
  XML_SetNotStandaloneHandler(parser, RS_XML(notStandAloneHandler));
}
/**
 * Creates a new Expat parser. Called from the Java ExpatParser constructor.
 *
 * @param object the Java ExpatParser instance
 * @param javaEncoding the character encoding name
 * @param processNamespaces true if the parser should handle namespaces
 * @returns the pointer to the C Expat parser
 */
static jlong ExpatParser_initialize(JNIEnv* env, jobject object, jstring javaEncoding,
        jboolean processNamespaces) {
    // Allocate parsing context.
    UniquePtr<ParsingContext> context(new ParsingContext(object));
    if (context.get() == NULL) {
        jniThrowOutOfMemoryError(env, NULL);
        return 0;
    }

    context->processNamespaces = processNamespaces;

    // Create a parser.
    XML_Parser parser;
    ScopedUtfChars encoding(env, javaEncoding);
    if (encoding.c_str() == NULL) {
        return 0;
    }
    if (processNamespaces) {
        // Use '|' to separate URIs from local names.
        parser = XML_ParserCreateNS(encoding.c_str(), '|');
    } else {
        parser = XML_ParserCreate(encoding.c_str());
    }

    if (parser != NULL) {
        if (processNamespaces) {
            XML_SetNamespaceDeclHandler(parser, startNamespace, endNamespace);
            XML_SetReturnNSTriplet(parser, 1);
        }

        XML_SetCdataSectionHandler(parser, startCdata, endCdata);
        XML_SetCharacterDataHandler(parser, text);
        XML_SetCommentHandler(parser, comment);
        XML_SetDoctypeDeclHandler(parser, startDtd, endDtd);
        XML_SetElementHandler(parser, startElement, endElement);
        XML_SetExternalEntityRefHandler(parser, handleExternalEntity);
        XML_SetNotationDeclHandler(parser, notationDecl);
        XML_SetProcessingInstructionHandler(parser, processingInstruction);
        XML_SetUnparsedEntityDeclHandler(parser, unparsedEntityDecl);
        XML_SetUserData(parser, context.release());
    } else {
        jniThrowOutOfMemoryError(env, NULL);
        return 0;
    }

    return fromXMLParser(parser);
}
Ejemplo n.º 14
0
bool_t reset_handlers_parser(parser_t *parser) {
  if( parser ) {
    XML_SetStartElementHandler(parser->p, parser->callbacks.start_tag ?
			       xml_startelementhandler : NULL);
    XML_SetEndElementHandler(parser->p, parser->callbacks.end_tag ?
			     xml_endelementhandler : NULL);
    XML_SetCommentHandler(parser->p, parser->callbacks.comment ?
			  xml_commenthandler : NULL);
    XML_SetCharacterDataHandler(parser->p, parser->callbacks.chardata ?
				xml_characterdatahandler : NULL);
    XML_SetProcessingInstructionHandler(parser->p, parser->callbacks.pidata ?
					xml_processinginstructionhandler : NULL);
    XML_SetStartCdataSectionHandler(parser->p, parser->callbacks.start_cdata ?
				    xml_startcdatahandler : NULL);
    /* if start_cdata, always set endcdatahandler */
    XML_SetEndCdataSectionHandler(parser->p, parser->callbacks.start_cdata ?
				  xml_endcdatahandler : NULL);
    XML_SetDefaultHandler(parser->p, parser->callbacks.dfault ?
    			  xml_defaulthandler : NULL);


    XML_SetStartDoctypeDeclHandler(parser->p, parser->callbacks.start_doctypedecl ?
				   xml_startdoctypedeclhandler : NULL);
    /* if start_doctypedecl, always set enddoctypedeclhandler */
    XML_SetEndDoctypeDeclHandler(parser->p, parser->callbacks.start_doctypedecl ?
				 xml_enddoctypedeclhandler : NULL);
    XML_SetEntityDeclHandler(parser->p, parser->callbacks.entitydecl ?
			     xml_entitydeclhandler : NULL);

    /* what to do about entities: there can be internal and external
     * entities, which expat treats differently. We want all our
     * entities to stay unprocessed, otherwise we'll get well formedness
     * errors. When a handler is called, the raw string is passed to
     * the default handler (provided it exists). */
    XML_UseForeignDTD(parser->p, XML_TRUE);
    XML_SetParamEntityParsing(parser->p, XML_PARAM_ENTITY_PARSING_NEVER);
    XML_SetExternalEntityRefHandler(parser->p,
				    xml_externalentityrefhandler);
    XML_SetExternalEntityRefHandlerArg(parser->p, parser);
    XML_SetSkippedEntityHandler(parser->p, xml_skippedentityhandler);


    return TRUE;
  }
  return FALSE;
}
Ejemplo n.º 15
0
static int lxp_make_parser (lua_State *L) {
  XML_Parser p;
  int bufferCharData = (lua_type(L, 3) != LUA_TBOOLEAN) || (lua_toboolean(L, 3) != 0);
  char sep = *luaL_optstring(L, 2, "");
  lxp_userdata *xpu = createlxp(L);
  xpu->bufferCharData = bufferCharData;
  p = xpu->parser = (sep == '\0') ? XML_ParserCreate(NULL) :
                                    XML_ParserCreateNS(NULL, sep);
  if (!p)
    luaL_error(L, "XML_ParserCreate failed");
  luaL_checktype(L, 1, LUA_TTABLE);
  checkcallbacks(L);
  lua_pushvalue(L, 1);
  xpu->tableref = luaL_ref(L, LUA_REGISTRYINDEX);
  XML_SetUserData(p, xpu);
  if (hasfield(L, StartCdataKey) || hasfield(L, EndCdataKey))
    XML_SetCdataSectionHandler(p, f_StartCdata, f_EndCdataKey);
  if (hasfield(L, CharDataKey))
    XML_SetCharacterDataHandler(p, f_CharData);
  if (hasfield(L, CommentKey))
    XML_SetCommentHandler(p, f_Comment);
  if (hasfield(L, DefaultKey))
    XML_SetDefaultHandler(p, f_Default);
  if (hasfield(L, DefaultExpandKey))
    XML_SetDefaultHandlerExpand(p, f_DefaultExpand);
  if (hasfield(L, StartElementKey) || hasfield(L, EndElementKey))
    XML_SetElementHandler(p, f_StartElement, f_EndElement);
  if (hasfield(L, ExternalEntityKey))
    XML_SetExternalEntityRefHandler(p, f_ExternaEntity);
  if (hasfield(L, StartNamespaceDeclKey) || hasfield(L, EndNamespaceDeclKey))
    XML_SetNamespaceDeclHandler(p, f_StartNamespaceDecl, f_EndNamespaceDecl);
  if (hasfield(L, NotationDeclKey))
    XML_SetNotationDeclHandler(p, f_NotationDecl);
  if (hasfield(L, NotStandaloneKey))
    XML_SetNotStandaloneHandler(p, f_NotStandalone);
  if (hasfield(L, ProcessingInstructionKey))
    XML_SetProcessingInstructionHandler(p, f_ProcessingInstruction);
  if (hasfield(L, UnparsedEntityDeclKey))
    XML_SetUnparsedEntityDeclHandler(p, f_UnparsedEntityDecl);
  if (hasfield(L, StartDoctypeDeclKey))
    XML_SetStartDoctypeDeclHandler(p, f_StartDoctypeDecl);
  if (hasfield(L, XmlDeclKey))
    XML_SetXmlDeclHandler(p, f_XmlDecl);
  return 1;
}
Ejemplo n.º 16
0
LLXmlParser::LLXmlParser()
	:
	mParser( NULL ),
	mDepth( 0 )
{
	mAuxErrorString = "no error";

	// Override the document's declared encoding.
	mParser = XML_ParserCreate(NULL);

	XML_SetUserData(mParser, this);
	XML_SetElementHandler(					mParser,	startElementHandler, endElementHandler);
	XML_SetCharacterDataHandler(			mParser,	characterDataHandler);
	XML_SetProcessingInstructionHandler(	mParser,	processingInstructionHandler);
	XML_SetCommentHandler(					mParser,	commentHandler);

	XML_SetCdataSectionHandler(				mParser,	startCdataSectionHandler, endCdataSectionHandler);

	// This sets the default handler but does not inhibit expansion of internal entities.
	// The entity reference will not be passed to the default handler.
	XML_SetDefaultHandlerExpand(			mParser,	defaultDataHandler);
	
	XML_SetUnparsedEntityDeclHandler(		mParser,	unparsedEntityDeclHandler);
}
Ejemplo n.º 17
0
void
runexpatNSuD(enum isource from, 
	     void * list,
	     XML_StartElementHandler startElement, 
	     XML_EndElementHandler endElement,
	     XML_Char *ns_sep_p,
	     void *uData)
{
  const char *buf;
  XML_Parser parser;
  static int depth;
  struct runinfo *rip = calloc(1,sizeof(struct runinfo));

  if (curr_rip)
    {
      runinfo_stack = list_create(LIST_LIFO);
      list_add(runinfo_stack,curr_rip);
    }
  curr_rip = rip;

  if (ns_sep_p)
    parser = XML_ParserCreateNS("utf-8",*ns_sep_p);
  else
    parser = XML_ParserCreate("utf-8");

  if (!parser)
    {
      fprintf(stderr,"runexpat: NULL return from XML_ParserCreate()\n");
      return;
    }

  depth = 0;
  runexpat_init(rip);
  rip->from = from;
  rip->list = list;

  rip->charData_buflen = 1024;
  rip->charData_buf = malloc(sizeof(XML_Char) * rip->charData_buflen);
  rip->charData_bufused = 0;
  if (uData)
    XML_SetUserData(parser, uData);
  else
    XML_SetUserData(parser, &depth);
  XML_SetElementHandler(parser, startElement, endElement);
  XML_SetProcessingInstructionHandler(parser, piHandler);
  XML_SetCharacterDataHandler(parser, charHandler);

  if (!XML_Parse(parser,"<rp-wrap>",9,0))
    fail(parser, rip);

  set_input(rip);
  for (;;)
    {
      if (NULL == (buf = get_input(rip)))
	break;
      if (rip->input_len && !XML_Parse(parser, buf, rip->input_len, 0))
	{
	  fprintf(stderr,"===\n%s\n===\n",buf);
	  fail(parser, rip);
	}
    }

  if (!XML_Parse(parser,"</rp-wrap>",10,1))
    fail(parser, rip);

  XML_ParserFree(parser);
  runexpat_term(rip);
  free(rip->fname);
  free(rip);

  if (runinfo_stack)
    {
      curr_rip = list_pop(runinfo_stack);
      curr_fname = curr_rip->fname;
      if (!list_len(runinfo_stack))
	{
	  free(runinfo_stack);
	  runinfo_stack = NULL;
	}
    }
  else
    curr_rip = NULL;
}
Ejemplo n.º 18
0
bool wxXmlDocument::Load(wxInputStream& stream, const wxString& encoding, int flags)
{
#if wxUSE_UNICODE
    (void)encoding;
#else
    m_encoding = encoding;
#endif

    const size_t BUFSIZE = 1024;
    char buf[BUFSIZE];
    wxXmlParsingContext ctx;
    bool done;
    XML_Parser parser = XML_ParserCreate(NULL);
    wxXmlNode *root = new wxXmlNode(wxXML_DOCUMENT_NODE, wxEmptyString);

    ctx.encoding = wxS("UTF-8"); // default in absence of encoding=""
    ctx.conv = NULL;
#if !wxUSE_UNICODE
    if ( encoding.CmpNoCase(wxS("UTF-8")) != 0 )
        ctx.conv = new wxCSConv(encoding);
#endif
    ctx.removeWhiteOnlyNodes = (flags & wxXMLDOC_KEEP_WHITESPACE_NODES) == 0;
    ctx.parser = parser;
    ctx.node = root;

    XML_SetUserData(parser, (void*)&ctx);
    XML_SetElementHandler(parser, StartElementHnd, EndElementHnd);
    XML_SetCharacterDataHandler(parser, TextHnd);
    XML_SetCdataSectionHandler(parser, StartCdataHnd, EndCdataHnd);;
    XML_SetCommentHandler(parser, CommentHnd);
    XML_SetProcessingInstructionHandler(parser, PIHnd);
    XML_SetDefaultHandler(parser, DefaultHnd);
    XML_SetUnknownEncodingHandler(parser, UnknownEncodingHnd, NULL);

    bool ok = true;
    do
    {
        size_t len = stream.Read(buf, BUFSIZE).LastRead();
        done = (len < BUFSIZE);
        if (!XML_Parse(parser, buf, len, done))
        {
            wxString error(XML_ErrorString(XML_GetErrorCode(parser)),
                           *wxConvCurrent);
            wxLogError(_("XML parsing error: '%s' at line %d"),
                       error.c_str(),
                       (int)XML_GetCurrentLineNumber(parser));
            ok = false;
            break;
        }
    } while (!done);

    if (ok)
    {
        if (!ctx.version.empty())
            SetVersion(ctx.version);
        if (!ctx.encoding.empty())
            SetFileEncoding(ctx.encoding);
        SetDocumentNode(root);
    }
    else
    {
        delete root;
    }

    XML_ParserFree(parser);
#if !wxUSE_UNICODE
    if ( ctx.conv )
        delete ctx.conv;
#endif

    return ok;

}
Ejemplo n.º 19
0
void bmx_expat_XML_SetProcessingInstructionHandler(XML_Parser parser) {
	XML_SetProcessingInstructionHandler(parser, bmx_expat_ProcessingInstructionHandler);
}
Ejemplo n.º 20
0
void xs_infoset_scan(xsMachine* the)
{
	int c = xsToInteger(xsArgc);
	Scanner scanner;
	Scanner* self;
	xsVars(COUNT);
	xsTry {
		self = &scanner;
		c_memset(self, 0, sizeof(Scanner));
		if (c < 1)
			xsSyntaxError("no buffer");
		
		self->expat = XML_ParserCreate(NULL);
		xsThrowIfNULL(self->expat);
		XML_SetUserData(self->expat, self);
		XML_SetElementHandler(self->expat, scanStartTag, scanStopTag);
		XML_SetCdataSectionHandler(self->expat, scanStartCdata, scanStopCdata);
		XML_SetCharacterDataHandler(self->expat, scanCharacter);
		XML_SetCommentHandler(self->expat, scanComment);
		XML_SetProcessingInstructionHandler(self->expat, scanProcessingInstruction);
		XML_SetUnknownEncodingHandler(self->expat, scanUnknownEncoding, NULL);
		XML_SetSkippedEntityHandler(self->expat, scanEntity);
		
		self->result = 1;
		self->textBuffer = c_malloc(8192);
		xsThrowIfNULL(self->textBuffer);
		self->textSize = 8192;
		
		self->the = the;
		xsVar(ATTRIBUTE_PROTOTYPE) = xsGet(xsThis, xsID_attribute);
		xsVar(CDATA_PROTOTYPE) = xsGet(xsThis, xsID_cdata);
		xsVar(COMMENT_PROTOTYPE) = xsGet(xsThis, xsID_comment);
		xsVar(DOCUMENT_PROTOTYPE) = xsGet(xsThis, xsID_document);
		xsVar(ELEMENT_PROTOTYPE) = xsGet(xsThis, xsID_element);
		xsVar(NO_NAMESPACE) = xsString("");
		xsVar(NO_PREFIX) = xsString("");
		xsVar(PATH) = (c > 1) ? xsArg(1) : xsUndefined;
		xsVar(PI_PROTOTYPE) = xsGet(xsThis, xsID_pi);
		xsVar(XML_NAMESPACE) = xsGet(xsThis, xsID_xmlnsNamespace);
		xsVar(XML_PREFIX) = xsGet(xsThis, xsID_xmlnsPrefix);
		
		xsResult = xsNewInstanceOf(xsVar(DOCUMENT_PROTOTYPE));
		xsSet(xsResult, xsID_encoding, xsString("UTF-8"));
		xsSet(xsResult, xsID_version, xsString("1.0"));
		xsVar(CHILDREN) = xsNewInstanceOf(xsArrayPrototype);
		xsArrayCacheBegin(xsVar(CHILDREN));
		xsSet(xsResult, xsID_children, xsVar(CHILDREN));
		xsSet(xsResult, xsID_parent, xsNull);
		xsSet(xsResult, xsID_xmlnsAttributes, xsNull);

		if (xsIsInstanceOf(xsArg(0), xsChunkPrototype)) {
			xsStringValue buffer = xsGetHostData(xsArg(0));
			xsIntegerValue size = xsToInteger(xsGet(xsArg(0), xsID_length));
			self->result = XML_Parse(self->expat, (const char *)buffer, size, 1);
		}
		else if (xsTypeOf(xsArg(0)) == xsStringType) {
			xsStringValue string = xsToString(xsArg(0));
			xsIntegerValue stringOffset = 0;
			xsIntegerValue stringSize = c_strlen(string);
			while (self->result && (stringOffset < stringSize)) {
				xsIntegerValue size = stringSize - stringOffset;
				xsStringValue buffer = (char *)XML_GetBuffer(self->expat, 1024);
				xsThrowIfNULL(buffer);
				if (size > 1024) 
					size = 1024;
				c_memcpy(buffer, string + stringOffset, size);
				self->result = XML_ParseBuffer(self->expat, size, (size < 1024) ? 1 : 0);
				stringOffset += size;
				string = xsToString(xsArg(0)); // @@ gc
			}
		}
		else {
			xsStreamGetter* streamGetter = xsGetHostData(xsArg(0));
			while (self->result) {
				xsIntegerValue i;
				xsStringValue p, buffer = (char *)XML_GetBuffer(self->expat, 1024);
				xsThrowIfNULL(buffer);
				for (i = 0, p = buffer; i < 1024; i++, p++) {
					int c = (*(streamGetter->getter))(streamGetter->stream);
					if (c == C_EOF)
						break;
					*p = (char)c;
				}
				self->result = XML_ParseBuffer(self->expat, i, (i < 1024) ? 1 : 0);
				if (i < 1024)
					break;
			}
		}
		
		xsDelete(xsResult, xsID_xmlnsAttributes);
		xsDelete(xsResult, xsID_parent);
		xsArrayCacheEnd(xsVar(CHILDREN));
		
		if (!self->result) {
			xsVar(LINE) = xsInteger(XML_GetCurrentLineNumber(self->expat));
			xsVar(VALUE) = xsString((char*)XML_ErrorString(XML_GetErrorCode(self->expat)));
			if (xsHas(xsThis, xsID_reportError))
				xsCall3_noResult(xsThis, xsID_reportError, xsVar(PATH), xsVar(LINE), xsVar(VALUE));
			xsThrow(xsNewInstanceOf(xsSyntaxErrorPrototype));
		}
		c_free(self->textBuffer);
		self->textBuffer = NULL;
		XML_ParserFree(self->expat);
		self->expat = NULL;
	}
	xsCatch {
		if (self->textBuffer)
			c_free(self->textBuffer);
		if (self->expat)
			XML_ParserFree(self->expat);
	}
}
Ejemplo n.º 21
0
NS_IMETHODIMP
nsExpatDriver::WillBuildModel(const CParserContext& aParserContext,
                              nsITokenizer* aTokenizer,
                              nsIContentSink* aSink)
{
  mSink = do_QueryInterface(aSink);
  if (!mSink) {
    NS_ERROR("nsExpatDriver didn't get an nsIExpatSink");
    // Make sure future calls to us bail out as needed
    mInternalState = NS_ERROR_UNEXPECTED;
    return mInternalState;
  }

  mOriginalSink = aSink;

  static const XML_Memory_Handling_Suite memsuite =
    {
      (void *(*)(size_t))PR_Malloc,
      (void *(*)(void *, size_t))PR_Realloc,
      PR_Free
    };

  static const PRUnichar kExpatSeparator[] = { kExpatSeparatorChar, '\0' };

  mExpatParser = XML_ParserCreate_MM(kUTF16, &memsuite, kExpatSeparator);
  NS_ENSURE_TRUE(mExpatParser, NS_ERROR_FAILURE);

  XML_SetReturnNSTriplet(mExpatParser, XML_TRUE);

#ifdef XML_DTD
  XML_SetParamEntityParsing(mExpatParser, XML_PARAM_ENTITY_PARSING_ALWAYS);
#endif

  mURISpec = aParserContext.mScanner->GetFilename();

  XML_SetBase(mExpatParser, mURISpec.get());

  nsCOMPtr<nsIDocument> doc = do_QueryInterface(mOriginalSink->GetTarget());
  if (doc) {
    nsCOMPtr<nsPIDOMWindow> win = doc->GetWindow();
    if (!win) {
      PRBool aHasHadScriptHandlingObject;
      nsIScriptGlobalObject *global =
        doc->GetScriptHandlingObject(aHasHadScriptHandlingObject);
      if (global) {
        win = do_QueryInterface(global);
      }
    }
    if (win && !win->IsInnerWindow()) {
      win = win->GetCurrentInnerWindow();
    }
    if (win) {
      mInnerWindowID = win->WindowID();
    }
  }

  // Set up the callbacks
  XML_SetXmlDeclHandler(mExpatParser, Driver_HandleXMLDeclaration); 
  XML_SetElementHandler(mExpatParser, Driver_HandleStartElement,
                        Driver_HandleEndElement);
  XML_SetCharacterDataHandler(mExpatParser, Driver_HandleCharacterData);
  XML_SetProcessingInstructionHandler(mExpatParser,
                                      Driver_HandleProcessingInstruction);
  XML_SetDefaultHandlerExpand(mExpatParser, Driver_HandleDefault);
  XML_SetExternalEntityRefHandler(mExpatParser,
                                  (XML_ExternalEntityRefHandler)
                                          Driver_HandleExternalEntityRef);
  XML_SetExternalEntityRefHandlerArg(mExpatParser, this);
  XML_SetCommentHandler(mExpatParser, Driver_HandleComment);
  XML_SetCdataSectionHandler(mExpatParser, Driver_HandleStartCdataSection,
                             Driver_HandleEndCdataSection);

  XML_SetParamEntityParsing(mExpatParser,
                            XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
  XML_SetDoctypeDeclHandler(mExpatParser, Driver_HandleStartDoctypeDecl,
                            Driver_HandleEndDoctypeDecl);

  // If the sink is an nsIExtendedExpatSink,
  // register some addtional handlers.
  mExtendedSink = do_QueryInterface(mSink);
  if (mExtendedSink) {
    XML_SetNamespaceDeclHandler(mExpatParser,
                                Driver_HandleStartNamespaceDecl,
                                Driver_HandleEndNamespaceDecl);
    XML_SetUnparsedEntityDeclHandler(mExpatParser,
                                     Driver_HandleUnparsedEntityDecl);
    XML_SetNotationDeclHandler(mExpatParser,
                               Driver_HandleNotationDecl);
  }

  // Set up the user data.
  XML_SetUserData(mExpatParser, this);

  // XML must detect invalid character convertion
  aParserContext.mScanner->OverrideReplacementCharacter(0xffff);

  return mInternalState;
}
Ejemplo n.º 22
0
bool PG_Layout::Load(PG_Widget* parent, const std::string& filename, void (* WorkCallback)(int now, int max), void *UserSpace) {
	ParseUserData_t	XMLParser;
	bool status = true;
	int bytes_pos = 0;

	XMLParser.Parser = XML_ParserCreate(NULL);
	XMLParser.Section = XML_SECTION_DOC;
	XMLParser.PrevUserData = NULL;
	XMLParser.ParentObject = parent;
	XMLParser.InhTagFlags = 0;
	XMLParser.UserSpace = UserSpace;

	XML_SetUserData(XMLParser.Parser, &XMLParser);
	XML_SetElementHandler(XMLParser.Parser, XMLStartDoc, XMLEndDoc);
	XML_SetCharacterDataHandler(XMLParser.Parser, XMLTextDoc);
	XML_SetProcessingInstructionHandler(XMLParser.Parser, XMLProcInstr);

	XMLParser.FileName = filename.c_str();
	PG_File* f = PG_Application::OpenFile(XMLParser.FileName);
	if (f == NULL) {

		PG_LogWRN("Layout file %s not found !", filename.c_str());
		return false;
	}

	for (;;) {
		int bytes_read;
		void* buff;

		if ((buff = XML_GetBuffer(XMLParser.Parser, XML_BUFF_SIZE)) == NULL) {
			PG_LogWRN("Can`t get parse buffer");
			status = false;
			break;
		}

		bytes_read = f->read(buff, XML_BUFF_SIZE);
		bytes_pos += bytes_read;

		if (WorkCallback != NULL) {
			WorkCallback(bytes_pos , f->fileLength());
		}

		if (XML_ParseBuffer(XMLParser.Parser, bytes_read, bytes_read == 0) == 0) {
			PG_LogWRN("%s on the line %d pos %d",XML_ErrorString(XML_GetErrorCode(XMLParser.Parser)),XML_GetCurrentLineNumber(XMLParser.Parser), XML_GetCurrentColumnNumber(XMLParser.Parser));
			status = false;
			break;
		}

		if (bytes_read == 0) {
			status = true;
			break;
		}
	}

	if (XMLParser.Parser != NULL) {
		XML_ParserFree(XMLParser.Parser);
	}

	delete f;

	while (XMLParser.PrevUserData != NULL)
		RestoreUserData(&XMLParser);

	return true;
}
Ejemplo n.º 23
0
WBXML_DECLARE(WBXMLError) wbxml_tree_from_xml(WB_UTINY *xml, WB_ULONG xml_len, WBXMLTree **tree)
{
#if defined( HAVE_EXPAT )

    const XML_Feature *feature_list = NULL;
    XML_Parser         xml_parser   = NULL;
    WBXMLError         ret          = WBXML_OK;
    WB_BOOL            expat_utf16  = FALSE;
    WBXMLTreeClbCtx    wbxml_tree_clb_ctx;

    /* First Check if Expat is outputing UTF-16 strings */
    feature_list = (const XML_Feature *)XML_GetFeatureList();

    if ((feature_list != NULL) && (feature_list[0].value != sizeof(WB_TINY))) {
#if !defined( HAVE_ICONV )
        /* Ouch, can't convert from UTF-16 to UTF-8 */
        return WBXML_ERROR_XMLPARSER_OUTPUT_UTF16;
#else
        /* Expat returns UTF-16 encoded strings in its callbacks */
        expat_utf16 = TRUE;
#endif /* !HAVE_ICONV */
    }

    if (tree != NULL)
        *tree = NULL;

    /* Create Expat XML Parser */
    if ((xml_parser = XML_ParserCreateNS(NULL, WBXML_NAMESPACE_SEPARATOR)) == NULL)
        return WBXML_ERROR_NOT_ENOUGH_MEMORY;

    /* Init context */
    wbxml_tree_clb_ctx.current = NULL;
    wbxml_tree_clb_ctx.error = WBXML_OK;
    wbxml_tree_clb_ctx.skip_lvl = 0;
    wbxml_tree_clb_ctx.skip_start = 0;
    wbxml_tree_clb_ctx.xml_parser = xml_parser;
    wbxml_tree_clb_ctx.input_buff = xml;
    wbxml_tree_clb_ctx.expat_utf16 = expat_utf16;

    /* Create WBXML Tree */
    if ((wbxml_tree_clb_ctx.tree = wbxml_tree_create(WBXML_LANG_UNKNOWN, WBXML_CHARSET_UNKNOWN)) == NULL) {
        XML_ParserFree(xml_parser);
        WBXML_ERROR((WBXML_PARSER, "Can't create WBXML Tree"));
        return WBXML_ERROR_NOT_ENOUGH_MEMORY;
    }
    
    /* Set Handlers Callbacks */
    XML_SetXmlDeclHandler(xml_parser, wbxml_tree_clb_xml_decl);
    XML_SetStartDoctypeDeclHandler(xml_parser, wbxml_tree_clb_xml_doctype_decl);
    XML_SetElementHandler(xml_parser, wbxml_tree_clb_xml_start_element, wbxml_tree_clb_xml_end_element);
    XML_SetCdataSectionHandler(xml_parser, wbxml_tree_clb_xml_start_cdata, wbxml_tree_clb_xml_end_cdata);
    XML_SetProcessingInstructionHandler(xml_parser , wbxml_tree_clb_xml_pi);
    XML_SetCharacterDataHandler(xml_parser, wbxml_tree_clb_xml_characters);
    XML_SetUserData(xml_parser, (void*)&wbxml_tree_clb_ctx);

    /* Parse the XML Document to WBXML Tree */
    if (XML_Parse(xml_parser, (WB_TINY*) xml, xml_len, TRUE) == 0)
    {
        WBXML_ERROR((WBXML_CONV, "xml2wbxml conversion failed - expat error %i\n"
            "\tdescription: %s\n"
            "\tline: %i\n"
            "\tcolumn: %i\n"
            "\tbyte index: %i\n"
            "\ttotal bytes: %i\n%s",
            XML_GetErrorCode(xml_parser), 
            XML_ErrorString(XML_GetErrorCode(xml_parser)), 
            XML_GetCurrentLineNumber(xml_parser), 
            XML_GetCurrentColumnNumber(xml_parser), 
            XML_GetCurrentByteIndex(xml_parser), 
            XML_GetCurrentByteCount(xml_parser), xml));

        wbxml_tree_destroy(wbxml_tree_clb_ctx.tree);

        ret = WBXML_ERROR_XML_PARSING_FAILED;
    }
    else {
        if ((ret = wbxml_tree_clb_ctx.error) != WBXML_OK)
        {
            WBXML_ERROR((WBXML_CONV, "xml2wbxml conversion failed - context error %i", ret));
            wbxml_tree_destroy(wbxml_tree_clb_ctx.tree);
        }
        else
            *tree = wbxml_tree_clb_ctx.tree;
    }

    /* Clean-up */
    XML_ParserFree(xml_parser);

    return ret;

#else /* HAVE_EXPAT */

#if defined( HAVE_LIBXML )

    /** @todo Use LibXML2 SAX interface ! */
    return WBXML_ERROR_NO_XMLPARSER;

#else /* HAVE_LIBXML */
    
    /** @note You can add here another XML Parser support */
    return WBXML_ERROR_NO_XMLPARSER;

#endif /* HAVE_LIBXML */

#endif /* HAVE_EXPAT */
}
Ejemplo n.º 24
0
/* The handler.  Create a new parser and/or filter context where appropriate
 * and parse the chunks of data received from the brigade
 */
static int idlChunkHandler( ap_filter_t *f, apr_bucket_brigade *brigade ) {

	idlChunkContext* ctx = f->ctx;
	apr_bucket* currentBucket = NULL;
	apr_pool_t* pool = f->r->pool;
	const char* data;
  	apr_size_t len;
    osrfStringArray* params = NULL;
    mparams = NULL;

	/* load the per-dir/location config */
	idlChunkConfig* config = ap_get_module_config( 
			f->r->per_dir_config, &idlchunk_module );

	ap_log_rerror(APLOG_MARK, APLOG_ERR, 
			0, f->r, "IDLCHUNK Config:\nContent Type = %s, "
			"Strip PI = %s, Strip Comments = %s, Doctype = %s", 
			config->contentType, 
			(config->stripPI) ? "yes" : "no", 
			(config->stripComments) ? "yes" : "no",
			config->doctype);

	/* set the content type based on the config */
	ap_set_content_type(f->r, config->contentType);

	//ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r, "Set content type");

    params = apacheParseParms(f->r); /* free me */
    mparams = apacheGetParamValues( params, "class" ); /* free me */

    all = 1;

    if (mparams && mparams->size > 0) all = 0;

	//ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r, "Parsed the params, if any");

	/* create the XML parser */
	int firstrun = 0;
	if( parser == NULL ) {
		firstrun = 1;
		parser = XML_ParserCreate("UTF-8");
		XML_SetUserData(parser, f);
		XML_SetElementHandler(parser, startElement, endElement);
		XML_SetCharacterDataHandler(parser, charHandler);
		if(!config->stripDoctype)
			XML_SetStartDoctypeDeclHandler( parser, doctypeHandler );
		if(!config->stripPI)
			XML_SetProcessingInstructionHandler(parser, handlePI);
		if(!config->stripComments)
			XML_SetCommentHandler(parser, handleComment);
	}

	/* create the filter context */
	if( ctx == NULL ) {
		f->ctx = ctx = apr_pcalloc( pool, sizeof(*ctx));
		ctx->brigade = apr_brigade_create( pool, f->c->bucket_alloc );
		ctx->parser = parser;
	}


	if(firstrun) { /* we haven't started writing the data to the stream yet */

		/* go ahead and write the doctype out if we have one defined */
		if(config->doctype) {
			ap_log_rerror( APLOG_MARK, APLOG_DEBUG, 
					0, f->r, "IDLCHUNK DOCTYPE => %s", config->doctype);
			_fwrite(f, "%s\n", config->doctype);
		}
	}


	/* cycle through the buckets in the brigade */
	while (!APR_BRIGADE_EMPTY(brigade)) {

		/* grab the next bucket */
		currentBucket = APR_BRIGADE_FIRST(brigade);

		/* clean up when we're done */
		if (APR_BUCKET_IS_EOS(currentBucket) || APR_BUCKET_IS_FLUSH(currentBucket)) {
    	  	APR_BUCKET_REMOVE(currentBucket);
			APR_BRIGADE_INSERT_TAIL(ctx->brigade, currentBucket);
			ap_pass_brigade(f->next, ctx->brigade);
			XML_ParserFree(parser);
            if (params) osrfStringArrayFree(params);
            if (mparams) osrfStringArrayFree(mparams);
			parser = NULL;
		  	return APR_SUCCESS;
    	}

		/* read the incoming data */
		int s = apr_bucket_read(currentBucket, &data, &len, APR_NONBLOCK_READ);
		if( s != APR_SUCCESS ) {
			ap_log_rerror( APLOG_MARK, APLOG_ERR, 0, f->r, 
					"IDLCHUNK error reading data from filter with status %d", s);
            if (params) osrfStringArrayFree(params);
            if (mparams) osrfStringArrayFree(mparams);
			return s;
		}

		if (len > 0) {

			ap_log_rerror( APLOG_MARK, APLOG_DEBUG, 
					0, f->r, "IDLCHUNK read %d bytes", (int)len);

			/* push data into the XML push parser */
			if ( XML_Parse(ctx->parser, data, len, 0) == XML_STATUS_ERROR ) {

                char tmp[len+1];
                memcpy(tmp, data, len);
                tmp[len] = '\0';

				/* log and die on XML errors */
				ap_log_rerror( APLOG_MARK, APLOG_ERR, 0, f->r, 
                    "IDLCHUNK XML Parse Error: %s at line %d: parsing %s: data %s",
					XML_ErrorString(XML_GetErrorCode(ctx->parser)), 
					(int) XML_GetCurrentLineNumber(ctx->parser), f->r->filename, tmp);

				XML_ParserFree(parser);
                if (params) osrfStringArrayFree(params);
                if (mparams) osrfStringArrayFree(mparams);
				parser = NULL;
				return HTTP_INTERNAL_SERVER_ERROR; 
			}
    	}

		/* so a subrequest doesn't re-read this bucket */
		apr_bucket_delete(currentBucket); 
  	}

	apr_brigade_destroy(brigade);
    if (params) osrfStringArrayFree(params);
    if (mparams) osrfStringArrayFree(mparams);
  	return APR_SUCCESS;	
}
Ejemplo n.º 25
0
int
DOM_DocumentLS_load(DOM_Document *doc, const DOM_String *uri)
{
	FILE *fd;
	XML_Parser p;
	struct stack *stk;
	size_t n;	// was ssize_t (DBL)
	void *buf;
	int ret, done;

	DOM_Exception=DOM_NO_ERR; //line added by DBL

	if (doc == NULL || uri == NULL) {
		DOM_Exception = DOM_NULL_POINTER_ERR;
		return 0;
	}

	fd = fopen(uri, "r");
	if (fd == NULL) {
		DOM_Exception = DOM_SYSTEM_ERR;
		return 0;
	}

	p = XML_ParserCreate(NULL);
	if (p == NULL) {
		DOM_Exception = DOM_XML_PARSER_ERR;
		fclose(fd);
		return 0;
	}

	stk = stack_new(INT_MAX);
	if (stk == NULL || stack_push(stk, doc) == 0) {
		DOM_Exception = DOM_SYSTEM_ERR;
		XML_ParserFree(p);
		fclose(fd);
		stack_del(stk, NULL);
		return 0;
	}

	XML_SetElementHandler(p, start_fn, end_fn);
	XML_SetCharacterDataHandler(p, chardata_fn);
	XML_SetCommentHandler(p, comment_fn);
	XML_SetProcessingInstructionHandler(p , processing_fn);
	XML_SetUserData(p, stk);

	ret = 1;
	for ( ;; ) {
		if ((buf = XML_GetBuffer(p, BUF_SIZ)) == NULL) {
			DOM_Exception = DOM_NO_MEMORY_ERR;
			ret = 0;
			break;
		}
		if ((n = fread(buf, 1, BUF_SIZ, fd)) == 0 && ferror(fd)) {
			DOM_Exception = DOM_SYSTEM_ERR;
			ret = 0;
			break;
		}
		if (XML_ParseBuffer(p, (int) n, (done = feof(fd))) == 0 || DOM_Exception) {
			if (DOM_Exception == 0) {
				DOM_Exception = DOM_XML_PARSER_ERR;
			}
			ret = 0;
			break;
		}
		if (done) {
			break;
		}
	}

	stack_del(stk, NULL);
	XML_ParserFree(p);
	fclose(fd);

	return ret;
}
Ejemplo n.º 26
0
void XMLParser::EnableProcessingInstructionHandler(bool enable)
{
	assert(m_parser != NULL);
	XML_SetProcessingInstructionHandler(m_parser,
	enable ? ProcessingInstructionHandler : NULL);
}
Ejemplo n.º 27
0
void _Expat_XML_SetProcessingInstructionHandler(struct ExpatIFace * Self, XML_Parser parser, XML_ProcessingInstructionHandler handler)
{
	XML_SetProcessingInstructionHandler(parser, handler);
}
Ejemplo n.º 28
0
/**
 *  Parses the given input stream and returns a DOM Document.
 *  A NULL pointer will be returned if errors occurred
 */
nsresult
txXMLParser::parse(istream& aInputStream, const nsAString& aUri,
                   txXPathNode** aResultDoc)
{
    mErrorString.Truncate();
    *aResultDoc = nsnull;
    if (!aInputStream) {
        mErrorString.AppendLiteral("unable to parse xml: invalid or unopen stream encountered.");
        return NS_ERROR_FAILURE;
    }

    static const XML_Memory_Handling_Suite memsuite = {
        (void *(*)(size_t))PR_Malloc,
        (void *(*)(void *, size_t))PR_Realloc,
        PR_Free
    };
    static const PRUnichar expatSeparator = kExpatSeparatorChar;
    mExpatParser = XML_ParserCreate_MM(nsnull, &memsuite, &expatSeparator);
    if (!mExpatParser) {
        return NS_ERROR_OUT_OF_MEMORY;
    }
    mDocument = new Document();
    if (!mDocument) {
        XML_ParserFree(mExpatParser);
        return NS_ERROR_OUT_OF_MEMORY;
    }
    mDocument->documentBaseURI = aUri;
    mCurrentNode = mDocument;

    XML_SetReturnNSTriplet(mExpatParser, XML_TRUE);
    XML_SetUserData(mExpatParser, this);
    XML_SetElementHandler(mExpatParser, startElement, endElement);
    XML_SetCharacterDataHandler(mExpatParser, charData);
    XML_SetProcessingInstructionHandler(mExpatParser, piHandler);
    XML_SetCommentHandler(mExpatParser, commentHandler);
#ifdef XML_DTD
    XML_SetParamEntityParsing(mExpatParser, XML_PARAM_ENTITY_PARSING_ALWAYS);
#endif
    XML_SetExternalEntityRefHandler(mExpatParser, externalEntityRefHandler);
    XML_SetExternalEntityRefHandlerArg(mExpatParser, this);
    XML_SetBase(mExpatParser,
                (const XML_Char*)(PromiseFlatString(aUri).get()));

    const int bufferSize = 1024;
    char buf[bufferSize];
    PRBool done;
    do {
        aInputStream.read(buf, bufferSize);
        done = aInputStream.eof();

        if (!XML_Parse(mExpatParser, buf, aInputStream.gcount(), done)) {
            createErrorString();
            done = MB_TRUE;
            delete mDocument;
            mDocument = nsnull;
        }
    } while (!done);
    aInputStream.clear();

    // clean up
    XML_ParserFree(mExpatParser);
    // ownership to the caller
    *aResultDoc = txXPathNativeNode::createXPathNode(mDocument);
    mDocument = nsnull;
    return *aResultDoc ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}