1 /*
2     Copyright (c) 2008-2009 NetAllied Systems GmbH
3 
4     This file is part of GeneratedSaxParser.
5 
6     Licensed under the MIT Open Source License,
7     for details please see LICENSE file or the website
8     http://www.opensource.org/licenses/mit-license.php
9 */
10 
11 #include "GeneratedSaxParserParserTemplateBase.h"
12 #include "GeneratedSaxParserIErrorHandler.h"
13 
14 namespace GeneratedSaxParser
15 {
16 
17 
18 	//--------------------------------------------------------------------
toFloatPrefix(const ParserChar * prefixedBuffer,const ParserChar * prefixedBufferEnd,const ParserChar ** buffer,const ParserChar * bufferEnd,bool & failed)19 	float ParserTemplateBase::toFloatPrefix( const ParserChar* prefixedBuffer, const ParserChar* prefixedBufferEnd, const ParserChar** buffer, const ParserChar* bufferEnd, bool& failed )
20 	{
21 		return toDataPrefix<float, Utils::toFloat>(prefixedBuffer, prefixedBufferEnd, buffer, bufferEnd, failed);
22 	}
23 
24 
25 	//--------------------------------------------------------------------
toDoublePrefix(const ParserChar * prefixedBuffer,const ParserChar * prefixedBufferEnd,const ParserChar ** buffer,const ParserChar * bufferEnd,bool & failed)26 	double ParserTemplateBase::toDoublePrefix( const ParserChar* prefixedBuffer, const ParserChar* prefixedBufferEnd, const ParserChar** buffer, const ParserChar* bufferEnd, bool& failed )
27 	{
28 		return toDataPrefix<double, Utils::toDouble>(prefixedBuffer, prefixedBufferEnd, buffer, bufferEnd, failed);
29 	}
30 
31 
32 
33 	//--------------------------------------------------------------------
toSint8Prefix(const ParserChar * prefixedBuffer,const ParserChar * prefixedBufferEnd,const ParserChar ** buffer,const ParserChar * bufferEnd,bool & failed)34 	sint8 ParserTemplateBase::toSint8Prefix( const ParserChar* prefixedBuffer, const ParserChar* prefixedBufferEnd, const ParserChar** buffer, const ParserChar* bufferEnd, bool& failed )
35 	{
36 		return toDataPrefix<sint8, Utils::toSint8>(prefixedBuffer, prefixedBufferEnd, buffer, bufferEnd, failed);
37 	}
38 
39 	//--------------------------------------------------------------------
toUint8Prefix(const ParserChar * prefixedBuffer,const ParserChar * prefixedBufferEnd,const ParserChar ** buffer,const ParserChar * bufferEnd,bool & failed)40 	uint8 ParserTemplateBase::toUint8Prefix( const ParserChar* prefixedBuffer, const ParserChar* prefixedBufferEnd, const ParserChar** buffer, const ParserChar* bufferEnd, bool& failed )
41 	{
42 		return toDataPrefix<uint8, Utils::toUint8>(prefixedBuffer, prefixedBufferEnd, buffer, bufferEnd, failed);
43 	}
44 
45 	//--------------------------------------------------------------------
toSint16Prefix(const ParserChar * prefixedBuffer,const ParserChar * prefixedBufferEnd,const ParserChar ** buffer,const ParserChar * bufferEnd,bool & failed)46 	sint16 ParserTemplateBase::toSint16Prefix( const ParserChar* prefixedBuffer, const ParserChar* prefixedBufferEnd, const ParserChar** buffer, const ParserChar* bufferEnd, bool& failed )
47 	{
48 		return toDataPrefix<sint16, Utils::toSint16>(prefixedBuffer, prefixedBufferEnd, buffer, bufferEnd, failed);
49 	}
50 
51 	//--------------------------------------------------------------------
toUint16Prefix(const ParserChar * prefixedBuffer,const ParserChar * prefixedBufferEnd,const ParserChar ** buffer,const ParserChar * bufferEnd,bool & failed)52 	uint16 ParserTemplateBase::toUint16Prefix( const ParserChar* prefixedBuffer, const ParserChar* prefixedBufferEnd, const ParserChar** buffer, const ParserChar* bufferEnd, bool& failed )
53 	{
54 		return toDataPrefix<uint16, Utils::toUint16>(prefixedBuffer, prefixedBufferEnd, buffer, bufferEnd, failed);
55 	}
56 
57 	//--------------------------------------------------------------------
toSint32Prefix(const ParserChar * prefixedBuffer,const ParserChar * prefixedBufferEnd,const ParserChar ** buffer,const ParserChar * bufferEnd,bool & failed)58 	sint32 ParserTemplateBase::toSint32Prefix( const ParserChar* prefixedBuffer, const ParserChar* prefixedBufferEnd, const ParserChar** buffer, const ParserChar* bufferEnd, bool& failed )
59 	{
60 		return toDataPrefix<sint32, Utils::toSint32>(prefixedBuffer, prefixedBufferEnd, buffer, bufferEnd, failed);
61 	}
62 
63 	//--------------------------------------------------------------------
toUint32Prefix(const ParserChar * prefixedBuffer,const ParserChar * prefixedBufferEnd,const ParserChar ** buffer,const ParserChar * bufferEnd,bool & failed)64 	uint32 ParserTemplateBase::toUint32Prefix( const ParserChar* prefixedBuffer, const ParserChar* prefixedBufferEnd, const ParserChar** buffer, const ParserChar* bufferEnd, bool& failed )
65 	{
66 		return toDataPrefix<uint32, Utils::toUint32>(prefixedBuffer, prefixedBufferEnd, buffer, bufferEnd, failed);
67 	}
68 
69 	//--------------------------------------------------------------------
toSint64Prefix(const ParserChar * prefixedBuffer,const ParserChar * prefixedBufferEnd,const ParserChar ** buffer,const ParserChar * bufferEnd,bool & failed)70 	sint64 ParserTemplateBase::toSint64Prefix( const ParserChar* prefixedBuffer, const ParserChar* prefixedBufferEnd, const ParserChar** buffer, const ParserChar* bufferEnd, bool& failed )
71 	{
72 		return toDataPrefix<sint64, Utils::toSint64>(prefixedBuffer, prefixedBufferEnd, buffer, bufferEnd, failed);
73 	}
74 
75 	//--------------------------------------------------------------------
toUint64Prefix(const ParserChar * prefixedBuffer,const ParserChar * prefixedBufferEnd,const ParserChar ** buffer,const ParserChar * bufferEnd,bool & failed)76 	uint64 ParserTemplateBase::toUint64Prefix( const ParserChar* prefixedBuffer, const ParserChar* prefixedBufferEnd, const ParserChar** buffer, const ParserChar* bufferEnd, bool& failed )
77 	{
78 		return toDataPrefix<uint64, Utils::toUint64>(prefixedBuffer, prefixedBufferEnd, buffer, bufferEnd, failed);
79 	}
80 
81 	//--------------------------------------------------------------------
toBoolPrefix(const ParserChar * prefixedBuffer,const ParserChar * prefixedBufferEnd,const ParserChar ** buffer,const ParserChar * bufferEnd,bool & failed)82 	bool ParserTemplateBase::toBoolPrefix(const ParserChar* prefixedBuffer, const ParserChar* prefixedBufferEnd, const ParserChar** buffer, const ParserChar* bufferEnd, bool& failed)
83 	{
84 		return toDataPrefix<bool, Utils::toBool>(prefixedBuffer, prefixedBufferEnd, buffer, bufferEnd, failed);
85 	}
86 
87     //--------------------------------------------------------------------
toStringListPrefix(const ParserChar * prefixedBuffer,const ParserChar * prefixedBufferEnd,const ParserChar ** buffer,const ParserChar * bufferEnd,bool & failed)88     ParserString ParserTemplateBase::toStringListPrefix(const ParserChar* prefixedBuffer, const ParserChar* prefixedBufferEnd, const ParserChar** buffer, const ParserChar* bufferEnd, bool& failed)
89     {
90         return toDataPrefix<ParserString, Utils::toStringListItem>(prefixedBuffer, prefixedBufferEnd, buffer, bufferEnd, failed);
91     }
92 
93 	//--------------------------------------------------------------------
handleError(ParserError::Severity severity,ParserError::ErrorType errorType,StringHash elementHash,StringHash attributeHash,const ParserChar * additionalText)94 	bool ParserTemplateBase::handleError( ParserError::Severity severity,
95 												 ParserError::ErrorType errorType,
96 												 StringHash elementHash,
97 												 StringHash attributeHash,
98 												 const ParserChar* additionalText /*= ""*/ )
99 	{
100         return handleError(severity,
101             errorType,
102             elementHash,
103             getNameByStringHash(attributeHash),
104             additionalText);
105 	}
106 
107 	//--------------------------------------------------------------------
handleError(ParserError::Severity severity,ParserError::ErrorType errorType,StringHash attributeHash,const ParserChar * additionalText)108 	bool ParserTemplateBase::handleError( ParserError::Severity severity,
109 												 ParserError::ErrorType errorType,
110 												 StringHash attributeHash,
111 												 const ParserChar* additionalText )
112 	{
113 		StringHash elementHash = mElementDataStack.empty() ? 0 : mElementDataStack.back().elementHash;
114 
115 		return handleError(severity,
116 			errorType,
117 			elementHash,
118 			attributeHash,
119 			additionalText);
120 	}
121 
122     //--------------------------------------------------------------------
handleError(ParserError::Severity severity,ParserError::ErrorType errorType,StringHash elementHash,const ParserChar * attribute,const ParserChar * additionalText)123     bool ParserTemplateBase::handleError( ParserError::Severity severity,
124         ParserError::ErrorType errorType,
125         StringHash elementHash,
126         const ParserChar* attribute,
127         const ParserChar* additionalText /*= ""*/ )
128     {
129         IErrorHandler* errorHandler = getErrorHandler();
130         if ( !errorHandler )
131             return (severity == ParserError::SEVERITY_CRITICAL) ? true : false;
132 
133         ParserError error(severity,
134             errorType,
135             getNameByStringHash(elementHash),
136             attribute,
137             getLineNumber(),
138             getColumnNumber(),
139             additionalText ? (const char*)additionalText : "");
140         bool handlerWantsToAbort = errorHandler->handleError(error);
141 
142         return (severity == ParserError::SEVERITY_CRITICAL) ? true : handlerWantsToAbort;
143     }
144 
145 	//--------------------------------------------------------------------
getNameByStringHash(const StringHash & hash) const146 	const char* ParserTemplateBase::getNameByStringHash( const StringHash& hash ) const
147 	{
148 		if ( hash == 0 )
149 			return 0;
150 		ElementNameMap::const_iterator it = mHashNameMap.find(hash);
151 		if ( it == mHashNameMap.end() )
152 			return 0;
153 		else
154 			return it->second;
155 	}
156 
157 	//--------------------------------------------------------------------
getElementHash(size_t level) const158 	StringHash ParserTemplateBase::getElementHash( size_t level /*= 0 */ )const
159 	{
160 		size_t elementDataStackSize = mElementDataStack.size();
161 		if ( level < elementDataStackSize )
162 		{
163 			return mElementDataStack[elementDataStackSize - 1 - level].elementHash;
164 		}
165 		else
166 		{
167 			return 0;
168 		}
169 	}
170 } // namespace GeneratedSaxParser
171