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