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 "CreateElemTContext.hxx"
27 #include "MutableAttrList.hxx"
28 #ifndef _XMLOFF_TRANSFORMERBASE_HXX
29 #include "TransformerBase.hxx"
30 #endif
31 #include "TransformerActions.hxx"
32 #include "TContextVector.hxx"
33 #include "FlatTContext.hxx"
34 #include "AttrTransformerAction.hxx"
35 #include <xmloff/nmspmap.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( XMLCreateElemTransformerContext, XMLTransformerContext );
43 
XMLCreateElemTransformerContext(XMLTransformerBase & rImp,const OUString & rQName,sal_uInt16 nActionMap)44 XMLCreateElemTransformerContext::XMLCreateElemTransformerContext(
45 		XMLTransformerBase& rImp,
46 		const OUString& rQName,
47 	   sal_uInt16 nActionMap ) :
48 	XMLTransformerContext( rImp, rQName ),
49 	m_nActionMap( nActionMap )
50 {
51 }
52 
~XMLCreateElemTransformerContext()53 XMLCreateElemTransformerContext::~XMLCreateElemTransformerContext()
54 {
55 }
56 
StartElement(const Reference<XAttributeList> & rAttrList)57 void XMLCreateElemTransformerContext::StartElement(
58 		const Reference< XAttributeList >& rAttrList )
59 {
60 	Reference< XAttributeList > xAttrList( rAttrList );
61 
62 	XMLTransformerContextVector aChildContexts;
63 
64 	XMLMutableAttributeList *pMutableAttrList = 0;
65 	XMLTransformerActions *pActions =
66 		GetTransformer().GetUserDefinedActions( m_nActionMap );
67 	OSL_ENSURE( pActions, "go no actions" );
68 	if( pActions )
69 	{
70 		sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
71 		for( sal_Int16 i=0; i < nAttrCount; ++i )
72 		{
73 			const OUString& rAttrName = xAttrList->getNameByIndex( i );
74 			const OUString& rAttrValue = xAttrList->getValueByIndex( i );
75 			OUString aLocalName;
76 			sal_uInt16 nPrefix =
77 				GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
78 														   &aLocalName );
79 
80 			XMLTransformerActions::key_type aKey( nPrefix, aLocalName );
81 			XMLTransformerActions::const_iterator aIter =
82 					pActions->find( aKey );
83 			if( !(aIter == pActions->end() ) )
84 			{
85 				if( !pMutableAttrList )
86 				{
87 					pMutableAttrList = new XMLMutableAttributeList( xAttrList );
88 					xAttrList = pMutableAttrList;
89 				}
90 				sal_uInt32 nAction = (*aIter).second.m_nActionType;
91 				switch( nAction )
92 				{
93 				case XML_ATACTION_MOVE_TO_ELEM:
94 					{
95 						OUString aElemQName(
96 							GetTransformer().GetNamespaceMap().GetQNameByKey(
97 								(*aIter).second.GetQNamePrefixFromParam1(),
98 								::xmloff::token::GetXMLToken(
99 								(*aIter).second.GetQNameTokenFromParam1()) ) );
100 						XMLTransformerContext *pContext =
101 							new XMLPersTextContentTContext( GetTransformer(),
102 													   aElemQName );
103 						pContext->Characters( rAttrValue );
104 						XMLTransformerContextVector::value_type aVal(
105 								pContext );
106 						aChildContexts.push_back( aVal );
107 						pMutableAttrList->RemoveAttributeByIndex( i );
108 						--i;
109 						--nAttrCount;
110 					}
111 					break;
112 				default:
113 					OSL_ENSURE( !this, "unknown action" );
114 					break;
115 				}
116 			}
117 		}
118 	}
119 	XMLTransformerContext::StartElement( xAttrList );
120 
121 	XMLTransformerContextVector::iterator aIter = aChildContexts.begin();
122 
123 	for( ; aIter != aChildContexts.end(); ++aIter )
124 	{
125 		(*aIter)->Export();
126 	}
127 }
128