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