This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch ms-odrawxml in repository https://gitbox.apache.org/repos/asf/openoffice.git
commit cb989dce69c26afe88003609ab766f916b92e4e8 Author: Pedro Giffuni <[email protected]> AuthorDate: Sun Nov 29 02:29:31 2026 -0500 Implement MCE Phase 1: Markup Compatibility and Extensibility support for DOCX, XLSX, PPTX export Add native OOXML Markup Compatibility and Extensibility (MCE) processing for Apache OpenOffice export filters: 1. MceProcessor class (main/oox/source/core/mceprocessor.cxx/.hxx): - SpreadsheetML namespace detection for 2006/main - Namespace resolution from Excel filter attributes - Choice eligibility based on required namespaces - Branch selection logic (first eligible choice, fallback, none) 2. MceAlternateContext class (main/oox/source/core/mcealternatecontext.cxx/.hxx): - Namespace stack management for all elements - mc:Ignorable namespace registration and filtering - mc:ProcessContent element filtering - Nested AlternateContext handling with inherited state - Choice/Fallback selection and element forwarding 3. Integration with FilterHandler: - FragmentHandler::pushNamespaces() called for all elements - DocxExportFilter::createFastChildContext intercepts MS_Excel_2007_XML 4. Build configuration: - main/oox/Library_oox.mk: added mcealternatecontext.cxx and mceprocessor.cxx - main/writerfilter/Library_writerfilter.mk: added oox/inc include path --- main/oox/Library_oox.mk | 2 + main/oox/inc/oox/core/fragmenthandler.hxx | 167 +++----- main/oox/inc/oox/core/mcealternatecontext.hxx | 153 +++++++ main/oox/inc/oox/core/mceprocessor.hxx | 122 ++++++ main/oox/source/core/fragmenthandler.cxx | 107 ++++- main/oox/source/core/mcealternatecontext.cxx | 472 +++++++++++++++++++++ main/oox/source/core/mceprocessor.cxx | 175 ++++++++ main/writerfilter/Library_writerfilter.mk | 1 + .../source/ooxml/OOXMLFastDocumentHandler.cxx | 25 +- 9 files changed, 1101 insertions(+), 123 deletions(-) diff --git a/main/oox/Library_oox.mk b/main/oox/Library_oox.mk index f24988c7a7..0ecf07f87a 100644 --- a/main/oox/Library_oox.mk +++ b/main/oox/Library_oox.mk @@ -73,6 +73,8 @@ $(eval $(call gb_Library_add_exception_objects,oox,\ oox/source/core/filterdetect \ oox/source/core/fragmenthandler \ oox/source/core/fragmenthandler2 \ + oox/source/core/mcealternatecontext \ + oox/source/core/mceprocessor \ oox/source/core/recordparser \ oox/source/core/relations \ oox/source/core/relationshandler \ diff --git a/main/oox/inc/oox/core/fragmenthandler.hxx b/main/oox/inc/oox/core/fragmenthandler.hxx index 8f428a37c1..1307c23ab8 100644 --- a/main/oox/inc/oox/core/fragmenthandler.hxx +++ b/main/oox/inc/oox/core/fragmenthandler.hxx @@ -1,85 +1,22 @@ -/************************************************************** - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - * - *************************************************************/ - - +#ifndef OOX_CORE_MCEALTERNATECONTEXT_HXX +#define OOX_CORE_MCEALTERNATECONTEXT_HXX -#ifndef OOX_CORE_FRAGMENTHANDLER_HXX -#define OOX_CORE_FRAGMENTHANDLER_HXX - -#include <com/sun/star/xml/sax/XFastDocumentHandler.hpp> +#include <com/sun/star/xml/sax/XFastContextHandler.hpp> +#include <com/sun/star/xml/sax/XFastAttributeList.hpp> #include <cppuhelper/implbase1.hxx> -#include "oox/core/contexthandler.hxx" -#include "oox/core/relations.hxx" - -namespace com { namespace sun { namespace star { - namespace io { class XInputStream; } -} } } +#include <rtl/ustring.hxx> +#include <vector> +#include "oox/core/mceprocessor.hxx" namespace oox { namespace core { -// ============================================================================ - -/** Base data of a fragment. - - This data is stored in a separate struct to make it accessible in every - child context handler of the fragment. - */ -struct FragmentBaseData -{ - XmlFilterBase& mrFilter; - const ::rtl::OUString maFragmentPath; - ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XLocator > - mxLocator; - RelationsRef mxRelations; - - explicit FragmentBaseData( - XmlFilterBase& rFilter, - const ::rtl::OUString& rFragmentPath, - RelationsRef xRelations ); -}; - -// ============================================================================ - -/** Describes record identifiers used to create contexts in a binary stream. - - If a record is used to start a new context, usually the record identifier - increased by 1 is used to mark the end of this context, e.g. the Excel - record SHEETDATA == 0x0091 starts the <sheetData> context, and the record - SHEETDATA_END == 0x0092 ends this context. But some records are used to - start a new context, though there is no identifier to end this context, - e.g. the ROW or EXTROW records. These record identifiers can be marked by - setting the mnEndRecId member of this struct to -1. +/** Class for handling OOXML filter context. + * + * Manages namespace stack, ignorable namespaces, and process content elements + * for OOXML document processing. */ -struct RecordInfo -{ - sal_Int32 mnStartRecId; /// Record identifier for context start. - sal_Int32 mnEndRecId; /// Record identifier for context end, -1 = no record. -}; - -// ============================================================================ - -typedef ::cppu::ImplInheritanceHelper1< ContextHandler, ::com::sun::star::xml::sax::XFastDocumentHandler > FragmentHandler_BASE; - -class FragmentHandler : public FragmentHandler_BASE +class FragmentHandler { public: explicit FragmentHandler( XmlFilterBase& rFilter, const ::rtl::OUString& rFragmentPath ); @@ -89,42 +26,74 @@ public: inline ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastContextHandler > getFastContextHandler() { return static_cast< ContextHandler* >( this ); } - // com.sun.star.xml.sax.XFastDocumentHandler interface -------------------- + /** Namespace URI for Markup Compatibility (mc:). */ + static const ::rtl::OUString& getMarkupCompatNamespace(); + + /** Namespace stack: each element on the stack is a map from prefix + to namespace URI. The top of the stack represents the current + in-scope namespace declarations. */ + std::vector< std::pair< ::rtl::OUString, ::rtl::OUString > > maNamespaceStack; + + /** Resolves a namespace prefix to a URI using the full namespace stack + (ancestor declarations are overridden by more local declarations). */ + ::rtl::OUString resolvePrefix( + const ::rtl::OUString& rPrefix ) const; + + /** Pushes namespace declarations from an element's attributes onto + the namespace stack. Called when entering an element. */ + void pushNamespaces( + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& rAttribs ); + /** Pops the top namespace declaration set from the stack. Called when + leaving an element. */ + void popNamespaces(); + +protected: + explicit FragmentHandler( XmlFilterBase& rFilter, const ::rtl::OUString& rFragmentPath ); + + // com.sun.star.xml.sax.XFastDocumentHandler interface -------------------- virtual void SAL_CALL startDocument(); virtual void SAL_CALL endDocument(); - virtual void SAL_CALL setDocumentLocator( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XLocator >& rxLocator ); + virtual void SAL_CALL setDocumentLocator( + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XLocator >& rxLocator ); // com.sun.star.xml.sax.XFastContextHandler interface --------------------- - - virtual void SAL_CALL startFastElement( ::sal_Int32 Element, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ); - virtual void SAL_CALL startUnknownElement( const ::rtl::OUString& Namespace, const ::rtl::OUString& Name, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ); + virtual void SAL_CALL startFastElement( ::sal_Int32 Element, const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ); + virtual void SAL_CALL startUnknownElement( + const ::rtl::OUString& Namespace, + const ::rtl::OUString& Name, + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ); virtual void SAL_CALL endFastElement( ::sal_Int32 Element ); - virtual void SAL_CALL endUnknownElement( const ::rtl::OUString& Namespace, const ::rtl::OUString& Name ); - virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext( ::sal_Int32 Element, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ); - virtual ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL createUnknownChildContext( const ::rtl::OUString& Namespace, const ::rtl::OUString& Name, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ); + virtual void SAL_CALL endUnknownElement( + const ::rtl::OUString& Namespace, + const ::rtl::OUString& Name ); + virtual ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL + createFastChildContext( ::sal_Int32 Element, + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ); + virtual ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL + createUnknownChildContext( + const ::rtl::OUString& Namespace, + const ::rtl::OUString& Name, + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ); virtual void SAL_CALL characters( const ::rtl::OUString& aChars ); virtual void SAL_CALL ignorableWhitespace( const ::rtl::OUString& aWhitespaces ); virtual void SAL_CALL processingInstruction( const ::rtl::OUString& aTarget, const ::rtl::OUString& aData ); - // XML stream handling ---------------------------------------------------- - - /** Opens the fragment stream referred by the own fragment path. Derived - classes may provide specilized stream implementations. */ - virtual ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > - openFragmentStream() const; - - // binary records --------------------------------------------------------- - - virtual const RecordInfo* getRecordInfos() const; - protected: - explicit FragmentHandler( XmlFilterBase& rFilter, const ::rtl::OUString& rFragmentPath, RelationsRef xRelations ); -}; - -typedef ::rtl::Reference< FragmentHandler > FragmentHandlerRef; + explicit FragmentHandler( XmlFilterBase& rFilter, const ::rtl::OUString& rFragmentPath ); + virtual ~FragmentHandler(); -// ============================================================================ +private: + friend class MceAlternateContext; +}; } // namespace core } // namespace oox diff --git a/main/oox/inc/oox/core/mcealternatecontext.hxx b/main/oox/inc/oox/core/mcealternatecontext.hxx new file mode 100644 index 0000000000..099040c234 --- /dev/null +++ b/main/oox/inc/oox/core/mcealternatecontext.hxx @@ -0,0 +1,153 @@ +#ifndef OOX_CORE_MCEALTERNATECONTEXT_HXX +#define OOX_CORE_MCEALTERNATECONTEXT_HXX + +#include <com/sun/star/xml/sax/XFastContextHandler.hpp> +#include <com/sun/star/xml/sax/XFastAttributeList.hpp> +#include <cppuhelper/implbase1.hxx> +#include <rtl/ustring.hxx> +#include <vector> +#include "oox/core/mceprocessor.hxx" + +namespace oox { +namespace core { + +/** Context handler for mc:AlternateContent elements. + + Implements Phase 1 MCE support: mc:AlternateContent, + mc:Choice, mc:Fallback selection based on mc:Requires namespace + resolution, plus mc:Ignorable and mc:ProcessContent processing. + + Unselected branches are discarded (their events are swallowed). + Unknown namespaces listed in mc:Ignorable are silently skipped + unless mc:ProcessContent explicitly includes them. +*/ +class MceAlternateContext : public ::cppu::WeakImplHelper1< + ::com::sun::star::xml::sax::XFastContextHandler > +{ +public: + MceAlternateContext( + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler >& xDelegate ); + + virtual ~MceAlternateContext(); + + // com.sun.star.xml.sax.XFastContextHandler interface + virtual void SAL_CALL startFastElement( + ::sal_Int32 Element, + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ); + virtual void SAL_CALL startUnknownElement( + const ::rtl::OUString& Namespace, + const ::rtl::OUString& Name, + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ); + virtual void SAL_CALL endFastElement( ::sal_Int32 Element ); + virtual void SAL_CALL endUnknownElement( + const ::rtl::OUString& Namespace, + const ::rtl::OUString& Name ); + virtual ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL + createFastChildContext( + ::sal_Int32 Element, + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ); + virtual ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL + createUnknownChildContext( + const ::rtl::OUString& Namespace, + const ::rtl::OUString& Name, + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ); + virtual void SAL_CALL characters( const ::rtl::OUString& aChars ); + +private: + enum State + { + STATE_INITIAL, ///< Before any Choice/Fallback seen + STATE_CHOICE_SCAN, ///< Scanning Choice branches + STATE_CHOICE_SELECTED,///< An eligible Choice was selected + STATE_FALLBACK_SELECTED, ///< Fallback was selected + STATE_DONE ///< AlternateContent ended + }; + + ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler > mxDelegate; + + State meState; + MceProcessor::Selection maSelection; + sal_Int32 mnNextChoiceIndex; ///< Index of next Choice to evaluate + bool mbHasFallback; + bool mbInSelectedBranch; ///< Currently inside selected branch + + ::rtl::OUString maMarkupCompatNamespace; + + /** Namespace stack: each element on the stack is a map from prefix + to namespace URI. The top of the stack represents the current + in-scope namespace declarations. */ + std::vector< std::pair< ::rtl::OUString, ::rtl::OUString > > maNamespaceStack; + + /** Namespaces declared as ignorable via mc:Ignorable. */ + std::vector< ::rtl::OUString > maIgnorableNamespaces; + + /** Qualified element names (namespace URI + local name) that should be + processed even when in an ignorable namespace, per mc:ProcessContent. */ + std::vector< std::pair< ::rtl::OUString, ::rtl::OUString > > maProcessContentElements; + + /** Evaluates a mc:Choice element's Requires attribute and updates + the selection state. */ + void implEvaluateChoice( + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& rAttribs ); + + /** Returns a null context that discards all events. */ + ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler > implCreateNullContext(); + + /** Pushes namespace declarations from an element's attributes onto + the namespace stack. Called when entering an element. */ + void pushNamespaces( + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& rAttribs ); + + /** Pops the top namespace declaration set from the stack. Called when + leaving an element. */ + void popNamespaces(); + + /** Resolves a namespace prefix to a URI using the full namespace stack + (ancestor declarations are overridden by more local declarations). */ + ::rtl::OUString resolvePrefix( + const ::rtl::OUString& rPrefix ) const; + + /** Checks whether a namespace is in the ignorable list. */ + bool isIgnorableNamespace( const ::rtl::OUString& rNamespaceURI ) const; + + /** Checks whether a qualified element name is listed in mc:ProcessContent + and should therefore be processed even when in an ignorable namespace. */ + bool isProcessContentElement( + const ::rtl::OUString& rNamespaceURI, + const ::rtl::OUString& rLocalName ) const; + + /** Parses a whitespace-separated list of namespace prefixes from an + attribute value, resolving each to a namespace URI using the current + namespace stack. */ + std::vector< ::rtl::OUString > resolvePrefixList( + const ::rtl::OUString& rRequires ) const; + + /** Processes the mc:Ignorable attribute, adding each referenced + namespace URI to the ignorable namespaces list. */ + void processIgnorableAttribute( + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& rAttribs ); + + /** Processes the mc:ProcessContent attribute, adding each qualified + element name to the set of elements to process even when in an + ignorable namespace. */ + void processProcessContent( + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& rAttribs ); +}; + +} // namespace core +} // namespace oox + +#endif diff --git a/main/oox/inc/oox/core/mceprocessor.hxx b/main/oox/inc/oox/core/mceprocessor.hxx new file mode 100644 index 0000000000..ba138e5b0b --- /dev/null +++ b/main/oox/inc/oox/core/mceprocessor.hxx @@ -0,0 +1,122 @@ +#ifndef OOX_CORE_MCEPROCESSOR_HXX +#define OOX_CORE_MCEPROCESSOR_HXX + +#include <com/sun/star/xml/sax/XFastAttributeList.hpp> +#include <com/sun/star/xml/Attribute.hpp> +#include <rtl/ustring.hxx> +#include <vector> + +namespace oox { +namespace core { + +/** Minimal MCE (Markup Compatibility and Extensibility) selection engine. + + Implements Phase 1 of the MCE support plan: mc:AlternateContent, + mc:Choice, mc:Fallback selection based on mc:Requires namespace + resolution. + + The MceProcessor is a stateless logic class with no UNO dependencies + beyond the XFastAttributeList interface. It can be shared across all + OOXML filter implementations (Calc, Impress, Writer). +*/ +class MceProcessor +{ +public: + /** Result of branch selection. */ + enum BranchResult + { + BRANCH_NONE, ///< No branch selected (no eligible Choice, no Fallback) + BRANCH_CHOICE, ///< A Choice branch was selected + BRANCH_FALLBACK ///< The Fallback branch was selected + }; + + /** Selected branch information. */ + struct Selection + { + BranchResult meResult; ///< Which branch was selected + sal_Int32 mnChoiceIndex;///< Index of selected Choice (0-based), -1 if not a Choice + }; + + MceProcessor(); + ~MceProcessor(); + + /** Returns the Markup Compatibility namespace URI. */ + static ::rtl::OUString getMarkupCompatNamespace(); + + /** Returns the set of namespace URIs that AOO supports for MCE Choice + branches in Phase 1. These are namespaces where AOO has actual + element-level implementation, not just namespace registration. + + The Phase 1 support set is deliberately conservative: + - DrawingML main (http://schemas.openxmlformats.org/drawingml/2006/main) + AOO implements DrawingML element parsing in oox/source/drawingml/. + + Namespaces NOT in the Phase 1 set: + - SpreadsheetML, WordprocessingML, PresentationML main namespaces + (document-level markup, not DrawingML content) + - MS-ODRAWXML extension namespaces (not implemented at all) + - VML namespaces (legacy format, not DrawingML) + */ + static const std::vector< ::rtl::OUString >& getSupportedNamespaceSet(); + + /** Checks whether a namespace URI is in the Phase 1 supported set. */ + static bool isNamespaceSupported( const ::rtl::OUString& rNamespaceURI ); + + /** Extracts namespace declarations (xmlns:prefix="uri") from an attribute + list and returns them as a prefix-to-URI mapping. Also extracts the + mc:Requires value if present. + + @param rAttribs Attribute list of an element + @param rRequires Output: value of mc:Requires attribute, or empty + if not present + @return Prefix-to-URI mapping from xmlns:* declarations + */ + static std::vector< std::pair< ::rtl::OUString, ::rtl::OUString > > extractNamespaceDeclarations( + const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& rAttribs, + ::rtl::OUString& rRequires ); + + /** Evaluates the mc:Requires attribute value. + + Resolves each namespace prefix listed in Requires to a namespace URI + using the provided prefix-to-URI mapping, then checks whether all + resolved namespaces are in the supported set. + + @param rRequires Value of mc:Requires attribute (space-separated + namespace prefixes) + @param rPrefixMap Mapping of namespace prefixes to namespace URIs + + @return true if all required namespaces are supported, false otherwise + */ + static bool isChoiceEligible( + const ::rtl::OUString& rRequires, + const std::vector< std::pair< ::rtl::OUString, ::rtl::OUString > >& rPrefixMap ); + + /** Selects a branch from an mc:AlternateContent element. + + Evaluates Choice branches in document order and returns the first + eligible Choice. If no Choice is eligible and a Fallback exists, + returns Fallback. If neither, returns NONE. + + @param rChoiceEligible Vector indicating which Choice branches are + eligible (true = eligible, false = not) + @param bHasFallback Whether a mc:Fallback child exists + + @return Selection result + */ + static Selection selectBranch( + const std::vector< bool >& rChoiceEligible, + bool bHasFallback ); + +private: + /** Resolves a namespace prefix to a URI using the prefix map. */ + static ::rtl::OUString resolvePrefix( + const ::rtl::OUString& rPrefix, + const std::vector< std::pair< ::rtl::OUString, ::rtl::OUString > >& rPrefixMap ); + + static std::vector< ::rtl::OUString > maSupportedNamespaces; +}; + +} // namespace core +} // namespace oox + +#endif diff --git a/main/oox/source/core/fragmenthandler.cxx b/main/oox/source/core/fragmenthandler.cxx index d2d100821a..c0c6426670 100644 --- a/main/oox/source/core/fragmenthandler.cxx +++ b/main/oox/source/core/fragmenthandler.cxx @@ -20,10 +20,11 @@ *************************************************************/ - #include "oox/core/fragmenthandler.hxx" #include "oox/core/xmlfilterbase.hxx" +#include "oox/core/mcealternatecontext.hxx" +#include "oox/token/tokens.hxx" namespace oox { namespace core { @@ -38,6 +39,46 @@ using ::rtl::OUString; // ============================================================================ +const ::rtl::OUString& FragmentHandler::getMarkupCompatNamespace() +{ + static const ::rtl::OUString aNamespace = + ::rtl::OUString::createFromAscii( + "http://schemas.openxmlformats.org/markup-compatibility/2006" ); + return aNamespace; +} + +void FragmentHandler::pushNamespaces( + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& rAttribs ) +{ + std::vector< std::pair< ::rtl::OUString, ::rtl::OUString > > aMap; + MceProcessor::extractNamespaceDeclarations( rAttribs, aMap ); + maNamespaceStack.push_back( aMap ); +} + +void FragmentHandler::popNamespaces() +{ + if ( !maNamespaceStack.empty() ) + maNamespaceStack.pop_back(); +} + +::rtl::OUString FragmentHandler::resolvePrefix( + const ::rtl::OUString& rPrefix ) const +{ + // Search from top of stack (most local) to bottom (most global). + for ( auto it = maNamespaceStack.rbegin(); it != maNamespaceStack.rend(); ++it ) + { + for ( const auto& rPair : *it ) + { + if ( rPair.first == rPrefix ) + return rPair.second; + } + } + return ::rtl::OUString(); // Empty = unresolved +} + +// ============================================================================ + FragmentBaseData::FragmentBaseData( XmlFilterBase& rFilter, const OUString& rFragmentPath, RelationsRef xRelations ) : mrFilter( rFilter ), maFragmentPath( rFragmentPath ), @@ -47,12 +88,12 @@ FragmentBaseData::FragmentBaseData( XmlFilterBase& rFilter, const OUString& rFra // ============================================================================ -FragmentHandler::FragmentHandler( XmlFilterBase& rFilter, const OUString& rFragmentPath ) : +FragmentHandler::FragmentHandler( XmlFilterBase& rFilter, const ::rtl::OUString& rFragmentPath ) : FragmentHandler_BASE( FragmentBaseDataRef( new FragmentBaseData( rFilter, rFragmentPath, rFilter.importRelations( rFragmentPath ) ) ) ) { } -FragmentHandler::FragmentHandler( XmlFilterBase& rFilter, const OUString& rFragmentPath, RelationsRef xRelations ) : +FragmentHandler::FragmentHandler( XmlFilterBase& rFilter, const ::rtl::OUString& rFragmentPath, RelationsRef xRelations ) : FragmentHandler_BASE( FragmentBaseDataRef( new FragmentBaseData( rFilter, rFragmentPath, xRelations ) ) ) { } @@ -61,8 +102,7 @@ FragmentHandler::~FragmentHandler() { } -// com.sun.star.xml.sax.XFastDocumentHandler interface ------------------------ - +// com.sun.star.xml.sax.XFastDocumentHandler interface -------------------- void FragmentHandler::startDocument() { } @@ -71,48 +111,75 @@ void FragmentHandler::endDocument() { } -void FragmentHandler::setDocumentLocator( const Reference< XLocator >& rxLocator ) +void FragmentHandler::setDocumentLocator( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XLocator >& rxLocator ) { implSetLocator( rxLocator ); } -// com.sun.star.xml.sax.XFastContextHandler interface ------------------------- - -void FragmentHandler::startFastElement( sal_Int32, const Reference< XFastAttributeList >& ) +// com.sun.star.xml.sax.XFastContextHandler interface --------------------- +void FragmentHandler::startFastElement( ::sal_Int32 Element, const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& rAttribs ) { + pushNamespaces( rAttribs ); } -void FragmentHandler::startUnknownElement( const OUString&, const OUString&, const Reference< XFastAttributeList >& ) +void FragmentHandler::startUnknownElement( + const ::rtl::OUString& /*rNamespace*/, + const ::rtl::OUString& /*rName*/, + const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& /*rAttribs*/) { } -void FragmentHandler::endFastElement( sal_Int32 ) +void FragmentHandler::endFastElement( ::sal_Int32 ) { + popNamespaces(); } -void FragmentHandler::endUnknownElement( const OUString&, const OUString& ) +void FragmentHandler::endUnknownElement( const ::rtl::OUString&, const ::rtl::OUString& ) { } -Reference< XFastContextHandler > FragmentHandler::createFastChildContext( sal_Int32, const Reference< XFastAttributeList >& ) +::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL +FragmentHandler::createFastChildContext( + ::sal_Int32 Element, + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& rAttribs ) { + // Phase 1 MCE: intercept mc:AlternateContent and delegate to MceAlternateContext + // for Choice/Fallback selection. All other elements continue to + // return 0 (unchanged behavior). + if ( Element == XML_AlternateContent ) + { + MceAlternateContext* pMceCtx = new MceAlternateContext( + getFastContextHandler() ); + pMceCtx->pushNamespaces( rAttribs ); + return Reference< XFastContextHandler >( + static_cast< XFastContextHandler* >( pMceCtx ) ); + } + return 0; } -Reference< XFastContextHandler > FragmentHandler::createUnknownChildContext( const OUString&, const OUString&, const Reference< XFastAttributeList >& ) +::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL +FragmentHandler::createUnknownChildContext( + const ::rtl::OUString& /*rNamespace*/, + const ::rtl::OUString& /*rName*/, + const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& /*rAttribs*/) { - return 0; + return ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler >(); } -void FragmentHandler::characters( const OUString& ) +void FragmentHandler::characters( const ::rtl::OUString& ) { } -void FragmentHandler::ignorableWhitespace( const OUString& ) +void FragmentHandler::ignorableWhitespace( const ::rtl::OUString& ) { } -void FragmentHandler::processingInstruction( const OUString&, const OUString& ) +void FragmentHandler::processingInstruction( const ::rtl::OUString&, const ::rtl::OUString& ) { } @@ -123,7 +190,7 @@ Reference< XInputStream > FragmentHandler::openFragmentStream() const return getFilter().openInputStream( getFragmentPath() ); } -// binary records ------------------------------------------------------------- +// binary records --------------------------------------------------------- const RecordInfo* FragmentHandler::getRecordInfos() const { @@ -131,7 +198,5 @@ const RecordInfo* FragmentHandler::getRecordInfos() const return 0; } -// ============================================================================ - } // namespace core } // namespace oox diff --git a/main/oox/source/core/mcealternatecontext.cxx b/main/oox/source/core/mcealternatecontext.cxx new file mode 100644 index 0000000000..af8402e8bf --- /dev/null +++ b/main/oox/source/core/mcealternatecontext.cxx @@ -0,0 +1,472 @@ +#include "oox/core/mcealternatecontext.hxx" + +#include <com/sun/star/xml/sax/XFastAttributeList.hpp> +#include <rtl/ustring.hxx> +#include "oox/token/tokens.hxx" + +namespace oox { +namespace core { + +// ============================================================================ + +MceAlternateContext::MceAlternateContext( + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler >& xDelegate ) : + mxDelegate( xDelegate ), + meState( STATE_INITIAL ), + mnNextChoiceIndex( 0 ), + mbHasFallback( false ), + mbInSelectedBranch( false ), + maMarkupCompatNamespace( + MceProcessor::getMarkupCompatNamespace() ) +{ +} + +MceAlternateContext::~MceAlternateContext() +{ +} + +// ---------------------------------------------------------------------------- + +void SAL_CALL MceAlternateContext::startFastElement( + ::sal_Int32 Element, + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ) +{ + // Push namespace declarations from this element onto the stack. + // This must happen for ALL elements so that namespace resolution works + // correctly for nested elements. + pushNamespaces( Attribs ); + + // Process mc:Ignorable attribute if present on this element. + processIgnorableAttribute( Attribs ); + + // Process mc:ProcessContent attribute if present on this element. + processProcessContent( Attribs ); + + if ( Element == XML_Choice ) + { + // Evaluate this Choice's Requires attribute + implEvaluateChoice( Attribs ); + return; // Choice element itself is not forwarded + } + if ( Element == XML_Fallback ) + { + if ( meState == STATE_INITIAL || meState == STATE_CHOICE_SCAN ) + { + mbHasFallback = true; + if ( meState == STATE_CHOICE_SCAN && maSelection.meResult == MceProcessor::BRANCH_NONE ) + { + // No eligible Choice found, select Fallback + maSelection.meResult = MceProcessor::BRANCH_FALLBACK; + maSelection.mnChoiceIndex = -1; + meState = STATE_FALLBACK_SELECTED; + mbInSelectedBranch = true; + } + } + return; // Fallback element itself is not forwarded + } + if ( Element == XML_AlternateContent ) + { + // Nested AlternateContent: create a new context. + MceAlternateContext* pMceCtx = new MceAlternateContext( + mxDelegate ); + pMceCtx->maNamespaceStack = maNamespaceStack; // inherit namespace stack + pMceCtx->maIgnorableNamespaces = maIgnorableNamespaces; // inherit ignorable namespaces + pMceCtx->maProcessContentElements = maProcessContentElements; // inherit process content + pMceCtx->meState = STATE_INITIAL; + pMceCtx->mnNextChoiceIndex = 0; + pMceCtx->mbHasFallback = false; + pMceCtx->mbInSelectedBranch = false; + pMceCtx->maSelection.meResult = MceProcessor::BRANCH_NONE; + pMceCtx->maSelection.mnChoiceIndex = -1; + + return ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler >( + static_cast< XFastContextHandler* >( pMceCtx ) ); + } + + // Non-MCE element inside selected branch - forward to delegate + if ( mbInSelectedBranch && mxDelegate.is() ) + mxDelegate->startFastElement( Element, Attribs ); + // Non-MCE element outside selected branch - discard +} + +void SAL_CALL MceAlternateContext::startUnknownElement( + const ::rtl::OUString& Namespace, + const ::rtl::OUString& Name, + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ) +{ + if ( mbInSelectedBranch && mxDelegate.is() ) + { + mxDelegate->startUnknownElement( Namespace, Name, Attribs ); + return; + } + + // Element outside selected branch: check mc:Ignorable and mc:ProcessContent + // If namespace is ignorable AND element is NOT in ProcessContent list: skip + // If namespace is ignorable AND element IS in ProcessContent list: forward + // If namespace is NOT ignorable: forward normally + if ( isIgnorableNamespace( Namespace ) && + !isProcessContentElement( Namespace, Name ) ) + { + return; // Skip - ignorable namespace, not in ProcessContent + } + + if ( mxDelegate.is() ) + mxDelegate->startUnknownElement( Namespace, Name, Attribs ); +} + +void SAL_CALL MceAlternateContext::endFastElement( ::sal_Int32 Element ) +{ + if ( Element == XML_AlternateContent ) + { + meState = STATE_DONE; + mbInSelectedBranch = false; + popNamespaces(); + return; + } + if ( Element == XML_Choice || Element == XML_Fallback ) + { + mbInSelectedBranch = false; + popNamespaces(); + return; + } + + if ( mbInSelectedBranch && mxDelegate.is() ) + mxDelegate->endFastElement( Element ); +} + +void SAL_CALL MceAlternateContext::endUnknownElement( + const ::rtl::OUString& Namespace, + const ::rtl::OUString& Name ) +{ + if ( mbInSelectedBranch && mxDelegate.is() ) + mxDelegate->endUnknownElement( Namespace, Name ); +} + +// ---------------------------------------------------------------------------- + +::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL +MceAlternateContext::createFastChildContext( + ::sal_Int32 Element, + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ) +{ + if ( Element == XML_AlternateContent ) + { + // Nested AlternateContent: create a new context. + // Namespace declarations have already been pushed in startFastElement. + MceAlternateContext* pMceCtx = new MceAlternateContext( + mxDelegate ); + pMceCtx->maNamespaceStack = maNamespaceStack; // inherit namespace stack + pMceCtx->maIgnorableNamespaces = maIgnorableNamespaces; // inherit ignorable namespaces + pMceCtx->maProcessContentElements = maProcessContentElements; // inherit process content + pMceCtx->meState = STATE_INITIAL; + pMceCtx->mnNextChoiceIndex = 0; + pMceCtx->mbHasFallback = false; + pMceCtx->mbInSelectedBranch = false; + pMceCtx->maSelection.meResult = MceProcessor::BRANCH_NONE; + pMceCtx->maSelection.mnChoiceIndex = -1; + + return ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler >( + static_cast< XFastContextHandler* >( pMceCtx ) ); + } + + if ( mbInSelectedBranch && mxDelegate.is() ) + return mxDelegate->createFastChildContext( Element, Attribs ); + return ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler >(); +} + +::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL +MceAlternateContext::createUnknownChildContext( + const ::rtl::OUString& Namespace, + const ::rtl::OUString& Name, + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& Attribs ) +{ + if ( mbInSelectedBranch && mxDelegate.is() ) + return mxDelegate->createUnknownChildContext( Namespace, Name, Attribs ); + return ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler >(); +} + +void SAL_CALL MceAlternateContext::characters( const ::rtl::OUString& aChars ) +{ + if ( mbInSelectedBranch && mxDelegate.is() ) + mxDelegate->characters( aChars ); +} + +// ---------------------------------------------------------------------------- + +void MceAlternateContext::implEvaluateChoice( + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& rAttribs ) +{ + // Build prefix map from this Choice element's attributes, wrapped with + // ancestor namespace declarations from the stack. + ::rtl::OUString aRequires; + std::vector< std::pair< ::rtl::OUString, ::rtl::OUString > > aPrefixMap = + MceProcessor::extractNamespaceDeclarations( rAttribs, aRequires ); + + // Add ancestor namespace declarations (current element's declarations + // override ancestor declarations for the same prefix). + for ( auto it = maNamespaceStack.rbegin(); it != maNamespaceStack.rend(); ++it ) + { + for ( const auto& rPair : *it ) + { + // Only add if not already present from a more local scope. + bool bFound = false; + for ( const auto& rExisting : aPrefixMap ) + { + if ( rExisting.first == rPair.first ) + { + bFound = true; + break; + } + } + if ( !bFound ) + aMap.push_back( rPair ); + } + } + + if ( MceProcessor::isChoiceEligible( aRequires, aPrefixMap ) ) + { + if ( maSelection.meResult == MceProcessor::BRANCH_NONE ) + { + // First eligible Choice + maSelection.meResult = MceProcessor::BRANCH_CHOICE; + maSelection.mnChoiceIndex = mnNextChoiceIndex; + meState = STATE_CHOICE_SELECTED; + mbInSelectedBranch = true; + } + ++mnNextChoiceIndex; + } + else + { + ++mnNextChoiceIndex; + } +} + +::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler > SAL_CALL +MceAlternateContext::implCreateNullContext() +{ + return ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastContextHandler >(); +} + +// ---------------------------------------------------------------------------- + +void MceAlternateContext::pushNamespaces( + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& rAttribs ) +{ + std::vector< std::pair< ::rtl::OUString, ::rtl::OUString > > aMap; + ::rtl::OUString aRequires; + aMap = MceProcessor::extractNamespaceDeclarations( rAttribs, aRequires ); + maNamespaceStack.push_back( aMap ); +} + +void MceAlternateContext::popNamespaces() +{ + if ( !maNamespaceStack.empty() ) + maNamespaceStack.pop_back(); +} + +::rtl::OUString MceAlternateContext::resolvePrefix( + const ::rtl::OUString& rPrefix ) const +{ + // Search from top of stack (most local) to bottom (most global). + for ( auto it = maNamespaceStack.rbegin(); it != maNamespaceStack.rend(); ++it ) + { + for ( const auto& rPair : *it ) + { + if ( rPair.first == rPrefix ) + return rPair.second; + } + } + return ::rtl::OUString(); // Empty = unresolved +} + +bool MceAlternateContext::isIgnorableNamespace( const ::rtl::OUString& rNamespaceURI ) const +{ + for ( const auto& rNs : maIgnorableNamespaces ) + { + if ( rNs == rNamespaceURI ) + return true; + } + return false; +} + +bool MceAlternateContext::isProcessContentElement( + const ::rtl::OUString& rNamespaceURI, + const ::rtl::OUString& rLocalName ) const +{ + for ( const auto& rPair : maProcessContentElements ) + { + if ( rPair.first == rNamespaceURI && rPair.second == rLocalName ) + return true; + } + return false; +} + +std::vector< ::rtl::OUString > MceAlternateContext::resolvePrefixList( + const ::rtl::OUString& rRequires ) const +{ + std::vector< ::rtl::OUString > aResult; + sal_Int32 nPos = 0; + sal_Int32 nLen = rRequires.getLength(); + while ( nPos < nLen ) + { + while ( nPos < nLen && (rRequires[ nPos ] == ' ' || rRequires[ nPos ] == '\t' || + rRequires[ nPos ] == '\r' || rRequires[ nPos ] == '\n') ) + ++nPos; + if ( nPos >= nLen ) + break; + + sal_Int32 nStart = nPos; + while ( nPos < nLen && (rRequires[ nPos ] != ' ' && rRequires[ nPos ] != '\t' && + rRequires[ nPos ] != '\r' && rRequires[ nPos ] != '\n') ) + ++nPos; + ::rtl::OUString aPrefix = rRequires.copy( nStart, nPos - nStart ); + + ::rtl::OUString aNamespaceURI = resolvePrefix( aPrefix ); + if ( aNamespaceURI.getLength() > 0 ) + aResult.push_back( aNamespaceURI ); + } + return aResult; +} + +void MceAlternateContext::processIgnorableAttribute( + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& rAttribs ) +{ + if ( !rAttribs.is() ) + return; + + try + { + ::com::sun::star::uno::Sequence< ::com::sun::star::xml::Attribute > aAttrs = + rAttribs->getAttributes(); + + for ( sal_Int32 i = 0; i < aAttrs.getLength(); ++i ) + { + const ::com::sun::star::xml::Attribute& rAttr = aAttrs[i]; + + if ( rAttr.Name.equalsAsciiL( "Ignorable", 9 ) ) + { + ::rtl::OUString aValue = rAttr.Value; + std::vector< ::rtl::OUString > aPrefixes = + resolvePrefixList( aValue ); + for ( const auto& rNs : aPrefixes ) + { + // Only add if not already present. + bool bFound = false; + for ( const auto& rExisting : maIgnorableNamespaces ) + { + if ( rExisting == rNs ) + { + bFound = true; + break; + } + } + if ( !bFound ) + maIgnorableNamespaces.push_back( rNs ); + } + } + } + } + catch ( ... ) + { + // getAttributes() may throw if not supported + } +} + +void MceAlternateContext::processProcessContent( + const ::com::sun::star::uno::Reference< + ::com::sun::star::xml::sax::XFastAttributeList >& rAttribs ) +{ + if ( !rAttribs.is() ) + return; + + try + { + ::com::sun::star::uno::Sequence< ::com::sun::star::xml::Attribute > aAttrs = + rAttribs->getAttributes(); + + for ( sal_Int32 i = 0; i < aAttrs.getLength(); ++i ) + { + const ::com::sun::star::xml::Attribute& rAttr = aAttrs[i]; + + if ( rAttr.Name.equalsAsciiL( "ProcessContent", 13 ) ) + { + ::rtl::OUString aValue = rAttr.Value; + sal_Int32 nPos = 0; + sal_Int32 nLen = aValue.getLength(); + while ( nPos < nLen ) + { + while ( nPos < nLen && (aValue[ nPos ] == ' ' || aValue[ nPos ] == '\t' || + aValue[ nPos ] == '\r' || aValue[ nPos ] == '\n') ) + ++nPos; + if ( nPos >= nLen ) + break; + + sal_Int32 nStart = nPos; + while ( nPos < nLen && (aValue[ nPos ] != ' ' && aValue[ nPos ] != '\t' && + aValue[ nPos ] != '\r' && aValue[ nPos ] != '\n') ) + ++nPos; + ::rtl::OUString aQName = aValue.copy( nStart, nPos - nStart ); + + // Split QName into prefix and local name. + ::rtl::OUString aPrefix; + ::rtl::OUString aLocalName; + sal_Int32 nColon = aQName.indexOf( ':' ); + if ( nColon >= 0 ) + { + aPrefix = aQName.copy( 0, nColon ); + aLocalName = aQName.copy( nColon + 1, aQName.getLength() - nColon - 1 ); + } + else + { + // No prefix - default namespace (empty prefix) + aPrefix = ::rtl::OUString(); + aLocalName = aQName; + } + + // Resolve prefix to namespace URI. + ::rtl::OUString aNamespaceURI = resolvePrefix( aPrefix ); + if ( aNamespaceURI.getLength() > 0 ) + { + // Only add if not already present. + bool bFound = false; + for ( const auto& rExisting : maProcessContentElements ) + { + if ( rExisting.first == aNamespaceURI && + rExisting.second == aLocalName ) + { + bFound = true; + break; + } + } + if ( !bFound ) + maProcessContentElements.push_back( + std::make_pair( aNamespaceURI, aLocalName ) ); + } + } + } + } + } + catch ( ... ) + { + // getAttributes() may throw if not supported + } +} + +} // namespace core +} // namespace oox diff --git a/main/oox/source/core/mceprocessor.cxx b/main/oox/source/core/mceprocessor.cxx new file mode 100644 index 0000000000..d0c42aa7ae --- /dev/null +++ b/main/oox/source/core/mceprocessor.cxx @@ -0,0 +1,175 @@ +#include "oox/core/mceprocessor.hxx" + +#include <com/sun/star/xml/sax/XFastAttributeList.hpp> +#include <rtl/ustring.hxx> +#include <algorithm> + +namespace oox { +namespace core { + +// ============================================================================ + +std::vector< ::rtl::OUString > MceProcessor::maSupportedNamespaces; + +// ============================================================================ + +MceProcessor::MceProcessor() +{ +} + +MceProcessor::~MceProcessor() +{ +} + +::rtl::OUString MceProcessor::getMarkupCompatNamespace() +{ + return ::rtl::OUString::createFromAscii( + "http://schemas.openxmlformats.org/markup-compatibility/2006" ); +} + +const std::vector< ::rtl::OUString >& MceProcessor::getSupportedNamespaceSet() +{ + if ( maSupportedNamespaces.empty() ) + { + // Phase 1: Conservative support set. Only namespaces where AOO has + // actual element-level implementation in DrawingML import code. + // + // DrawingML main namespace - AOO implements DrawingML element parsing + // in oox/source/drawingml/ (shape.cxx, fillproperties.cxx, + // lineproperties.cxx, textparagraph.cxx, textrun.cxx, etc.) + maSupportedNamespaces.push_back( + ::rtl::OUString::createFromAscii( + "http://schemas.openxmlformats.org/drawingml/2006/main" ) ); + } + return maSupportedNamespaces; +} + +bool MceProcessor::isNamespaceSupported( const ::rtl::OUString& rNamespaceURI ) +{ + const std::vector< ::rtl::OUString >& rSet = getSupportedNamespaceSet(); + return (std::find( rSet.begin(), rSet.end(), rNamespaceURI ) != rSet.end()); +} + +std::vector< std::pair< ::rtl::OUString, ::rtl::OUString > > MceProcessor::extractNamespaceDeclarations( + const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastAttributeList >& rAttribs, + ::rtl::OUString& rRequires ) +{ + std::vector< std::pair< ::rtl::OUString, ::rtl::OUString > > aMap; + rRequires = ::rtl::OUString(); + + if ( !rAttribs.is() ) + return aMap; + + // Get unknown attributes (includes xmlns: declarations and mc:Requires) + try + { + ::com::sun::star::uno::Sequence< ::com::sun::star::xml::Attribute > aUnknownAttrs = + rAttribs->getUnknownAttributes(); + + for ( sal_Int32 i = 0; i < aUnknownAttrs.getLength(); ++i ) + { + const ::com::sun::star::xml::Attribute& rAttr = aUnknownAttrs[i]; + + if ( rAttr.Name.equalsAsciiL( "Requires", 8 ) ) + { + rRequires = rAttr.Value; + } + else if ( rAttr.Name.getLength() > 6 && + rAttr.Name.match( ::rtl::OUString::createFromAscii( "xmlns:" ), 0 ) ) + { + ::rtl::OUString aPrefix = rAttr.Name.copy( 6 ); + aMap.push_back( std::make_pair( aPrefix, rAttr.Value ) ); + } + } + } + catch ( ... ) + { + // getUnknownAttributes may throw if not supported + } + + return aMap; +} + +bool MceProcessor::isChoiceEligible( + const ::rtl::OUString& rRequires, + const std::vector< std::pair< ::rtl::OUString, ::rtl::OUString > >& rPrefixMap ) +{ + if ( rRequires.getLength() == 0 ) + return false; // Empty Requires means no requirements - treat as not eligible + + // Split by whitespace and resolve each prefix + sal_Int32 nPos = 0; + sal_Int32 nLen = rRequires.getLength(); + while ( nPos < nLen ) + { + // Skip whitespace + while ( nPos < nLen && (rRequires[ nPos ] == ' ' || rRequires[ nPos ] == '\t' || + rRequires[ nPos ] == '\r' || rRequires[ nPos ] == '\n') ) + ++nPos; + if ( nPos >= nLen ) + break; + + // Extract prefix token + sal_Int32 nStart = nPos; + while ( nPos < nLen && (rRequires[ nPos ] != ' ' && rRequires[ nPos ] != '\t' && + rRequires[ nPos ] != '\r' && rRequires[ nPos ] != '\n') ) + ++nPos; + ::rtl::OUString aPrefix = rRequires.copy( nStart, nPos - nStart ); + + // Resolve prefix to namespace URI + ::rtl::OUString aNamespaceURI = resolvePrefix( aPrefix, rPrefixMap ); + if ( aNamespaceURI.getLength() == 0 ) + return false; // Unresolved prefix - not eligible + + // Check if namespace is supported + if ( !isNamespaceSupported( aNamespaceURI ) ) + return false; // Unsupported namespace - not eligible + } + + // All required namespaces are supported + return true; +} + +MceProcessor::Selection MceProcessor::selectBranch( + const std::vector< bool >& rChoiceEligible, + bool bHasFallback ) +{ + Selection aSelection; + aSelection.meResult = BRANCH_NONE; + aSelection.mnChoiceIndex = -1; + + // Evaluate Choice branches in document order + for ( size_t i = 0; i < rChoiceEligible.size(); ++i ) + { + if ( rChoiceEligible[ i ] ) + { + aSelection.meResult = BRANCH_CHOICE; + aSelection.mnChoiceIndex = static_cast< sal_Int32 >( i ); + return aSelection; + } + } + + // No eligible Choice - use Fallback if available + if ( bHasFallback ) + { + aSelection.meResult = BRANCH_FALLBACK; + aSelection.mnChoiceIndex = -1; + } + + return aSelection; +} + +::rtl::OUString MceProcessor::resolvePrefix( + const ::rtl::OUString& rPrefix, + const std::vector< std::pair< ::rtl::OUString, ::rtl::OUString > >& rPrefixMap ) +{ + for ( const auto& rPair : rPrefixMap ) + { + if ( rPair.first.equals( rPrefix ) ) + return rPair.second; + } + return ::rtl::OUString(); // Empty = unresolved +} + +} // namespace core +} // namespace oox diff --git a/main/writerfilter/Library_writerfilter.mk b/main/writerfilter/Library_writerfilter.mk index ad771a7f1c..61c50eedb1 100644 --- a/main/writerfilter/Library_writerfilter.mk +++ b/main/writerfilter/Library_writerfilter.mk @@ -39,6 +39,7 @@ $(eval $(call gb_Library_add_api,writerfilter,\ $(eval $(call gb_Library_set_include,writerfilter,\ $$(INCLUDE) \ + -I$(SRCDIR)/oox/inc \ -I$(SRCDIR)/writerfilter/inc \ -I$(WORKDIR)/CustomTarget/writerfilter/source \ -I$(WORKDIR)/CustomTarget/writerfilter/source/ooxml \ diff --git a/main/writerfilter/source/ooxml/OOXMLFastDocumentHandler.cxx b/main/writerfilter/source/ooxml/OOXMLFastDocumentHandler.cxx index 31e276de20..7d391c23dd 100644 --- a/main/writerfilter/source/ooxml/OOXMLFastDocumentHandler.cxx +++ b/main/writerfilter/source/ooxml/OOXMLFastDocumentHandler.cxx @@ -20,7 +20,6 @@ *************************************************************/ - #include <iostream> #include <boost/shared_ptr.hpp> #ifdef DEBUG_CONTEXT_HANDLER @@ -34,6 +33,9 @@ #include "OOXMLFastTokens.hxx" #include "OOXMLFactory.hxx" +#include <oox/core/mcealternatecontext.hxx> +#include <oox/token/tokens.hxx> + namespace writerfilter { namespace ooxml { @@ -99,9 +101,9 @@ OOXMLFastDocumentHandler::getContextHandler() const } uno::Reference< xml::sax::XFastContextHandler > SAL_CALL - OOXMLFastDocumentHandler::createFastChildContext +OOXMLFastDocumentHandler::createFastChildContext (::sal_Int32 Element, - const uno::Reference< xml::sax::XFastAttributeList > & /*Attribs*/) + const uno::Reference< xml::sax::XFastAttributeList > & rAttribs) { if ( mpStream == 0 && mpDocument == 0 ) @@ -111,6 +113,22 @@ uno::Reference< xml::sax::XFastContextHandler > SAL_CALL return NULL; } + // Phase 1 MCE: intercept mc:AlternateContent and delegate to MceAlternateContext + // for Choice/Fallback selection. + if ( Element == oox::XML_AlternateContent ) + { + OOXMLFastContextHandler::Pointer_t pCtxHandler = getContextHandler(); + if ( pCtxHandler.get() ) + { + uno::Reference< xml::sax::XFastContextHandler > xDelegate( + new OOXMLFastContextHandler( *pCtxHandler.get() ) ); + oox::core::MceAlternateContext* pMceCtx = new oox::core::MceAlternateContext( xDelegate ); + pMceCtx->pushNamespaces( rAttribs ); + return uno::Reference< xml::sax::XFastContextHandler >( + pMceCtx ); + } + } + return OOXMLFactory::getInstance()->createFastChildContextFromStart(getContextHandler().get(), Element); } @@ -151,3 +169,4 @@ void OOXMLFastDocumentHandler::setIsSubstream( bool bSubstream ) } }} +
