Version 24.2.2.2, tag libreoffice-24.2.2.2
[LibreOffice.git] / xmloff / source / transform / MergeElemTContext.cxx
blobe853240f9e2426f5c715aea424174bb7100b4d04
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 "MergeElemTContext.hxx"
21 #include "MutableAttrList.hxx"
22 #include "TransformerBase.hxx"
23 #include "TransformerActions.hxx"
24 #include "ElemTransformerAction.hxx"
25 #include "IgnoreTContext.hxx"
26 #include <xmloff/xmlnamespace.hxx>
27 #include <osl/diagnose.h>
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::xml::sax;
31 using namespace ::xmloff::token;
33 namespace {
35 class XMLParagraphTransformerContext : public XMLTransformerContext
37 public:
38 XMLParagraphTransformerContext( XMLTransformerBase& rTransformer,
39 const OUString& rQName );
41 // Create a children element context. By default, the import's
42 // CreateContext method is called to create a new default context.
43 virtual rtl::Reference<XMLTransformerContext> CreateChildContext( sal_uInt16 nPrefix,
44 const OUString& rLocalName,
45 const OUString& rQName,
46 const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList ) override;
51 XMLParagraphTransformerContext::XMLParagraphTransformerContext(
52 XMLTransformerBase& rImp,
53 const OUString& rQName ) :
54 XMLTransformerContext( rImp, rQName )
58 rtl::Reference<XMLTransformerContext> XMLParagraphTransformerContext::CreateChildContext(
59 sal_uInt16 /*nPrefix*/,
60 const OUString& /*rLocalName*/,
61 const OUString& rQName,
62 const Reference< XAttributeList >& )
64 return new XMLIgnoreTransformerContext( GetTransformer(),
65 rQName, true );
68 namespace {
70 class XMLPersTextContentRNGTransformTContext : public XMLPersTextContentTContext
72 public:
73 XMLPersTextContentRNGTransformTContext(
74 XMLTransformerBase& rTransformer,
75 const OUString& rQName,
76 sal_uInt16 nPrefix,
77 ::xmloff::token::XMLTokenEnum eToken );
79 virtual void Characters( const OUString& rChars ) override;
84 XMLPersTextContentRNGTransformTContext::XMLPersTextContentRNGTransformTContext(
85 XMLTransformerBase& rTransformer,
86 const OUString& rQName,
87 sal_uInt16 nPrefix,
88 ::xmloff::token::XMLTokenEnum eToken ) :
89 XMLPersTextContentTContext(
90 rTransformer, rQName, nPrefix, eToken )
93 void XMLPersTextContentRNGTransformTContext::Characters( const OUString& rChars )
95 OUString aConvChars( rChars );
96 XMLTransformerBase::ConvertRNGDateTimeToISO( aConvChars );
97 XMLPersTextContentTContext::Characters( aConvChars );
101 void XMLMergeElemTransformerContext::ExportStartElement()
103 for( const auto& rChildContext : m_aChildContexts )
105 XMLPersTextContentTContext *pContext = rChildContext.get();
106 static_cast< XMLMutableAttributeList * >( m_xAttrList.get() )
107 ->AddAttribute( pContext->GetExportQName(),
108 pContext->GetTextContent() );
110 XMLTransformerContext::StartElement( m_xAttrList );
112 m_bStartElementExported = true;
115 XMLMergeElemTransformerContext::XMLMergeElemTransformerContext(
116 XMLTransformerBase& rImp,
117 const OUString& rQName,
118 sal_uInt16 nActionMap ) :
119 XMLTransformerContext( rImp, rQName ),
120 m_nActionMap( nActionMap ),
121 m_bStartElementExported( false )
125 void XMLMergeElemTransformerContext::StartElement(
126 const Reference< XAttributeList >& rAttrList )
128 rtl::Reference<XMLMutableAttributeList> pMutableAttrList =
129 new XMLMutableAttributeList( rAttrList, true );
130 m_xAttrList = pMutableAttrList;
132 sal_Int16 nAttrCount = m_xAttrList.is() ? m_xAttrList->getLength() : 0;
133 for( sal_Int16 i=0; i < nAttrCount; i++ )
135 const OUString& rAttrName = m_xAttrList->getNameByIndex( i );
136 OUString aLocalName;
137 sal_uInt16 nPrefix =
138 GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
139 &aLocalName );
140 bool bRemove = true;
141 if( XML_NAMESPACE_OFFICE == nPrefix)
143 if (IsXMLToken( aLocalName, XML_DISPLAY ) )
144 bRemove = false;
145 else if (IsXMLToken( aLocalName, XML_AUTHOR ) )
146 bRemove = false;
147 else if (IsXMLToken( aLocalName, XML_CREATE_DATE ) )
148 bRemove = false;
149 else if (IsXMLToken( aLocalName, XML_CREATE_DATE_STRING ) )
150 bRemove = false;
152 if (bRemove)
154 pMutableAttrList->RemoveAttributeByIndex( i );
155 --i;
156 --nAttrCount;
161 rtl::Reference<XMLTransformerContext> XMLMergeElemTransformerContext::CreateChildContext(
162 sal_uInt16 nPrefix,
163 const OUString& rLocalName,
164 const OUString& rQName,
165 const Reference< XAttributeList >& rAttrList )
167 rtl::Reference<XMLTransformerContext> pContext;
169 if( !m_bStartElementExported )
171 XMLTransformerActions *pActions =
172 GetTransformer().GetUserDefinedActions( m_nActionMap );
173 OSL_ENSURE( pActions, "go no actions" );
174 if( pActions )
176 XMLTransformerActions::key_type aKey( nPrefix, rLocalName );
177 XMLTransformerActions::const_iterator aIter =
178 pActions->find( aKey );
180 if( aIter != pActions->end() )
182 switch( (*aIter).second.m_nActionType )
184 case XML_ETACTION_MOVE_TO_ATTR_RNG2ISO_DATETIME:
186 rtl::Reference<XMLPersTextContentTContext> pTC(
187 new XMLPersTextContentRNGTransformTContext(
188 GetTransformer(), rQName,
189 (*aIter).second.GetQNamePrefixFromParam1(),
190 (*aIter).second.GetQNameTokenFromParam1() ));
191 m_aChildContexts.push_back(pTC);
192 pContext = pTC;
194 break;
195 case XML_ETACTION_MOVE_TO_ATTR:
197 rtl::Reference<XMLPersTextContentTContext> pTC(
198 new XMLPersTextContentTContext(
199 GetTransformer(), rQName,
200 (*aIter).second.GetQNamePrefixFromParam1(),
201 (*aIter).second.GetQNameTokenFromParam1() ));
202 m_aChildContexts.push_back(pTC);
203 pContext = pTC;
205 break;
206 case XML_ETACTION_EXTRACT_CHARACTERS:
208 if( !m_bStartElementExported )
209 ExportStartElement();
210 pContext.set(
211 new XMLParagraphTransformerContext( GetTransformer(),
212 rQName));
214 break;
215 default:
216 OSL_ENSURE( false, "unknown action" );
217 break;
222 else
224 XMLTransformerActions *pActions =
225 GetTransformer().GetUserDefinedActions( m_nActionMap );
226 OSL_ENSURE( pActions, "go no actions" );
227 if( pActions )
229 XMLTransformerActions::key_type aKey( nPrefix, rLocalName );
230 XMLTransformerActions::const_iterator aIter =
231 pActions->find( aKey );
233 if( aIter != pActions->end() )
235 switch( (*aIter).second.m_nActionType )
237 case XML_ETACTION_EXTRACT_CHARACTERS:
239 if( !m_bStartElementExported )
240 ExportStartElement();
241 pContext.set(
242 new XMLParagraphTransformerContext( GetTransformer(),
243 rQName));
245 break;
246 default:
247 OSL_ENSURE( false, "unknown action" );
248 break;
254 // default is copying
255 if( !pContext.is() )
257 if( !m_bStartElementExported )
258 ExportStartElement();
259 pContext = XMLTransformerContext::CreateChildContext( nPrefix,
260 rLocalName,
261 rQName,
262 rAttrList );
265 return pContext;
268 void XMLMergeElemTransformerContext::EndElement()
270 if( !m_bStartElementExported )
271 ExportStartElement();
272 XMLTransformerContext::EndElement();
275 void XMLMergeElemTransformerContext::Characters( const OUString& )
277 // ignore
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */