bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / transform / MergeElemTContext.cxx
blob135eb3f40c173e93edf4fe167e688e15d1e25ecd
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 "AttrTransformerAction.hxx"
25 #include "ElemTransformerAction.hxx"
26 #include "IgnoreTContext.hxx"
27 #include <xmloff/xmlnmspe.hxx>
28 #include <osl/diagnose.h>
30 using namespace ::com::sun::star::uno;
31 using namespace ::com::sun::star::xml::sax;
32 using namespace ::xmloff::token;
34 class XMLParagraphTransformerContext : public XMLTransformerContext
36 public:
37 TYPEINFO_OVERRIDE();
39 XMLParagraphTransformerContext( XMLTransformerBase& rTransformer,
40 const OUString& rQName );
42 virtual ~XMLParagraphTransformerContext();
44 // Create a children element context. By default, the import's
45 // CreateContext method is called to create a new default context.
46 virtual XMLTransformerContext *CreateChildContext( sal_uInt16 nPrefix,
47 const OUString& rLocalName,
48 const OUString& rQName,
49 const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList ) SAL_OVERRIDE;
51 // StartElement is called after a context has been constructed and
52 // before a elements context is parsed. It may be used for actions that
53 // require virtual methods. The default is to do nothing.
54 virtual void StartElement( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList ) SAL_OVERRIDE;
56 // EndElement is called before a context will be destructed, but
57 // after a elements context has been parsed. It may be used for actions
58 // that require virtual methods. The default is to do nothing.
59 virtual void EndElement() SAL_OVERRIDE;
61 // This method is called for all characters that are contained in the
62 // current element. The default is to ignore them.
63 virtual void Characters( const OUString& rChars ) SAL_OVERRIDE;
66 TYPEINIT1( XMLParagraphTransformerContext, XMLTransformerContext );
68 XMLParagraphTransformerContext::XMLParagraphTransformerContext(
69 XMLTransformerBase& rImp,
70 const OUString& rQName ) :
71 XMLTransformerContext( rImp, rQName )
75 XMLParagraphTransformerContext::~XMLParagraphTransformerContext()
79 XMLTransformerContext *XMLParagraphTransformerContext::CreateChildContext(
80 sal_uInt16 /*nPrefix*/,
81 const OUString& /*rLocalName*/,
82 const OUString& rQName,
83 const Reference< XAttributeList >& )
85 XMLTransformerContext *pContext = 0;
87 pContext = new XMLIgnoreTransformerContext( GetTransformer(),
88 rQName, true );
90 return pContext;
93 void XMLParagraphTransformerContext::StartElement( const Reference< XAttributeList >& rAttrList )
95 XMLTransformerContext::StartElement( rAttrList );
98 void XMLParagraphTransformerContext::EndElement()
100 XMLTransformerContext::EndElement();
103 void XMLParagraphTransformerContext::Characters( const OUString& rChars )
105 XMLTransformerContext::Characters( rChars );
108 class XMLPersTextContentRNGTransformTContext : public XMLPersTextContentTContext
110 public:
111 TYPEINFO_OVERRIDE();
113 XMLPersTextContentRNGTransformTContext(
114 XMLTransformerBase& rTransformer,
115 const OUString& rQName,
116 sal_uInt16 nPrefix,
117 ::xmloff::token::XMLTokenEnum eToken );
118 virtual ~XMLPersTextContentRNGTransformTContext();
120 virtual void Characters( const OUString& rChars ) SAL_OVERRIDE;
123 TYPEINIT1( XMLPersTextContentRNGTransformTContext, XMLPersAttrListTContext );
125 XMLPersTextContentRNGTransformTContext::XMLPersTextContentRNGTransformTContext(
126 XMLTransformerBase& rTransformer,
127 const OUString& rQName,
128 sal_uInt16 nPrefix,
129 ::xmloff::token::XMLTokenEnum eToken ) :
130 XMLPersTextContentTContext(
131 rTransformer, rQName, nPrefix, eToken )
134 XMLPersTextContentRNGTransformTContext::~XMLPersTextContentRNGTransformTContext()
137 void XMLPersTextContentRNGTransformTContext::Characters( const OUString& rChars )
139 OUString aConvChars( rChars );
140 XMLTransformerBase::ConvertRNGDateTimeToISO( aConvChars );
141 XMLPersTextContentTContext::Characters( aConvChars );
145 TYPEINIT1( XMLMergeElemTransformerContext, XMLTransformerContext );
147 void XMLMergeElemTransformerContext::ExportStartElement()
149 XMLPersTextContentTContextVector::iterator aIter = m_aChildContexts.begin();
151 for( ; aIter != m_aChildContexts.end(); ++aIter )
153 XMLPersTextContentTContext *pContext = (*aIter).get();
154 static_cast< XMLMutableAttributeList * >( m_xAttrList.get() )
155 ->AddAttribute( pContext->GetExportQName(),
156 pContext->GetTextContent() );
158 XMLTransformerContext::StartElement( m_xAttrList );
160 m_bStartElementExported = true;
163 XMLMergeElemTransformerContext::XMLMergeElemTransformerContext(
164 XMLTransformerBase& rImp,
165 const OUString& rQName,
166 sal_uInt16 nActionMap ) :
167 XMLTransformerContext( rImp, rQName ),
168 m_nActionMap( nActionMap ),
169 m_bStartElementExported( false )
173 XMLMergeElemTransformerContext::~XMLMergeElemTransformerContext()
177 void XMLMergeElemTransformerContext::StartElement(
178 const Reference< XAttributeList >& rAttrList )
180 XMLMutableAttributeList *pMutableAttrList =
181 new XMLMutableAttributeList( rAttrList, true );
182 m_xAttrList = pMutableAttrList;
184 sal_Int16 nAttrCount = m_xAttrList.is() ? m_xAttrList->getLength() : 0;
185 for( sal_Int16 i=0; i < nAttrCount; i++ )
187 const OUString& rAttrName = m_xAttrList->getNameByIndex( i );
188 OUString aLocalName;
189 sal_uInt16 nPrefix =
190 GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
191 &aLocalName );
192 bool bRemove = true;
193 if( XML_NAMESPACE_OFFICE == nPrefix)
195 if (IsXMLToken( aLocalName, XML_DISPLAY ) )
196 bRemove = false;
197 else if (IsXMLToken( aLocalName, XML_AUTHOR ) )
198 bRemove = false;
199 else if (IsXMLToken( aLocalName, XML_CREATE_DATE ) )
200 bRemove = false;
201 else if (IsXMLToken( aLocalName, XML_CREATE_DATE_STRING ) )
202 bRemove = false;
204 if (bRemove)
206 pMutableAttrList->RemoveAttributeByIndex( i );
207 --i;
208 --nAttrCount;
213 XMLTransformerContext *XMLMergeElemTransformerContext::CreateChildContext(
214 sal_uInt16 nPrefix,
215 const OUString& rLocalName,
216 const OUString& rQName,
217 const Reference< XAttributeList >& rAttrList )
219 XMLTransformerContext *pContext = 0;
221 if( !m_bStartElementExported )
223 XMLTransformerActions *pActions =
224 GetTransformer().GetUserDefinedActions( m_nActionMap );
225 OSL_ENSURE( pActions, "go no actions" );
226 if( pActions )
228 XMLTransformerActions::key_type aKey( nPrefix, rLocalName );
229 XMLTransformerActions::const_iterator aIter =
230 pActions->find( aKey );
232 if( !(aIter == pActions->end()) )
234 switch( (*aIter).second.m_nActionType )
236 case XML_ETACTION_MOVE_TO_ATTR_RNG2ISO_DATETIME:
238 XMLPersTextContentTContext *pTC =
239 new XMLPersTextContentRNGTransformTContext(
240 GetTransformer(), rQName,
241 (*aIter).second.GetQNamePrefixFromParam1(),
242 (*aIter).second.GetQNameTokenFromParam1() );
243 XMLPersTextContentTContextVector::value_type aVal(pTC);
244 m_aChildContexts.push_back( aVal );
245 pContext = pTC;
247 break;
248 case XML_ETACTION_MOVE_TO_ATTR:
250 XMLPersTextContentTContext *pTC =
251 new XMLPersTextContentTContext(
252 GetTransformer(), rQName,
253 (*aIter).second.GetQNamePrefixFromParam1(),
254 (*aIter).second.GetQNameTokenFromParam1() );
255 XMLPersTextContentTContextVector::value_type aVal(pTC);
256 m_aChildContexts.push_back( aVal );
257 pContext = pTC;
259 break;
260 case XML_ETACTION_EXTRACT_CHARACTERS:
262 if( !m_bStartElementExported )
263 ExportStartElement();
264 XMLParagraphTransformerContext* pPTC =
265 new XMLParagraphTransformerContext( GetTransformer(),
266 rQName);
267 pContext = pPTC;
269 break;
270 default:
271 OSL_ENSURE( false, "unknown action" );
272 break;
277 else
279 XMLTransformerActions *pActions =
280 GetTransformer().GetUserDefinedActions( m_nActionMap );
281 OSL_ENSURE( pActions, "go no actions" );
282 if( pActions )
284 XMLTransformerActions::key_type aKey( nPrefix, rLocalName );
285 XMLTransformerActions::const_iterator aIter =
286 pActions->find( aKey );
288 if( !(aIter == pActions->end()) )
290 switch( (*aIter).second.m_nActionType )
292 case XML_ETACTION_EXTRACT_CHARACTERS:
294 if( !m_bStartElementExported )
295 ExportStartElement();
296 XMLParagraphTransformerContext* pPTC =
297 new XMLParagraphTransformerContext( GetTransformer(),
298 rQName);
299 pContext = pPTC;
301 break;
302 default:
303 OSL_ENSURE( false, "unknown action" );
304 break;
310 // default is copying
311 if( !pContext )
313 if( !m_bStartElementExported )
314 ExportStartElement();
315 pContext = XMLTransformerContext::CreateChildContext( nPrefix,
316 rLocalName,
317 rQName,
318 rAttrList );
321 return pContext;
324 void XMLMergeElemTransformerContext::EndElement()
326 if( !m_bStartElementExported )
327 ExportStartElement();
328 XMLTransformerContext::EndElement();
331 void XMLMergeElemTransformerContext::Characters( const OUString& )
333 // ignore
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */