Debian Codegraph @

xmloff/source/transform/MergeElemTContext.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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/xmlnamespace.hxx>
27 #include <osl/diagnose.h>
28
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::xml::sax;
31 using namespace ::xmloff::token;
32
33 namespace {
34
35 class XMLParagraphTransformerContext : public XMLTransformerContext
36 {
37 public:
38 XMLParagraphTransformerContext( XMLTransformerBase& rTransformer,
39 const OUString& rQName );
40
41 // Create a children element context. By default, the import's
42 // CreateContext method is called to create a new default context.
43 virtual rtl::Reference<XMLTransformerContext> CreateChildContext( sal_uInt16 nPrefix,
44 const OUString& rLocalName,
45 const OUString& rQName,
46 const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList ) override;
47 };
48
49 }
50
51 XMLParagraphTransformerContext::XMLParagraphTransformerContext(
52 XMLTransformerBase& rImp,
53 const OUString& rQName ) :
54 XMLTransformerContext( rImp, rQName )
55 {
56 }
57
58 rtl::Reference<XMLTransformerContext> XMLParagraphTransformerContext::CreateChildContext(
59 sal_uInt16 /*nPrefix*/,
60 const OUString& /*rLocalName*/,
61 const OUString& rQName,
62 const Reference< XAttributeList >& )
63 {
64 return new XMLIgnoreTransformerContext( GetTransformer(),
65 rQName, true );
66 }
67
68 namespace {
69
70 class XMLPersTextContentRNGTransformTContext : public XMLPersTextContentTContext
71 {
72 public:
73 XMLPersTextContentRNGTransformTContext(
74 XMLTransformerBase& rTransformer,
75 const OUString& rQName,
76 sal_uInt16 nPrefix,
77 ::xmloff::token::XMLTokenEnum eToken );
78
79 virtual void Characters( const OUString& rChars ) override;
80 };
81
82 }
83
84 XMLPersTextContentRNGTransformTContext::XMLPersTextContentRNGTransformTContext(
85 XMLTransformerBase& rTransformer,
86 const OUString& rQName,
87 sal_uInt16 nPrefix,
88 ::xmloff::token::XMLTokenEnum eToken ) :
89 XMLPersTextContentTContext(
90 rTransformer, rQName, nPrefix, eToken )
91 {}
92
93 void XMLPersTextContentRNGTransformTContext::Characters( const OUString& rChars )
94 {
95 OUString aConvChars( rChars );
96 XMLTransformerBase::ConvertRNGDateTimeToISO( aConvChars );
97 XMLPersTextContentTContext::Characters( aConvChars );
98 }
99
100
101 void XMLMergeElemTransformerContext::ExportStartElement()
102 {
103 for( const auto& rChildContext : m_aChildContexts )
104 {
105 XMLPersTextContentTContext *pContext = rChildContext.get();
106 m_xAttrList->AddAttribute( pContext->GetExportQName(),
107 pContext->GetTextContent() );
108 }
109 XMLTransformerContext::StartElement( m_xAttrList );
110
111 m_bStartElementExported = true;
112 }
113
114 XMLMergeElemTransformerContext::XMLMergeElemTransformerContext(
115 XMLTransformerBase& rImp,
116 const OUString& rQName,
117 sal_uInt16 nActionMap ) :
118 XMLTransformerContext( rImp, rQName ),
119 m_nActionMap( nActionMap ),
120 m_bStartElementExported( false )
121 {
122 }
123
124 void XMLMergeElemTransformerContext::StartElement(
125 const Reference< XAttributeList >& rAttrList )
126 {
127 rtl::Reference<XMLMutableAttributeList> pMutableAttrList =
128 new XMLMutableAttributeList( rAttrList, true );
129 m_xAttrList = pMutableAttrList;
130
131 sal_Int16 nAttrCount = m_xAttrList.is() ? m_xAttrList->getLength() : 0;
132 for( sal_Int16 i=0; i < nAttrCount; i++ )
133 {
134 const OUString aAttrName = m_xAttrList->getNameByIndex( i );
135 OUString aLocalName;
136 sal_uInt16 nPrefix =
137 GetTransformer().GetNamespaceMap().GetKeyByAttrName( aAttrName,
138 &aLocalName );
139 bool bRemove = true;
140 if( XML_NAMESPACE_OFFICE == nPrefix)
141 {
142 if (IsXMLToken( aLocalName, XML_DISPLAY ) )
143 bRemove = false;
144 else if (IsXMLToken( aLocalName, XML_AUTHOR ) )
145 bRemove = false;
146 else if (IsXMLToken( aLocalName, XML_CREATE_DATE ) )
147 bRemove = false;
148 else if (IsXMLToken( aLocalName, XML_CREATE_DATE_STRING ) )
149 bRemove = false;
150 }
151 if (bRemove)
152 {
153 pMutableAttrList->RemoveAttributeByIndex( i );
154 --i;
155 --nAttrCount;
156 }
157 }
158 }
159
160 rtl::Reference<XMLTransformerContext> XMLMergeElemTransformerContext::CreateChildContext(
161 sal_uInt16 nPrefix,
162 const OUString& rLocalName,
163 const OUString& rQName,
164 const Reference< XAttributeList >& rAttrList )
165 {
166 rtl::Reference<XMLTransformerContext> pContext;
167
168 if( !m_bStartElementExported )
169 {
170 XMLTransformerActions *pActions =
171 GetTransformer().GetUserDefinedActions( m_nActionMap );
172 OSL_ENSURE( pActions, "go no actions" );
173 if( pActions )
174 {
175 XMLTransformerActions::key_type aKey( nPrefix, rLocalName );
176 XMLTransformerActions::const_iterator aIter =
177 pActions->find( aKey );
178
179 if( aIter != pActions->end() )
180 {
181 switch( (*aIter).second.m_nActionType )
182 {
183 case XML_ETACTION_MOVE_TO_ATTR_RNG2ISO_DATETIME:
184 {
185 rtl::Reference<XMLPersTextContentTContext> pTC(
186 new XMLPersTextContentRNGTransformTContext(
187 GetTransformer(), rQName,
188 (*aIter).second.GetQNamePrefixFromParam1(),
189 (*aIter).second.GetQNameTokenFromParam1() ));
190 m_aChildContexts.push_back(pTC);
191 pContext = pTC;
192 }
193 break;
194 case XML_ETACTION_MOVE_TO_ATTR:
195 {
196 rtl::Reference<XMLPersTextContentTContext> pTC(
197 new XMLPersTextContentTContext(
198 GetTransformer(), rQName,
199 (*aIter).second.GetQNamePrefixFromParam1(),
200 (*aIter).second.GetQNameTokenFromParam1() ));
201 m_aChildContexts.push_back(pTC);
202 pContext = pTC;
203 }
204 break;
205 case XML_ETACTION_EXTRACT_CHARACTERS:
206 {
207 if( !m_bStartElementExported )
208 ExportStartElement();
209 pContext.set(
210 new XMLParagraphTransformerContext( GetTransformer(),
211 rQName));
212 }
213 break;
214 default:
215 OSL_ENSURE( false, "unknown action" );
216 break;
217 }
218 }
219 }
220 }
221 else
222 {
223 XMLTransformerActions *pActions =
224 GetTransformer().GetUserDefinedActions( m_nActionMap );
225 OSL_ENSURE( pActions, "go no actions" );
226 if( pActions )
227 {
228 XMLTransformerActions::key_type aKey( nPrefix, rLocalName );
229 XMLTransformerActions::const_iterator aIter =
230 pActions->find( aKey );
231
232 if( aIter != pActions->end() )
233 {
234 switch( (*aIter).second.m_nActionType )
235 {
236 case XML_ETACTION_EXTRACT_CHARACTERS:
237 {
238 if( !m_bStartElementExported )
239 ExportStartElement();
240 pContext.set(
241 new XMLParagraphTransformerContext( GetTransformer(),
242 rQName));
243 }
244 break;
245 default:
246 OSL_ENSURE( false, "unknown action" );
247 break;
248 }
249 }
250 }
251 }
252
253 // default is copying
254 if( !pContext.is() )
255 {
256 if( !m_bStartElementExported )
257 ExportStartElement();
258 pContext = XMLTransformerContext::CreateChildContext( nPrefix,
259 rLocalName,
260 rQName,
261 rAttrList );
262 }
263
264 return pContext;
265 }
266
267 void XMLMergeElemTransformerContext::EndElement()
268 {
269 if( !m_bStartElementExported )
270 ExportStartElement();
271 XMLTransformerContext::EndElement();
272 }
273
274 void XMLMergeElemTransformerContext::Characters( const OUString& )
275 {
276 // ignore
277 }
278
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */