xmlstream.cpp

来自「用bcg库编写的java IDE 源码」· C++ 代码 · 共 220 行

CPP
220
字号
#include "stdafx.h"
#include "XmlStream.h"
#include "XmlParser.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// parser id for debugging
long XmlParser::_parseid = 0;


// show if display debug info
const bool showDebugInfo = true;

// save/load stream
bool XmlStream::save ( char * buffer )
{
	// show success
	bool success = true;
	try
	{
		// save stream to buffer
		memcpy( buffer,(char *) c_str(), size() );
	}
	catch(...)
	{
		success = false;
	}

	return success;
}

bool XmlStream::load ( char * buffer )
{
	// if invalid show failed
	if ( !buffer )
		return false;

	// show success
	bool success = true;
	try
	{
		// load stream from buffer
		str() = buffer;
	}
	catch(...)
	{
		success = false;
	}

	return success;
}




// notify methods
void XmlStream::foundNode ( string & name, string & attributes )
{
	// if no name stop
	if ( name.empty() )
		return;

	if ( showDebugInfo )
	{
		// debug info
		TRACE("Found Node: \n");
	    TRACE("name: \n");;

		if ( !attributes.empty() )
			TRACE(CString(attributes.c_str())+'\n');
	}
	
	// tell subscriber
	if ( _subscriber )
		_subscriber->foundNode(name,attributes);
}

void XmlStream::foundElement ( string & name, string & value, string & attributes )
{
	// if no name stop
	if ( name.empty() )
		return;

	// debug info
	if ( showDebugInfo )
	{
		TRACE("Found Element: \n");
		TRACE("name: "+CString(name.c_str())+'\n');

		if ( !value.empty() )
			TRACE("value: "+CString(value.c_str())+'\n');

		if ( !attributes.empty() )
			TRACE("attributes: "+CString(attributes.c_str())+'\n');
	}
	
	// tell subscriber
	if ( _subscriber )
		_subscriber->foundElement(name,value,attributes);
}

void XmlStream::startElement ( string & name, string & value, string & attributes )
{
	if ( _subscriber )
		_subscriber->startElement(name,value,attributes);
}

void XmlStream::endElement ( string & name, string & value, string & attributes )
{
	if ( _subscriber )
		_subscriber->endElement(name,value,attributes);
}



bool XmlStream::parseNodes ( XmlParser & parser, char * buffer, long parseLength )
{
	// #DGH note
	// need to address a null node within another node
	// i.e.
	// <Contacts>
	//		<Contact/>
	// </Contacts>
	// in this instance Contact will be reported as an
	// element 

	// debug info
	string::iterator buf = buffer;
	if ( showDebugInfo )
	{
		TRACE1("parse node:%d \n",parser._id);
	}

	// while parse success, note for the first parser
	// this set the internal state also
	while ( parser.parse(buffer,parseLength) )
	{
		// if the value has a tag marker
		if ( parser.valueHasTag() )
		{
			// show found node
			string   name		 = parser.getName();
			string	 attributes  = parser.getAttributes();

			foundNode( name, attributes );

			// get the parse state
			long valueLength;
			char * valueBuffer =
			parser.getValueState(valueLength);

			// if parsing the node fails
			XmlParser parserNode;
			if ( !parseNodes(parserNode,valueBuffer,valueLength) )
				return false;

			// if new parse cur position is in the last parser
			// last tag position we are done with the node
			char * curPos     = parserNode.getCurPos();
			char * lastCurPos = parser.getLastTagPos();
			if ( curPos >= lastCurPos )
			{
				break;
			}
		}
		else
		{
			// show found element
			string   name		 = parser.getName();
			string   value		 = parser.getValue();
			string	 attributes  = parser.getAttributes();

			foundElement(name,value,attributes);
		}

		// get new parse state
		buffer =
		parser.getParseState(parseLength);
	}

	return true;
}



// parse the buffer

bool XmlStream::parse ( char * buffer, long length )
{
	// if invalid stop
	if ( !buffer || length <= 0 )
		return false;

	// debug info
	assign( buffer, length );

	// debug info
	if ( showDebugInfo )
	{
		TRACE("----- parse document -----\n");
		//TRACE(CString(buffer)+'\n');
		TRACE("----- start parsing -----\n");
	}

	// declare parser
	XmlParser parser;

	// parse nodes
	bool docParsed = parseNodes(parser,buffer,length);

	return docParsed;
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?