file & line # reporting on xml syntax errors
authorRoyce Mitchell III <royce3@ev1.net>
Thu, 6 Jan 2005 02:06:44 +0000 (02:06 +0000)
committerRoyce Mitchell III <royce3@ev1.net>
Thu, 6 Jan 2005 02:06:44 +0000 (02:06 +0000)
svn path=/branches/xmlbuildsystem/; revision=12844

reactos/tools/rbuild/XML.cpp
reactos/tools/rbuild/XML.h
reactos/tools/rbuild/exception.cpp
reactos/tools/rbuild/exception.h

index 11878ac..8ec6a8d 100644 (file)
@@ -7,7 +7,8 @@
 #include <assert.h>\r
 \r
 #include "XML.h"\r
 #include <assert.h>\r
 \r
 #include "XML.h"\r
-#include "rbuild.h"\r
+#include "exception.h"\r
+#include "ssprintf.h"\r
 \r
 using std::string;\r
 using std::vector;\r
 \r
 using std::string;\r
 using std::vector;\r
@@ -274,6 +275,19 @@ XMLFile::get_token(string& token)
        return true;\r
 }\r
 \r
        return true;\r
 }\r
 \r
+string\r
+XMLFile::Location() const\r
+{\r
+       int line = 1;\r
+       const char* p = strchr ( _buf.c_str(), '\n' );\r
+       while ( p && p < _p )\r
+       {\r
+               ++line;\r
+               p = strchr ( p+1, '\n' );\r
+       }\r
+       return ssprintf ( "%s(%i)",_filename.c_str(), line );\r
+}\r
+\r
 XMLAttribute::XMLAttribute()\r
 {\r
 }\r
 XMLAttribute::XMLAttribute()\r
 {\r
 }\r
@@ -445,7 +459,9 @@ XMLParse(XMLFile& f,
        while ( token[0] != '<' || !strncmp ( token.c_str(), "<!--", 4 ) )\r
        {\r
                if ( token[0] != '<' )\r
        while ( token[0] != '<' || !strncmp ( token.c_str(), "<!--", 4 ) )\r
        {\r
                if ( token[0] != '<' )\r
-                       printf ( "syntax error: expecting xml tag, not '%s'\n", token.c_str() );\r
+                       throw XMLSyntaxErrorException ( f.Location(),\r
+                                                       "expecting xml tag, not '%s'",\r
+                                                       token.c_str() );\r
                if ( !f.get_token(token) )\r
                        return NULL;\r
        }\r
                if ( !f.get_token(token) )\r
                        return NULL;\r
        }\r
@@ -464,7 +480,10 @@ XMLParse(XMLFile& f,
                e->attributes.push_back ( new XMLAttribute ( "top_href", top_file ) );\r
                XMLFile fInc;\r
                if ( !fInc.open ( file ) )\r
                e->attributes.push_back ( new XMLAttribute ( "top_href", top_file ) );\r
                XMLFile fInc;\r
                if ( !fInc.open ( file ) )\r
-                       throw FileNotFoundException ( file );\r
+                       throw FileNotFoundException (\r
+                               ssprintf("%s (referenced from %s)",\r
+                                       file.c_str(),\r
+                                       f.Location().c_str() ) );\r
                else\r
                {\r
                        Path path2 ( path, att->value );\r
                else\r
                {\r
                        Path path2 ( path, att->value );\r
@@ -485,7 +504,9 @@ XMLParse(XMLFile& f,
                else if ( end_tag )\r
                {\r
                        delete e;\r
                else if ( end_tag )\r
                {\r
                        delete e;\r
-                       printf ( "syntax error: end tag '%s' not expected\n", token.c_str() );\r
+                       throw XMLSyntaxErrorException ( f.Location(),\r
+                                                       "end tag '%s' not expected",\r
+                                                       token.c_str() );\r
                        return NULL;\r
                }\r
                return e;\r
                        return NULL;\r
                }\r
                return e;\r
@@ -497,17 +518,24 @@ XMLParse(XMLFile& f,
                {\r
                        if ( !f.get_token ( token ) || !token.size() )\r
                        {\r
                {\r
                        if ( !f.get_token ( token ) || !token.size() )\r
                        {\r
-                               printf ( "internal tool error - get_token() failed when more_tokens() returned true\n" );\r
+                               throw Exception ( "internal tool error - get_token() failed when more_tokens() returned true" );\r
                                break;\r
                        }\r
                        if ( e->subElements.size() && !bThisMixingErrorReported )\r
                        {\r
                                break;\r
                        }\r
                        if ( e->subElements.size() && !bThisMixingErrorReported )\r
                        {\r
-                               printf ( "syntax error: mixing of inner text with sub elements\n" );\r
+                               throw XMLSyntaxErrorException ( f.Location(),\r
+                                                               "mixing of inner text with sub elements" );\r
                                bThisMixingErrorReported = true;\r
                        }\r
                                bThisMixingErrorReported = true;\r
                        }\r
+                       if ( strchr ( token.c_str(), '>' ) )\r
+                       {\r
+                               throw XMLSyntaxErrorException ( f.Location(),\r
+                                                               "invalid symbol '>'" );\r
+                       }\r
                        if ( e->value.size() )\r
                        {\r
                        if ( e->value.size() )\r
                        {\r
-                               printf ( "syntax error: multiple instances of inner text\n" );\r
+                               throw XMLSyntaxErrorException ( f.Location(),\r
+                                                               "multiple instances of inner text" );\r
                                e->value += " " + token;\r
                        }\r
                        else\r
                                e->value += " " + token;\r
                        }\r
                        else\r
@@ -519,13 +547,15 @@ XMLParse(XMLFile& f,
                        if ( end_tag )\r
                        {\r
                                if ( e->name != e2->name )\r
                        if ( end_tag )\r
                        {\r
                                if ( e->name != e2->name )\r
-                                       printf ( "syntax error: end tag name mismatch\n" );\r
+                                       throw XMLSyntaxErrorException ( f.Location(),\r
+                                                                       "end tag name mismatch" );\r
                                delete e2;\r
                                break;\r
                        }\r
                        if ( e->value.size() && !bThisMixingErrorReported )\r
                        {\r
                                delete e2;\r
                                break;\r
                        }\r
                        if ( e->value.size() && !bThisMixingErrorReported )\r
                        {\r
-                               printf ( "syntax error: mixing of inner text with sub elements\n" );\r
+                               throw XMLSyntaxErrorException ( f.Location(),\r
+                                                               "mixing of inner text with sub elements" );\r
                                bThisMixingErrorReported = true;\r
                        }\r
                        e->AddSubElement ( e2 );\r
                                bThisMixingErrorReported = true;\r
                        }\r
                        e->AddSubElement ( e2 );\r
index 4be3170..d255456 100644 (file)
@@ -35,6 +35,7 @@ public:
        bool more_tokens();\r
        bool get_token(std::string& token);\r
        const std::string& filename() { return _filename; }\r
        bool more_tokens();\r
        bool get_token(std::string& token);\r
        const std::string& filename() { return _filename; }\r
+       std::string Location() const;\r
 \r
 private:\r
        std::string _buf, _filename;\r
 \r
 private:\r
        std::string _buf, _filename;\r
index e950b9a..3027b42 100644 (file)
@@ -56,6 +56,21 @@ InvalidBuildFileException::InvalidBuildFileException(const char* message,
        va_end(args);\r
 }\r
 \r
        va_end(args);\r
 }\r
 \r
+InvalidBuildFileException::InvalidBuildFileException()\r
+{\r
+}\r
+\r
+\r
+XMLSyntaxErrorException::XMLSyntaxErrorException ( const std::string& location,\r
+                                                      const char* message,\r
+                                                      ... )\r
+{\r
+       va_list args;\r
+       va_start ( args, message );\r
+       Message = location + ": " + ssvprintf ( message, args );\r
+       va_end ( args );\r
+}\r
+\r
 \r
 RequiredAttributeNotFoundException::RequiredAttributeNotFoundException(const std::string& attributeName,\r
                                                                        const std::string& elementName)\r
 \r
 RequiredAttributeNotFoundException::RequiredAttributeNotFoundException(const std::string& attributeName,\r
                                                                        const std::string& elementName)\r
index 90ef6b3..3e2207f 100644 (file)
@@ -36,8 +36,19 @@ public:
 class InvalidBuildFileException : public Exception\r
 {\r
 public:\r
 class InvalidBuildFileException : public Exception\r
 {\r
 public:\r
-       InvalidBuildFileException(const char* message,\r
-                                 ...);\r
+       InvalidBuildFileException ( const char* message,\r
+                                   ...);\r
+protected:\r
+       InvalidBuildFileException();\r
+};\r
+\r
+\r
+class XMLSyntaxErrorException : public InvalidBuildFileException\r
+{\r
+public:\r
+       XMLSyntaxErrorException ( const std::string& location,\r
+                                 const char* message,\r
+                                 ... );\r
 };\r
 \r
 \r
 };\r
 \r
 \r