xref: /trunk/main/xmloff/source/transform/ControlOASISTContext.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 #include "MutableAttrList.hxx"
31 #include "xmloff/xmlnmspe.hxx"
32 #include <xmloff/nmspmap.hxx>
33 #include "ActionMapTypesOASIS.hxx"
34 #include "AttrTransformerAction.hxx"
35 #include "TransformerActions.hxx"
36 #ifndef _XMLOFF_TRANSFORMERBASE_HXX
37 #include "TransformerBase.hxx"
38 #endif
39 #include "ControlOASISTContext.hxx"
40 
41 using ::rtl::OUString;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::xml::sax;
44 using namespace ::xmloff::token;
45 
46 TYPEINIT1( XMLControlOASISTransformerContext, XMLTransformerContext );
47 
48 XMLControlOASISTransformerContext::XMLControlOASISTransformerContext(
49         XMLTransformerBase& rImp,
50         const OUString& rQName,
51         sal_Bool bCreateControl ) :
52     XMLTransformerContext( rImp, rQName ),
53     m_aElemQName( rImp.GetNamespaceMap().GetQNameByKey( XML_NAMESPACE_FORM,
54                             ::xmloff::token::GetXMLToken( XML_CONTROL ) ) ),
55     m_bCreateControl( bCreateControl )
56 {
57 }
58 
59 XMLControlOASISTransformerContext::~XMLControlOASISTransformerContext()
60 {
61 }
62 
63 void XMLControlOASISTransformerContext::StartElement(
64     const Reference< XAttributeList >& rAttrList )
65 {
66 
67     XMLTransformerActions *pActions =
68         GetTransformer().GetUserDefinedActions( OASIS_FORM_CONTROL_ACTIONS );
69     OSL_ENSURE( pActions, "go no actions" );
70 
71     Reference< XAttributeList > xAttrList( rAttrList );
72     XMLMutableAttributeList *pMutableAttrList = 0;
73 //      GetTransformer().ProcessAttrList( xAttrList, OOO_SHAPE_ACTIONS,
74 //                                        sal_True );
75 
76     XMLMutableAttributeList *pControlMutableAttrList =
77         m_bCreateControl ? new XMLMutableAttributeList : 0;
78     Reference< XAttributeList > xControlAttrList( pControlMutableAttrList );
79 
80     sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
81     for( sal_Int16 i=0; i < nAttrCount; i++ )
82     {
83         const OUString& rAttrName = xAttrList->getNameByIndex( i );
84         OUString aLocalName;
85         sal_uInt16 nPrefix =
86             GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
87                                                                  &aLocalName );
88         XMLTransformerActions::key_type aKey( nPrefix, aLocalName );
89         XMLTransformerActions::const_iterator aIter =
90             pActions->find( aKey );
91         if( !(aIter == pActions->end() ) )
92         {
93             if( !pMutableAttrList )
94             {
95                 pMutableAttrList =
96                     new XMLMutableAttributeList( rAttrList );
97                 xAttrList = pMutableAttrList;
98             }
99             const OUString& rAttrValue = xAttrList->getValueByIndex( i );
100             switch( (*aIter).second.m_nActionType )
101             {
102             case XML_ATACTION_MOVE_TO_ELEM:
103                 if( m_bCreateControl )
104                 {
105                     pControlMutableAttrList->AddAttribute( rAttrName,
106                                                            rAttrValue );
107                     pMutableAttrList->RemoveAttributeByIndex( i );
108                     --i;
109                     --nAttrCount;
110                 }
111                 break;
112             case XML_ATACTION_RENAME_REMOVE_NAMESPACE_PREFIX:
113                 {
114                     OUString aAttrValue( rAttrValue );
115                     sal_uInt16 nValPrefix =
116                         static_cast<sal_uInt16>( (*aIter).second.m_nParam2 );
117                     GetTransformer().RemoveNamespacePrefix( aAttrValue,
118                                                             nValPrefix );
119                     OUString aNewAttrQName(
120                         GetTransformer().GetNamespaceMap().GetQNameByKey(
121                             (*aIter).second.GetQNamePrefixFromParam1(),
122                             ::xmloff::token::GetXMLToken(
123                                 (*aIter).second.GetQNameTokenFromParam1()) ) );
124                     if( m_bCreateControl )
125                     {
126                         pControlMutableAttrList->AddAttribute( aNewAttrQName,
127                                                                aAttrValue );
128                         pMutableAttrList->RemoveAttributeByIndex( i );
129                         --i;
130                         --nAttrCount;
131                     }
132                     else
133                     {
134                         pMutableAttrList->RenameAttributeByIndex( i,
135                                                               aNewAttrQName );
136                         pMutableAttrList->SetValueByIndex( i, aAttrValue );
137                     }
138                 }
139             case XML_ATACTION_URI_OASIS:
140                 {
141                     OUString aAttrValue( rAttrValue );
142                     if( GetTransformer().ConvertURIToOOo( aAttrValue,
143                            static_cast< sal_Bool >((*aIter).second.m_nParam1)) )
144                         pMutableAttrList->SetValueByIndex( i, aAttrValue );
145                 }
146                 break;
147             default:
148                 OSL_ENSURE( !this, "unknown action" );
149                 break;
150             }
151         }
152     }
153 
154     if( m_bCreateControl )
155         GetTransformer().GetDocHandler()->startElement( m_aElemQName,
156                                                         xControlAttrList );
157     XMLTransformerContext::StartElement( xAttrList );
158 }
159 
160 void XMLControlOASISTransformerContext::EndElement()
161 {
162     XMLTransformerContext::EndElement();
163     if( m_bCreateControl )
164         GetTransformer().GetDocHandler()->endElement( m_aElemQName );
165 }
166