1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
30*cdf0e10cSrcweir #include "MutableAttrList.hxx"
31*cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
32*cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
33*cdf0e10cSrcweir #include "IgnoreTContext.hxx"
34*cdf0e10cSrcweir #ifndef _XMLOFF_ACTIONMAPTYPESOOo_HXX
35*cdf0e10cSrcweir #include "ActionMapTypesOOo.hxx"
36*cdf0e10cSrcweir #endif
37*cdf0e10cSrcweir #include "AttrTransformerAction.hxx"
38*cdf0e10cSrcweir #include "TransformerActions.hxx"
39*cdf0e10cSrcweir #ifndef _XMLOFF_TRANSFORMERBASE_HXX
40*cdf0e10cSrcweir #include "TransformerBase.hxx"
41*cdf0e10cSrcweir #endif
42*cdf0e10cSrcweir #include "FormPropOOoTContext.hxx"
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir using ::rtl::OUString;
45*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
46*cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax;
47*cdf0e10cSrcweir using namespace ::xmloff::token;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir class XMLFormPropValueTContext_Impl : public XMLTransformerContext
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir 	OUString m_aAttrQName;
52*cdf0e10cSrcweir 	::rtl::OUString m_aCharacters;
53*cdf0e10cSrcweir 	sal_Bool m_bPersistent;
54*cdf0e10cSrcweir 	sal_Bool m_bIsVoid;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir public:
57*cdf0e10cSrcweir 	TYPEINFO();
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 	// element content persistence only
60*cdf0e10cSrcweir 	XMLFormPropValueTContext_Impl( XMLTransformerBase& rTransformer,
61*cdf0e10cSrcweir 						   const OUString& rQName );
62*cdf0e10cSrcweir 	XMLFormPropValueTContext_Impl( XMLTransformerBase& rTransformer,
63*cdf0e10cSrcweir 						   const OUString& rQName,
64*cdf0e10cSrcweir 		   				   sal_uInt16 nAttrPrefix,
65*cdf0e10cSrcweir 						   XMLTokenEnum eAttrToken );
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir 	virtual ~XMLFormPropValueTContext_Impl();
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 	virtual void StartElement( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList );
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 	virtual void EndElement();
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir 	virtual void Characters( const ::rtl::OUString& rChars );
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 	virtual sal_Bool IsPersistent() const;
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 	sal_Bool IsVoid() const { return m_bIsVoid; }
78*cdf0e10cSrcweir 	const ::rtl::OUString& GetTextContent() const { return m_aCharacters; }
79*cdf0e10cSrcweir };
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir TYPEINIT1( XMLFormPropValueTContext_Impl, XMLTransformerContext );
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir XMLFormPropValueTContext_Impl::XMLFormPropValueTContext_Impl(
84*cdf0e10cSrcweir 		XMLTransformerBase& rTransformer,
85*cdf0e10cSrcweir 		const ::rtl::OUString& rQName ) :
86*cdf0e10cSrcweir 	XMLTransformerContext( rTransformer, rQName ),
87*cdf0e10cSrcweir 	m_bPersistent( sal_True ),
88*cdf0e10cSrcweir 	m_bIsVoid( sal_False )
89*cdf0e10cSrcweir {
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir XMLFormPropValueTContext_Impl::XMLFormPropValueTContext_Impl(
93*cdf0e10cSrcweir 		XMLTransformerBase& rTransformer,
94*cdf0e10cSrcweir 		const ::rtl::OUString& rQName,
95*cdf0e10cSrcweir 	    sal_uInt16 nAttrPrefix,
96*cdf0e10cSrcweir 	    XMLTokenEnum eAttrToken ) :
97*cdf0e10cSrcweir 	XMLTransformerContext( rTransformer, rQName ),
98*cdf0e10cSrcweir 	m_aAttrQName( rTransformer.GetNamespaceMap().GetQNameByKey(
99*cdf0e10cSrcweir 					nAttrPrefix, GetXMLToken(eAttrToken) ) ),
100*cdf0e10cSrcweir 	m_bPersistent( sal_True ),
101*cdf0e10cSrcweir 	m_bIsVoid( sal_False )
102*cdf0e10cSrcweir {
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir XMLFormPropValueTContext_Impl::~XMLFormPropValueTContext_Impl()
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir void XMLFormPropValueTContext_Impl::StartElement(
110*cdf0e10cSrcweir 	const Reference< XAttributeList >& rAttrList )
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir 	sal_Int16 nAttrCount = rAttrList.is() ? rAttrList->getLength() : 0;
113*cdf0e10cSrcweir 	for( sal_Int16 i=0; i < nAttrCount; i++ )
114*cdf0e10cSrcweir 	{
115*cdf0e10cSrcweir 		const OUString& rAttrName = rAttrList->getNameByIndex( i );
116*cdf0e10cSrcweir 		OUString aLocalName;
117*cdf0e10cSrcweir 		sal_uInt16 nPrefix =
118*cdf0e10cSrcweir 			GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
119*cdf0e10cSrcweir 																 &aLocalName );
120*cdf0e10cSrcweir 		if( XML_NAMESPACE_FORM == nPrefix &&
121*cdf0e10cSrcweir 			IsXMLToken( aLocalName, XML_PROPERTY_IS_VOID ) &&
122*cdf0e10cSrcweir 		 	IsXMLToken( rAttrList->getValueByIndex( i ), XML_TRUE ) )
123*cdf0e10cSrcweir 			m_bIsVoid = sal_True;
124*cdf0e10cSrcweir 	}
125*cdf0e10cSrcweir }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir void XMLFormPropValueTContext_Impl::EndElement()
128*cdf0e10cSrcweir {
129*cdf0e10cSrcweir 	if( !m_bPersistent )
130*cdf0e10cSrcweir 	{
131*cdf0e10cSrcweir 		XMLMutableAttributeList *pMutableAttrList =
132*cdf0e10cSrcweir 			new XMLMutableAttributeList;
133*cdf0e10cSrcweir 		Reference< XAttributeList > xAttrList( pMutableAttrList );
134*cdf0e10cSrcweir 		pMutableAttrList->AddAttribute( m_aAttrQName,
135*cdf0e10cSrcweir 										m_aCharacters );
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 		OUString aElemQName( GetTransformer().GetNamespaceMap().GetQNameByKey(
138*cdf0e10cSrcweir 					XML_NAMESPACE_FORM, GetXMLToken(XML_LIST_VALUE) ) );
139*cdf0e10cSrcweir 		GetTransformer().GetDocHandler()->startElement( aElemQName,
140*cdf0e10cSrcweir 													xAttrList );
141*cdf0e10cSrcweir 		GetTransformer().GetDocHandler()->endElement( aElemQName );
142*cdf0e10cSrcweir 	}
143*cdf0e10cSrcweir }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir void XMLFormPropValueTContext_Impl::Characters( const OUString& rChars )
146*cdf0e10cSrcweir {
147*cdf0e10cSrcweir 	m_aCharacters += rChars;
148*cdf0e10cSrcweir }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir sal_Bool XMLFormPropValueTContext_Impl::IsPersistent() const
152*cdf0e10cSrcweir {
153*cdf0e10cSrcweir 	return m_bPersistent;
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir //------------------------------------------------------------------------------
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir TYPEINIT1( XMLFormPropOOoTransformerContext, XMLTransformerContext );
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir XMLFormPropOOoTransformerContext::XMLFormPropOOoTransformerContext(
161*cdf0e10cSrcweir 		XMLTransformerBase& rImp,
162*cdf0e10cSrcweir 		const OUString& rQName ) :
163*cdf0e10cSrcweir 	XMLTransformerContext( rImp, rQName ),
164*cdf0e10cSrcweir 	m_aElemQName( rQName ),
165*cdf0e10cSrcweir 	m_nValueTypeAttr( -1 ),
166*cdf0e10cSrcweir 	m_eValueToken( XML_VALUE ),
167*cdf0e10cSrcweir 	m_eValueTypeToken( XML_TOKEN_END ),
168*cdf0e10cSrcweir 	m_bIsList( sal_False )
169*cdf0e10cSrcweir {
170*cdf0e10cSrcweir }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir XMLFormPropOOoTransformerContext::~XMLFormPropOOoTransformerContext()
173*cdf0e10cSrcweir {
174*cdf0e10cSrcweir }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir XMLTransformerContext *XMLFormPropOOoTransformerContext::CreateChildContext(
177*cdf0e10cSrcweir 		sal_uInt16 nPrefix,
178*cdf0e10cSrcweir 		const OUString& rLocalName,
179*cdf0e10cSrcweir 		const OUString& rQName,
180*cdf0e10cSrcweir 		const Reference< XAttributeList >& )
181*cdf0e10cSrcweir {
182*cdf0e10cSrcweir 	XMLTransformerContext *pContext = 0;
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 	if( XML_NAMESPACE_FORM == nPrefix &&
185*cdf0e10cSrcweir 		IsXMLToken( rLocalName, XML_PROPERTY_VALUE ) )
186*cdf0e10cSrcweir 	{
187*cdf0e10cSrcweir 		if( m_bIsList )
188*cdf0e10cSrcweir 		{
189*cdf0e10cSrcweir 			pContext = new XMLFormPropValueTContext_Impl( GetTransformer(),
190*cdf0e10cSrcweir 														  rQName,
191*cdf0e10cSrcweir 														  XML_NAMESPACE_OFFICE,
192*cdf0e10cSrcweir 														  m_eValueToken );
193*cdf0e10cSrcweir 		}
194*cdf0e10cSrcweir 		else if( !m_xValueContext.is() )
195*cdf0e10cSrcweir 		{
196*cdf0e10cSrcweir 			m_xValueContext=
197*cdf0e10cSrcweir 				new XMLFormPropValueTContext_Impl( GetTransformer(), rQName );
198*cdf0e10cSrcweir 			pContext = m_xValueContext.get();
199*cdf0e10cSrcweir 		}
200*cdf0e10cSrcweir 	}
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 	// default is ignore
203*cdf0e10cSrcweir 	if( !pContext )
204*cdf0e10cSrcweir 		pContext = new XMLIgnoreTransformerContext( GetTransformer(), rQName,
205*cdf0e10cSrcweir 			   							  sal_True, sal_True );
206*cdf0e10cSrcweir 	return pContext;
207*cdf0e10cSrcweir }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir void XMLFormPropOOoTransformerContext::StartElement(
210*cdf0e10cSrcweir 	const Reference< XAttributeList >& rAttrList )
211*cdf0e10cSrcweir {
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir 	XMLTransformerActions *pActions =
214*cdf0e10cSrcweir 		GetTransformer().GetUserDefinedActions( OOO_FORM_PROP_ACTIONS );
215*cdf0e10cSrcweir 	OSL_ENSURE( pActions, "go no actions" );
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 	XMLMutableAttributeList *pMutableAttrList =
218*cdf0e10cSrcweir 		new XMLMutableAttributeList( rAttrList, sal_True );
219*cdf0e10cSrcweir 	m_xAttrList = pMutableAttrList;
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir 	OUString aValueType;
222*cdf0e10cSrcweir 	sal_Int16 nAttrCount = rAttrList.is() ? rAttrList->getLength() : 0;
223*cdf0e10cSrcweir 	for( sal_Int16 i=0; i < nAttrCount; i++ )
224*cdf0e10cSrcweir 	{
225*cdf0e10cSrcweir 		const OUString& rAttrName = rAttrList->getNameByIndex( i );
226*cdf0e10cSrcweir 		OUString aLocalName;
227*cdf0e10cSrcweir 		sal_uInt16 nPrefix =
228*cdf0e10cSrcweir 			GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
229*cdf0e10cSrcweir 																 &aLocalName );
230*cdf0e10cSrcweir 		XMLTransformerActions::key_type aKey( nPrefix, aLocalName );
231*cdf0e10cSrcweir 		XMLTransformerActions::const_iterator aIter =
232*cdf0e10cSrcweir 			pActions->find( aKey );
233*cdf0e10cSrcweir 		if( !(aIter == pActions->end() ) )
234*cdf0e10cSrcweir 		{
235*cdf0e10cSrcweir 			const OUString& rAttrValue = rAttrList->getValueByIndex( i );
236*cdf0e10cSrcweir 			switch( (*aIter).second.m_nActionType )
237*cdf0e10cSrcweir 			{
238*cdf0e10cSrcweir 			case XML_ATACTION_RENAME:
239*cdf0e10cSrcweir 				if( IsXMLToken( aLocalName, XML_PROPERTY_TYPE ) )
240*cdf0e10cSrcweir 				{
241*cdf0e10cSrcweir 					aValueType = rAttrValue;
242*cdf0e10cSrcweir 					m_nValueTypeAttr = i;
243*cdf0e10cSrcweir 				}
244*cdf0e10cSrcweir 				{
245*cdf0e10cSrcweir 					OUString aNewAttrQName(
246*cdf0e10cSrcweir 						GetTransformer().GetNamespaceMap().GetQNameByKey(
247*cdf0e10cSrcweir 								(*aIter).second.GetQNamePrefixFromParam1(),
248*cdf0e10cSrcweir 								::xmloff::token::GetXMLToken(
249*cdf0e10cSrcweir 									(*aIter).second.GetQNameTokenFromParam1()) ) );
250*cdf0e10cSrcweir 					pMutableAttrList->RenameAttributeByIndex( i, aNewAttrQName );
251*cdf0e10cSrcweir 				}
252*cdf0e10cSrcweir 				break;
253*cdf0e10cSrcweir 			case XML_ATACTION_REMOVE:
254*cdf0e10cSrcweir 				if( IsXMLToken( aLocalName, XML_PROPERTY_IS_LIST ) )
255*cdf0e10cSrcweir 				{
256*cdf0e10cSrcweir 					m_aElemQName =
257*cdf0e10cSrcweir 						GetTransformer().GetNamespaceMap().GetQNameByKey(
258*cdf0e10cSrcweir 						XML_NAMESPACE_FORM, GetXMLToken( XML_LIST_PROPERTY ) );
259*cdf0e10cSrcweir 					m_bIsList = sal_True;
260*cdf0e10cSrcweir 				}
261*cdf0e10cSrcweir 				pMutableAttrList->RemoveAttributeByIndex( i );
262*cdf0e10cSrcweir 				--i;
263*cdf0e10cSrcweir 				--nAttrCount;
264*cdf0e10cSrcweir 				break;
265*cdf0e10cSrcweir 			default:
266*cdf0e10cSrcweir 				OSL_ENSURE( !this, "unknown action" );
267*cdf0e10cSrcweir 				break;
268*cdf0e10cSrcweir 			}
269*cdf0e10cSrcweir 		}
270*cdf0e10cSrcweir 	}
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir 	if( aValueType.getLength() )
273*cdf0e10cSrcweir 	{
274*cdf0e10cSrcweir 		if( IsXMLToken( aValueType, XML_STRING ) )
275*cdf0e10cSrcweir 			m_eValueToken = XML_STRING_VALUE;
276*cdf0e10cSrcweir 		else if( IsXMLToken( aValueType, XML_BOOLEAN ) )
277*cdf0e10cSrcweir 			m_eValueToken = XML_BOOLEAN_VALUE;
278*cdf0e10cSrcweir 		else if( IsXMLToken( aValueType, XML_SHORT ) ||
279*cdf0e10cSrcweir 				 IsXMLToken( aValueType, XML_INT ) ||
280*cdf0e10cSrcweir 				 IsXMLToken( aValueType, XML_LONG ) ||
281*cdf0e10cSrcweir 				 IsXMLToken( aValueType, XML_DOUBLE ) )
282*cdf0e10cSrcweir 			m_eValueTypeToken = XML_FLOAT;
283*cdf0e10cSrcweir 	}
284*cdf0e10cSrcweir }
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir void XMLFormPropOOoTransformerContext::EndElement()
287*cdf0e10cSrcweir {
288*cdf0e10cSrcweir 	if( m_xValueContext.is() )
289*cdf0e10cSrcweir 	{
290*cdf0e10cSrcweir 		if( m_xValueContext->IsVoid() )
291*cdf0e10cSrcweir 		{
292*cdf0e10cSrcweir 			m_eValueTypeToken = XML_VOID;
293*cdf0e10cSrcweir 		}
294*cdf0e10cSrcweir 		else
295*cdf0e10cSrcweir 		{
296*cdf0e10cSrcweir 			OUString aAttrQName(
297*cdf0e10cSrcweir 					GetTransformer().GetNamespaceMap().GetQNameByKey(
298*cdf0e10cSrcweir 					XML_NAMESPACE_OFFICE, GetXMLToken(m_eValueToken) ) );
299*cdf0e10cSrcweir 			static_cast< XMLMutableAttributeList * >( m_xAttrList.get() )
300*cdf0e10cSrcweir 				->AddAttribute( aAttrQName, m_xValueContext->GetTextContent() );
301*cdf0e10cSrcweir 		}
302*cdf0e10cSrcweir 	}
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir 	if( m_nValueTypeAttr != -1 && m_eValueTypeToken != XML_TOKEN_END )
305*cdf0e10cSrcweir 	{
306*cdf0e10cSrcweir 		static_cast< XMLMutableAttributeList * >( m_xAttrList.get() )
307*cdf0e10cSrcweir 			->SetValueByIndex( m_nValueTypeAttr ,
308*cdf0e10cSrcweir 								GetXMLToken( m_eValueTypeToken ) );
309*cdf0e10cSrcweir 	}
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir 	GetTransformer().GetDocHandler()->startElement( m_aElemQName,
312*cdf0e10cSrcweir 													m_xAttrList );
313*cdf0e10cSrcweir 	GetTransformer().GetDocHandler()->endElement( m_aElemQName );
314*cdf0e10cSrcweir }
315