Update git submodules
[LibreOffice.git] / xmloff / source / transform / FormPropOOoTContext.cxx
blobf77ce2884a99ed562d55ffb380329988f28105cb
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/xmlnamespace.hxx>
22 #include <xmloff/namespacemap.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_aCharacters;
38 bool m_bIsVoid;
40 public:
41 // element content persistence only
42 XMLFormPropValueTContext_Impl( XMLTransformerBase& rTransformer,
43 const OUString& rQName );
45 virtual void StartElement( const css::uno::Reference< css::xml::sax::XAttributeList >& xAttrList ) override;
47 virtual void EndElement() override;
49 virtual void Characters( const OUString& rChars ) override;
51 virtual bool IsPersistent() const override;
53 bool IsVoid() const { return m_bIsVoid; }
54 const OUString& GetTextContent() const { return m_aCharacters; }
57 XMLFormPropValueTContext_Impl::XMLFormPropValueTContext_Impl(
58 XMLTransformerBase& rTransformer,
59 const OUString& rQName ) :
60 XMLTransformerContext( rTransformer, rQName ),
61 m_bIsVoid( false )
65 void XMLFormPropValueTContext_Impl::StartElement(
66 const Reference< XAttributeList >& rAttrList )
68 sal_Int16 nAttrCount = rAttrList.is() ? rAttrList->getLength() : 0;
69 for( sal_Int16 i=0; i < nAttrCount; i++ )
71 const OUString aAttrName = rAttrList->getNameByIndex( i );
72 OUString aLocalName;
73 sal_uInt16 nPrefix =
74 GetTransformer().GetNamespaceMap().GetKeyByAttrName( aAttrName,
75 &aLocalName );
76 if( XML_NAMESPACE_FORM == nPrefix &&
77 IsXMLToken( aLocalName, XML_PROPERTY_IS_VOID ) &&
78 IsXMLToken( rAttrList->getValueByIndex( i ), XML_TRUE ) )
79 m_bIsVoid = true;
83 void XMLFormPropValueTContext_Impl::EndElement()
87 void XMLFormPropValueTContext_Impl::Characters( const OUString& rChars )
89 m_aCharacters += rChars;
92 bool XMLFormPropValueTContext_Impl::IsPersistent() const
94 return true;
97 XMLFormPropOOoTransformerContext::XMLFormPropOOoTransformerContext(
98 XMLTransformerBase& rImp,
99 const OUString& rQName ) :
100 XMLTransformerContext( rImp, rQName ),
101 m_aElemQName( rQName ),
102 m_nValueTypeAttr( -1 ),
103 m_eValueToken( XML_VALUE ),
104 m_eValueTypeToken( XML_TOKEN_END ),
105 m_bIsList( false )
109 XMLFormPropOOoTransformerContext::~XMLFormPropOOoTransformerContext()
113 rtl::Reference<XMLTransformerContext> XMLFormPropOOoTransformerContext::CreateChildContext(
114 sal_uInt16 nPrefix,
115 const OUString& rLocalName,
116 const OUString& rQName,
117 const Reference< XAttributeList >& )
119 rtl::Reference<XMLTransformerContext> pContext;
121 if( XML_NAMESPACE_FORM == nPrefix &&
122 IsXMLToken( rLocalName, XML_PROPERTY_VALUE ) )
124 if( m_bIsList )
126 pContext.set(new XMLFormPropValueTContext_Impl( GetTransformer(),
127 rQName ));
129 else if( !m_xValueContext.is() )
131 m_xValueContext=
132 new XMLFormPropValueTContext_Impl( GetTransformer(), rQName );
133 pContext = m_xValueContext;
137 // default is ignore
138 if( !pContext.is() )
139 pContext.set(new XMLIgnoreTransformerContext( GetTransformer(), rQName,
140 true, true ));
141 return pContext;
144 void XMLFormPropOOoTransformerContext::StartElement(
145 const Reference< XAttributeList >& rAttrList )
148 XMLTransformerActions *pActions =
149 GetTransformer().GetUserDefinedActions( OOO_FORM_PROP_ACTIONS );
150 OSL_ENSURE( pActions, "go no actions" );
152 rtl::Reference<XMLMutableAttributeList> pMutableAttrList =
153 new XMLMutableAttributeList( rAttrList, true );
154 m_xAttrList = pMutableAttrList;
156 OUString aValueType;
157 sal_Int16 nAttrCount = rAttrList.is() ? rAttrList->getLength() : 0;
158 for( sal_Int16 i=0; i < nAttrCount; i++ )
160 const OUString aAttrName = rAttrList->getNameByIndex( i );
161 OUString aLocalName;
162 sal_uInt16 nPrefix =
163 GetTransformer().GetNamespaceMap().GetKeyByAttrName( aAttrName,
164 &aLocalName );
165 XMLTransformerActions::key_type aKey( nPrefix, aLocalName );
166 XMLTransformerActions::const_iterator aIter =
167 pActions->find( aKey );
168 if( aIter != pActions->end() )
170 const OUString aAttrValue = rAttrList->getValueByIndex( i );
171 switch( (*aIter).second.m_nActionType )
173 case XML_ATACTION_RENAME:
174 if( IsXMLToken( aLocalName, XML_PROPERTY_TYPE ) )
176 aValueType = aAttrValue;
177 m_nValueTypeAttr = i;
180 OUString aNewAttrQName(
181 GetTransformer().GetNamespaceMap().GetQNameByKey(
182 (*aIter).second.GetQNamePrefixFromParam1(),
183 ::xmloff::token::GetXMLToken(
184 (*aIter).second.GetQNameTokenFromParam1()) ) );
185 pMutableAttrList->RenameAttributeByIndex( i, aNewAttrQName );
187 break;
188 case XML_ATACTION_REMOVE:
189 if( IsXMLToken( aLocalName, XML_PROPERTY_IS_LIST ) )
191 m_aElemQName =
192 GetTransformer().GetNamespaceMap().GetQNameByKey(
193 XML_NAMESPACE_FORM, GetXMLToken( XML_LIST_PROPERTY ) );
194 m_bIsList = true;
196 pMutableAttrList->RemoveAttributeByIndex( i );
197 --i;
198 --nAttrCount;
199 break;
200 default:
201 OSL_ENSURE( false, "unknown action" );
202 break;
207 if( !aValueType.isEmpty() )
209 if( IsXMLToken( aValueType, XML_STRING ) )
210 m_eValueToken = XML_STRING_VALUE;
211 else if( IsXMLToken( aValueType, XML_BOOLEAN ) )
212 m_eValueToken = XML_BOOLEAN_VALUE;
213 else if( IsXMLToken( aValueType, XML_SHORT ) ||
214 IsXMLToken( aValueType, XML_INT ) ||
215 IsXMLToken( aValueType, XML_LONG ) ||
216 IsXMLToken( aValueType, XML_DOUBLE ) )
217 m_eValueTypeToken = XML_FLOAT;
221 void XMLFormPropOOoTransformerContext::EndElement()
223 if( m_xValueContext.is() )
225 if( m_xValueContext->IsVoid() )
227 m_eValueTypeToken = XML_VOID;
229 else
231 OUString aAttrQName(
232 GetTransformer().GetNamespaceMap().GetQNameByKey(
233 XML_NAMESPACE_OFFICE, GetXMLToken(m_eValueToken) ) );
234 m_xAttrList->AddAttribute( aAttrQName, m_xValueContext->GetTextContent() );
238 if( m_nValueTypeAttr != -1 && m_eValueTypeToken != XML_TOKEN_END )
240 m_xAttrList->SetValueByIndex( m_nValueTypeAttr ,
241 GetXMLToken( m_eValueTypeToken ) );
244 GetTransformer().GetDocHandler()->startElement( m_aElemQName, m_xAttrList );
245 GetTransformer().GetDocHandler()->endElement( m_aElemQName );
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */