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 "ActionMapTypesOASIS.hxx"
30 #include "AttrTransformerAction.hxx"
31 #include "TransformerActions.hxx"
32 #ifndef _XMLOFF_TRANSFORMERBASE_HXX
33 #include "TransformerBase.hxx"
34 #endif
35 #include "ControlOASISTContext.hxx"
36 
37 using ::rtl::OUString;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::xml::sax;
40 using namespace ::xmloff::token;
41 
42 TYPEINIT1( XMLControlOASISTransformerContext, XMLTransformerContext );
43 
XMLControlOASISTransformerContext(XMLTransformerBase & rImp,const OUString & rQName,sal_Bool bCreateControl)44 XMLControlOASISTransformerContext::XMLControlOASISTransformerContext(
45 		XMLTransformerBase& rImp,
46 		const OUString& rQName,
47 	    sal_Bool bCreateControl	) :
48 	XMLTransformerContext( rImp, rQName ),
49 	m_aElemQName( rImp.GetNamespaceMap().GetQNameByKey( XML_NAMESPACE_FORM,
50 							::xmloff::token::GetXMLToken( XML_CONTROL ) ) ),
51 	m_bCreateControl( bCreateControl )
52 {
53 }
54 
~XMLControlOASISTransformerContext()55 XMLControlOASISTransformerContext::~XMLControlOASISTransformerContext()
56 {
57 }
58 
StartElement(const Reference<XAttributeList> & rAttrList)59 void XMLControlOASISTransformerContext::StartElement(
60 	const Reference< XAttributeList >& rAttrList )
61 {
62 
63 	XMLTransformerActions *pActions =
64 		GetTransformer().GetUserDefinedActions( OASIS_FORM_CONTROL_ACTIONS );
65 	OSL_ENSURE( pActions, "go no actions" );
66 
67 	Reference< XAttributeList > xAttrList( rAttrList );
68 	XMLMutableAttributeList *pMutableAttrList = 0;
69 //		GetTransformer().ProcessAttrList( xAttrList, OOO_SHAPE_ACTIONS,
70 //										  sal_True );
71 
72 	XMLMutableAttributeList *pControlMutableAttrList =
73 		m_bCreateControl ? new XMLMutableAttributeList : 0;
74 	Reference< XAttributeList > xControlAttrList( pControlMutableAttrList );
75 
76 	sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
77 	for( sal_Int16 i=0; i < nAttrCount; i++ )
78 	{
79 		const OUString& rAttrName = xAttrList->getNameByIndex( i );
80 		OUString aLocalName;
81 		sal_uInt16 nPrefix =
82 			GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
83 																 &aLocalName );
84 		XMLTransformerActions::key_type aKey( nPrefix, aLocalName );
85 		XMLTransformerActions::const_iterator aIter =
86 			pActions->find( aKey );
87 		if( !(aIter == pActions->end() ) )
88 		{
89 			if( !pMutableAttrList )
90 			{
91 				pMutableAttrList =
92 					new XMLMutableAttributeList( rAttrList );
93 				xAttrList = pMutableAttrList;
94 			}
95 			const OUString& rAttrValue = xAttrList->getValueByIndex( i );
96 			switch( (*aIter).second.m_nActionType )
97 			{
98 			case XML_ATACTION_MOVE_TO_ELEM:
99 				if( m_bCreateControl )
100 				{
101 					pControlMutableAttrList->AddAttribute( rAttrName,
102 														   rAttrValue );
103 					pMutableAttrList->RemoveAttributeByIndex( i );
104 					--i;
105 					--nAttrCount;
106 				}
107 				break;
108 			case XML_ATACTION_RENAME_REMOVE_NAMESPACE_PREFIX:
109 				{
110 					OUString aAttrValue( rAttrValue );
111 					sal_uInt16 nValPrefix =
112 						static_cast<sal_uInt16>( (*aIter).second.m_nParam2 );
113 					GetTransformer().RemoveNamespacePrefix( aAttrValue,
114 															nValPrefix );
115 					OUString aNewAttrQName(
116 						GetTransformer().GetNamespaceMap().GetQNameByKey(
117 							(*aIter).second.GetQNamePrefixFromParam1(),
118 							::xmloff::token::GetXMLToken(
119 								(*aIter).second.GetQNameTokenFromParam1()) ) );
120 					if( m_bCreateControl )
121 					{
122 						pControlMutableAttrList->AddAttribute( aNewAttrQName,
123 															   aAttrValue );
124 						pMutableAttrList->RemoveAttributeByIndex( i );
125 						--i;
126 						--nAttrCount;
127 					}
128 					else
129 					{
130 						pMutableAttrList->RenameAttributeByIndex( i,
131 															  aNewAttrQName );
132 						pMutableAttrList->SetValueByIndex( i, aAttrValue );
133 					}
134 				}
135 			case XML_ATACTION_URI_OASIS:
136 				{
137 					OUString aAttrValue( rAttrValue );
138 					if( GetTransformer().ConvertURIToOOo( aAttrValue,
139 						   static_cast< sal_Bool >((*aIter).second.m_nParam1)) )
140 						pMutableAttrList->SetValueByIndex( i, aAttrValue );
141 				}
142 				break;
143 			default:
144 				OSL_ENSURE( !this, "unknown action" );
145 				break;
146 			}
147 		}
148 	}
149 
150 	if( m_bCreateControl )
151 		GetTransformer().GetDocHandler()->startElement( m_aElemQName,
152 														xControlAttrList );
153 	XMLTransformerContext::StartElement( xAttrList );
154 }
155 
EndElement()156 void XMLControlOASISTransformerContext::EndElement()
157 {
158 	XMLTransformerContext::EndElement();
159 	if( m_bCreateControl )
160 		GetTransformer().GetDocHandler()->endElement( m_aElemQName );
161 }
162