Exemple #1
0
// attr1="value1" attr2='value2' attr3=value3 />
//                                            ^- return pointer
//========================================================
// Name   : LoadAttributes
// Desc   : loading attribute plain xml text
// Param  : pszAttrs - xml of attributes
//          pszEnd - last string
//          pi = parser information
// Return : advanced string pointer. (error return NULL)
//--------------------------------------------------------
// Coder    Date                      Desc
// bro      2004-06-14
//========================================================
LPTSTR _tagXMLNode::LoadAttributes( LPCTSTR pszAttrs, LPCTSTR pszEnd, LPPARSEINFO pi /*= &piDefault*/ )
{
	LPTSTR xml = (LPTSTR)pszAttrs;

	while( xml && *xml )
	{
		if( xml = _tcsskip( xml ) )
		{
			// close tag
			if( xml >= pszEnd )
				// wel-formed tag
				return xml;

			// XML Attr Name
			TCHAR* pEnd = _tcspbrk( xml, _T(" =") );
			if( pEnd == NULL ) 
			{
				// error
				if( pi->erorr_occur == false ) 
				{
					pi->erorr_occur = true;
					pi->error_pointer = xml;
					pi->error_code = PIE_ATTR_NO_VALUE;
					pi->error_string.Format( _T("<%s> attribute has error "), name );
				}
				return NULL;
			}
			
			LPXAttr attr = new XAttr;
			attr->parent = this;

			// XML Attr Name
			_SetString( xml, pEnd, &attr->name );
			
			// add new attribute
			attrs.push_back( attr );
			xml = pEnd;
			
			// XML Attr Value
			if( xml = _tcsskip( xml ) )
			{
				//if( xml = _tcschr( xml, '=' ) )
				if( *xml == '=' )
				{
					if( xml = _tcsskip( ++xml ) )
					{
						// if " or '
						// or none quote
						int quote = *xml;
						if( quote == '"' || quote == '\'' )
							pEnd = _tcsechr( ++xml, quote, pi ? pi->escape_value : 0 );
						else
						{
							//attr= value> 
							// none quote mode
							//pEnd = _tcsechr( xml, ' ', '\\' );
							pEnd = _tcsepbrk( xml, _T(" >"), pi ? pi->escape_value : 0 );
						}

						bool trim = pi->trim_value;
						TCHAR escape = pi->escape_value;
						//_SetString( xml, pEnd, &attr->value, trim, pi ? pi->escape_value : 0 );	
						_SetString( xml, pEnd, &attr->value, trim, escape );
						xml = pEnd;
						// ATTRVALUE 
						if( pi->entity_value && pi->entitys )
							attr->value = pi->entitys->Ref2Entity(attr->value);

						if( quote == '"' || quote == '\'' )
							xml++;
					}
				}
			}
		}
	}

	// not wel-formed tag
	return NULL;
}
// attr1="value1" attr2='value2' attr3=value3 />
// ^- return pointer
//========================================================
// Desc   : loading attribute plain xml text
// Param  : pszAttrs - xml of attributes
//          pi = parser information
// Return : advanced std::string pointer.
//========================================================
char* _tagXMLNode::loadAttributes( const char* pszAttrs , LPPARSEINFO pi /*= &piDefault*/)
{
	char* xml = (char*)pszAttrs;

	while( xml && *xml )
	{
		if( xml = _tcsskip( xml ) )
		{
			if ( xml = _tctskip(xml) )
			{
				if ( xml = _tcrskip(xml) )
				{
					// close tag
					if( *xml == chXMLTagClose || *xml == chXMLTagPre )
						// wel-formed tag
						return xml;

					// XML Attr Name
					char* pEnd = _tcspbrk( xml, " =" );
					if( pEnd == NULL ) 
					{
						// error
						if( pi->erorr_occur == false ) 
						{
							pi->erorr_occur = true;
							pi->error_pointer = xml;
							pi->error_code = PIE_ATTR_NO_VALUE;
							pi->error_string = "<%s> attribute has error ";
							pi->error_string += name;
						}
						return xml;
					}

					LPXAttr attr = new XAttr;
					attr->parent = this;

					// XML Attr Name
					_Setstring( xml, pEnd, &attr->name );

					// add new attribute
					attrs.push_back( attr );
					xml = pEnd;

					// XML Attr Value
					if( xml = _tcsskip( xml ) )
					{
						//if( xml = _tcschr( xml, '=' ) )
						if( *xml == '=' )
						{
							if( xml = _tcsskip( ++xml ) )
							{
								// if " or '
								// or none quote
								int quote = *xml;
								if( quote == '"' || quote == '\'' )
									pEnd = _tcsechr( ++xml, quote, chXMLEscape );
								else
								{
									//attr= value> 
									// none quote mode
									//pEnd = _tcsechr( xml, ' ', '\\' );
									pEnd = _tcsepbrk( xml, " >", chXMLEscape );
								}

								bool trim = pi->trim_value;
								_Setstring( xml, pEnd, &attr->value, trim, chXMLEscape );	
								xml = pEnd;
								// ATTRVALUE 
								if( pi->entity_value && pi->entitys )
									attr->value = pi->entitys->ref2Entity(attr->value.c_str());

								if( quote == '"' || quote == '\'' )
									xml++;
							}
						}
					}
				}
			}
		}
	}

	// not wel-formed tag
	return NULL;
}