[Libreoffice-commits] core.git: include/sax include/xmloff offapi/com sax/source xmloff/source

dante (via logerrit) logerrit at kemper.freedesktop.org
Sun Nov 29 18:54:01 UTC 2020


 include/sax/fastparser.hxx                  |    2 +
 include/xmloff/xmlimp.hxx                   |    2 +
 offapi/com/sun/star/xml/sax/XFastParser.idl |    7 ++++
 sax/source/fastparser/fastparser.cxx        |   43 +++++++++++++++++++++++++++-
 xmloff/source/core/xmlimp.cxx               |    5 +++
 5 files changed, 58 insertions(+), 1 deletion(-)

New commits:
commit f28c8085d14f65aa40da434c1d9d5ae60bfad862
Author:     dante <dante19031999 at gmail.com>
AuthorDate: Sat Nov 28 14:26:40 2020 +0100
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Sun Nov 29 19:53:20 2020 +0100

    Preparing for mathml support of custom entity references.
    
    This should be enough for the starmath mathml project.
    It can be reused from other modules for doing custom stuff.
    It keeps to minimum changes on generic modules.
    My current abilities don't allow me to go much beyond this approach.
    
    Change-Id: If7f157f8a71d6c3bff50fdbcd80bed23c92f40bb
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/106804
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/include/sax/fastparser.hxx b/include/sax/fastparser.hxx
index a7f1e0c015f9..5af3ac1d1f57 100644
--- a/include/sax/fastparser.hxx
+++ b/include/sax/fastparser.hxx
@@ -64,6 +64,8 @@ public:
     virtual void SAL_CALL setEntityResolver( const css::uno::Reference< css::xml::sax::XEntityResolver >& Resolver ) override;
     virtual void SAL_CALL setLocale( const css::lang::Locale& rLocale ) override;
     virtual void SAL_CALL setNamespaceHandler( const css::uno::Reference< css::xml::sax::XFastNamespaceHandler >& Handler) override;
+    virtual void SAL_CALL setCustomEntityNames( const ::css::uno::Sequence< ::rtl::OUString >& names,
+                                                const ::css::uno::Sequence< ::rtl::OUString >& replacements )  override;
 
     // XServiceInfo
     virtual OUString SAL_CALL getImplementationName(  ) override;
diff --git a/include/xmloff/xmlimp.hxx b/include/xmloff/xmlimp.hxx
index 763e24ae6db9..73f35b90f631 100644
--- a/include/xmloff/xmlimp.hxx
+++ b/include/xmloff/xmlimp.hxx
@@ -335,6 +335,8 @@ public:
     virtual void SAL_CALL setEntityResolver( const css::uno::Reference< css::xml::sax::XEntityResolver >& Resolver ) override;
     virtual void SAL_CALL setLocale( const css::lang::Locale& rLocale ) override;
     virtual void SAL_CALL setNamespaceHandler( const css::uno::Reference< css::xml::sax::XFastNamespaceHandler >& Handler) override;
+    virtual void SAL_CALL setCustomEntityNames( const ::css::uno::Sequence< ::rtl::OUString >& names,
+                                                const ::css::uno::Sequence< ::rtl::OUString >& replacements )  override;
 
     // XImporter
     virtual void SAL_CALL setTargetDocument( const css::uno::Reference< css::lang::XComponent >& xDoc ) override;
diff --git a/offapi/com/sun/star/xml/sax/XFastParser.idl b/offapi/com/sun/star/xml/sax/XFastParser.idl
index 6ea4278a2c8d..e9081bbd6235 100644
--- a/offapi/com/sun/star/xml/sax/XFastParser.idl
+++ b/offapi/com/sun/star/xml/sax/XFastParser.idl
@@ -40,6 +40,7 @@
 #include <com/sun/star/lang/IllegalArgumentException.idl>
 
 #include <com/sum/star/xml/sax/XFastNamespaceHandler.idl>
+//#include <com/sun/star/xml/dom/XEntity.idl>
 
 
 module com {  module sun {  module star {  module xml {  module sax {
@@ -157,6 +158,12 @@ interface XFastParser: com::sun::star::uno::XInterface
 
     /** @since LibreOffice 5.3 */
     void setNamespaceHandler( [in] XFastNamespaceHandler Handler);
+
+    /**
+      * Simulate a DTD file.
+      * Will allow to use customized entity references like ∞ .
+      */
+    void setCustomEntityNames( [in] sequence<string> names, [in] sequence<string> replacements);
 };
 
 
diff --git a/sax/source/fastparser/fastparser.cxx b/sax/source/fastparser/fastparser.cxx
index e7f21da8e658..a12bd23fc2d5 100644
--- a/sax/source/fastparser/fastparser.cxx
+++ b/sax/source/fastparser/fastparser.cxx
@@ -119,7 +119,6 @@ struct SaxContext
     }
 };
 
-
 struct ParserData
 {
     css::uno::Reference< css::xml::sax::XFastDocumentHandler > mxDocumentHandler;
@@ -211,6 +210,11 @@ public:
     explicit FastSaxParserImpl();
     ~FastSaxParserImpl();
 
+private:
+    ::css::uno::Sequence< ::rtl::OUString > mEntityNames;
+    ::css::uno::Sequence< ::rtl::OUString > mEntityReplacements;
+
+public:
     // XFastParser
     /// @throws css::xml::sax::SAXException
     /// @throws css::io::IOException
@@ -230,6 +234,8 @@ public:
     void setErrorHandler( const css::uno::Reference< css::xml::sax::XErrorHandler >& Handler );
     /// @throws css::uno::RuntimeException
     void setNamespaceHandler( const css::uno::Reference< css::xml::sax::XFastNamespaceHandler >& Handler);
+    // Fake DTD file
+    void setCustomEntityNames( const ::css::uno::Sequence< ::rtl::OUString >& names, const ::css::uno::Sequence< ::rtl::OUString >& replacements );
 
     // called by the C callbacks of the expat parser
     void callbackStartElement( const xmlChar *localName , const xmlChar* prefix, const xmlChar* URI,
@@ -237,6 +243,7 @@ public:
     void callbackEndElement();
     void callbackCharacters( const xmlChar* s, int nLen );
     void callbackProcessingInstruction( const xmlChar *target, const xmlChar *data );
+    xmlEntityPtr callbackGetEntity( const xmlChar *name );
 
     void pushEntity(const ParserData&, xml::sax::InputSource const&);
     void popEntity();
@@ -325,6 +332,12 @@ static void call_callbackProcessingInstruction( void *userData, const xmlChar *t
     pFastParser->callbackProcessingInstruction( target, data );
 }
 
+static xmlEntityPtr call_callbackGetEntity( void *userData, const xmlChar *name)
+{
+    FastSaxParserImpl* pFastParser = static_cast<FastSaxParserImpl*>( userData );
+    return pFastParser->callbackGetEntity( name );
+}
+
 }
 
 class FastLocatorImpl : public WeakImplHelper< XLocator >
@@ -921,6 +934,12 @@ void FastSaxParserImpl::setNamespaceHandler( const Reference< XFastNamespaceHand
     maData.mxNamespaceHandler = Handler;
 }
 
+void FastSaxParserImpl::setCustomEntityNames( const ::css::uno::Sequence< ::rtl::OUString >& names, const ::css::uno::Sequence< ::rtl::OUString >& replacements )
+{
+    mEntityNames = names;
+    mEntityReplacements = replacements;
+}
+
 void FastSaxParserImpl::deleteUsedEvents()
 {
     Entity& rEntity = getEntity();
@@ -1036,6 +1055,7 @@ void FastSaxParserImpl::parse()
     callbacks.endElementNs = call_callbackEndElement;
     callbacks.characters = call_callbackCharacters;
     callbacks.processingInstruction = call_callbackProcessingInstruction;
+    callbacks.getEntity = call_callbackGetEntity;
     callbacks.initialized = XML_SAX2_MAGIC;
     int nRead = 0;
     do
@@ -1344,6 +1364,21 @@ void FastSaxParserImpl::callbackProcessingInstruction( const xmlChar *target, co
         rEntity.processingInstruction( rEvent.msNamespace, rEvent.msElementName );
 }
 
+xmlEntityPtr FastSaxParserImpl::callbackGetEntity( const xmlChar *name )
+{
+    for( size_t i = 0; i < mEntityNames.size(); ++i )
+    {
+        if( mEntityNames[i].compareToAscii(XML_CAST(name)) == 0 )
+        {
+            return xmlNewEntity( nullptr,
+                BAD_CAST(OUStringToOString(mEntityNames[i],RTL_TEXTENCODING_UTF8).getStr()),
+                XML_INTERNAL_GENERAL_ENTITY, nullptr, nullptr,
+                BAD_CAST(OUStringToOString(mEntityReplacements[i],RTL_TEXTENCODING_UTF8).getStr()));
+        }
+    }
+    return xmlGetPredefinedEntity(name);
+}
+
 FastSaxParser::FastSaxParser() : mpImpl(new FastSaxParserImpl) {}
 
 FastSaxParser::~FastSaxParser()
@@ -1421,6 +1456,12 @@ OUString FastSaxParser::getImplementationName()
     return "com.sun.star.comp.extensions.xml.sax.FastParser";
 }
 
+void FastSaxParser::setCustomEntityNames( const ::css::uno::Sequence< ::rtl::OUString >& names, const ::css::uno::Sequence< ::rtl::OUString >& replacements )
+{
+    assert(names.size() == replacements.size());
+    mpImpl->setCustomEntityNames(names, replacements);
+}
+
 sal_Bool FastSaxParser::supportsService( const OUString& ServiceName )
 {
     return cppu::supportsService(this, ServiceName);
diff --git a/xmloff/source/core/xmlimp.cxx b/xmloff/source/core/xmlimp.cxx
index c47ad94ceffa..e96e65072ead 100644
--- a/xmloff/source/core/xmlimp.cxx
+++ b/xmloff/source/core/xmlimp.cxx
@@ -545,6 +545,11 @@ void SAL_CALL SvXMLImport::setNamespaceHandler( const uno::Reference< xml::sax::
     mxParser->setNamespaceHandler( Handler );
 }
 
+void SAL_CALL SvXMLImport::setCustomEntityNames( const ::css::uno::Sequence< ::rtl::OUString >& names,
+                                                 const ::css::uno::Sequence< ::rtl::OUString >& replacements )
+{
+    mxParser->setCustomEntityNames( names, replacements );
+}
 
 void SAL_CALL SvXMLImport::startDocument()
 {


More information about the Libreoffice-commits mailing list