merge the formfield patch from ooo-build
[ooovba.git] / xmloff / source / transform / MergeElemTContext.cxx
blobe6fc9e9cfdb92c283c8272f130db397e25ee6984
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MergeElemTContext.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
33 #include "MergeElemTContext.hxx"
34 #include "MutableAttrList.hxx"
35 #ifndef _XMLOFF_TRANSFORMERBASE_HXX
36 #include "TransformerBase.hxx"
37 #endif
38 #include "TransformerActions.hxx"
39 #include "AttrTransformerAction.hxx"
40 #include "ElemTransformerAction.hxx"
41 #include "IgnoreTContext.hxx"
42 #include "xmlnmspe.hxx"
44 using ::rtl::OUString;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::xml::sax;
47 using namespace ::xmloff::token;
49 class XMLParagraphTransformerContext : public XMLTransformerContext
51 public:
52 TYPEINFO();
54 XMLParagraphTransformerContext( XMLTransformerBase& rTransformer,
55 const ::rtl::OUString& rQName );
57 virtual ~XMLParagraphTransformerContext();
59 // Create a childs element context. By default, the import's
60 // CreateContext method is called to create a new default context.
61 virtual XMLTransformerContext *CreateChildContext( sal_uInt16 nPrefix,
62 const ::rtl::OUString& rLocalName,
63 const ::rtl::OUString& rQName,
64 const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList );
66 // StartElement is called after a context has been constructed and
67 // before a elements context is parsed. It may be used for actions that
68 // require virtual methods. The default is to do nothing.
69 virtual void StartElement( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList );
71 // EndElement is called before a context will be destructed, but
72 // after a elements context has been parsed. It may be used for actions
73 // that require virtual methods. The default is to do nothing.
74 virtual void EndElement();
76 // This method is called for all characters that are contained in the
77 // current element. The default is to ignore them.
78 virtual void Characters( const ::rtl::OUString& rChars );
81 TYPEINIT1( XMLParagraphTransformerContext, XMLTransformerContext );
83 XMLParagraphTransformerContext::XMLParagraphTransformerContext(
84 XMLTransformerBase& rImp,
85 const OUString& rQName ) :
86 XMLTransformerContext( rImp, rQName )
90 XMLParagraphTransformerContext::~XMLParagraphTransformerContext()
94 XMLTransformerContext *XMLParagraphTransformerContext::CreateChildContext(
95 sal_uInt16 /*nPrefix*/,
96 const OUString& /*rLocalName*/,
97 const OUString& rQName,
98 const Reference< XAttributeList >& )
100 XMLTransformerContext *pContext = 0;
102 pContext = new XMLIgnoreTransformerContext( GetTransformer(),
103 rQName, sal_True );
105 return pContext;
108 void XMLParagraphTransformerContext::StartElement( const Reference< XAttributeList >& rAttrList )
110 XMLTransformerContext::StartElement( rAttrList );
113 void XMLParagraphTransformerContext::EndElement()
115 XMLTransformerContext::EndElement();
118 void XMLParagraphTransformerContext::Characters( const OUString& rChars )
120 XMLTransformerContext::Characters( rChars );
123 class XMLPersTextContentRNGTransformTContext : public XMLPersTextContentTContext
125 public:
126 TYPEINFO();
128 XMLPersTextContentRNGTransformTContext(
129 XMLTransformerBase& rTransformer,
130 const ::rtl::OUString& rQName,
131 sal_uInt16 nPrefix,
132 ::xmloff::token::XMLTokenEnum eToken );
133 virtual ~XMLPersTextContentRNGTransformTContext();
135 virtual void Characters( const ::rtl::OUString& rChars );
138 TYPEINIT1( XMLPersTextContentRNGTransformTContext, XMLPersAttrListTContext );
140 XMLPersTextContentRNGTransformTContext::XMLPersTextContentRNGTransformTContext(
141 XMLTransformerBase& rTransformer,
142 const ::rtl::OUString& rQName,
143 sal_uInt16 nPrefix,
144 ::xmloff::token::XMLTokenEnum eToken ) :
145 XMLPersTextContentTContext(
146 rTransformer, rQName, nPrefix, eToken )
149 XMLPersTextContentRNGTransformTContext::~XMLPersTextContentRNGTransformTContext()
152 void XMLPersTextContentRNGTransformTContext::Characters( const ::rtl::OUString& rChars )
154 OUString aConvChars( rChars );
155 GetTransformer().ConvertRNGDateTimeToISO( aConvChars );
156 XMLPersTextContentTContext::Characters( aConvChars );
160 TYPEINIT1( XMLMergeElemTransformerContext, XMLTransformerContext );
162 void XMLMergeElemTransformerContext::ExportStartElement()
164 XMLPersTextContentTContextVector::iterator aIter = m_aChildContexts.begin();
166 for( ; aIter != m_aChildContexts.end(); ++aIter )
168 XMLPersTextContentTContext *pContext = (*aIter).get();
169 static_cast< XMLMutableAttributeList * >( m_xAttrList.get() )
170 ->AddAttribute( pContext->GetExportQName(),
171 pContext->GetTextContent() );
173 XMLTransformerContext::StartElement( m_xAttrList );
175 m_bStartElementExported = sal_True;
178 XMLMergeElemTransformerContext::XMLMergeElemTransformerContext(
179 XMLTransformerBase& rImp,
180 const OUString& rQName,
181 sal_uInt16 nActionMap ) :
182 XMLTransformerContext( rImp, rQName ),
183 m_nActionMap( nActionMap ),
184 m_bStartElementExported( sal_False )
188 XMLMergeElemTransformerContext::~XMLMergeElemTransformerContext()
192 void XMLMergeElemTransformerContext::StartElement(
193 const Reference< XAttributeList >& rAttrList )
195 XMLMutableAttributeList *pMutableAttrList =
196 new XMLMutableAttributeList( rAttrList, sal_True );
197 m_xAttrList = pMutableAttrList;
199 sal_Int16 nAttrCount = m_xAttrList.is() ? m_xAttrList->getLength() : 0;
200 for( sal_Int16 i=0; i < nAttrCount; i++ )
202 const OUString& rAttrName = m_xAttrList->getNameByIndex( i );
203 OUString aLocalName;
204 sal_uInt16 nPrefix =
205 GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
206 &aLocalName );
207 sal_Bool bRemove = sal_True;
208 if( XML_NAMESPACE_OFFICE == nPrefix)
210 if (IsXMLToken( aLocalName, XML_DISPLAY ) )
211 bRemove = sal_False;
212 else if (IsXMLToken( aLocalName, XML_AUTHOR ) )
213 bRemove = sal_False;
214 else if (IsXMLToken( aLocalName, XML_CREATE_DATE ) )
215 bRemove = sal_False;
216 else if (IsXMLToken( aLocalName, XML_CREATE_DATE_STRING ) )
217 bRemove = sal_False;
219 if (bRemove)
221 pMutableAttrList->RemoveAttributeByIndex( i );
222 --i;
223 --nAttrCount;
228 XMLTransformerContext *XMLMergeElemTransformerContext::CreateChildContext(
229 sal_uInt16 nPrefix,
230 const OUString& rLocalName,
231 const OUString& rQName,
232 const Reference< XAttributeList >& rAttrList )
234 XMLTransformerContext *pContext = 0;
236 if( !m_bStartElementExported )
238 XMLTransformerActions *pActions =
239 GetTransformer().GetUserDefinedActions( m_nActionMap );
240 OSL_ENSURE( pActions, "go no actions" );
241 if( pActions )
243 XMLTransformerActions::key_type aKey( nPrefix, rLocalName );
244 XMLTransformerActions::const_iterator aIter =
245 pActions->find( aKey );
247 if( !(aIter == pActions->end()) )
249 switch( (*aIter).second.m_nActionType )
251 case XML_ATACTION_MOVE_FROM_ELEM_RNG2ISO_DATETIME:
253 XMLPersTextContentTContext *pTC =
254 new XMLPersTextContentRNGTransformTContext(
255 GetTransformer(), rQName,
256 (*aIter).second.GetQNamePrefixFromParam1(),
257 (*aIter).second.GetQNameTokenFromParam1() );
258 XMLPersTextContentTContextVector::value_type aVal(pTC);
259 m_aChildContexts.push_back( aVal );
260 pContext = pTC;
262 break;
263 case XML_ATACTION_MOVE_FROM_ELEM:
265 XMLPersTextContentTContext *pTC =
266 new XMLPersTextContentTContext(
267 GetTransformer(), rQName,
268 (*aIter).second.GetQNamePrefixFromParam1(),
269 (*aIter).second.GetQNameTokenFromParam1() );
270 XMLPersTextContentTContextVector::value_type aVal(pTC);
271 m_aChildContexts.push_back( aVal );
272 pContext = pTC;
274 break;
275 case XML_ETACTION_EXTRACT_CHARACTERS:
277 if( !m_bStartElementExported )
278 ExportStartElement();
279 XMLParagraphTransformerContext* pPTC =
280 new XMLParagraphTransformerContext( GetTransformer(),
281 rQName);
282 pContext = pPTC;
284 break;
285 default:
286 OSL_ENSURE( !this, "unknown action" );
287 break;
292 else
294 XMLTransformerActions *pActions =
295 GetTransformer().GetUserDefinedActions( m_nActionMap );
296 OSL_ENSURE( pActions, "go no actions" );
297 if( pActions )
299 XMLTransformerActions::key_type aKey( nPrefix, rLocalName );
300 XMLTransformerActions::const_iterator aIter =
301 pActions->find( aKey );
303 if( !(aIter == pActions->end()) )
305 switch( (*aIter).second.m_nActionType )
307 case XML_ETACTION_EXTRACT_CHARACTERS:
309 if( !m_bStartElementExported )
310 ExportStartElement();
311 XMLParagraphTransformerContext* pPTC =
312 new XMLParagraphTransformerContext( GetTransformer(),
313 rQName);
314 pContext = pPTC;
316 break;
317 default:
318 OSL_ENSURE( !this, "unknown action" );
319 break;
325 // default is copying
326 if( !pContext )
328 if( !m_bStartElementExported )
329 ExportStartElement();
330 pContext = XMLTransformerContext::CreateChildContext( nPrefix,
331 rLocalName,
332 rQName,
333 rAttrList );
336 return pContext;
339 void XMLMergeElemTransformerContext::EndElement()
341 if( !m_bStartElementExported )
342 ExportStartElement();
343 XMLTransformerContext::EndElement();
346 void XMLMergeElemTransformerContext::Characters( const OUString& )
348 // ignore