1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MergeElemTContext.cxx,v $
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"
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
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(),
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
128 XMLPersTextContentRNGTransformTContext(
129 XMLTransformerBase
& rTransformer
,
130 const ::rtl::OUString
& rQName
,
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
,
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
);
205 GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName
,
207 sal_Bool bRemove
= sal_True
;
208 if( XML_NAMESPACE_OFFICE
== nPrefix
)
210 if (IsXMLToken( aLocalName
, XML_DISPLAY
) )
212 else if (IsXMLToken( aLocalName
, XML_AUTHOR
) )
214 else if (IsXMLToken( aLocalName
, XML_CREATE_DATE
) )
216 else if (IsXMLToken( aLocalName
, XML_CREATE_DATE_STRING
) )
221 pMutableAttrList
->RemoveAttributeByIndex( i
);
228 XMLTransformerContext
*XMLMergeElemTransformerContext::CreateChildContext(
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" );
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
);
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
);
275 case XML_ETACTION_EXTRACT_CHARACTERS
:
277 if( !m_bStartElementExported
)
278 ExportStartElement();
279 XMLParagraphTransformerContext
* pPTC
=
280 new XMLParagraphTransformerContext( GetTransformer(),
286 OSL_ENSURE( !this, "unknown action" );
294 XMLTransformerActions
*pActions
=
295 GetTransformer().GetUserDefinedActions( m_nActionMap
);
296 OSL_ENSURE( pActions
, "go no actions" );
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(),
318 OSL_ENSURE( !this, "unknown action" );
325 // default is copying
328 if( !m_bStartElementExported
)
329 ExportStartElement();
330 pContext
= XMLTransformerContext::CreateChildContext( nPrefix
,
339 void XMLMergeElemTransformerContext::EndElement()
341 if( !m_bStartElementExported
)
342 ExportStartElement();
343 XMLTransformerContext::EndElement();
346 void XMLMergeElemTransformerContext::Characters( const OUString
& )