⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlparser.cpp

📁 简单的xml解析类
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//************************************************************************************************************************** 
//* Blue Xml Extension
//* Copyright (c) 2002-2004 Josh Harler
//* 
//* Blue - General Purpose C++ Library
//* Copyright (c) 2002-2004 Josh Harler
//* 
//* This software is provided 'as-is', without any express or implied warranty. In no event
//* will the authors be held liable for any damages arising from the use of this software.
//* 
//* Permission is granted to anyone to use this software for any purpose, including commercial
//* applications, and to alter it and redistribute it freely, subject to the following restrictions:
//* 
//* 	1. The origin of this software must not be misrepresented; you must not claim that you
//* 	wrote the original software. If you use this software in a product, an acknowledgment in the
//* 	product documentation would be appreciated but is not required.
//* 
//* 	2. Altered source versions must be plainly marked as such, and must not be misrepresented as
//* 	being the original software.
//* 
//* 	3. This notice may not be removed or altered from any source distribution.
//*
//*
//* file   Blue/Extension/Xml/XmlParser.cpp
//**

// Private Headers =========================================================================================================

// matching header
#include "XmlParser.h"

// Extension headers
#include "Blue/Extension/Xml/internal/Tokenizer.h"


// Private Defines/Enums/Typedefs/Etc ======================================================================================

// Private Classes/Structs =================================================================================================

// Private Global Variables ================================================================================================

// External Global Variables ===============================================================================================

// Private Functions =======================================================================================================

// Functions ===============================================================================================================

namespace blue {
namespace ext {
namespace xml {

	// ---------------------------------------------------------------------------------------------------------------------

	XmlParser::XmlParser()
		:m_input(0)
	{
		init();
	}

	// ---------------------------------------------------------------------------------------------------------------------

	XmlParser::XmlParser( data::InputStream* input )
		:m_input(input)
	{
		init();
	}

	// ---------------------------------------------------------------------------------------------------------------------

	XmlParser::~XmlParser()
	{
	}

	// ---------------------------------------------------------------------------------------------------------------------

	const data::InputStream* XmlParser::getInputStream() const
	{
		return (m_input);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	String XmlParser::decodeText( String text ) const
	{
		int ampPos = 0;
		while( (ampPos = text.findPos("&", ampPos)) != String::npos ) {
			int semiPos = text.findPos(";", ampPos);
			if( semiPos == String::npos ) {
				break;
			}

			String entity = text.subString(ampPos + 1, (semiPos - ampPos)-1);

			String replace;

			if( entity.left(1).trim().getLength() == 0 ) {
				// whitespace after ampersand... treat like &
				semiPos = ampPos;
				replace = "&";
			}
			else if( entity.beginsWith("#") ) {
				String numRef = entity.stripFromLeft(1);
				int number;
				if( numRef.beginsWithIgnoreCase("x") ) {
					number = numRef.stripFromLeft(1).getAsInt(16);
				}
				else {
					number = numRef.getAsInt();
				}

				if( number >= 0 && number <= 255 ) {
					replace = String( (char)number );
				}
				else {
					replace = "?";
				}
			}
			else {
				bool found = false;
				for( int i = 0; i < m_entities.getSize(); ++i ) {
					if( entity == m_entities[i].m_entity ) {
						replace = m_entities[i].m_value;
						found = true;
						break;
					}
				}

				if( !found ) {
					throw XmlSyntaxException("Invalid entity encountered: '" + entity + "'");
				}
			}

			text = text.remove(ampPos, (semiPos-ampPos) + 1);
			text = text.insert(replace, ampPos);

			ampPos += replace.getLength();
		}

		return (text);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	Array<String> XmlParser::getEntities() const
	{
		Array<String> entities(m_entities.getSize());

		for( int i = 0; i < m_entities.getSize(); ++i ) {
			entities[i] = m_entities[i].m_entity;
		}

		return (entities);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	String XmlParser::getEntityValue( String entity )
	{
		for( int i = 0; i < m_entities.getSize(); ++i ) {
			if( entity == m_entities[i].m_entity ) {
				return (m_entities[i].m_value);
			}
		}

		return (String::null);
	}

	// ---------------------------------------------------------------------------------------------------------------------
	
	void XmlParser::setInputStream( data::InputStream* input )
	{
		m_input = input;
	}

	// ---------------------------------------------------------------------------------------------------------------------

	void XmlParser::addEntity( String entity, String value )
	{
		entity_item item;
		item.m_entity = entity;
		item.m_value  = decodeText(value);
		m_entities.append(item);
	}

	// ---------------------------------------------------------------------------------------------------------------------

	void XmlParser::removeEntity( String entity )
	{
		for( int i = 0; i < m_entities.getSize(); ++i ) {
			if( entity == m_entities[i].m_entity ) {
				m_entities.remove(i);
				break;
			}
		}
	}

	// ---------------------------------------------------------------------------------------------------------------------

	void XmlParser::parse()
	{
		if( m_input == 0 ) {
			throw XmlException($("Cannot parse Xml without an input stream"));
		}

		Tokenizer tokenizer;
		tokenizer.setInputStream(m_input);

		xmlDocumentBegin();
		while( m_input->isReading() ) {
			if( !readGeneral(tokenizer) ) {
				break;
			}
		}
		xmlDocumentEnd();
	}

	// ---------------------------------------------------------------------------------------------------------------------

	void XmlParser::reset()
	{
		init();
		xmlReset();
	}

	// ---------------------------------------------------------------------------------------------------------------------

	//virtual
	void XmlParser::xmlReset()
	{
	}

	// ---------------------------------------------------------------------------------------------------------------------

	//virtual
	void XmlParser::xmlDocumentBegin()
	{
	}

	// ---------------------------------------------------------------------------------------------------------------------

	//virtual
	void XmlParser::xmlDocumentEnd()
	{
	}

	// ---------------------------------------------------------------------------------------------------------------------
	
	//virtual
	void XmlParser::xmlProcInstBegin( String instruction )
	{
	}

	// ---------------------------------------------------------------------------------------------------------------------
	
	//virtual
	void XmlParser::xmlProcInstAttribute( String name, String value )
	{
	}

	// ---------------------------------------------------------------------------------------------------------------------
	
	//virtual
	void XmlParser::xmlProcInstEnd( String instruction )
	{
	}

	// ---------------------------------------------------------------------------------------------------------------------
	
	//virtual
	void XmlParser::xmlElementBegin( String element )
	{
	}

	// ---------------------------------------------------------------------------------------------------------------------

	//virtual
	void XmlParser::xmlElementAttribute( String name, String value )
	{
	}

	// ---------------------------------------------------------------------------------------------------------------------

	//virtual
	void XmlParser::xmlElementText( String data )
	{
	}

	// ---------------------------------------------------------------------------------------------------------------------

	//virtual
	void XmlParser::xmlElementCData( String element )
	{
	}

	// ---------------------------------------------------------------------------------------------------------------------

	//virtual
	void XmlParser::xmlElementEnd( String element )
	{
	}

	// ---------------------------------------------------------------------------------------------------------------------

	//virtual
	void XmlParser::xmlComment( String comment )
	{
	}

	// ---------------------------------------------------------------------------------------------------------------------
	
	void XmlParser::init()
	{
		m_elemNames.setSize(5);
		m_elemIdx = -1;

		m_entities.clear();

		addEntity("amp", "&");
		addEntity("quot", "\"");
		addEntity("apos", "\'");
		addEntity("lt", "<" );
		addEntity("gt", ">" );
	}

⌨️ 快捷键说明

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