23 using namespace Teuchos;
102 #define XMLPARSER_TFE( T , S ) \
103 TEUCHOS_TEST_FOR_EXCEPTION( T, std::runtime_error, "XML parse error at line " << _lineNo << ": " << S )
119 bool gotRoot =
false;
120 std::stack<long> tagLineStarts;
121 std::stack<string> tags;
125 std::string tag, cdata;
126 unsigned char c1, c2;
140 if (
_is->readBytes(&c1,1) < 1) {
150 XMLPARSER_TFE(
_is->readBytes(&c2,1) < 1 ,
"stream ended in tag begin/end");
157 XMLPARSER_TFE( curopen == 0,
"document not well-formed: encountered end element '" << tag <<
"' while not enclosed." );
159 <<
" did not match start element '" << tags.top()
160 <<
"' from line " << tagLineStarts.top() );
165 else if (
isLetter(c2) || c2==
':' || c2==
'_') {
169 getSTag(c2, tag, attrs, emptytag);
173 XMLPARSER_TFE(gotRoot ==
true,
"document not well-formed: more than one root element specified" );
179 XMLPARSER_TFE( handler->
endElement(tag)!=0,
"unknown failure from handler while processing tag '" << tag <<
"'" );
185 else if (c2 ==
'?') {
187 XMLPARSER_TFE(
assertChar(
'x') != 0 ,
"was expecting an XML declaration; element not well-formed or exploits unsupported feature" );
188 XMLPARSER_TFE(
assertChar(
'm') != 0 ,
"was expecting an XML declaration; element not well-formed or exploits unsupported feature" );
189 XMLPARSER_TFE(
assertChar(
'l') != 0 ,
"was expecting an XML declaration; element not well-formed or exploits unsupported feature" );
192 else if (c2 ==
'!') {
203 XMLPARSER_TFE(
true,
"element not well-formed or exploits unsupported feature" );
206 else if ( (curopen > 0) && (c1 ==
'&') ) {
207 std::string chars =
"";
211 else if ( (curopen > 0) ) {
212 std::string chars =
"";
217 XMLPARSER_TFE(1 ,
"document not well-formed: character data outside of an enclosing tag");
221 XMLPARSER_TFE( curopen != 0 ,
"file ended before closing element '" << tags.top() <<
"' from line " << tagLineStarts.top() );
237 bool tagover =
false;
241 XMLPARSER_TFE(
_is->readBytes(&c,1) < 1 ,
"EOF before end element was terminated");
245 XMLPARSER_TFE(
_is->readBytes(&c,1) < 1 ,
"EOF before end element was terminated");
248 XMLPARSER_TFE(1,
"end element not well-formed: expected '>'");
296 XMLPARSER_TFE(
_is->readBytes(&c,1) < 1 ,
"EOF before start element was terminated");
309 bool hadspace =
false;
318 if ( (
isLetter(c) || c==
'_' || c==
':') && hadspace ) {
322 std::string attname, attval;
325 XMLPARSER_TFE(
_is->readBytes(&c,1) < 1,
"EOF before start element was terminated");
327 attname.push_back(c);
329 else if (
isSpace(c) || c==
'=' ) {
333 XMLPARSER_TFE(1,
"attribute not well-formed: expected whitespace or '='");
347 XMLPARSER_TFE(
_is->readBytes(&c,1) < 1,
"EOF before start element was terminated");
358 else if (c ==
'\"') {
362 XMLPARSER_TFE(1,
"attribute value must be quoted with either ''' or '\"'");
365 XMLPARSER_TFE(
_is->readBytes(&c,1) < 1,
"EOF before start element was terminated");
366 if (apost && c==
'\'') {
370 else if (!apost && c==
'\"') {
374 else if ( c ==
'&' ) {
390 XMLPARSER_TFE( attrs.find(attname) != attrs.end() ,
"cannot have two attributes with the same name");
391 attrs[attname] = attval;
403 XMLPARSER_TFE(1,
"start element not well-formed: invalid character");
407 XMLPARSER_TFE(
_is->readBytes(&c,1) < 1,
"EOF before start element was terminated");
450 unsigned int num, base;
453 XMLPARSER_TFE(
_is->readBytes(&c,1) < 1,
"EOF before reference was terminated");
459 XMLPARSER_TFE(
_is->readBytes(&c,1) < 1,
"EOF before reference was terminated");
464 else if (
'0' <= c && c <=
'9') {
469 XMLPARSER_TFE(1,
"invalid character in character reference: expected 'x' or [0-9]");
473 XMLPARSER_TFE(
_is->readBytes(&c,1) < 1,
"EOF before reference was terminated");
474 XMLPARSER_TFE( c !=
';' && !(
'0' <= c && c <=
'9') ,
"invalid character in character reference: expected [0-9] or ';'");
478 num = num*base + (c-
'0');
480 XMLPARSER_TFE(num > 0xFF,
"character reference value out of range");
481 refstr.push_back( (
unsigned char)num );
483 else if (
isLetter(c) || c==
'_' || c==
':') {
486 std::string entname =
"";
487 entname.push_back(c);
489 XMLPARSER_TFE(
_is->readBytes(&c,1) < 1,
"EOF before reference was terminated");
493 else if (
isLetter(c) || (
'0' <= c && c <=
'9')
494 || c==
'.' || c==
'-' || c==
'_' || c==
':'
496 entname.push_back(c);
499 XMLPARSER_TFE(1,
"entity reference not well-formed: invalid character");
506 XMLPARSER_TFE(1,
"reference not well-formed: expected name or '#'");
514 if (lookahead ==
'\n') ++
_lineNo;
515 if (
_is->readBytes(&lookahead,1) < 1) {
525 if ( (0x41 <= c && c <= 0x5A) || (0x61 <= c && c <= 0x7A) ||
526 (0xC0 <= c && c <= 0xD6) || (0xD8 <= c && c <= 0xF6) ||
536 if (
isLetter(c) || (
'0' <= c && c <=
'9') ||
537 c==
'.' || c==
'-' || c==
'_' || c==
':' || c==0xB7 )
546 if ( c==0x20 || c==0x9 || c==0xD || c==0xA )
555 if ( c==0x9 || c==0xA || c==0xD || 0x20 <= c) {
568 if (
_is->readBytes(&c,1) < 1) {
static bool isSpace(unsigned char c)
Determine whether c matches the Space production according to the XML specification.
void getETag(std::string &tag)
Consume a ETag production according to the XML specification. getETag throws an std::exception if the...
void characters(const std::string &chars)
Process character data.
#define XMLPARSER_TFE(T, S)
RCP< XMLInputStream > _is
Defines a class for assembling an XMLObject from XML input.
int endElement(const std::string &tag)
Receive notification of the end of an element.
XMLObject parse()
Consume the XMLInputStream to build an XMLObject.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Representation of an XML data tree. XMLObject is a ref-counted handle to a XMLObjectImplem object...
void startElement(const std::string &tag, const Map &attributes)
Receive notification of the start of an element.
Teuchos::map< std::string, string > _entities
static bool isNameChar(unsigned char c)
Determine whether c matches the NameChar production according to the XML specification.
int assertChar(unsigned char cexp)
Determines if the next character on the stream.
const XMLObject & getObject() const
Retrieve the entire XML tree.
void getSTag(unsigned char lookahead, std::string &tag, Teuchos::map< std::string, string > &attrs, bool &emptytag)
Consume a STag production according to the XML specification. getSTag throws an std::exception if the...
int getSpace(unsigned char &lookahead)
Consumes a Space (block of whitepace) production according to the XML specification.
void getComment(long startLine)
Consume a Comment production according to the XML specification. getComment throws an std::exception ...
static bool isChar(unsigned char c)
Determine whether c matches the Char production according to the XML specification.
TreeBuildingXMLHandler assembles a XMLObject from your XML input.
void getReference(std::string &refstr)
Consumes a Reference production according to the XML specification.
Smart reference counting pointer class for automatic garbage collection.
static bool isLetter(unsigned char c)
Determine whether c matches the Letter production according to the XML specification.
A class providing a simple XML parser. Methods can be overloaded to exploit external XML parsing libr...
void ignoreXMLDeclaration()
Ignore the rest of an XML declaration tag.