1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/xmlnmspe.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 class XMLParagraphTransformerContext
: public XMLTransformerContext
36 XMLParagraphTransformerContext( XMLTransformerBase
& rTransformer
,
37 const OUString
& rQName
);
39 // Create a children element context. By default, the import's
40 // CreateContext method is called to create a new default context.
41 virtual rtl::Reference
<XMLTransformerContext
> CreateChildContext( sal_uInt16 nPrefix
,
42 const OUString
& rLocalName
,
43 const OUString
& rQName
,
44 const css::uno::Reference
< css::xml::sax::XAttributeList
>& xAttrList
) override
;
47 XMLParagraphTransformerContext::XMLParagraphTransformerContext(
48 XMLTransformerBase
& rImp
,
49 const OUString
& rQName
) :
50 XMLTransformerContext( rImp
, rQName
)
54 rtl::Reference
<XMLTransformerContext
> XMLParagraphTransformerContext::CreateChildContext(
55 sal_uInt16
/*nPrefix*/,
56 const OUString
& /*rLocalName*/,
57 const OUString
& rQName
,
58 const Reference
< XAttributeList
>& )
60 return new XMLIgnoreTransformerContext( GetTransformer(),
64 class XMLPersTextContentRNGTransformTContext
: public XMLPersTextContentTContext
67 XMLPersTextContentRNGTransformTContext(
68 XMLTransformerBase
& rTransformer
,
69 const OUString
& rQName
,
71 ::xmloff::token::XMLTokenEnum eToken
);
73 virtual void Characters( const OUString
& rChars
) override
;
76 XMLPersTextContentRNGTransformTContext::XMLPersTextContentRNGTransformTContext(
77 XMLTransformerBase
& rTransformer
,
78 const OUString
& rQName
,
80 ::xmloff::token::XMLTokenEnum eToken
) :
81 XMLPersTextContentTContext(
82 rTransformer
, rQName
, nPrefix
, eToken
)
85 void XMLPersTextContentRNGTransformTContext::Characters( const OUString
& rChars
)
87 OUString
aConvChars( rChars
);
88 XMLTransformerBase::ConvertRNGDateTimeToISO( aConvChars
);
89 XMLPersTextContentTContext::Characters( aConvChars
);
93 void XMLMergeElemTransformerContext::ExportStartElement()
95 for( const auto& rChildContext
: m_aChildContexts
)
97 XMLPersTextContentTContext
*pContext
= rChildContext
.get();
98 static_cast< XMLMutableAttributeList
* >( m_xAttrList
.get() )
99 ->AddAttribute( pContext
->GetExportQName(),
100 pContext
->GetTextContent() );
102 XMLTransformerContext::StartElement( m_xAttrList
);
104 m_bStartElementExported
= true;
107 XMLMergeElemTransformerContext::XMLMergeElemTransformerContext(
108 XMLTransformerBase
& rImp
,
109 const OUString
& rQName
,
110 sal_uInt16 nActionMap
) :
111 XMLTransformerContext( rImp
, rQName
),
112 m_nActionMap( nActionMap
),
113 m_bStartElementExported( false )
117 void XMLMergeElemTransformerContext::StartElement(
118 const Reference
< XAttributeList
>& rAttrList
)
120 XMLMutableAttributeList
*pMutableAttrList
=
121 new XMLMutableAttributeList( rAttrList
, true );
122 m_xAttrList
= pMutableAttrList
;
124 sal_Int16 nAttrCount
= m_xAttrList
.is() ? m_xAttrList
->getLength() : 0;
125 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
127 const OUString
& rAttrName
= m_xAttrList
->getNameByIndex( i
);
130 GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName
,
133 if( XML_NAMESPACE_OFFICE
== nPrefix
)
135 if (IsXMLToken( aLocalName
, XML_DISPLAY
) )
137 else if (IsXMLToken( aLocalName
, XML_AUTHOR
) )
139 else if (IsXMLToken( aLocalName
, XML_CREATE_DATE
) )
141 else if (IsXMLToken( aLocalName
, XML_CREATE_DATE_STRING
) )
146 pMutableAttrList
->RemoveAttributeByIndex( i
);
153 rtl::Reference
<XMLTransformerContext
> XMLMergeElemTransformerContext::CreateChildContext(
155 const OUString
& rLocalName
,
156 const OUString
& rQName
,
157 const Reference
< XAttributeList
>& rAttrList
)
159 rtl::Reference
<XMLTransformerContext
> pContext
;
161 if( !m_bStartElementExported
)
163 XMLTransformerActions
*pActions
=
164 GetTransformer().GetUserDefinedActions( m_nActionMap
);
165 OSL_ENSURE( pActions
, "go no actions" );
168 XMLTransformerActions::key_type
aKey( nPrefix
, rLocalName
);
169 XMLTransformerActions::const_iterator aIter
=
170 pActions
->find( aKey
);
172 if( aIter
!= pActions
->end() )
174 switch( (*aIter
).second
.m_nActionType
)
176 case XML_ETACTION_MOVE_TO_ATTR_RNG2ISO_DATETIME
:
178 rtl::Reference
<XMLPersTextContentTContext
> pTC(
179 new XMLPersTextContentRNGTransformTContext(
180 GetTransformer(), rQName
,
181 (*aIter
).second
.GetQNamePrefixFromParam1(),
182 (*aIter
).second
.GetQNameTokenFromParam1() ));
183 m_aChildContexts
.push_back(pTC
);
184 pContext
.set(pTC
.get());
187 case XML_ETACTION_MOVE_TO_ATTR
:
189 rtl::Reference
<XMLPersTextContentTContext
> pTC(
190 new XMLPersTextContentTContext(
191 GetTransformer(), rQName
,
192 (*aIter
).second
.GetQNamePrefixFromParam1(),
193 (*aIter
).second
.GetQNameTokenFromParam1() ));
194 m_aChildContexts
.push_back(pTC
);
195 pContext
.set(pTC
.get());
198 case XML_ETACTION_EXTRACT_CHARACTERS
:
200 if( !m_bStartElementExported
)
201 ExportStartElement();
203 new XMLParagraphTransformerContext( GetTransformer(),
208 OSL_ENSURE( false, "unknown action" );
216 XMLTransformerActions
*pActions
=
217 GetTransformer().GetUserDefinedActions( m_nActionMap
);
218 OSL_ENSURE( pActions
, "go no actions" );
221 XMLTransformerActions::key_type
aKey( nPrefix
, rLocalName
);
222 XMLTransformerActions::const_iterator aIter
=
223 pActions
->find( aKey
);
225 if( aIter
!= pActions
->end() )
227 switch( (*aIter
).second
.m_nActionType
)
229 case XML_ETACTION_EXTRACT_CHARACTERS
:
231 if( !m_bStartElementExported
)
232 ExportStartElement();
234 new XMLParagraphTransformerContext( GetTransformer(),
239 OSL_ENSURE( false, "unknown action" );
246 // default is copying
249 if( !m_bStartElementExported
)
250 ExportStartElement();
251 pContext
= XMLTransformerContext::CreateChildContext( nPrefix
,
260 void XMLMergeElemTransformerContext::EndElement()
262 if( !m_bStartElementExported
)
263 ExportStartElement();
264 XMLTransformerContext::EndElement();
267 void XMLMergeElemTransformerContext::Characters( const OUString
& )
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */