bump product version to 4.1.6.2
[LibreOffice.git] / xmloff / source / transform / MergeElemTContext.cxx
blobcbe26744ce2691a950166aab103c8049c54a8222
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"
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::xml::sax;
31 using namespace ::xmloff::token;
33 class XMLParagraphTransformerContext : public XMLTransformerContext
35 public:
36 TYPEINFO();
38 XMLParagraphTransformerContext( XMLTransformerBase& rTransformer,
39 const OUString& rQName );
41 virtual ~XMLParagraphTransformerContext();
43 // Create a children element context. By default, the import's
44 // CreateContext method is called to create a new default context.
45 virtual XMLTransformerContext *CreateChildContext( sal_uInt16 nPrefix,
46 const OUString& rLocalName,
47 const OUString& rQName,
48 const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList );
50 // StartElement is called after a context has been constructed and
51 // before a elements context is parsed. It may be used for actions that
52 // require virtual methods. The default is to do nothing.
53 virtual void StartElement( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList );
55 // EndElement is called before a context will be destructed, but
56 // after a elements context has been parsed. It may be used for actions
57 // that require virtual methods. The default is to do nothing.
58 virtual void EndElement();
60 // This method is called for all characters that are contained in the
61 // current element. The default is to ignore them.
62 virtual void Characters( const OUString& rChars );
65 TYPEINIT1( XMLParagraphTransformerContext, XMLTransformerContext );
67 XMLParagraphTransformerContext::XMLParagraphTransformerContext(
68 XMLTransformerBase& rImp,
69 const OUString& rQName ) :
70 XMLTransformerContext( rImp, rQName )
74 XMLParagraphTransformerContext::~XMLParagraphTransformerContext()
78 XMLTransformerContext *XMLParagraphTransformerContext::CreateChildContext(
79 sal_uInt16 /*nPrefix*/,
80 const OUString& /*rLocalName*/,
81 const OUString& rQName,
82 const Reference< XAttributeList >& )
84 XMLTransformerContext *pContext = 0;
86 pContext = new XMLIgnoreTransformerContext( GetTransformer(),
87 rQName, sal_True );
89 return pContext;
92 void XMLParagraphTransformerContext::StartElement( const Reference< XAttributeList >& rAttrList )
94 XMLTransformerContext::StartElement( rAttrList );
97 void XMLParagraphTransformerContext::EndElement()
99 XMLTransformerContext::EndElement();
102 void XMLParagraphTransformerContext::Characters( const OUString& rChars )
104 XMLTransformerContext::Characters( rChars );
107 class XMLPersTextContentRNGTransformTContext : public XMLPersTextContentTContext
109 public:
110 TYPEINFO();
112 XMLPersTextContentRNGTransformTContext(
113 XMLTransformerBase& rTransformer,
114 const OUString& rQName,
115 sal_uInt16 nPrefix,
116 ::xmloff::token::XMLTokenEnum eToken );
117 virtual ~XMLPersTextContentRNGTransformTContext();
119 virtual void Characters( const OUString& rChars );
122 TYPEINIT1( XMLPersTextContentRNGTransformTContext, XMLPersAttrListTContext );
124 XMLPersTextContentRNGTransformTContext::XMLPersTextContentRNGTransformTContext(
125 XMLTransformerBase& rTransformer,
126 const OUString& rQName,
127 sal_uInt16 nPrefix,
128 ::xmloff::token::XMLTokenEnum eToken ) :
129 XMLPersTextContentTContext(
130 rTransformer, rQName, nPrefix, eToken )
133 XMLPersTextContentRNGTransformTContext::~XMLPersTextContentRNGTransformTContext()
136 void XMLPersTextContentRNGTransformTContext::Characters( const OUString& rChars )
138 OUString aConvChars( rChars );
139 GetTransformer().ConvertRNGDateTimeToISO( aConvChars );
140 XMLPersTextContentTContext::Characters( aConvChars );
144 TYPEINIT1( XMLMergeElemTransformerContext, XMLTransformerContext );
146 void XMLMergeElemTransformerContext::ExportStartElement()
148 XMLPersTextContentTContextVector::iterator aIter = m_aChildContexts.begin();
150 for( ; aIter != m_aChildContexts.end(); ++aIter )
152 XMLPersTextContentTContext *pContext = (*aIter).get();
153 static_cast< XMLMutableAttributeList * >( m_xAttrList.get() )
154 ->AddAttribute( pContext->GetExportQName(),
155 pContext->GetTextContent() );
157 XMLTransformerContext::StartElement( m_xAttrList );
159 m_bStartElementExported = sal_True;
162 XMLMergeElemTransformerContext::XMLMergeElemTransformerContext(
163 XMLTransformerBase& rImp,
164 const OUString& rQName,
165 sal_uInt16 nActionMap ) :
166 XMLTransformerContext( rImp, rQName ),
167 m_nActionMap( nActionMap ),
168 m_bStartElementExported( sal_False )
172 XMLMergeElemTransformerContext::~XMLMergeElemTransformerContext()
176 void XMLMergeElemTransformerContext::StartElement(
177 const Reference< XAttributeList >& rAttrList )
179 XMLMutableAttributeList *pMutableAttrList =
180 new XMLMutableAttributeList( rAttrList, sal_True );
181 m_xAttrList = pMutableAttrList;
183 sal_Int16 nAttrCount = m_xAttrList.is() ? m_xAttrList->getLength() : 0;
184 for( sal_Int16 i=0; i < nAttrCount; i++ )
186 const OUString& rAttrName = m_xAttrList->getNameByIndex( i );
187 OUString aLocalName;
188 sal_uInt16 nPrefix =
189 GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
190 &aLocalName );
191 sal_Bool bRemove = sal_True;
192 if( XML_NAMESPACE_OFFICE == nPrefix)
194 if (IsXMLToken( aLocalName, XML_DISPLAY ) )
195 bRemove = sal_False;
196 else if (IsXMLToken( aLocalName, XML_AUTHOR ) )
197 bRemove = sal_False;
198 else if (IsXMLToken( aLocalName, XML_CREATE_DATE ) )
199 bRemove = sal_False;
200 else if (IsXMLToken( aLocalName, XML_CREATE_DATE_STRING ) )
201 bRemove = sal_False;
203 if (bRemove)
205 pMutableAttrList->RemoveAttributeByIndex( i );
206 --i;
207 --nAttrCount;
212 XMLTransformerContext *XMLMergeElemTransformerContext::CreateChildContext(
213 sal_uInt16 nPrefix,
214 const OUString& rLocalName,
215 const OUString& rQName,
216 const Reference< XAttributeList >& rAttrList )
218 XMLTransformerContext *pContext = 0;
220 if( !m_bStartElementExported )
222 XMLTransformerActions *pActions =
223 GetTransformer().GetUserDefinedActions( m_nActionMap );
224 OSL_ENSURE( pActions, "go no actions" );
225 if( pActions )
227 XMLTransformerActions::key_type aKey( nPrefix, rLocalName );
228 XMLTransformerActions::const_iterator aIter =
229 pActions->find( aKey );
231 if( !(aIter == pActions->end()) )
233 switch( (*aIter).second.m_nActionType )
235 case XML_ATACTION_MOVE_FROM_ELEM_RNG2ISO_DATETIME:
237 XMLPersTextContentTContext *pTC =
238 new XMLPersTextContentRNGTransformTContext(
239 GetTransformer(), rQName,
240 (*aIter).second.GetQNamePrefixFromParam1(),
241 (*aIter).second.GetQNameTokenFromParam1() );
242 XMLPersTextContentTContextVector::value_type aVal(pTC);
243 m_aChildContexts.push_back( aVal );
244 pContext = pTC;
246 break;
247 case XML_ATACTION_MOVE_FROM_ELEM:
249 XMLPersTextContentTContext *pTC =
250 new XMLPersTextContentTContext(
251 GetTransformer(), rQName,
252 (*aIter).second.GetQNamePrefixFromParam1(),
253 (*aIter).second.GetQNameTokenFromParam1() );
254 XMLPersTextContentTContextVector::value_type aVal(pTC);
255 m_aChildContexts.push_back( aVal );
256 pContext = pTC;
258 break;
259 case XML_ETACTION_EXTRACT_CHARACTERS:
261 if( !m_bStartElementExported )
262 ExportStartElement();
263 XMLParagraphTransformerContext* pPTC =
264 new XMLParagraphTransformerContext( GetTransformer(),
265 rQName);
266 pContext = pPTC;
268 break;
269 default:
270 OSL_ENSURE( !this, "unknown action" );
271 break;
276 else
278 XMLTransformerActions *pActions =
279 GetTransformer().GetUserDefinedActions( m_nActionMap );
280 OSL_ENSURE( pActions, "go no actions" );
281 if( pActions )
283 XMLTransformerActions::key_type aKey( nPrefix, rLocalName );
284 XMLTransformerActions::const_iterator aIter =
285 pActions->find( aKey );
287 if( !(aIter == pActions->end()) )
289 switch( (*aIter).second.m_nActionType )
291 case XML_ETACTION_EXTRACT_CHARACTERS:
293 if( !m_bStartElementExported )
294 ExportStartElement();
295 XMLParagraphTransformerContext* pPTC =
296 new XMLParagraphTransformerContext( GetTransformer(),
297 rQName);
298 pContext = pPTC;
300 break;
301 default:
302 OSL_ENSURE( !this, "unknown action" );
303 break;
309 // default is copying
310 if( !pContext )
312 if( !m_bStartElementExported )
313 ExportStartElement();
314 pContext = XMLTransformerContext::CreateChildContext( nPrefix,
315 rLocalName,
316 rQName,
317 rAttrList );
320 return pContext;
323 void XMLMergeElemTransformerContext::EndElement()
325 if( !m_bStartElementExported )
326 ExportStartElement();
327 XMLTransformerContext::EndElement();
330 void XMLMergeElemTransformerContext::Characters( const OUString& )
332 // ignore
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */