xref: /AOO41X/main/xmloff/source/transform/MergeElemTContext.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 "MergeElemTContext.hxx"
31*cdf0e10cSrcweir #include "MutableAttrList.hxx"
32*cdf0e10cSrcweir #ifndef _XMLOFF_TRANSFORMERBASE_HXX
33*cdf0e10cSrcweir #include "TransformerBase.hxx"
34*cdf0e10cSrcweir #endif
35*cdf0e10cSrcweir #include "TransformerActions.hxx"
36*cdf0e10cSrcweir #include "AttrTransformerAction.hxx"
37*cdf0e10cSrcweir #include "ElemTransformerAction.hxx"
38*cdf0e10cSrcweir #include "IgnoreTContext.hxx"
39*cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir using ::rtl::OUString;
42*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
43*cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax;
44*cdf0e10cSrcweir using namespace ::xmloff::token;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir class XMLParagraphTransformerContext : public XMLTransformerContext
47*cdf0e10cSrcweir {
48*cdf0e10cSrcweir public:
49*cdf0e10cSrcweir 	TYPEINFO();
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 	XMLParagraphTransformerContext( XMLTransformerBase& rTransformer,
52*cdf0e10cSrcweir 						   const ::rtl::OUString& rQName );
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 	virtual ~XMLParagraphTransformerContext();
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 	// Create a childs element context. By default, the import's
57*cdf0e10cSrcweir 	// CreateContext method is called to create a new default context.
58*cdf0e10cSrcweir 	virtual XMLTransformerContext *CreateChildContext( sal_uInt16 nPrefix,
59*cdf0e10cSrcweir 								   const ::rtl::OUString& rLocalName,
60*cdf0e10cSrcweir 								   const ::rtl::OUString& rQName,
61*cdf0e10cSrcweir 								   const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList );
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 	// StartElement is called after a context has been constructed and
64*cdf0e10cSrcweir 	// before a elements context is parsed. It may be used for actions that
65*cdf0e10cSrcweir 	// require virtual methods. The default is to do nothing.
66*cdf0e10cSrcweir 	virtual void StartElement( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList );
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 	// EndElement is called before a context will be destructed, but
69*cdf0e10cSrcweir 	// after a elements context has been parsed. It may be used for actions
70*cdf0e10cSrcweir 	// that require virtual methods. The default is to do nothing.
71*cdf0e10cSrcweir 	virtual void EndElement();
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir 	// This method is called for all characters that are contained in the
74*cdf0e10cSrcweir 	// current element. The default is to ignore them.
75*cdf0e10cSrcweir 	virtual void Characters( const ::rtl::OUString& rChars );
76*cdf0e10cSrcweir };
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir TYPEINIT1( XMLParagraphTransformerContext, XMLTransformerContext );
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir XMLParagraphTransformerContext::XMLParagraphTransformerContext(
81*cdf0e10cSrcweir 		XMLTransformerBase& rImp,
82*cdf0e10cSrcweir 		const OUString& rQName ) :
83*cdf0e10cSrcweir 	XMLTransformerContext( rImp, rQName )
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir XMLParagraphTransformerContext::~XMLParagraphTransformerContext()
88*cdf0e10cSrcweir {
89*cdf0e10cSrcweir }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir XMLTransformerContext *XMLParagraphTransformerContext::CreateChildContext(
92*cdf0e10cSrcweir 		sal_uInt16 /*nPrefix*/,
93*cdf0e10cSrcweir 		const OUString& /*rLocalName*/,
94*cdf0e10cSrcweir 		const OUString& rQName,
95*cdf0e10cSrcweir 		const Reference< XAttributeList >& )
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir 	XMLTransformerContext *pContext = 0;
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir     pContext = new XMLIgnoreTransformerContext( GetTransformer(),
100*cdf0e10cSrcweir 												rQName, sal_True );
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir 	return pContext;
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir void XMLParagraphTransformerContext::StartElement( const Reference< XAttributeList >& rAttrList )
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir     XMLTransformerContext::StartElement( rAttrList );
108*cdf0e10cSrcweir }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir void XMLParagraphTransformerContext::EndElement()
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir     XMLTransformerContext::EndElement();
113*cdf0e10cSrcweir }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir void XMLParagraphTransformerContext::Characters( const OUString& rChars )
116*cdf0e10cSrcweir {
117*cdf0e10cSrcweir     XMLTransformerContext::Characters( rChars );
118*cdf0e10cSrcweir }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir class XMLPersTextContentRNGTransformTContext : public XMLPersTextContentTContext
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir public:
123*cdf0e10cSrcweir 	TYPEINFO();
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     XMLPersTextContentRNGTransformTContext(
126*cdf0e10cSrcweir         XMLTransformerBase& rTransformer,
127*cdf0e10cSrcweir         const ::rtl::OUString& rQName,
128*cdf0e10cSrcweir         sal_uInt16 nPrefix,
129*cdf0e10cSrcweir         ::xmloff::token::XMLTokenEnum eToken );
130*cdf0e10cSrcweir     virtual ~XMLPersTextContentRNGTransformTContext();
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 	virtual void Characters( const ::rtl::OUString& rChars );
133*cdf0e10cSrcweir };
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir TYPEINIT1( XMLPersTextContentRNGTransformTContext, XMLPersAttrListTContext );
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir XMLPersTextContentRNGTransformTContext::XMLPersTextContentRNGTransformTContext(
138*cdf0e10cSrcweir     XMLTransformerBase& rTransformer,
139*cdf0e10cSrcweir     const ::rtl::OUString& rQName,
140*cdf0e10cSrcweir     sal_uInt16 nPrefix,
141*cdf0e10cSrcweir     ::xmloff::token::XMLTokenEnum eToken ) :
142*cdf0e10cSrcweir         XMLPersTextContentTContext(
143*cdf0e10cSrcweir             rTransformer, rQName, nPrefix, eToken )
144*cdf0e10cSrcweir {}
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir XMLPersTextContentRNGTransformTContext::~XMLPersTextContentRNGTransformTContext()
147*cdf0e10cSrcweir {}
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir void XMLPersTextContentRNGTransformTContext::Characters( const ::rtl::OUString& rChars )
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir     OUString aConvChars( rChars );
152*cdf0e10cSrcweir     GetTransformer().ConvertRNGDateTimeToISO( aConvChars );
153*cdf0e10cSrcweir     XMLPersTextContentTContext::Characters( aConvChars );
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir TYPEINIT1( XMLMergeElemTransformerContext, XMLTransformerContext );
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir void XMLMergeElemTransformerContext::ExportStartElement()
160*cdf0e10cSrcweir {
161*cdf0e10cSrcweir 	XMLPersTextContentTContextVector::iterator aIter = m_aChildContexts.begin();
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 	for( ; aIter != m_aChildContexts.end(); ++aIter )
164*cdf0e10cSrcweir 	{
165*cdf0e10cSrcweir 		XMLPersTextContentTContext *pContext = (*aIter).get();
166*cdf0e10cSrcweir 		static_cast< XMLMutableAttributeList * >( m_xAttrList.get() )
167*cdf0e10cSrcweir 			->AddAttribute( pContext->GetExportQName(),
168*cdf0e10cSrcweir 							pContext->GetTextContent() );
169*cdf0e10cSrcweir 	}
170*cdf0e10cSrcweir 	XMLTransformerContext::StartElement( m_xAttrList );
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 	m_bStartElementExported = sal_True;
173*cdf0e10cSrcweir }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir XMLMergeElemTransformerContext::XMLMergeElemTransformerContext(
176*cdf0e10cSrcweir 		XMLTransformerBase& rImp,
177*cdf0e10cSrcweir 		const OUString& rQName,
178*cdf0e10cSrcweir 	   sal_uInt16 nActionMap ) :
179*cdf0e10cSrcweir 	XMLTransformerContext( rImp, rQName ),
180*cdf0e10cSrcweir 	m_nActionMap( nActionMap ),
181*cdf0e10cSrcweir 	m_bStartElementExported( sal_False )
182*cdf0e10cSrcweir {
183*cdf0e10cSrcweir }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir XMLMergeElemTransformerContext::~XMLMergeElemTransformerContext()
186*cdf0e10cSrcweir {
187*cdf0e10cSrcweir }
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir void XMLMergeElemTransformerContext::StartElement(
190*cdf0e10cSrcweir 	const Reference< XAttributeList >& rAttrList )
191*cdf0e10cSrcweir {
192*cdf0e10cSrcweir 	XMLMutableAttributeList *pMutableAttrList =
193*cdf0e10cSrcweir 		new XMLMutableAttributeList( rAttrList, sal_True );
194*cdf0e10cSrcweir 	m_xAttrList = pMutableAttrList;
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 	sal_Int16 nAttrCount = m_xAttrList.is() ? m_xAttrList->getLength() : 0;
197*cdf0e10cSrcweir 	for( sal_Int16 i=0; i < nAttrCount; i++ )
198*cdf0e10cSrcweir 	{
199*cdf0e10cSrcweir 		const OUString& rAttrName = m_xAttrList->getNameByIndex( i );
200*cdf0e10cSrcweir 		OUString aLocalName;
201*cdf0e10cSrcweir 		sal_uInt16 nPrefix =
202*cdf0e10cSrcweir 			GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
203*cdf0e10cSrcweir 																&aLocalName );
204*cdf0e10cSrcweir         sal_Bool bRemove = sal_True;
205*cdf0e10cSrcweir         if( XML_NAMESPACE_OFFICE == nPrefix)
206*cdf0e10cSrcweir         {
207*cdf0e10cSrcweir             if (IsXMLToken( aLocalName, XML_DISPLAY ) )
208*cdf0e10cSrcweir                 bRemove = sal_False;
209*cdf0e10cSrcweir             else if (IsXMLToken( aLocalName, XML_AUTHOR ) )
210*cdf0e10cSrcweir                 bRemove = sal_False;
211*cdf0e10cSrcweir             else if (IsXMLToken( aLocalName, XML_CREATE_DATE ) )
212*cdf0e10cSrcweir                 bRemove = sal_False;
213*cdf0e10cSrcweir             else if (IsXMLToken( aLocalName, XML_CREATE_DATE_STRING ) )
214*cdf0e10cSrcweir                 bRemove = sal_False;
215*cdf0e10cSrcweir         }
216*cdf0e10cSrcweir         if (bRemove)
217*cdf0e10cSrcweir         {
218*cdf0e10cSrcweir             pMutableAttrList->RemoveAttributeByIndex( i );
219*cdf0e10cSrcweir             --i;
220*cdf0e10cSrcweir             --nAttrCount;
221*cdf0e10cSrcweir         }
222*cdf0e10cSrcweir 	}
223*cdf0e10cSrcweir }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir XMLTransformerContext *XMLMergeElemTransformerContext::CreateChildContext(
226*cdf0e10cSrcweir 		sal_uInt16 nPrefix,
227*cdf0e10cSrcweir 		const OUString& rLocalName,
228*cdf0e10cSrcweir 		const OUString& rQName,
229*cdf0e10cSrcweir 		const Reference< XAttributeList >& rAttrList )
230*cdf0e10cSrcweir {
231*cdf0e10cSrcweir 	XMLTransformerContext *pContext = 0;
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir 	if( !m_bStartElementExported )
234*cdf0e10cSrcweir 	{
235*cdf0e10cSrcweir 		XMLTransformerActions *pActions =
236*cdf0e10cSrcweir 			GetTransformer().GetUserDefinedActions( m_nActionMap );
237*cdf0e10cSrcweir 		OSL_ENSURE( pActions, "go no actions" );
238*cdf0e10cSrcweir 		if( pActions )
239*cdf0e10cSrcweir 		{
240*cdf0e10cSrcweir 			XMLTransformerActions::key_type aKey( nPrefix, rLocalName );
241*cdf0e10cSrcweir 			XMLTransformerActions::const_iterator aIter =
242*cdf0e10cSrcweir 				pActions->find( aKey );
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir 			if( !(aIter == pActions->end()) )
245*cdf0e10cSrcweir 			{
246*cdf0e10cSrcweir 				switch( (*aIter).second.m_nActionType )
247*cdf0e10cSrcweir 				{
248*cdf0e10cSrcweir 				case XML_ATACTION_MOVE_FROM_ELEM_RNG2ISO_DATETIME:
249*cdf0e10cSrcweir 					{
250*cdf0e10cSrcweir 						XMLPersTextContentTContext *pTC =
251*cdf0e10cSrcweir 							new XMLPersTextContentRNGTransformTContext(
252*cdf0e10cSrcweir 									GetTransformer(), rQName,
253*cdf0e10cSrcweir 								    (*aIter).second.GetQNamePrefixFromParam1(),
254*cdf0e10cSrcweir 									(*aIter).second.GetQNameTokenFromParam1() );
255*cdf0e10cSrcweir 						XMLPersTextContentTContextVector::value_type aVal(pTC);
256*cdf0e10cSrcweir 						m_aChildContexts.push_back( aVal );
257*cdf0e10cSrcweir 						pContext = pTC;
258*cdf0e10cSrcweir 					}
259*cdf0e10cSrcweir 					break;
260*cdf0e10cSrcweir                 case XML_ATACTION_MOVE_FROM_ELEM:
261*cdf0e10cSrcweir 					{
262*cdf0e10cSrcweir 						XMLPersTextContentTContext *pTC =
263*cdf0e10cSrcweir 							new XMLPersTextContentTContext(
264*cdf0e10cSrcweir 									GetTransformer(), rQName,
265*cdf0e10cSrcweir 								    (*aIter).second.GetQNamePrefixFromParam1(),
266*cdf0e10cSrcweir 									(*aIter).second.GetQNameTokenFromParam1() );
267*cdf0e10cSrcweir 						XMLPersTextContentTContextVector::value_type aVal(pTC);
268*cdf0e10cSrcweir 						m_aChildContexts.push_back( aVal );
269*cdf0e10cSrcweir 						pContext = pTC;
270*cdf0e10cSrcweir 					}
271*cdf0e10cSrcweir 					break;
272*cdf0e10cSrcweir                 case XML_ETACTION_EXTRACT_CHARACTERS:
273*cdf0e10cSrcweir                     {
274*cdf0e10cSrcweir 		                if( !m_bStartElementExported )
275*cdf0e10cSrcweir 			                ExportStartElement();
276*cdf0e10cSrcweir                         XMLParagraphTransformerContext* pPTC =
277*cdf0e10cSrcweir 	                        new XMLParagraphTransformerContext( GetTransformer(),
278*cdf0e10cSrcweir 						    rQName);
279*cdf0e10cSrcweir 						pContext = pPTC;
280*cdf0e10cSrcweir                     }
281*cdf0e10cSrcweir                     break;
282*cdf0e10cSrcweir 				default:
283*cdf0e10cSrcweir 					OSL_ENSURE( !this, "unknown action" );
284*cdf0e10cSrcweir 					break;
285*cdf0e10cSrcweir 				}
286*cdf0e10cSrcweir 			}
287*cdf0e10cSrcweir 		}
288*cdf0e10cSrcweir 	}
289*cdf0e10cSrcweir     else
290*cdf0e10cSrcweir 	{
291*cdf0e10cSrcweir 		XMLTransformerActions *pActions =
292*cdf0e10cSrcweir 			GetTransformer().GetUserDefinedActions( m_nActionMap );
293*cdf0e10cSrcweir 		OSL_ENSURE( pActions, "go no actions" );
294*cdf0e10cSrcweir 		if( pActions )
295*cdf0e10cSrcweir 		{
296*cdf0e10cSrcweir 			XMLTransformerActions::key_type aKey( nPrefix, rLocalName );
297*cdf0e10cSrcweir 			XMLTransformerActions::const_iterator aIter =
298*cdf0e10cSrcweir 				pActions->find( aKey );
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir 			if( !(aIter == pActions->end()) )
301*cdf0e10cSrcweir 			{
302*cdf0e10cSrcweir 				switch( (*aIter).second.m_nActionType )
303*cdf0e10cSrcweir 				{
304*cdf0e10cSrcweir                 case XML_ETACTION_EXTRACT_CHARACTERS:
305*cdf0e10cSrcweir                     {
306*cdf0e10cSrcweir 		                if( !m_bStartElementExported )
307*cdf0e10cSrcweir 			                ExportStartElement();
308*cdf0e10cSrcweir                         XMLParagraphTransformerContext* pPTC =
309*cdf0e10cSrcweir 	                        new XMLParagraphTransformerContext( GetTransformer(),
310*cdf0e10cSrcweir 						    rQName);
311*cdf0e10cSrcweir 						pContext = pPTC;
312*cdf0e10cSrcweir                     }
313*cdf0e10cSrcweir                     break;
314*cdf0e10cSrcweir 				default:
315*cdf0e10cSrcweir 					OSL_ENSURE( !this, "unknown action" );
316*cdf0e10cSrcweir 					break;
317*cdf0e10cSrcweir 				}
318*cdf0e10cSrcweir 			}
319*cdf0e10cSrcweir 		}
320*cdf0e10cSrcweir 	}
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir 	// default is copying
323*cdf0e10cSrcweir 	if( !pContext )
324*cdf0e10cSrcweir 	{
325*cdf0e10cSrcweir 		if( !m_bStartElementExported )
326*cdf0e10cSrcweir 			ExportStartElement();
327*cdf0e10cSrcweir 		pContext = XMLTransformerContext::CreateChildContext( nPrefix,
328*cdf0e10cSrcweir 															  rLocalName,
329*cdf0e10cSrcweir 															  rQName,
330*cdf0e10cSrcweir 															  rAttrList );
331*cdf0e10cSrcweir 	}
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir 	return pContext;
334*cdf0e10cSrcweir }
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir void XMLMergeElemTransformerContext::EndElement()
337*cdf0e10cSrcweir {
338*cdf0e10cSrcweir 	if( !m_bStartElementExported )
339*cdf0e10cSrcweir 		ExportStartElement();
340*cdf0e10cSrcweir 	XMLTransformerContext::EndElement();
341*cdf0e10cSrcweir }
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir void XMLMergeElemTransformerContext::Characters( const OUString& )
344*cdf0e10cSrcweir {
345*cdf0e10cSrcweir 	// ignore
346*cdf0e10cSrcweir }
347