Improved catalog resolving

This commit is contained in:
Zane U. Ji 2013-10-26 21:15:42 +08:00
parent 51cd9699c3
commit c0db1726e9
11 changed files with 53 additions and 57 deletions

View File

@ -6,8 +6,8 @@
<public publicId="-//W3C//ENTITIES Latin 1 for XHTML//EN" uri="../dtd/xhtml-lat1.ent"/> <public publicId="-//W3C//ENTITIES Latin 1 for XHTML//EN" uri="../dtd/xhtml-lat1.ent"/>
<public publicId="-//W3C//ENTITIES Symbols for XHTML//EN" uri="../dtd/xhtml-symbol.ent"/> <public publicId="-//W3C//ENTITIES Symbols for XHTML//EN" uri="../dtd/xhtml-symbol.ent"/>
<public publicId="-//W3C//ENTITIES Special for XHTML//EN" uri="../dtd/xhtml-special.ent"/> <public publicId="-//W3C//ENTITIES Special for XHTML//EN" uri="../dtd/xhtml-special.ent"/>
<public publicId="-//OASIS//DTD DocBook XML 5.0b3//EN" uri="../dtd/docbook/5.0b/docbook.dtd"/> <public publicId="-//OASIS//DTD DocBook XML V5.0b3//EN" uri="../dtd/docbook/5.0b/docbook.dtd"/>
<public publicId="-//OASIS//DTD DocBook XML 4.4//EN" uri="../dtd/docbook/4.4/docbookx.dtd"/> <public publicId="-//OASIS//DTD DocBook XML V4.4//EN" uri="../dtd/docbook/4.4/docbookx.dtd"/>
<public publicId="-//TEI P4//DTD Main Document Type//EN" uri="../dtd/tei/p4/tei2.dtd"/> <public publicId="-//TEI P4//DTD Main Document Type//EN" uri="../dtd/tei/p4/tei2.dtd"/>
<public publicId="-//TEI P5//DTD Main Document Type//EN" uri="../dtd/tei/p5/tei_all.dtd"/> <public publicId="-//TEI P5//DTD Main Document Type//EN" uri="../dtd/tei/p5/tei_all.dtd"/>
<public publicId="-//OASIS//DTD DITA Map//EN" uri="../dtd/dita/map.dtd"/> <public publicId="-//OASIS//DTD DITA Map//EN" uri="../dtd/dita/map.dtd"/>
@ -21,4 +21,5 @@
<public publicId="-//OpenLearn//DTD" uri="../dtd/OUGenericFull.dtd"/> <public publicId="-//OpenLearn//DTD" uri="../dtd/OUGenericFull.dtd"/>
<public publicId="-//Moodle Glossary//DTD" uri="../dtd/moodle_glossary.dtd"/> <public publicId="-//Moodle Glossary//DTD" uri="../dtd/moodle_glossary.dtd"/>
<public publicId="-//Moodle XML//DTD" uri="../dtd/moodle_xml.dtd"/> <public publicId="-//Moodle XML//DTD" uri="../dtd/moodle_xml.dtd"/>
<rewriteSystem systemIdStartString="http://www.w3.org/TR/xhtml1/DTD/" rewritePrefix="../dtd/"/>
</catalog> </catalog>

View File

@ -28,8 +28,10 @@ CatalogResolver::~CatalogResolver()
{ {
} }
std::string CatalogResolver::lookupPublicId ( const std::string &publicId ) wxString CatalogResolver::catalogResolve (
const wxString &publicId,
const wxString &systemId )
{ {
//std::auto_ptr<WrapLibxml> libxml ( new WrapLibxml ( false, catalogPath ) ); //std::auto_ptr<WrapLibxml> libxml ( new WrapLibxml ( false, catalogPath ) );
return WrapLibxml::lookupPublicId ( publicId ); return WrapLibxml::catalogResolve ( publicId, systemId );
} }

View File

@ -28,8 +28,10 @@ class CatalogResolver : protected WrapLibxml
public: public:
CatalogResolver(); CatalogResolver();
~CatalogResolver(); ~CatalogResolver();
std::string lookupPublicId ( wxString catalogResolve
const std::string& publicId); ( const wxString &pubIdUtf8
, const wxString &sysIdUtf8
);
}; };
#endif #endif

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML 4.4//EN" "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"> <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN" "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd">
<article> <article>
<title>Title</title> <title>Title</title>
<para>First paragraph</para> <para>First paragraph</para>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML 4.4//EN" "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"> <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN" "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd">
<book> <book>
<title>Book title</title> <title>Book title</title>
<chapter> <chapter>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML 5.0b3//EN" "http://www.oasis-open.org/docbook/xml/5.0b3/dtd/docbook.dtd"> <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V5.0b3//EN" "http://www.oasis-open.org/docbook/xml/5.0b3/dtd/docbook.dtd">
<article xmlns="http://docbook.org/ns/docbook" version="5.0" xml:lang="en"> <article xmlns="http://docbook.org/ns/docbook" version="5.0" xml:lang="en">
<title>Title</title> <title>Title</title>
<para>First paragraph</para> <para>First paragraph</para>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML 5.0b3//EN" "http://www.oasis-open.org/docbook/xml/5.0b3/dtd/docbook.dtd"> <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V5.0b3//EN" "http://www.oasis-open.org/docbook/xml/5.0b3/dtd/docbook.dtd">
<book> <book>
<title>Book title</title> <title>Book title</title>
<titleabbrev>Abbreviated book title</titleabbrev> <titleabbrev>Abbreviated book title</titleabbrev>

View File

@ -561,21 +561,23 @@ std::string WrapLibxml::getOutput()
return output; return output;
} }
std::string WrapLibxml::lookupPublicId ( const std::string& id ) wxString WrapLibxml::catalogResolve
( const wxString &publicId
, const wxString &systemId
)
{ {
std::string ret; char *s = ( char * ) xmlCatalogResolve (
char *s = ( char * ) xmlCatalogResolvePublic ( ( const xmlChar * ) id.c_str() ); ( const xmlChar * ) ( const char *) publicId.mb_str ( wxConvUTF8 ),
( const xmlChar * ) ( const char *) systemId.mb_str ( wxConvUTF8 ) );
if ( s == NULL ) if ( s == NULL )
return ret; return wxEmptyString;
wxString url ( s, wxConvUTF8 ); wxString url ( s, wxConvUTF8 );
wxFileName file = wxFileSystem::URLToFileName ( url );
if ( file.IsFileReadable() )
ret = file.GetFullPath().mb_str ( wxConvLocal );
else
ret = s;
xmlFree ( s ); xmlFree ( s );
return ret; wxFileName file = wxFileSystem::URLToFileName ( url );
if ( file.IsFileReadable() )
return file.GetFullPath();
return url;
} }

View File

@ -68,7 +68,9 @@ class WrapLibxml
const std::string& fileNameSource, const std::string& fileNameSource,
const std::string& fileNameDestination, const std::string& fileNameDestination,
const std::string& encoding ); const std::string& encoding );
std::string lookupPublicId ( const std::string& id ); wxString catalogResolve (
const wxString &publicId,
const wxString &systemId );
private: private:
bool netAccess; bool netAccess;
std::string output, nonParserError; std::string output, nonParserError;

View File

@ -7,8 +7,10 @@
#include "replace.h" #include "replace.h"
#endif #endif
#include "wrapxerces.h"
InputSource *XercesCatalogResolver::resolveEntity ( InputSource *XercesCatalogResolver::resolveEntity (
const XMLCh* const publicID, const XMLCh* const publicId,
const XMLCh* const systemId ) const XMLCh* const systemId )
{ {
/* the following _should_ work but doesn't always, so drop it for now /* the following _should_ work but doesn't always, so drop it for now
@ -69,27 +71,15 @@ InputSource *XercesCatalogResolver::resolveEntity (
#endif #endif
*/ */
if ( publicID == NULL || *publicID == '\0' ) wxString pubId, sysId, resolved;
return NULL; pubId = WrapXerces::toString ( publicId );
sysId = WrapXerces::toString ( systemId );
// Since public IDs consist of ANSII characters, it doesn't matter how resolved = catalogResolve ( pubId, sysId );
// publicID is encoded as long as it's a multibyte string
char *narrowPublicId = XMLString::transcode ( publicID );
if ( narrowPublicId == NULL )
return NULL;
std::string resolved;
resolved = lookupPublicId ( narrowPublicId );
XMLString::release ( &narrowPublicId );
if ( resolved.empty() ) if ( resolved.empty() )
return NULL; return NULL;
XMLCh *wideResolved = XMLString::transcode ( resolved.c_str() ); InputSource *source = new LocalFileInputSource (
( const XMLCh * ) WrapXerces::toString ( resolved ).GetData() );
InputSource *source = (InputSource *)new LocalFileInputSource ( wideResolved );
XMLString::release ( &wideResolved );
return source; return source;
} }

View File

@ -319,30 +319,31 @@ int XMLCALL XmlPromptGenerator::externalentityrefhandler (
return ret; return ret;
} }
std::string stdPublicId; wxString widePublicId ( publicId, wxConvUTF8 );
if ( publicId ) wxString wideSystemId ( systemId, wxConvUTF8 );
stdPublicId = publicId;
CatalogResolver cr; CatalogResolver cr;
std::string stdSystemId = cr.lookupPublicId ( stdPublicId ); wideSystemId = cr.catalogResolve ( widePublicId, wideSystemId );
if ( stdSystemId.empty() ) if ( wideSystemId.empty() )
{ {
if ( systemId ) if ( systemId )
stdSystemId = systemId; wideSystemId = wxString ( systemId, wxConvUTF8 );
if ( base ) if ( base )
{ {
std::string test = PathResolver::run ( stdSystemId, base ); wxString test = PathResolver::run ( wideSystemId,
wxString ( base, wxConvUTF8 ) );
if ( !test.empty() ) if ( !test.empty() )
{ {
stdSystemId = test; wideSystemId = test;
} }
} }
} }
if ( !stdSystemId.empty() ) std::string localName;
localName = wideSystemId.mb_str ( wxConvLocal );
if ( !localName.empty() )
{ {
ReadFile::run ( stdSystemId, buffer ); ReadFile::run ( localName, buffer );
} }
std::string encoding = XmlEncodingHandler::get ( buffer ); std::string encoding = XmlEncodingHandler::get ( buffer );
@ -350,11 +351,7 @@ int XMLCALL XmlPromptGenerator::externalentityrefhandler (
if ( !dtdParser ) if ( !dtdParser )
return XML_STATUS_ERROR; return XML_STATUS_ERROR;
wxString wideName, wideDir; XML_SetBase ( dtdParser, wideSystemId.utf8_str() );
wideName = wxString ( stdSystemId.c_str(), wxConvUTF8, stdSystemId.size() );
wxFileName fn ( wideName );
wideDir = fn.GetPath();
XML_SetBase ( dtdParser, wideName.mb_str ( wxConvUTF8 ) );
ret = XML_Parse ( dtdParser, buffer.c_str(), buffer.size(), true ); ret = XML_Parse ( dtdParser, buffer.c_str(), buffer.size(), true );
XML_ParserFree ( dtdParser ); XML_ParserFree ( dtdParser );