Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / oox / source / core / fragmenthandler2.cxx
blob79b58ce70f0ebb582b3036912d13b074b2022a54
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <oox/core/fragmenthandler2.hxx>
21 #include <oox/core/xmlfilterbase.hxx>
22 #include <oox/helper/attributelist.hxx>
23 #include <oox/token/namespaces.hxx>
24 #include <oox/token/tokens.hxx>
26 namespace oox {
27 namespace core {
29 using namespace ::com::sun::star;
30 using namespace ::com::sun::star::uno;
31 using namespace ::com::sun::star::xml::sax;
33 using ::com::sun::star::uno::Sequence;
35 FragmentHandler2::FragmentHandler2( XmlFilterBase& rFilter, const OUString& rFragmentPath, bool bEnableTrimSpace ) :
36 FragmentHandler( rFilter, rFragmentPath ),
37 ContextHandler2Helper( bEnableTrimSpace )
41 FragmentHandler2::~FragmentHandler2()
45 // com.sun.star.xml.sax.XFastDocumentHandler interface --------------------
47 void SAL_CALL FragmentHandler2::startDocument()
49 initializeImport();
52 void SAL_CALL FragmentHandler2::endDocument()
54 finalizeImport();
57 bool FragmentHandler2::prepareMceContext( sal_Int32 nElement, const AttributeList& rAttribs )
59 switch( nElement )
61 case MCE_TOKEN( AlternateContent ):
62 aMceState.push_back( MCE_STATE::Started );
63 break;
65 case MCE_TOKEN( Choice ):
67 if (aMceState.empty() || aMceState.back() != MCE_STATE::Started)
68 return false;
70 OUString aRequires = rAttribs.getString( XML_Requires, "none" );
72 // At this point we can't access namespaces as the correct xml filter
73 // is long gone. For now let's decide depending on a list of supported
74 // namespaces like we do in writerfilter
76 std::vector<OUString> aSupportedNS =
78 "a14", // Impress needs this to import math formulas.
79 "p14",
80 "p15",
81 "x12ac",
82 "v",
85 uno::Reference<lang::XServiceInfo> xModel(getFilter().getModel(), uno::UNO_QUERY);
86 if (xModel.is() && xModel->supportsService("com.sun.star.sheet.SpreadsheetDocument"))
88 // No a14 for Calc documents, it would cause duplicated shapes as-is.
89 auto it = std::find(aSupportedNS.begin(), aSupportedNS.end(), "a14");
90 if (it != aSupportedNS.end())
92 aSupportedNS.erase(it);
96 if (std::find(aSupportedNS.begin(), aSupportedNS.end(), aRequires) != aSupportedNS.end())
97 aMceState.back() = MCE_STATE::FoundChoice;
98 else
99 return false;
101 break;
103 case MCE_TOKEN( Fallback ):
104 if( !aMceState.empty() && aMceState.back() == MCE_STATE::Started )
105 break;
106 return false;
107 default:
109 OUString str = rAttribs.getString( MCE_TOKEN( Ignorable ), OUString() );
110 if( !str.isEmpty() )
112 // Sequence< css::xml::FastAttribute > attrs = rAttribs.getFastAttributeList()->getFastAttributes();
113 // printf("MCE: %s\n", OUStringToOString( str, RTL_TEXTENCODING_UTF8 ).getStr() );
114 // TODO: Check & Get the namespaces in "Ignorable"
115 // printf("NS: %d : %s\n", attrs.getLength(), OUStringToOString( str, RTL_TEXTENCODING_UTF8 ).getStr() );
118 return false;
120 return true;
123 // com.sun.star.xml.sax.XFastContextHandler interface -------------------------
125 Reference< XFastContextHandler > SAL_CALL FragmentHandler2::createFastChildContext(
126 sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs )
128 if( getNamespace( nElement ) == NMSP_mce ) // TODO for checking 'Ignorable'
130 if( prepareMceContext( nElement, AttributeList( rxAttribs ) ) )
131 return getFastContextHandler();
132 return nullptr;
134 return implCreateChildContext( nElement, rxAttribs );
137 void SAL_CALL FragmentHandler2::startFastElement(
138 sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs )
140 implStartElement( nElement, rxAttribs );
143 void SAL_CALL FragmentHandler2::characters( const OUString& rChars )
145 implCharacters( rChars );
148 void SAL_CALL FragmentHandler2::endFastElement( sal_Int32 nElement )
150 /* If MCE */
151 switch( nElement )
153 case MCE_TOKEN( AlternateContent ):
154 aMceState.pop_back();
155 break;
158 implEndElement( nElement );
161 // oox.core.ContextHandler interface ------------------------------------------
163 ContextHandlerRef FragmentHandler2::createRecordContext( sal_Int32 nRecId, SequenceInputStream& rStrm )
165 return implCreateRecordContext( nRecId, rStrm );
168 void FragmentHandler2::startRecord( sal_Int32 nRecId, SequenceInputStream& rStrm )
170 implStartRecord( nRecId, rStrm );
173 void FragmentHandler2::endRecord( sal_Int32 nRecId )
175 implEndRecord( nRecId );
178 // oox.core.ContextHandler2Helper interface -----------------------------------
180 ContextHandlerRef FragmentHandler2::onCreateContext( sal_Int32, const AttributeList& )
182 return nullptr;
185 void FragmentHandler2::onStartElement( const AttributeList& )
189 void FragmentHandler2::onCharacters( const OUString& )
193 void FragmentHandler2::onEndElement()
197 ContextHandlerRef FragmentHandler2::onCreateRecordContext( sal_Int32, SequenceInputStream& )
199 return nullptr;
202 void FragmentHandler2::onStartRecord( SequenceInputStream& )
206 void FragmentHandler2::onEndRecord()
210 // oox.core.FragmentHandler2 interface ----------------------------------------
212 void FragmentHandler2::initializeImport()
216 void FragmentHandler2::finalizeImport()
220 } // namespace core
221 } // namespace oox
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */