Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / xmloff / source / transform / FormPropOOoTContext.cxx
blobd2d893271fb7801b30ed957d3644f8c9fc9a65d7
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 "MutableAttrList.hxx"
21 #include <xmloff/xmlnmspe.hxx>
22 #include <xmloff/nmspmap.hxx>
23 #include "IgnoreTContext.hxx"
24 #include "ActionMapTypesOOo.hxx"
25 #include "AttrTransformerAction.hxx"
26 #include "TransformerActions.hxx"
27 #include "TransformerBase.hxx"
28 #include "FormPropOOoTContext.hxx"
29 #include <osl/diagnose.h>
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::xml::sax;
33 using namespace ::xmloff::token;
35 class XMLFormPropValueTContext_Impl : public XMLTransformerContext
37 OUString m_aAttrQName;
38 OUString m_aCharacters;
39 bool m_bPersistent;
40 bool m_bIsVoid;
42 public:
43 // element content persistence only
44 XMLFormPropValueTContext_Impl( XMLTransformerBase& rTransformer,
45 const OUString& rQName );
46 XMLFormPropValueTContext_Impl( XMLTransformerBase& rTransformer,
47 const OUString& rQName,
48 XMLTokenEnum eAttrToken );
50 virtual ~XMLFormPropValueTContext_Impl();
52 virtual void StartElement( const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList ) override;
54 virtual void EndElement() override;
56 virtual void Characters( const OUString& rChars ) override;
58 virtual bool IsPersistent() const override;
60 bool IsVoid() const { return m_bIsVoid; }
61 const OUString& GetTextContent() const { return m_aCharacters; }
64 XMLFormPropValueTContext_Impl::XMLFormPropValueTContext_Impl(
65 XMLTransformerBase& rTransformer,
66 const OUString& rQName ) :
67 XMLTransformerContext( rTransformer, rQName ),
68 m_bPersistent( true ),
69 m_bIsVoid( false )
73 XMLFormPropValueTContext_Impl::XMLFormPropValueTContext_Impl(
74 XMLTransformerBase& rTransformer,
75 const OUString& rQName,
76 XMLTokenEnum eAttrToken ) :
77 XMLTransformerContext( rTransformer, rQName ),
78 m_aAttrQName( rTransformer.GetNamespaceMap().GetQNameByKey(
79 XML_NAMESPACE_OFFICE, GetXMLToken(eAttrToken) ) ),
80 m_bPersistent( true ),
81 m_bIsVoid( false )
85 XMLFormPropValueTContext_Impl::~XMLFormPropValueTContext_Impl()
89 void XMLFormPropValueTContext_Impl::StartElement(
90 const Reference< XAttributeList >& rAttrList )
92 sal_Int16 nAttrCount = rAttrList.is() ? rAttrList->getLength() : 0;
93 for( sal_Int16 i=0; i < nAttrCount; i++ )
95 const OUString& rAttrName = rAttrList->getNameByIndex( i );
96 OUString aLocalName;
97 sal_uInt16 nPrefix =
98 GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
99 &aLocalName );
100 if( XML_NAMESPACE_FORM == nPrefix &&
101 IsXMLToken( aLocalName, XML_PROPERTY_IS_VOID ) &&
102 IsXMLToken( rAttrList->getValueByIndex( i ), XML_TRUE ) )
103 m_bIsVoid = true;
107 void XMLFormPropValueTContext_Impl::EndElement()
109 if( !m_bPersistent )
111 XMLMutableAttributeList *pMutableAttrList =
112 new XMLMutableAttributeList;
113 Reference< XAttributeList > xAttrList( pMutableAttrList );
114 pMutableAttrList->AddAttribute( m_aAttrQName,
115 m_aCharacters );
117 OUString aElemQName( GetTransformer().GetNamespaceMap().GetQNameByKey(
118 XML_NAMESPACE_FORM, GetXMLToken(XML_LIST_VALUE) ) );
119 GetTransformer().GetDocHandler()->startElement( aElemQName,
120 xAttrList );
121 GetTransformer().GetDocHandler()->endElement( aElemQName );
125 void XMLFormPropValueTContext_Impl::Characters( const OUString& rChars )
127 m_aCharacters += rChars;
130 bool XMLFormPropValueTContext_Impl::IsPersistent() const
132 return m_bPersistent;
135 XMLFormPropOOoTransformerContext::XMLFormPropOOoTransformerContext(
136 XMLTransformerBase& rImp,
137 const OUString& rQName ) :
138 XMLTransformerContext( rImp, rQName ),
139 m_aElemQName( rQName ),
140 m_nValueTypeAttr( -1 ),
141 m_eValueToken( XML_VALUE ),
142 m_eValueTypeToken( XML_TOKEN_END ),
143 m_bIsList( false )
147 XMLFormPropOOoTransformerContext::~XMLFormPropOOoTransformerContext()
151 rtl::Reference<XMLTransformerContext> XMLFormPropOOoTransformerContext::CreateChildContext(
152 sal_uInt16 nPrefix,
153 const OUString& rLocalName,
154 const OUString& rQName,
155 const Reference< XAttributeList >& )
157 rtl::Reference<XMLTransformerContext> pContext;
159 if( XML_NAMESPACE_FORM == nPrefix &&
160 IsXMLToken( rLocalName, XML_PROPERTY_VALUE ) )
162 if( m_bIsList )
164 pContext.set(new XMLFormPropValueTContext_Impl( GetTransformer(),
165 rQName,
166 m_eValueToken ));
168 else if( !m_xValueContext.is() )
170 m_xValueContext=
171 new XMLFormPropValueTContext_Impl( GetTransformer(), rQName );
172 pContext.set(m_xValueContext.get());
176 // default is ignore
177 if( !pContext.is() )
178 pContext.set(new XMLIgnoreTransformerContext( GetTransformer(), rQName,
179 true, true ));
180 return pContext;
183 void XMLFormPropOOoTransformerContext::StartElement(
184 const Reference< XAttributeList >& rAttrList )
187 XMLTransformerActions *pActions =
188 GetTransformer().GetUserDefinedActions( OOO_FORM_PROP_ACTIONS );
189 OSL_ENSURE( pActions, "go no actions" );
191 XMLMutableAttributeList *pMutableAttrList =
192 new XMLMutableAttributeList( rAttrList, true );
193 m_xAttrList = pMutableAttrList;
195 OUString aValueType;
196 sal_Int16 nAttrCount = rAttrList.is() ? rAttrList->getLength() : 0;
197 for( sal_Int16 i=0; i < nAttrCount; i++ )
199 const OUString& rAttrName = rAttrList->getNameByIndex( i );
200 OUString aLocalName;
201 sal_uInt16 nPrefix =
202 GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
203 &aLocalName );
204 XMLTransformerActions::key_type aKey( nPrefix, aLocalName );
205 XMLTransformerActions::const_iterator aIter =
206 pActions->find( aKey );
207 if( !(aIter == pActions->end() ) )
209 const OUString& rAttrValue = rAttrList->getValueByIndex( i );
210 switch( (*aIter).second.m_nActionType )
212 case XML_ATACTION_RENAME:
213 if( IsXMLToken( aLocalName, XML_PROPERTY_TYPE ) )
215 aValueType = rAttrValue;
216 m_nValueTypeAttr = i;
219 OUString aNewAttrQName(
220 GetTransformer().GetNamespaceMap().GetQNameByKey(
221 (*aIter).second.GetQNamePrefixFromParam1(),
222 ::xmloff::token::GetXMLToken(
223 (*aIter).second.GetQNameTokenFromParam1()) ) );
224 pMutableAttrList->RenameAttributeByIndex( i, aNewAttrQName );
226 break;
227 case XML_ATACTION_REMOVE:
228 if( IsXMLToken( aLocalName, XML_PROPERTY_IS_LIST ) )
230 m_aElemQName =
231 GetTransformer().GetNamespaceMap().GetQNameByKey(
232 XML_NAMESPACE_FORM, GetXMLToken( XML_LIST_PROPERTY ) );
233 m_bIsList = true;
235 pMutableAttrList->RemoveAttributeByIndex( i );
236 --i;
237 --nAttrCount;
238 break;
239 default:
240 OSL_ENSURE( false, "unknown action" );
241 break;
246 if( !aValueType.isEmpty() )
248 if( IsXMLToken( aValueType, XML_STRING ) )
249 m_eValueToken = XML_STRING_VALUE;
250 else if( IsXMLToken( aValueType, XML_BOOLEAN ) )
251 m_eValueToken = XML_BOOLEAN_VALUE;
252 else if( IsXMLToken( aValueType, XML_SHORT ) ||
253 IsXMLToken( aValueType, XML_INT ) ||
254 IsXMLToken( aValueType, XML_LONG ) ||
255 IsXMLToken( aValueType, XML_DOUBLE ) )
256 m_eValueTypeToken = XML_FLOAT;
260 void XMLFormPropOOoTransformerContext::EndElement()
262 if( m_xValueContext.is() )
264 if( m_xValueContext->IsVoid() )
266 m_eValueTypeToken = XML_VOID;
268 else
270 OUString aAttrQName(
271 GetTransformer().GetNamespaceMap().GetQNameByKey(
272 XML_NAMESPACE_OFFICE, GetXMLToken(m_eValueToken) ) );
273 static_cast< XMLMutableAttributeList * >( m_xAttrList.get() )
274 ->AddAttribute( aAttrQName, m_xValueContext->GetTextContent() );
278 if( m_nValueTypeAttr != -1 && m_eValueTypeToken != XML_TOKEN_END )
280 static_cast< XMLMutableAttributeList * >( m_xAttrList.get() )
281 ->SetValueByIndex( m_nValueTypeAttr ,
282 GetXMLToken( m_eValueTypeToken ) );
285 GetTransformer().GetDocHandler()->startElement( m_aElemQName,
286 m_xAttrList );
287 GetTransformer().GetDocHandler()->endElement( m_aElemQName );
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */