Keep the original format when associating a DTD/schema/XSL

This commit is contained in:
Zane U. Ji 2014-06-06 19:21:56 +08:00
parent a8a6bf6a07
commit b3ed8c5d69
6 changed files with 124 additions and 117 deletions

View File

@ -35,10 +35,10 @@ XmlAssociateDtd::XmlAssociateDtd (
d->buffer.reserve ( size ); d->buffer.reserve ( size );
d->path = path; d->path = path;
d->publicID = publicID; d->publicID = publicID;
d->rootElementSeen = false; d->associated = false;
d->insideDtd = false; d->insideDtd = false;
XML_SetUserData ( p, d.get() ); XML_SetUserData ( p, this );
XML_SetElementHandler ( p, start, end ); XML_SetElementHandler ( p, start, NULL );
XML_SetDoctypeDeclHandler ( p, startdoctypehandler, enddoctypehandler ); XML_SetDoctypeDeclHandler ( p, startdoctypehandler, enddoctypehandler );
XML_SetDefaultHandlerExpand ( p, defaulthandler ); XML_SetDefaultHandlerExpand ( p, defaulthandler );
} }
@ -51,8 +51,8 @@ void XMLCALL XmlAssociateDtd::defaulthandler (
const XML_Char *s, const XML_Char *s,
int len ) int len )
{ {
DtdData *d; XmlAssociateDtd *pThis = (XmlAssociateDtd *)data;
d = ( DtdData * ) data; DtdData *d = pThis->d.get();
if ( !d->insideDtd ) if ( !d->insideDtd )
d->buffer.append ( s, len ); d->buffer.append ( s, len );
} }
@ -61,51 +61,24 @@ void XMLCALL XmlAssociateDtd::start ( void *data,
const XML_Char *el, const XML_Char *el,
const XML_Char **attr ) const XML_Char **attr )
{ {
DtdData *d; XmlAssociateDtd *pThis = (XmlAssociateDtd *)data;
d = ( DtdData * ) data; DtdData *d = pThis->d.get();
if ( !d->rootElementSeen ) if ( !d->associated )
{ {
if ( d->publicID.empty() ) associate ( d, el );
{ d->buffer += "\n";
d->buffer += "<!DOCTYPE "; d->associated = true;
d->buffer += el;
d->buffer += " SYSTEM \"";
d->buffer += d->path.utf8_str(); // TODO: Apply the encoding of the parser
d->buffer += "\">\n";
}
else
{
d->buffer += "<!DOCTYPE ";
d->buffer += el;
d->buffer += " PUBLIC \"";
d->buffer += d->publicID.utf8_str(); // TODO: Apply the encoding of the parser
d->buffer += "\" \"";
d->buffer += d->path.utf8_str();
d->buffer += "\">\n";
}
d->rootElementSeen = true;
} }
d->buffer += "<"; XML_DefaultCurrent ( pThis->p );
d->buffer += el; XML_SetElementHandler ( pThis->p, NULL, NULL );
while ( *attr )
{
d->buffer += " ";
d->buffer += *attr;
d->buffer += "=\"";
d->buffer += xmliseAttribute ( * ( attr + 1 ) );
d->buffer += "\"";
attr += 2;
}
d->buffer += ">";
} }
void XMLCALL XmlAssociateDtd::end ( void *data, const XML_Char *el ) void XMLCALL XmlAssociateDtd::end ( void *data, const XML_Char *el )
{ {
DtdData *d; XmlAssociateDtd *pThis = (XmlAssociateDtd *)data;
d = ( DtdData * ) data; DtdData *d = pThis->d.get();
d->buffer += "</"; d->buffer += "</";
d->buffer += el; d->buffer += el;
d->buffer += ">"; d->buffer += ">";
@ -118,14 +91,38 @@ void XMLCALL XmlAssociateDtd::startdoctypehandler (
const XML_Char *pubid, const XML_Char *pubid,
int has_internal_subset ) int has_internal_subset )
{ {
DtdData *d; XmlAssociateDtd *pThis = (XmlAssociateDtd *)data;
d = ( DtdData * ) data; DtdData *d = pThis->d.get();
d->insideDtd = true; d->insideDtd = true;
associate ( d, doctypeName );
d->associated = true;
} }
void XMLCALL XmlAssociateDtd::enddoctypehandler ( void *data ) void XMLCALL XmlAssociateDtd::enddoctypehandler ( void *data )
{ {
DtdData *d; XmlAssociateDtd *pThis = (XmlAssociateDtd *)data;
d = ( DtdData * ) data; DtdData *d = pThis->d.get();
d->insideDtd = false; d->insideDtd = false;
} }
void XmlAssociateDtd::associate ( DtdData *d, const char *doctypeName )
{
wxASSERT ( d );
d->buffer += "<!DOCTYPE ";
d->buffer += doctypeName;
if ( d->publicID.empty() )
{
d->buffer += " SYSTEM \"";
d->buffer += d->path.utf8_str(); // TODO: Apply the encoding of the parser
}
else
{
d->buffer += " PUBLIC \"";
d->buffer += d->publicID.utf8_str();
d->buffer += "\" \"";
d->buffer += d->path.utf8_str();// TODO: Apply the encoding of the parser
}
d->buffer += "\">";
}

View File

@ -30,7 +30,7 @@ struct DtdData : public ParserData
{ {
std::string buffer; std::string buffer;
wxString path, publicID; wxString path, publicID;
bool rootElementSeen, insideDtd; bool associated, insideDtd;
}; };
class XmlAssociateDtd : public WrapExpat class XmlAssociateDtd : public WrapExpat
@ -67,6 +67,7 @@ class XmlAssociateDtd : public WrapExpat
const XML_Char *pubid, const XML_Char *pubid,
int has_internal_subset ); int has_internal_subset );
static void XMLCALL enddoctypehandler ( void *userData ); static void XMLCALL enddoctypehandler ( void *userData );
static void associate ( DtdData *d, const char *doctypeName );
}; };
#endif #endif

View File

@ -36,10 +36,10 @@ XmlAssociateXsd::XmlAssociateXsd (
{ {
d->buffer.reserve ( size ); d->buffer.reserve ( size );
d->path = path; d->path = path;
d->rootElementSeen = false; d->depth = 0;
XML_SetElementHandler ( p, start, end ); XML_SetElementHandler ( p, start, end );
XML_SetDefaultHandlerExpand ( p, defaulthandler ); XML_SetDefaultHandlerExpand ( p, defaulthandler );
XML_SetUserData ( p, d.get() ); XML_SetUserData ( p, this );
std::auto_ptr<XmlParseSchemaNs> parser ( new XmlParseSchemaNs() ); std::auto_ptr<XmlParseSchemaNs> parser ( new XmlParseSchemaNs() );
std::string normalisedPath, buffer; std::string normalisedPath, buffer;
@ -80,8 +80,8 @@ void XMLCALL XmlAssociateXsd::defaulthandler (
{ {
if ( !data || !s ) if ( !data || !s )
return; return;
AssociateXsdData *d; XmlAssociateXsd *pThis = (XmlAssociateXsd *)data;
d = ( AssociateXsdData * ) data; AssociateXsdData *d = pThis->d.get();
if ( d ) if ( d )
d->buffer.append ( s, len ); d->buffer.append ( s, len );
} }
@ -92,10 +92,17 @@ void XMLCALL XmlAssociateXsd::start ( void *data,
{ {
if ( !data ) if ( !data )
return; return;
AssociateXsdData *d; XmlAssociateXsd *pThis = (XmlAssociateXsd *)data;
d = ( AssociateXsdData * ) data; AssociateXsdData *d = pThis->d.get();
d->depth++;
if ( d->depth > 1 )
{
XML_DefaultCurrent ( pThis->p );
return;
}
d->buffer += "<"; d->buffer += "<";
d->buffer += el; d->buffer += el;
@ -116,33 +123,31 @@ void XMLCALL XmlAssociateXsd::start ( void *data,
} }
attr += 2; attr += 2;
} }
if ( !d->rootElementSeen )
{
d->buffer += " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
std::map<std::string, std::string>::iterator it;
for ( it = d->namespaceMap.begin(); it != d->namespaceMap.end(); ++it )
{
d->buffer += " ";
d->buffer += it->first;
d->buffer += "=\"";
d->buffer += it->second;
d->buffer += "\"";
}
d->buffer += " xsi:"; d->buffer += " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
bool withNamespace = std::map<std::string, std::string>::iterator it;
( d->namespaceMap.find ( "xmlns" ) != d->namespaceMap.end() ); for ( it = d->namespaceMap.begin(); it != d->namespaceMap.end(); ++it )
d->buffer += ( withNamespace ) ? "schemaLocation" : "noNamespaceSchemaLocation"; {
d->buffer += " ";
d->buffer += it->first;
d->buffer += "=\""; d->buffer += "=\"";
if ( withNamespace ) d->buffer += it->second;
{
d->buffer += d->namespaceMap["xmlns"];
d->buffer += " ";
}
d->buffer += d->path.utf8_str(); // TODO: Apply the encoding of the parser
d->buffer += "\""; d->buffer += "\"";
d->rootElementSeen = true;
} }
d->buffer += " xsi:";
bool withNamespace =
( d->namespaceMap.find ( "xmlns" ) != d->namespaceMap.end() );
d->buffer += ( withNamespace ) ? "schemaLocation" : "noNamespaceSchemaLocation";
d->buffer += "=\"";
if ( withNamespace )
{
d->buffer += d->namespaceMap["xmlns"];
d->buffer += " ";
}
d->buffer += d->path.utf8_str(); // TODO: Apply the encoding of the parser
d->buffer += "\"";
d->buffer += ">"; d->buffer += ">";
} }
@ -150,9 +155,15 @@ void XMLCALL XmlAssociateXsd::end ( void *data, const XML_Char *el )
{ {
if ( !data ) if ( !data )
return; return;
AssociateXsdData *d; XmlAssociateXsd *pThis = (XmlAssociateXsd *)data;
d = ( AssociateXsdData * ) data; AssociateXsdData *d = pThis->d.get();
d->buffer += "</"; d->depth--;
d->buffer += el; if ( d->depth )
d->buffer += ">"; XML_DefaultCurrent ( pThis->p );
else
{
d->buffer += "</";
d->buffer += el;
d->buffer += ">";
}
} }

View File

@ -32,7 +32,7 @@ struct AssociateXsdData : public ParserData
{ {
std::string buffer;//, namespaceAttribute; std::string buffer;//, namespaceAttribute;
wxString path; wxString path;
bool rootElementSeen; size_t depth;
std::map<std::string, std::string> namespaceMap; std::map<std::string, std::string> namespaceMap;
}; };

View File

@ -33,9 +33,9 @@ XmlAssociateXsl::XmlAssociateXsl (
{ {
d->buffer.reserve ( size ); d->buffer.reserve ( size );
d->path = path; d->path = path;
d->rootElementSeen = false; d->associated = false;
XML_SetUserData ( p, d.get() ); XML_SetUserData ( p, this );
XML_SetElementHandler ( p, start, end ); XML_SetElementHandler ( p, start, NULL );
XML_SetProcessingInstructionHandler ( p, processinghandler ); XML_SetProcessingInstructionHandler ( p, processinghandler );
XML_SetDefaultHandlerExpand ( p, defaulthandler ); XML_SetDefaultHandlerExpand ( p, defaulthandler );
} }
@ -48,8 +48,8 @@ void XMLCALL XmlAssociateXsl::defaulthandler (
const XML_Char *s, const XML_Char *s,
int len ) int len )
{ {
XslData *d; XmlAssociateXsl *pThis = (XmlAssociateXsl *)data;
d = ( XslData * ) data; XslData *d = pThis->d.get();
d->buffer.append ( s, len ); d->buffer.append ( s, len );
} }
@ -57,36 +57,25 @@ void XMLCALL XmlAssociateXsl::start ( void *data,
const XML_Char *el, const XML_Char *el,
const XML_Char **attr ) const XML_Char **attr )
{ {
XslData *d; XmlAssociateXsl *pThis = (XmlAssociateXsl *)data;
d = ( XslData * ) data; XslData *d = pThis->d.get();
if ( !d->rootElementSeen ) if ( !d->associated )
{ {
d->buffer += "<?xml-stylesheet type=\"text/xsl\" href=\""; d->buffer += "<?xml-stylesheet type=\"text/xsl\" href=\"";
d->buffer += d->path.utf8_str(); // TODO: Apply the encoding of the parser d->buffer += d->path.utf8_str(); // TODO: Apply the encoding of the parser
d->buffer += "\"?>\n"; d->buffer += "\"?>\n";
d->rootElementSeen = true; d->associated = true;
} }
d->buffer += "<"; XML_DefaultCurrent ( pThis->p );
d->buffer += el; XML_SetElementHandler ( pThis->p, NULL, NULL );
while ( *attr )
{
d->buffer += " ";
d->buffer += *attr;
d->buffer += "=\"";
d->buffer += xmliseAttribute ( * ( attr + 1 ) );
d->buffer += "\"";
attr += 2;
}
d->buffer += ">";
} }
void XMLCALL XmlAssociateXsl::end ( void *data, const XML_Char *el ) void XMLCALL XmlAssociateXsl::end ( void *data, const XML_Char *el )
{ {
XslData *d; XmlAssociateXsl *pThis = (XmlAssociateXsl *)data;
d = ( XslData * ) data; XslData *d = pThis->d.get();
d->buffer += "</"; d->buffer += "</";
d->buffer += el; d->buffer += el;
d->buffer += ">"; d->buffer += ">";
@ -97,15 +86,24 @@ void XMLCALL XmlAssociateXsl::processinghandler (
const XML_Char *target, const XML_Char *target,
const XML_Char *datastring ) const XML_Char *datastring )
{ {
XslData *d; XmlAssociateXsl *pThis = (XmlAssociateXsl *)data;
d = ( XslData * ) data; XslData *d = pThis->d.get();
if ( !strcmp ( target, "xml-stylesheet" ) ) if ( !strcmp ( target, "xml-stylesheet" ) )
return; {
d->buffer += "<?xml-stylesheet type=\"text/xsl\" href=\"";
d->buffer += d->path.utf8_str(); // TODO: Apply the encoding of the parser
d->buffer += "\"?>";
d->associated = true;
d->buffer += "<?"; XML_SetElementHandler ( pThis->p, NULL, NULL );
d->buffer += target; }
d->buffer += " "; else
d->buffer += datastring; {
d->buffer += "?>"; d->buffer += "<?";
d->buffer += target;
d->buffer += " ";
d->buffer += datastring;
d->buffer += "?>";
}
} }

View File

@ -31,7 +31,7 @@ struct XslData : public ParserData
{ {
std::string buffer; std::string buffer;
wxString path; wxString path;
bool rootElementSeen; bool associated;
}; };
class XmlAssociateXsl : public WrapExpat class XmlAssociateXsl : public WrapExpat