merge the formfield patch from ooo-build
[ooovba.git] / xmloff / source / transform / FormPropOOoTContext.cxx
blobaf214f874ab4f5c0aeb02667e94e47b17095e84c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: FormPropOOoTContext.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
33 #include "MutableAttrList.hxx"
34 #include "xmlnmspe.hxx"
35 #include <xmloff/nmspmap.hxx>
36 #include "IgnoreTContext.hxx"
37 #ifndef _XMLOFF_ACTIONMAPTYPESOOo_HXX
38 #include "ActionMapTypesOOo.hxx"
39 #endif
40 #include "AttrTransformerAction.hxx"
41 #include "TransformerActions.hxx"
42 #ifndef _XMLOFF_TRANSFORMERBASE_HXX
43 #include "TransformerBase.hxx"
44 #endif
45 #include "FormPropOOoTContext.hxx"
47 using ::rtl::OUString;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::xml::sax;
50 using namespace ::xmloff::token;
52 class XMLFormPropValueTContext_Impl : public XMLTransformerContext
54 OUString m_aAttrQName;
55 ::rtl::OUString m_aCharacters;
56 sal_Bool m_bPersistent;
57 sal_Bool m_bIsVoid;
59 public:
60 TYPEINFO();
62 // element content persistence only
63 XMLFormPropValueTContext_Impl( XMLTransformerBase& rTransformer,
64 const OUString& rQName );
65 XMLFormPropValueTContext_Impl( XMLTransformerBase& rTransformer,
66 const OUString& rQName,
67 sal_uInt16 nAttrPrefix,
68 XMLTokenEnum eAttrToken );
70 virtual ~XMLFormPropValueTContext_Impl();
72 virtual void StartElement( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList );
74 virtual void EndElement();
76 virtual void Characters( const ::rtl::OUString& rChars );
78 virtual sal_Bool IsPersistent() const;
80 sal_Bool IsVoid() const { return m_bIsVoid; }
81 const ::rtl::OUString& GetTextContent() const { return m_aCharacters; }
84 TYPEINIT1( XMLFormPropValueTContext_Impl, XMLTransformerContext );
86 XMLFormPropValueTContext_Impl::XMLFormPropValueTContext_Impl(
87 XMLTransformerBase& rTransformer,
88 const ::rtl::OUString& rQName ) :
89 XMLTransformerContext( rTransformer, rQName ),
90 m_bPersistent( sal_True ),
91 m_bIsVoid( sal_False )
95 XMLFormPropValueTContext_Impl::XMLFormPropValueTContext_Impl(
96 XMLTransformerBase& rTransformer,
97 const ::rtl::OUString& rQName,
98 sal_uInt16 nAttrPrefix,
99 XMLTokenEnum eAttrToken ) :
100 XMLTransformerContext( rTransformer, rQName ),
101 m_aAttrQName( rTransformer.GetNamespaceMap().GetQNameByKey(
102 nAttrPrefix, GetXMLToken(eAttrToken) ) ),
103 m_bPersistent( sal_True ),
104 m_bIsVoid( sal_False )
108 XMLFormPropValueTContext_Impl::~XMLFormPropValueTContext_Impl()
112 void XMLFormPropValueTContext_Impl::StartElement(
113 const Reference< XAttributeList >& rAttrList )
115 sal_Int16 nAttrCount = rAttrList.is() ? rAttrList->getLength() : 0;
116 for( sal_Int16 i=0; i < nAttrCount; i++ )
118 const OUString& rAttrName = rAttrList->getNameByIndex( i );
119 OUString aLocalName;
120 sal_uInt16 nPrefix =
121 GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
122 &aLocalName );
123 if( XML_NAMESPACE_FORM == nPrefix &&
124 IsXMLToken( aLocalName, XML_PROPERTY_IS_VOID ) &&
125 IsXMLToken( rAttrList->getValueByIndex( i ), XML_TRUE ) )
126 m_bIsVoid = sal_True;
130 void XMLFormPropValueTContext_Impl::EndElement()
132 if( !m_bPersistent )
134 XMLMutableAttributeList *pMutableAttrList =
135 new XMLMutableAttributeList;
136 Reference< XAttributeList > xAttrList( pMutableAttrList );
137 pMutableAttrList->AddAttribute( m_aAttrQName,
138 m_aCharacters );
140 OUString aElemQName( GetTransformer().GetNamespaceMap().GetQNameByKey(
141 XML_NAMESPACE_FORM, GetXMLToken(XML_LIST_VALUE) ) );
142 GetTransformer().GetDocHandler()->startElement( aElemQName,
143 xAttrList );
144 GetTransformer().GetDocHandler()->endElement( aElemQName );
148 void XMLFormPropValueTContext_Impl::Characters( const OUString& rChars )
150 m_aCharacters += rChars;
154 sal_Bool XMLFormPropValueTContext_Impl::IsPersistent() const
156 return m_bPersistent;
159 //------------------------------------------------------------------------------
161 TYPEINIT1( XMLFormPropOOoTransformerContext, XMLTransformerContext );
163 XMLFormPropOOoTransformerContext::XMLFormPropOOoTransformerContext(
164 XMLTransformerBase& rImp,
165 const OUString& rQName ) :
166 XMLTransformerContext( rImp, rQName ),
167 m_aElemQName( rQName ),
168 m_nValueTypeAttr( -1 ),
169 m_eValueToken( XML_VALUE ),
170 m_eValueTypeToken( XML_TOKEN_END ),
171 m_bIsList( sal_False )
175 XMLFormPropOOoTransformerContext::~XMLFormPropOOoTransformerContext()
179 XMLTransformerContext *XMLFormPropOOoTransformerContext::CreateChildContext(
180 sal_uInt16 nPrefix,
181 const OUString& rLocalName,
182 const OUString& rQName,
183 const Reference< XAttributeList >& )
185 XMLTransformerContext *pContext = 0;
187 if( XML_NAMESPACE_FORM == nPrefix &&
188 IsXMLToken( rLocalName, XML_PROPERTY_VALUE ) )
190 if( m_bIsList )
192 pContext = new XMLFormPropValueTContext_Impl( GetTransformer(),
193 rQName,
194 XML_NAMESPACE_OFFICE,
195 m_eValueToken );
197 else if( !m_xValueContext.is() )
199 m_xValueContext=
200 new XMLFormPropValueTContext_Impl( GetTransformer(), rQName );
201 pContext = m_xValueContext.get();
205 // default is ignore
206 if( !pContext )
207 pContext = new XMLIgnoreTransformerContext( GetTransformer(), rQName,
208 sal_True, sal_True );
209 return pContext;
212 void XMLFormPropOOoTransformerContext::StartElement(
213 const Reference< XAttributeList >& rAttrList )
216 XMLTransformerActions *pActions =
217 GetTransformer().GetUserDefinedActions( OOO_FORM_PROP_ACTIONS );
218 OSL_ENSURE( pActions, "go no actions" );
220 XMLMutableAttributeList *pMutableAttrList =
221 new XMLMutableAttributeList( rAttrList, sal_True );
222 m_xAttrList = pMutableAttrList;
224 OUString aValueType;
225 sal_Int16 nAttrCount = rAttrList.is() ? rAttrList->getLength() : 0;
226 for( sal_Int16 i=0; i < nAttrCount; i++ )
228 const OUString& rAttrName = rAttrList->getNameByIndex( i );
229 OUString aLocalName;
230 sal_uInt16 nPrefix =
231 GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
232 &aLocalName );
233 XMLTransformerActions::key_type aKey( nPrefix, aLocalName );
234 XMLTransformerActions::const_iterator aIter =
235 pActions->find( aKey );
236 if( !(aIter == pActions->end() ) )
238 const OUString& rAttrValue = rAttrList->getValueByIndex( i );
239 switch( (*aIter).second.m_nActionType )
241 case XML_ATACTION_RENAME:
242 if( IsXMLToken( aLocalName, XML_PROPERTY_TYPE ) )
244 aValueType = rAttrValue;
245 m_nValueTypeAttr = i;
248 OUString aNewAttrQName(
249 GetTransformer().GetNamespaceMap().GetQNameByKey(
250 (*aIter).second.GetQNamePrefixFromParam1(),
251 ::xmloff::token::GetXMLToken(
252 (*aIter).second.GetQNameTokenFromParam1()) ) );
253 pMutableAttrList->RenameAttributeByIndex( i, aNewAttrQName );
255 break;
256 case XML_ATACTION_REMOVE:
257 if( IsXMLToken( aLocalName, XML_PROPERTY_IS_LIST ) )
259 m_aElemQName =
260 GetTransformer().GetNamespaceMap().GetQNameByKey(
261 XML_NAMESPACE_FORM, GetXMLToken( XML_LIST_PROPERTY ) );
262 m_bIsList = sal_True;
264 pMutableAttrList->RemoveAttributeByIndex( i );
265 --i;
266 --nAttrCount;
267 break;
268 default:
269 OSL_ENSURE( !this, "unknown action" );
270 break;
275 if( aValueType.getLength() )
277 if( IsXMLToken( aValueType, XML_STRING ) )
278 m_eValueToken = XML_STRING_VALUE;
279 else if( IsXMLToken( aValueType, XML_BOOLEAN ) )
280 m_eValueToken = XML_BOOLEAN_VALUE;
281 else if( IsXMLToken( aValueType, XML_SHORT ) ||
282 IsXMLToken( aValueType, XML_INT ) ||
283 IsXMLToken( aValueType, XML_LONG ) ||
284 IsXMLToken( aValueType, XML_DOUBLE ) )
285 m_eValueTypeToken = XML_FLOAT;
289 void XMLFormPropOOoTransformerContext::EndElement()
291 if( m_xValueContext.is() )
293 if( m_xValueContext->IsVoid() )
295 m_eValueTypeToken = XML_VOID;
297 else
299 OUString aAttrQName(
300 GetTransformer().GetNamespaceMap().GetQNameByKey(
301 XML_NAMESPACE_OFFICE, GetXMLToken(m_eValueToken) ) );
302 static_cast< XMLMutableAttributeList * >( m_xAttrList.get() )
303 ->AddAttribute( aAttrQName, m_xValueContext->GetTextContent() );
307 if( m_nValueTypeAttr != -1 && m_eValueTypeToken != XML_TOKEN_END )
309 static_cast< XMLMutableAttributeList * >( m_xAttrList.get() )
310 ->SetValueByIndex( m_nValueTypeAttr ,
311 GetXMLToken( m_eValueTypeToken ) );
314 GetTransformer().GetDocHandler()->startElement( m_aElemQName,
315 m_xAttrList );
316 GetTransformer().GetDocHandler()->endElement( m_aElemQName );