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 "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
;
45 // element content persistence only
46 XMLFormPropValueTContext_Impl( XMLTransformerBase
& rTransformer
,
47 const OUString
& rQName
);
48 XMLFormPropValueTContext_Impl( XMLTransformerBase
& rTransformer
,
49 const OUString
& rQName
,
50 sal_uInt16 nAttrPrefix
,
51 XMLTokenEnum eAttrToken
);
53 virtual ~XMLFormPropValueTContext_Impl();
55 virtual void StartElement( const ::com::sun::star::uno::Reference
< ::com::sun::star::xml::sax::XAttributeList
>& xAttrList
) SAL_OVERRIDE
;
57 virtual void EndElement() SAL_OVERRIDE
;
59 virtual void Characters( const OUString
& rChars
) SAL_OVERRIDE
;
61 virtual bool IsPersistent() const SAL_OVERRIDE
;
63 bool IsVoid() const { return m_bIsVoid
; }
64 const OUString
& GetTextContent() const { return m_aCharacters
; }
67 TYPEINIT1( XMLFormPropValueTContext_Impl
, XMLTransformerContext
);
69 XMLFormPropValueTContext_Impl::XMLFormPropValueTContext_Impl(
70 XMLTransformerBase
& rTransformer
,
71 const OUString
& rQName
) :
72 XMLTransformerContext( rTransformer
, rQName
),
73 m_bPersistent( true ),
78 XMLFormPropValueTContext_Impl::XMLFormPropValueTContext_Impl(
79 XMLTransformerBase
& rTransformer
,
80 const OUString
& rQName
,
81 sal_uInt16 nAttrPrefix
,
82 XMLTokenEnum eAttrToken
) :
83 XMLTransformerContext( rTransformer
, rQName
),
84 m_aAttrQName( rTransformer
.GetNamespaceMap().GetQNameByKey(
85 nAttrPrefix
, GetXMLToken(eAttrToken
) ) ),
86 m_bPersistent( true ),
91 XMLFormPropValueTContext_Impl::~XMLFormPropValueTContext_Impl()
95 void XMLFormPropValueTContext_Impl::StartElement(
96 const Reference
< XAttributeList
>& rAttrList
)
98 sal_Int16 nAttrCount
= rAttrList
.is() ? rAttrList
->getLength() : 0;
99 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
101 const OUString
& rAttrName
= rAttrList
->getNameByIndex( i
);
104 GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName
,
106 if( XML_NAMESPACE_FORM
== nPrefix
&&
107 IsXMLToken( aLocalName
, XML_PROPERTY_IS_VOID
) &&
108 IsXMLToken( rAttrList
->getValueByIndex( i
), XML_TRUE
) )
113 void XMLFormPropValueTContext_Impl::EndElement()
117 XMLMutableAttributeList
*pMutableAttrList
=
118 new XMLMutableAttributeList
;
119 Reference
< XAttributeList
> xAttrList( pMutableAttrList
);
120 pMutableAttrList
->AddAttribute( m_aAttrQName
,
123 OUString
aElemQName( GetTransformer().GetNamespaceMap().GetQNameByKey(
124 XML_NAMESPACE_FORM
, GetXMLToken(XML_LIST_VALUE
) ) );
125 GetTransformer().GetDocHandler()->startElement( aElemQName
,
127 GetTransformer().GetDocHandler()->endElement( aElemQName
);
131 void XMLFormPropValueTContext_Impl::Characters( const OUString
& rChars
)
133 m_aCharacters
+= rChars
;
136 bool XMLFormPropValueTContext_Impl::IsPersistent() const
138 return m_bPersistent
;
141 TYPEINIT1( XMLFormPropOOoTransformerContext
, XMLTransformerContext
);
143 XMLFormPropOOoTransformerContext::XMLFormPropOOoTransformerContext(
144 XMLTransformerBase
& rImp
,
145 const OUString
& rQName
) :
146 XMLTransformerContext( rImp
, rQName
),
147 m_aElemQName( rQName
),
148 m_nValueTypeAttr( -1 ),
149 m_eValueToken( XML_VALUE
),
150 m_eValueTypeToken( XML_TOKEN_END
),
155 XMLFormPropOOoTransformerContext::~XMLFormPropOOoTransformerContext()
159 XMLTransformerContext
*XMLFormPropOOoTransformerContext::CreateChildContext(
161 const OUString
& rLocalName
,
162 const OUString
& rQName
,
163 const Reference
< XAttributeList
>& )
165 XMLTransformerContext
*pContext
= 0;
167 if( XML_NAMESPACE_FORM
== nPrefix
&&
168 IsXMLToken( rLocalName
, XML_PROPERTY_VALUE
) )
172 pContext
= new XMLFormPropValueTContext_Impl( GetTransformer(),
174 XML_NAMESPACE_OFFICE
,
177 else if( !m_xValueContext
.is() )
180 new XMLFormPropValueTContext_Impl( GetTransformer(), rQName
);
181 pContext
= m_xValueContext
.get();
187 pContext
= new XMLIgnoreTransformerContext( GetTransformer(), rQName
,
192 void XMLFormPropOOoTransformerContext::StartElement(
193 const Reference
< XAttributeList
>& rAttrList
)
196 XMLTransformerActions
*pActions
=
197 GetTransformer().GetUserDefinedActions( OOO_FORM_PROP_ACTIONS
);
198 OSL_ENSURE( pActions
, "go no actions" );
200 XMLMutableAttributeList
*pMutableAttrList
=
201 new XMLMutableAttributeList( rAttrList
, true );
202 m_xAttrList
= pMutableAttrList
;
205 sal_Int16 nAttrCount
= rAttrList
.is() ? rAttrList
->getLength() : 0;
206 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
208 const OUString
& rAttrName
= rAttrList
->getNameByIndex( i
);
211 GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName
,
213 XMLTransformerActions::key_type
aKey( nPrefix
, aLocalName
);
214 XMLTransformerActions::const_iterator aIter
=
215 pActions
->find( aKey
);
216 if( !(aIter
== pActions
->end() ) )
218 const OUString
& rAttrValue
= rAttrList
->getValueByIndex( i
);
219 switch( (*aIter
).second
.m_nActionType
)
221 case XML_ATACTION_RENAME
:
222 if( IsXMLToken( aLocalName
, XML_PROPERTY_TYPE
) )
224 aValueType
= rAttrValue
;
225 m_nValueTypeAttr
= i
;
228 OUString
aNewAttrQName(
229 GetTransformer().GetNamespaceMap().GetQNameByKey(
230 (*aIter
).second
.GetQNamePrefixFromParam1(),
231 ::xmloff::token::GetXMLToken(
232 (*aIter
).second
.GetQNameTokenFromParam1()) ) );
233 pMutableAttrList
->RenameAttributeByIndex( i
, aNewAttrQName
);
236 case XML_ATACTION_REMOVE
:
237 if( IsXMLToken( aLocalName
, XML_PROPERTY_IS_LIST
) )
240 GetTransformer().GetNamespaceMap().GetQNameByKey(
241 XML_NAMESPACE_FORM
, GetXMLToken( XML_LIST_PROPERTY
) );
244 pMutableAttrList
->RemoveAttributeByIndex( i
);
249 OSL_ENSURE( false, "unknown action" );
255 if( !aValueType
.isEmpty() )
257 if( IsXMLToken( aValueType
, XML_STRING
) )
258 m_eValueToken
= XML_STRING_VALUE
;
259 else if( IsXMLToken( aValueType
, XML_BOOLEAN
) )
260 m_eValueToken
= XML_BOOLEAN_VALUE
;
261 else if( IsXMLToken( aValueType
, XML_SHORT
) ||
262 IsXMLToken( aValueType
, XML_INT
) ||
263 IsXMLToken( aValueType
, XML_LONG
) ||
264 IsXMLToken( aValueType
, XML_DOUBLE
) )
265 m_eValueTypeToken
= XML_FLOAT
;
269 void XMLFormPropOOoTransformerContext::EndElement()
271 if( m_xValueContext
.is() )
273 if( m_xValueContext
->IsVoid() )
275 m_eValueTypeToken
= XML_VOID
;
280 GetTransformer().GetNamespaceMap().GetQNameByKey(
281 XML_NAMESPACE_OFFICE
, GetXMLToken(m_eValueToken
) ) );
282 static_cast< XMLMutableAttributeList
* >( m_xAttrList
.get() )
283 ->AddAttribute( aAttrQName
, m_xValueContext
->GetTextContent() );
287 if( m_nValueTypeAttr
!= -1 && m_eValueTypeToken
!= XML_TOKEN_END
)
289 static_cast< XMLMutableAttributeList
* >( m_xAttrList
.get() )
290 ->SetValueByIndex( m_nValueTypeAttr
,
291 GetXMLToken( m_eValueTypeToken
) );
294 GetTransformer().GetDocHandler()->startElement( m_aElemQName
,
296 GetTransformer().GetDocHandler()->endElement( m_aElemQName
);
299 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */