From: Daniel S. <dan...@kg...> - 2006-03-22 12:12:07
|
Moin, I have to parse a SGML document using openSP. And I have little problems with attributes. When an attribute has the type cdata, the CharString data of the Structure Attribute::CdataChunk is sometimes NULL, 0xBAADF00D or has very strange values. What could cause this effect? I am using: * Windows XP Pro * Microsoft Visual C++ .NET 7.0 * opensp 1.5.2 I have Also tested it with Linux and got the same error: * Linux 2.6.12-gentoo-r10 i686 AMD Athlon(tm) XP 1700+ AuthenticAMD GNU/Linux * gcc version 3.3.6 (Gentoo 3.3.6, ssp-3.3.6-1.0, pie-8.7.8) * app-text/openjade Latest version installed: 1.3.2-r1 the example files: catalog: -- snip -- PUBLIC "-//DST//DTD Some//EN" "some.dtd" -- snap -- some.dtd: -- snip -- <!ELEMENT SOME - - EMPTY> <!ATTLIST SOME ATTRIBUTE CDATA #IMPLIED > -- snap -- some.sgml: -- snip -- <!DOCTYPE SOME PUBLIC "-//DST//DTD Some//EN"> <SOME ATTRIBUTE=3D"value"> -- snap -- the source code looks like this: parser.h: -- snip -- #ifndef SGML_PARSER_H_ #define SGML_PARSER_H_ #include "ParserEventGeneratorKit.h" #include <string> using namespace std; class parser: public SGMLApplication { private: string attributeChars(const SGMLApplication::CharString &str); virtual void startElement(const SGMLApplication::StartElementEvent &event); }; #endif /*SGML_PARSER_H_*/ -- snap -- parser.cpp: -- snip -- #include <iostream> #include <windows.h> #include "parser.h" using namespace std; ostream &operator<<(ostream &os, SGMLApplication::CharString s) { if(NULL =3D=3D s.ptr){ os << "<!-- ptr=3DNULL -->"; } else if(0xbaadf00d =3D=3D (int)s.ptr){ os << "<!-- ptr=3D0xBAADF00D -->"; } else if(0xbaadf00d =3D=3D (int)s.len){ os << "<!-- len=3D0xBAADF00D -->"; =09 } else { for (size_t i =3D 0; i < s.len; i++) os << char(s.ptr[i*4]); } return os; } bool operator=3D=3D(const SGMLApplication::CharString &str1, const SGMLApplication::CharString &str2) { return (str1.len =3D=3D str2.len && (str1.len =3D=3D 0 || memcmp(str1.ptr, str2.ptr, str1.len*sizeof(SGMLApplication::Char)) =3D=3D 0)); } void parser::startElement(const StartElementEvent &event) { for (size_t i =3D 0; i < event.nAttributes; i++) { const Attribute &a =3D event.attributes[i]; switch (a.type) { case Attribute::cdata: if (a.defaulted !=3D Attribute::definition) { cout << ' ' << a.name << '=3D' << '"'; for (size_t j =3D 0; j < a.nCdataChunks; j++) if (a.cdataChunks[j].isSdata) cout << "&" << a.cdataChunks[j].entityName << ";"; else if (a.cdataChunks[j].isNonSgml) cout << "&#" << a.cdataChunks[j].nonSgmlChar << ";"; else cout << a.cdataChunks[j].data; cout << '"'; } break; case Attribute::tokenized: if (a.defaulted !=3D Attribute::definition) { if (a.isGroup && a.name =3D=3D a.tokens) cout << ' ' << a.tokens; else cout << ' ' << a.name << '=3D' << '"' << a.tokens << '"'; } break; default: break; } } } -- snap -- main.cpp: -- snip -- #include "stdafx.h" #include <iostream> #include "ParserEventGeneratorKit.h" #include "parser.h" using namespace std; int main(int argc, char* argv[]) { if(argc<2){ cerr << "arguments: catalog input.sgml" << endl; return -1; } ParserEventGeneratorKit parserKit; parserKit.setOption(ParserEventGeneratorKit::showOpenEntities); parserKit.setOption(ParserEventGeneratorKit::showOpenElements); =09 parserKit.setOption(ParserEventGeneratorKit::outputCommentDecls); =09 parserKit.setOption(ParserEventGeneratorKit::outputMarkedSections); =09 parserKit.setOption(ParserEventGeneratorKit::outputGeneralEntities); parserKit.setOption(ParserEventGeneratorKit::addCatalog, argv[1]); parserKit.setOption(ParserEventGeneratorKit::enableWarning, "all"); char* files[] =3D {argv[2]}; EventGenerator *egp =3D parserKit.makeEventGenerator(1, files); parser* par =3D new parser(); =09 int nErrors =3D -1; try{ nErrors =3D egp->run(*par); } catch (...) { cout << "EXCEPTION! "; } delete par; return nErrors; } -- snap -- example output: -- snip -- ATTRIBUTE=3D"<!-- ptr=3DNULL -->" -- snap -- or: -- snip -- ATTRIBUTE=3D"<!-- ptr=3D0xBAADF00D --> -- snap -- If somebody has any tip I would be very pleased. Thank You! --=20 KGU-Consulting GmbH Daniel Sturm Software development phone: +49 461 31852-14 Lise-Meitner-Str. 2 telefax: +49 461 31852-20 D 24941 Flensburg web: http://www.kgu-consulting.de |