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 "XMLRedlineExport.hxx"
27cdf0e10cSrcweir #include <tools/debug.hxx>
28cdf0e10cSrcweir #include <rtl/ustring.hxx>
29cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
30cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
31cdf0e10cSrcweir #include <com/sun/star/beans/UnknownPropertyException.hpp>
32cdf0e10cSrcweir #include <com/sun/star/container/XEnumerationAccess.hpp>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <com/sun/star/container/XEnumeration.hpp>
35cdf0e10cSrcweir #include <com/sun/star/document/XRedlinesSupplier.hpp>
36cdf0e10cSrcweir #include <com/sun/star/text/XText.hpp>
37cdf0e10cSrcweir #include <com/sun/star/text/XTextContent.hpp>
38cdf0e10cSrcweir #include <com/sun/star/text/XTextSection.hpp>
39cdf0e10cSrcweir #include <com/sun/star/util/DateTime.hpp>
40cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
41cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
42cdf0e10cSrcweir #include <xmloff/xmlexp.hxx>
43cdf0e10cSrcweir #include <xmloff/xmluconv.hxx>
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 
46cdf0e10cSrcweir using namespace ::com::sun::star;
47cdf0e10cSrcweir using namespace ::xmloff::token;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir using ::com::sun::star::beans::PropertyValue;
50cdf0e10cSrcweir using ::com::sun::star::beans::XPropertySet;
51cdf0e10cSrcweir using ::com::sun::star::beans::UnknownPropertyException;
52cdf0e10cSrcweir using ::com::sun::star::document::XRedlinesSupplier;
53cdf0e10cSrcweir using ::com::sun::star::container::XEnumerationAccess;
54cdf0e10cSrcweir using ::com::sun::star::container::XEnumeration;
55cdf0e10cSrcweir using ::com::sun::star::text::XText;
56cdf0e10cSrcweir using ::com::sun::star::text::XTextContent;
57cdf0e10cSrcweir using ::com::sun::star::text::XTextSection;
58cdf0e10cSrcweir using ::com::sun::star::uno::Any;
59cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
60cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
61cdf0e10cSrcweir using ::com::sun::star::util::DateTime;
62cdf0e10cSrcweir using ::rtl::OUString;
63cdf0e10cSrcweir using ::rtl::OUStringBuffer;
64cdf0e10cSrcweir using ::std::list;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 
XMLRedlineExport(SvXMLExport & rExp)67cdf0e10cSrcweir XMLRedlineExport::XMLRedlineExport(SvXMLExport& rExp)
68cdf0e10cSrcweir :	sDelete(RTL_CONSTASCII_USTRINGPARAM("Delete"))
69cdf0e10cSrcweir ,	sDeletion(GetXMLToken(XML_DELETION))
70cdf0e10cSrcweir ,	sFormat(RTL_CONSTASCII_USTRINGPARAM("Format"))
71cdf0e10cSrcweir ,	sFormatChange(GetXMLToken(XML_FORMAT_CHANGE))
72cdf0e10cSrcweir ,	sInsert(RTL_CONSTASCII_USTRINGPARAM("Insert"))
73cdf0e10cSrcweir ,	sInsertion(GetXMLToken(XML_INSERTION))
74cdf0e10cSrcweir ,	sIsCollapsed(RTL_CONSTASCII_USTRINGPARAM("IsCollapsed"))
75cdf0e10cSrcweir ,	sIsStart(RTL_CONSTASCII_USTRINGPARAM("IsStart"))
76cdf0e10cSrcweir ,	sRedlineAuthor(RTL_CONSTASCII_USTRINGPARAM("RedlineAuthor"))
77cdf0e10cSrcweir ,	sRedlineComment(RTL_CONSTASCII_USTRINGPARAM("RedlineComment"))
78cdf0e10cSrcweir ,	sRedlineDateTime(RTL_CONSTASCII_USTRINGPARAM("RedlineDateTime"))
79cdf0e10cSrcweir ,	sRedlineSuccessorData(RTL_CONSTASCII_USTRINGPARAM("RedlineSuccessorData"))
80cdf0e10cSrcweir ,	sRedlineText(RTL_CONSTASCII_USTRINGPARAM("RedlineText"))
81cdf0e10cSrcweir ,	sRedlineType(RTL_CONSTASCII_USTRINGPARAM("RedlineType"))
82cdf0e10cSrcweir ,	sStyle(RTL_CONSTASCII_USTRINGPARAM("Style"))
83cdf0e10cSrcweir ,	sTextTable(RTL_CONSTASCII_USTRINGPARAM("TextTable"))
84cdf0e10cSrcweir ,	sUnknownChange(RTL_CONSTASCII_USTRINGPARAM("UnknownChange"))
85cdf0e10cSrcweir ,	sStartRedline(RTL_CONSTASCII_USTRINGPARAM("StartRedline"))
86cdf0e10cSrcweir ,	sEndRedline(RTL_CONSTASCII_USTRINGPARAM("EndRedline"))
87cdf0e10cSrcweir ,	sRedlineIdentifier(RTL_CONSTASCII_USTRINGPARAM("RedlineIdentifier"))
88cdf0e10cSrcweir ,	sIsInHeaderFooter(RTL_CONSTASCII_USTRINGPARAM("IsInHeaderFooter"))
89cdf0e10cSrcweir ,	sRedlineProtectionKey(RTL_CONSTASCII_USTRINGPARAM("RedlineProtectionKey"))
90cdf0e10cSrcweir ,	sRecordChanges(RTL_CONSTASCII_USTRINGPARAM("RecordChanges"))
91cdf0e10cSrcweir ,	sMergeLastPara(RTL_CONSTASCII_USTRINGPARAM("MergeLastPara"))
92cdf0e10cSrcweir ,	sChangePrefix(RTL_CONSTASCII_USTRINGPARAM("ct"))
93cdf0e10cSrcweir ,	rExport(rExp)
94cdf0e10cSrcweir ,	pCurrentChangesList(NULL)
95cdf0e10cSrcweir {
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 
~XMLRedlineExport()99cdf0e10cSrcweir XMLRedlineExport::~XMLRedlineExport()
100cdf0e10cSrcweir {
101cdf0e10cSrcweir 	// delete changes lists
102cdf0e10cSrcweir 	for( ChangesMapType::iterator aIter = aChangeMap.begin();
103cdf0e10cSrcweir 		 aIter != aChangeMap.end();
104cdf0e10cSrcweir 		 aIter++ )
105cdf0e10cSrcweir 	{
106cdf0e10cSrcweir 		delete aIter->second;
107cdf0e10cSrcweir 	}
108cdf0e10cSrcweir 	aChangeMap.clear();
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 
ExportChange(const Reference<XPropertySet> & rPropSet,sal_Bool bAutoStyle)112cdf0e10cSrcweir void XMLRedlineExport::ExportChange(
113cdf0e10cSrcweir 	const Reference<XPropertySet> & rPropSet,
114cdf0e10cSrcweir 	sal_Bool bAutoStyle)
115cdf0e10cSrcweir {
116cdf0e10cSrcweir 	if (bAutoStyle)
117cdf0e10cSrcweir 	{
118cdf0e10cSrcweir         // For the headers/footers, we have to collect the autostyles
119cdf0e10cSrcweir         // here.  For the general case, however, it's better to collet
120cdf0e10cSrcweir         // the autostyles by iterating over the global redline
121cdf0e10cSrcweir         // list. So that's what we do: Here, we collect autostyles
122cdf0e10cSrcweir         // only if we have no current list of changes. For the
123cdf0e10cSrcweir         // main-document case, the autostyles are collected in
124cdf0e10cSrcweir         // ExportChangesListAutoStyles().
125cdf0e10cSrcweir         if (pCurrentChangesList != NULL)
126cdf0e10cSrcweir             ExportChangeAutoStyle(rPropSet);
127cdf0e10cSrcweir 	}
128cdf0e10cSrcweir 	else
129cdf0e10cSrcweir 	{
130cdf0e10cSrcweir 		ExportChangeInline(rPropSet);
131cdf0e10cSrcweir 	}
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 
ExportChangesList(sal_Bool bAutoStyles)135cdf0e10cSrcweir void XMLRedlineExport::ExportChangesList(sal_Bool bAutoStyles)
136cdf0e10cSrcweir {
137cdf0e10cSrcweir 	if (bAutoStyles)
138cdf0e10cSrcweir 	{
139cdf0e10cSrcweir 		ExportChangesListAutoStyles();
140cdf0e10cSrcweir 	}
141cdf0e10cSrcweir 	else
142cdf0e10cSrcweir 	{
143cdf0e10cSrcweir 		ExportChangesListElements();
144cdf0e10cSrcweir 	}
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 
ExportChangesList(const Reference<XText> & rText,sal_Bool bAutoStyles)148cdf0e10cSrcweir void XMLRedlineExport::ExportChangesList(
149cdf0e10cSrcweir 	const Reference<XText> & rText,
150cdf0e10cSrcweir 	sal_Bool bAutoStyles)
151cdf0e10cSrcweir {
152cdf0e10cSrcweir     // in the header/footer case, auto styles are collected from the
153cdf0e10cSrcweir     // inline change elements.
154cdf0e10cSrcweir     if (bAutoStyles)
155cdf0e10cSrcweir         return;
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 	// look for changes list for this XText
158cdf0e10cSrcweir 	ChangesMapType::iterator aFind = aChangeMap.find(rText);
159cdf0e10cSrcweir 	if (aFind != aChangeMap.end())
160cdf0e10cSrcweir 	{
161cdf0e10cSrcweir 		ChangesListType* pChangesList = aFind->second;
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 		// export only if changes are found
164cdf0e10cSrcweir 		if (pChangesList->size() > 0)
165cdf0e10cSrcweir 		{
166cdf0e10cSrcweir 			// changes container element
167cdf0e10cSrcweir 			SvXMLElementExport aChanges(rExport, XML_NAMESPACE_TEXT,
168cdf0e10cSrcweir 										XML_TRACKED_CHANGES,
169cdf0e10cSrcweir 										sal_True, sal_True);
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 			// iterate over changes list
172cdf0e10cSrcweir 			for( ChangesListType::iterator aIter = pChangesList->begin();
173cdf0e10cSrcweir 				 aIter != pChangesList->end();
174cdf0e10cSrcweir 				 aIter++ )
175cdf0e10cSrcweir 			{
176cdf0e10cSrcweir                 ExportChangedRegion( *aIter );
177cdf0e10cSrcweir 			}
178cdf0e10cSrcweir 		}
179cdf0e10cSrcweir 		// else: changes list empty -> ignore
180cdf0e10cSrcweir 	}
181cdf0e10cSrcweir 	// else: no changes list found -> empty
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
SetCurrentXText(const Reference<XText> & rText)184cdf0e10cSrcweir void XMLRedlineExport::SetCurrentXText(
185cdf0e10cSrcweir 	const Reference<XText> & rText)
186cdf0e10cSrcweir {
187cdf0e10cSrcweir 	if (rText.is())
188cdf0e10cSrcweir 	{
189cdf0e10cSrcweir 		// look for appropriate list in map; use the found one, or create new
190cdf0e10cSrcweir 		ChangesMapType::iterator aIter = aChangeMap.find(rText);
191cdf0e10cSrcweir 		if (aIter == aChangeMap.end())
192cdf0e10cSrcweir 		{
193cdf0e10cSrcweir 			ChangesListType* pList = new ChangesListType;
194cdf0e10cSrcweir 			aChangeMap[rText] = pList;
195cdf0e10cSrcweir 			pCurrentChangesList = pList;
196cdf0e10cSrcweir 		}
197cdf0e10cSrcweir 		else
198cdf0e10cSrcweir 			pCurrentChangesList = aIter->second;
199cdf0e10cSrcweir 	}
200cdf0e10cSrcweir 	else
201cdf0e10cSrcweir 	{
202cdf0e10cSrcweir 		// don't record changes
203cdf0e10cSrcweir 		SetCurrentXText();
204cdf0e10cSrcweir 	}
205cdf0e10cSrcweir }
206cdf0e10cSrcweir 
SetCurrentXText()207cdf0e10cSrcweir void XMLRedlineExport::SetCurrentXText()
208cdf0e10cSrcweir {
209cdf0e10cSrcweir 	pCurrentChangesList = NULL;
210cdf0e10cSrcweir }
211cdf0e10cSrcweir 
212cdf0e10cSrcweir 
ExportChangesListElements()213cdf0e10cSrcweir void XMLRedlineExport::ExportChangesListElements()
214cdf0e10cSrcweir {
215cdf0e10cSrcweir 	// get redlines (aka tracked changes) from the model
216cdf0e10cSrcweir 	Reference<XRedlinesSupplier> xSupplier(rExport.GetModel(), uno::UNO_QUERY);
217cdf0e10cSrcweir 	if (xSupplier.is())
218cdf0e10cSrcweir 	{
219cdf0e10cSrcweir 		Reference<XEnumerationAccess> aEnumAccess = xSupplier->getRedlines();
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 		// redline protection key
222cdf0e10cSrcweir 		Reference<XPropertySet> aDocPropertySet( rExport.GetModel(),
223cdf0e10cSrcweir 												 uno::UNO_QUERY );
224cdf0e10cSrcweir 		// redlining enabled?
225cdf0e10cSrcweir 		sal_Bool bEnabled = *(sal_Bool*)aDocPropertySet->getPropertyValue(
226cdf0e10cSrcweir 												sRecordChanges ).getValue();
227cdf0e10cSrcweir 
228cdf0e10cSrcweir 		// only export if we have redlines or attributes
229cdf0e10cSrcweir 		if ( aEnumAccess->hasElements() || bEnabled )
230cdf0e10cSrcweir 		{
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 			// export only if we have changes, but tracking is not enabled
233cdf0e10cSrcweir 			if ( !bEnabled != !aEnumAccess->hasElements() )
234cdf0e10cSrcweir 			{
235cdf0e10cSrcweir 				rExport.AddAttribute(
236cdf0e10cSrcweir 					XML_NAMESPACE_TEXT, XML_TRACK_CHANGES,
237cdf0e10cSrcweir 					bEnabled ? XML_TRUE : XML_FALSE );
238cdf0e10cSrcweir 			}
239cdf0e10cSrcweir 
240cdf0e10cSrcweir 			// changes container element
241cdf0e10cSrcweir 			SvXMLElementExport aChanges(rExport, XML_NAMESPACE_TEXT,
242cdf0e10cSrcweir 										XML_TRACKED_CHANGES,
243cdf0e10cSrcweir 										sal_True, sal_True);
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 			// get enumeration and iterate over elements
246cdf0e10cSrcweir 			Reference<XEnumeration> aEnum = aEnumAccess->createEnumeration();
247cdf0e10cSrcweir 			while (aEnum->hasMoreElements())
248cdf0e10cSrcweir 			{
249cdf0e10cSrcweir 				Any aAny = aEnum->nextElement();
250cdf0e10cSrcweir 				Reference<XPropertySet> xPropSet;
251cdf0e10cSrcweir 				aAny >>= xPropSet;
252cdf0e10cSrcweir 
253cdf0e10cSrcweir 				DBG_ASSERT(xPropSet.is(),
254cdf0e10cSrcweir 						   "can't get XPropertySet; skipping Redline");
255cdf0e10cSrcweir 				if (xPropSet.is())
256cdf0e10cSrcweir 				{
257cdf0e10cSrcweir 					// export only if not in header or footer
258cdf0e10cSrcweir 					// (those must be exported with their XText)
259cdf0e10cSrcweir 					aAny = xPropSet->getPropertyValue(sIsInHeaderFooter);
260cdf0e10cSrcweir 					if (! *(sal_Bool*)aAny.getValue())
261cdf0e10cSrcweir 					{
262cdf0e10cSrcweir 						// and finally, export change
263cdf0e10cSrcweir 						ExportChangedRegion(xPropSet);
264cdf0e10cSrcweir 					}
265cdf0e10cSrcweir 				}
266cdf0e10cSrcweir 				// else: no XPropertySet -> no export
267cdf0e10cSrcweir 			}
268cdf0e10cSrcweir 		}
269cdf0e10cSrcweir 		// else: no redlines -> no export
270cdf0e10cSrcweir 	}
271cdf0e10cSrcweir 	// else: no XRedlineSupplier -> no export
272cdf0e10cSrcweir }
273cdf0e10cSrcweir 
ExportChangeAutoStyle(const Reference<XPropertySet> & rPropSet)274cdf0e10cSrcweir void XMLRedlineExport::ExportChangeAutoStyle(
275cdf0e10cSrcweir 	const Reference<XPropertySet> & rPropSet)
276cdf0e10cSrcweir {
277cdf0e10cSrcweir 	// record change (if changes should be recorded)
278cdf0e10cSrcweir 	if (NULL != pCurrentChangesList)
279cdf0e10cSrcweir 	{
280cdf0e10cSrcweir 		// put redline in list if it's collapsed or the redline start
281cdf0e10cSrcweir 		Any aIsStart = rPropSet->getPropertyValue(sIsStart);
282cdf0e10cSrcweir 		Any aIsCollapsed = rPropSet->getPropertyValue(sIsCollapsed);
283cdf0e10cSrcweir 
284cdf0e10cSrcweir 		if ( *(sal_Bool*)aIsStart.getValue() ||
285cdf0e10cSrcweir 			 *(sal_Bool*)aIsCollapsed.getValue() )
286cdf0e10cSrcweir 			pCurrentChangesList->push_back(rPropSet);
287cdf0e10cSrcweir 	}
288cdf0e10cSrcweir 
289cdf0e10cSrcweir 	// get XText for export of redline auto styles
290cdf0e10cSrcweir 	Any aAny = rPropSet->getPropertyValue(sRedlineText);
291cdf0e10cSrcweir 	Reference<XText> xText;
292cdf0e10cSrcweir 	aAny >>= xText;
293cdf0e10cSrcweir 	if (xText.is())
294cdf0e10cSrcweir 	{
295cdf0e10cSrcweir 		// export the auto styles
296cdf0e10cSrcweir 		rExport.GetTextParagraphExport()->collectTextAutoStyles(xText);
297cdf0e10cSrcweir 	}
298cdf0e10cSrcweir }
299cdf0e10cSrcweir 
ExportChangesListAutoStyles()300cdf0e10cSrcweir void XMLRedlineExport::ExportChangesListAutoStyles()
301cdf0e10cSrcweir {
302cdf0e10cSrcweir 	// get redlines (aka tracked changes) from the model
303cdf0e10cSrcweir 	Reference<XRedlinesSupplier> xSupplier(rExport.GetModel(), uno::UNO_QUERY);
304cdf0e10cSrcweir 	if (xSupplier.is())
305cdf0e10cSrcweir 	{
306cdf0e10cSrcweir 		Reference<XEnumerationAccess> aEnumAccess = xSupplier->getRedlines();
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 		// only export if we actually have redlines
309cdf0e10cSrcweir 		if (aEnumAccess->hasElements())
310cdf0e10cSrcweir 		{
311cdf0e10cSrcweir 			// get enumeration and iterate over elements
312cdf0e10cSrcweir 			Reference<XEnumeration> aEnum = aEnumAccess->createEnumeration();
313cdf0e10cSrcweir 			while (aEnum->hasMoreElements())
314cdf0e10cSrcweir 			{
315cdf0e10cSrcweir 				Any aAny = aEnum->nextElement();
316cdf0e10cSrcweir 				Reference<XPropertySet> xPropSet;
317cdf0e10cSrcweir 				aAny >>= xPropSet;
318cdf0e10cSrcweir 
319cdf0e10cSrcweir 				DBG_ASSERT(xPropSet.is(),
320cdf0e10cSrcweir 						   "can't get XPropertySet; skipping Redline");
321cdf0e10cSrcweir 				if (xPropSet.is())
322cdf0e10cSrcweir 				{
323cdf0e10cSrcweir 
324cdf0e10cSrcweir                     // export only if not in header or footer
325cdf0e10cSrcweir                     // (those must be exported with their XText)
326cdf0e10cSrcweir                     aAny = xPropSet->getPropertyValue(sIsInHeaderFooter);
327cdf0e10cSrcweir                     if (! *(sal_Bool*)aAny.getValue())
328cdf0e10cSrcweir 					{
329cdf0e10cSrcweir                         ExportChangeAutoStyle(xPropSet);
330cdf0e10cSrcweir                     }
331cdf0e10cSrcweir 				}
332cdf0e10cSrcweir 			}
333cdf0e10cSrcweir 		}
334cdf0e10cSrcweir 	}
335cdf0e10cSrcweir }
336cdf0e10cSrcweir 
ExportChangeInline(const Reference<XPropertySet> & rPropSet)337cdf0e10cSrcweir void XMLRedlineExport::ExportChangeInline(
338cdf0e10cSrcweir 	const Reference<XPropertySet> & rPropSet)
339cdf0e10cSrcweir {
340cdf0e10cSrcweir 	// determine element name (depending on collapsed, start/end)
341cdf0e10cSrcweir 	enum XMLTokenEnum eElement = XML_TOKEN_INVALID;
342cdf0e10cSrcweir 	Any aAny = rPropSet->getPropertyValue(sIsCollapsed);
343cdf0e10cSrcweir 	sal_Bool bCollapsed = *(sal_Bool *)aAny.getValue();
344cdf0e10cSrcweir 	sal_Bool bStart = sal_True;	// ignored if bCollapsed = sal_True
345cdf0e10cSrcweir 	if (bCollapsed)
346cdf0e10cSrcweir 	{
347cdf0e10cSrcweir 		eElement = XML_CHANGE;
348cdf0e10cSrcweir 	}
349cdf0e10cSrcweir 	else
350cdf0e10cSrcweir 	{
351cdf0e10cSrcweir 		aAny = rPropSet->getPropertyValue(sIsStart);
352cdf0e10cSrcweir 		bStart = *(sal_Bool *)aAny.getValue();
353cdf0e10cSrcweir 		eElement = bStart ? XML_CHANGE_START : XML_CHANGE_END;
354cdf0e10cSrcweir 	}
355cdf0e10cSrcweir 
356cdf0e10cSrcweir 	if (XML_TOKEN_INVALID != eElement)
357cdf0e10cSrcweir 	{
358cdf0e10cSrcweir 		// we always need the ID
359cdf0e10cSrcweir 		rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_CHANGE_ID,
360cdf0e10cSrcweir 							 GetRedlineID(rPropSet));
361cdf0e10cSrcweir 
362cdf0e10cSrcweir 		// export the element (no whitespace because we're in the text body)
363cdf0e10cSrcweir 		SvXMLElementExport aChangeElem(rExport, XML_NAMESPACE_TEXT,
364cdf0e10cSrcweir 									   eElement, sal_False, sal_False);
365cdf0e10cSrcweir 	}
366cdf0e10cSrcweir }
367cdf0e10cSrcweir 
368cdf0e10cSrcweir 
ExportChangedRegion(const Reference<XPropertySet> & rPropSet)369cdf0e10cSrcweir void XMLRedlineExport::ExportChangedRegion(
370cdf0e10cSrcweir 	const Reference<XPropertySet> & rPropSet)
371cdf0e10cSrcweir {
372cdf0e10cSrcweir 	// Redline-ID
373cdf0e10cSrcweir     rExport.AddAttributeIdLegacy(XML_NAMESPACE_TEXT, GetRedlineID(rPropSet));
374cdf0e10cSrcweir 
375cdf0e10cSrcweir     // merge-last-paragraph
376cdf0e10cSrcweir     Any aAny = rPropSet->getPropertyValue(sMergeLastPara);
377cdf0e10cSrcweir     if( ! *(sal_Bool*)aAny.getValue() )
378cdf0e10cSrcweir         rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_MERGE_LAST_PARAGRAPH,
379cdf0e10cSrcweir                              XML_FALSE);
380cdf0e10cSrcweir 
381cdf0e10cSrcweir     // export change region element
382cdf0e10cSrcweir 	SvXMLElementExport aChangedRegion(rExport, XML_NAMESPACE_TEXT,
383cdf0e10cSrcweir 									  XML_CHANGED_REGION, sal_True, sal_True);
384cdf0e10cSrcweir 
385cdf0e10cSrcweir 
386cdf0e10cSrcweir 	// scope for (first) change element
387cdf0e10cSrcweir 	{
388cdf0e10cSrcweir 		aAny = rPropSet->getPropertyValue(sRedlineType);
389cdf0e10cSrcweir 		OUString sType;
390cdf0e10cSrcweir 		aAny >>= sType;
391cdf0e10cSrcweir 		SvXMLElementExport aChange(rExport, XML_NAMESPACE_TEXT,
392cdf0e10cSrcweir 								   ConvertTypeName(sType), sal_True, sal_True);
393cdf0e10cSrcweir 
394cdf0e10cSrcweir 		ExportChangeInfo(rPropSet);
395cdf0e10cSrcweir 
396cdf0e10cSrcweir 		// get XText from the redline and export (if the XText exists)
397cdf0e10cSrcweir 		aAny = rPropSet->getPropertyValue(sRedlineText);
398cdf0e10cSrcweir 		Reference<XText> xText;
399cdf0e10cSrcweir 		aAny >>= xText;
400cdf0e10cSrcweir 		if (xText.is())
401cdf0e10cSrcweir 		{
402cdf0e10cSrcweir 			rExport.GetTextParagraphExport()->exportText(xText);
403cdf0e10cSrcweir 			// default parameters: bProgress, bExportParagraph ???
404cdf0e10cSrcweir 		}
405cdf0e10cSrcweir 		// else: no text interface -> content is inline and will
406cdf0e10cSrcweir 		//       be exported there
407cdf0e10cSrcweir 	}
408cdf0e10cSrcweir 
409cdf0e10cSrcweir 	// changed change? Hierarchical changes can onl be two levels
410cdf0e10cSrcweir 	// deep. Here we check for the second level.
411cdf0e10cSrcweir 	aAny = rPropSet->getPropertyValue(sRedlineSuccessorData);
412cdf0e10cSrcweir 	Sequence<PropertyValue> aSuccessorData;
413cdf0e10cSrcweir 	aAny >>= aSuccessorData;
414cdf0e10cSrcweir 
415cdf0e10cSrcweir 	// if we actually got a hierarchical change, make element and
416cdf0e10cSrcweir 	// process change info
417cdf0e10cSrcweir 	if (aSuccessorData.getLength() > 0)
418cdf0e10cSrcweir 	{
419cdf0e10cSrcweir 		// The only change that can be "undone" is an insertion -
420cdf0e10cSrcweir 		// after all, you can't re-insert an deletion, but you can
421cdf0e10cSrcweir 		// delete an insertion. This assumption is asserted in
422cdf0e10cSrcweir 		// ExportChangeInfo(Sequence<PropertyValue>&).
423cdf0e10cSrcweir 		SvXMLElementExport aSecondChangeElem(
424cdf0e10cSrcweir 			rExport, XML_NAMESPACE_TEXT, XML_INSERTION,
425cdf0e10cSrcweir 			sal_True, sal_True);
426cdf0e10cSrcweir 
427cdf0e10cSrcweir 		ExportChangeInfo(aSuccessorData);
428cdf0e10cSrcweir 	}
429cdf0e10cSrcweir 	// else: no hierarchical change
430cdf0e10cSrcweir }
431cdf0e10cSrcweir 
432cdf0e10cSrcweir 
ConvertTypeName(const OUString & sApiName)433cdf0e10cSrcweir const OUString XMLRedlineExport::ConvertTypeName(
434cdf0e10cSrcweir 	const OUString& sApiName)
435cdf0e10cSrcweir {
436cdf0e10cSrcweir 	if (sApiName == sDelete)
437cdf0e10cSrcweir 	{
438cdf0e10cSrcweir 		return sDeletion;
439cdf0e10cSrcweir 	}
440cdf0e10cSrcweir 	else if (sApiName == sInsert)
441cdf0e10cSrcweir 	{
442cdf0e10cSrcweir 		return sInsertion;
443cdf0e10cSrcweir 	}
444cdf0e10cSrcweir 	else if (sApiName == sFormat)
445cdf0e10cSrcweir 	{
446cdf0e10cSrcweir 		return sFormatChange;
447cdf0e10cSrcweir 	}
448cdf0e10cSrcweir 	else
449cdf0e10cSrcweir 	{
450cdf0e10cSrcweir 		DBG_ERROR("unknown redline type");
451cdf0e10cSrcweir 		return sUnknownChange;
452cdf0e10cSrcweir 	}
453cdf0e10cSrcweir }
454cdf0e10cSrcweir 
455cdf0e10cSrcweir 
456cdf0e10cSrcweir /** Create a Redline-ID */
GetRedlineID(const Reference<XPropertySet> & rPropSet)457cdf0e10cSrcweir const OUString XMLRedlineExport::GetRedlineID(
458cdf0e10cSrcweir 	const Reference<XPropertySet> & rPropSet)
459cdf0e10cSrcweir {
460cdf0e10cSrcweir 	Any aAny = rPropSet->getPropertyValue(sRedlineIdentifier);
461cdf0e10cSrcweir 	OUString sTmp;
462cdf0e10cSrcweir 	aAny >>= sTmp;
463cdf0e10cSrcweir 
464cdf0e10cSrcweir 	OUStringBuffer sBuf(sChangePrefix);
465cdf0e10cSrcweir 	sBuf.append(sTmp);
466cdf0e10cSrcweir 	return sBuf.makeStringAndClear();
467cdf0e10cSrcweir }
468cdf0e10cSrcweir 
469cdf0e10cSrcweir 
ExportChangeInfo(const Reference<XPropertySet> & rPropSet)470cdf0e10cSrcweir void XMLRedlineExport::ExportChangeInfo(
471cdf0e10cSrcweir 	const Reference<XPropertySet> & rPropSet)
472cdf0e10cSrcweir {
473cdf0e10cSrcweir 
474cdf0e10cSrcweir 	SvXMLElementExport aChangeInfo(rExport, XML_NAMESPACE_OFFICE,
475cdf0e10cSrcweir 								   XML_CHANGE_INFO, sal_True, sal_True);
476cdf0e10cSrcweir 
477cdf0e10cSrcweir 	Any aAny = rPropSet->getPropertyValue(sRedlineAuthor);
478cdf0e10cSrcweir 	OUString sTmp;
479cdf0e10cSrcweir 	aAny >>= sTmp;
480cdf0e10cSrcweir 	if (sTmp.getLength() > 0)
481cdf0e10cSrcweir 	{
482cdf0e10cSrcweir 		SvXMLElementExport aCreatorElem( rExport, XML_NAMESPACE_DC,
483cdf0e10cSrcweir 										  XML_CREATOR, sal_True,
484cdf0e10cSrcweir 										  sal_False );
485cdf0e10cSrcweir 		rExport.Characters(sTmp);
486cdf0e10cSrcweir 	}
487cdf0e10cSrcweir 
488cdf0e10cSrcweir 	aAny = rPropSet->getPropertyValue(sRedlineDateTime);
489cdf0e10cSrcweir 	util::DateTime aDateTime;
490cdf0e10cSrcweir 	aAny >>= aDateTime;
491cdf0e10cSrcweir 	{
492cdf0e10cSrcweir 		OUStringBuffer sBuf;
493cdf0e10cSrcweir 		rExport.GetMM100UnitConverter().convertDateTime(sBuf, aDateTime);
494cdf0e10cSrcweir 		SvXMLElementExport aDateElem( rExport, XML_NAMESPACE_DC,
495cdf0e10cSrcweir 										  XML_DATE, sal_True,
496cdf0e10cSrcweir 										  sal_False );
497cdf0e10cSrcweir 		rExport.Characters(sBuf.makeStringAndClear());
498cdf0e10cSrcweir 	}
499cdf0e10cSrcweir 
500cdf0e10cSrcweir 	// comment as <text:p> sequence
501cdf0e10cSrcweir 	aAny = rPropSet->getPropertyValue(sRedlineComment);
502cdf0e10cSrcweir 	aAny >>= sTmp;
503cdf0e10cSrcweir     WriteComment( sTmp );
504cdf0e10cSrcweir }
505cdf0e10cSrcweir 
ExportChangeInfo(const Sequence<PropertyValue> & rPropertyValues)506cdf0e10cSrcweir void XMLRedlineExport::ExportChangeInfo(
507cdf0e10cSrcweir 	const Sequence<PropertyValue> & rPropertyValues)
508cdf0e10cSrcweir {
509cdf0e10cSrcweir     OUString sComment;
510cdf0e10cSrcweir 
511cdf0e10cSrcweir 	sal_Int32 nCount = rPropertyValues.getLength();
512cdf0e10cSrcweir 	for(sal_Int32 i = 0; i < nCount; i++)
513cdf0e10cSrcweir 	{
514cdf0e10cSrcweir 		const PropertyValue& rVal = rPropertyValues[i];
515cdf0e10cSrcweir 
516cdf0e10cSrcweir 		if( rVal.Name.equals(sRedlineAuthor) )
517cdf0e10cSrcweir 		{
518cdf0e10cSrcweir 			OUString sTmp;
519cdf0e10cSrcweir 			rVal.Value >>= sTmp;
520cdf0e10cSrcweir 			if (sTmp.getLength() > 0)
521cdf0e10cSrcweir 			{
522cdf0e10cSrcweir 				rExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_CHG_AUTHOR, sTmp);
523cdf0e10cSrcweir 			}
524cdf0e10cSrcweir 		}
525cdf0e10cSrcweir         else if( rVal.Name.equals(sRedlineComment) )
526cdf0e10cSrcweir         {
527cdf0e10cSrcweir             rVal.Value >>= sComment;
528cdf0e10cSrcweir         }
529cdf0e10cSrcweir 		else if( rVal.Name.equals(sRedlineDateTime) )
530cdf0e10cSrcweir 		{
531cdf0e10cSrcweir 			util::DateTime aDateTime;
532cdf0e10cSrcweir 			rVal.Value >>= aDateTime;
533cdf0e10cSrcweir 			OUStringBuffer sBuf;
534cdf0e10cSrcweir 			rExport.GetMM100UnitConverter().convertDateTime(sBuf, aDateTime);
535cdf0e10cSrcweir 			rExport.AddAttribute(XML_NAMESPACE_OFFICE, XML_CHG_DATE_TIME,
536cdf0e10cSrcweir 								 sBuf.makeStringAndClear());
537cdf0e10cSrcweir 		}
538cdf0e10cSrcweir 		else if( rVal.Name.equals(sRedlineType) )
539cdf0e10cSrcweir 		{
540cdf0e10cSrcweir 			// check if this is an insertion; cf. comment at calling location
541cdf0e10cSrcweir 			OUString sTmp;
542cdf0e10cSrcweir 			rVal.Value >>= sTmp;
543cdf0e10cSrcweir 			DBG_ASSERT(sTmp.equals(sInsert),
544cdf0e10cSrcweir 					   "hierarchical change must be insertion");
545cdf0e10cSrcweir 		}
546cdf0e10cSrcweir 		// else: unknown value -> ignore
547cdf0e10cSrcweir 	}
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 	// finally write element
550cdf0e10cSrcweir 	SvXMLElementExport aChangeInfo(rExport, XML_NAMESPACE_OFFICE,
551cdf0e10cSrcweir 								   XML_CHANGE_INFO, sal_True, sal_True);
552cdf0e10cSrcweir 
553cdf0e10cSrcweir     WriteComment( sComment );
554cdf0e10cSrcweir }
555cdf0e10cSrcweir 
ExportStartOrEndRedline(const Reference<XPropertySet> & rPropSet,sal_Bool bStart)556cdf0e10cSrcweir void XMLRedlineExport::ExportStartOrEndRedline(
557cdf0e10cSrcweir 	const Reference<XPropertySet> & rPropSet,
558cdf0e10cSrcweir 	sal_Bool bStart)
559cdf0e10cSrcweir {
560cdf0e10cSrcweir     if( ! rPropSet.is() )
561cdf0e10cSrcweir         return;
562cdf0e10cSrcweir 
563cdf0e10cSrcweir 	// get appropriate (start or end) property
564cdf0e10cSrcweir 	Any aAny;
565cdf0e10cSrcweir     try
566cdf0e10cSrcweir     {
567cdf0e10cSrcweir         aAny = rPropSet->getPropertyValue(bStart ? sStartRedline : sEndRedline);
568cdf0e10cSrcweir     }
569cdf0e10cSrcweir     catch( UnknownPropertyException e )
570cdf0e10cSrcweir     {
571cdf0e10cSrcweir         // If we don't have the property, there's nothing to do.
572cdf0e10cSrcweir         return;
573cdf0e10cSrcweir     }
574cdf0e10cSrcweir 
575cdf0e10cSrcweir 	Sequence<PropertyValue> aValues;
576cdf0e10cSrcweir 	aAny >>= aValues;
577cdf0e10cSrcweir     const PropertyValue* pValues = aValues.getConstArray();
578cdf0e10cSrcweir 
579cdf0e10cSrcweir 	// seek for redline properties
580cdf0e10cSrcweir     sal_Bool bIsCollapsed = sal_False;
581cdf0e10cSrcweir     sal_Bool bIsStart = sal_True;
582cdf0e10cSrcweir     OUString sId;
583cdf0e10cSrcweir     sal_Bool bIdOK = sal_False; // have we seen an ID?
584cdf0e10cSrcweir 	sal_Int32 nLength = aValues.getLength();
585cdf0e10cSrcweir 	for(sal_Int32 i = 0; i < nLength; i++)
586cdf0e10cSrcweir 	{
587cdf0e10cSrcweir 		if (sRedlineIdentifier.equals(pValues[i].Name))
588cdf0e10cSrcweir 		{
589cdf0e10cSrcweir 			pValues[i].Value >>= sId;
590cdf0e10cSrcweir             bIdOK = sal_True;
591cdf0e10cSrcweir         }
592cdf0e10cSrcweir         else if (sIsCollapsed.equals(pValues[i].Name))
593cdf0e10cSrcweir         {
594cdf0e10cSrcweir             bIsCollapsed = *(sal_Bool*)pValues[i].Value.getValue();
595cdf0e10cSrcweir         }
596cdf0e10cSrcweir         else if (sIsStart.equals(pValues[i].Name))
597cdf0e10cSrcweir         {
598cdf0e10cSrcweir             bIsStart = *(sal_Bool*)pValues[i].Value.getValue();
599cdf0e10cSrcweir         }
600cdf0e10cSrcweir     }
601cdf0e10cSrcweir 
602cdf0e10cSrcweir     if( bIdOK )
603cdf0e10cSrcweir     {
604cdf0e10cSrcweir         DBG_ASSERT( sId.getLength() > 0, "Redlines must have IDs" );
605cdf0e10cSrcweir 
606cdf0e10cSrcweir         // TODO: use GetRedlineID or elimiate that function
607cdf0e10cSrcweir         OUStringBuffer sBuffer(sChangePrefix);
608cdf0e10cSrcweir         sBuffer.append(sId);
609cdf0e10cSrcweir 
610cdf0e10cSrcweir         rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_CHANGE_ID,
611cdf0e10cSrcweir                              sBuffer.makeStringAndClear());
612cdf0e10cSrcweir 
613cdf0e10cSrcweir         // export the element
614cdf0e10cSrcweir         // (whitespace because we're not inside paragraphs)
615cdf0e10cSrcweir         SvXMLElementExport aChangeElem(
616cdf0e10cSrcweir             rExport, XML_NAMESPACE_TEXT,
617cdf0e10cSrcweir             bIsCollapsed ? XML_CHANGE :
618cdf0e10cSrcweir                 ( bIsStart ? XML_CHANGE_START : XML_CHANGE_END ),
619cdf0e10cSrcweir             sal_True, sal_True);
620cdf0e10cSrcweir     }
621cdf0e10cSrcweir }
622cdf0e10cSrcweir 
ExportStartOrEndRedline(const Reference<XTextContent> & rContent,sal_Bool bStart)623cdf0e10cSrcweir void XMLRedlineExport::ExportStartOrEndRedline(
624cdf0e10cSrcweir 	const Reference<XTextContent> & rContent,
625cdf0e10cSrcweir 	sal_Bool bStart)
626cdf0e10cSrcweir {
627cdf0e10cSrcweir 	Reference<XPropertySet> xPropSet(rContent, uno::UNO_QUERY);
628cdf0e10cSrcweir 	if (xPropSet.is())
629cdf0e10cSrcweir 	{
630cdf0e10cSrcweir 		ExportStartOrEndRedline(xPropSet, bStart);
631cdf0e10cSrcweir 	}
632cdf0e10cSrcweir 	else
633cdf0e10cSrcweir 	{
634cdf0e10cSrcweir 		DBG_ERROR("XPropertySet expected");
635cdf0e10cSrcweir 	}
636cdf0e10cSrcweir }
637cdf0e10cSrcweir 
ExportStartOrEndRedline(const Reference<XTextSection> & rSection,sal_Bool bStart)638cdf0e10cSrcweir void XMLRedlineExport::ExportStartOrEndRedline(
639cdf0e10cSrcweir 	const Reference<XTextSection> & rSection,
640cdf0e10cSrcweir 	sal_Bool bStart)
641cdf0e10cSrcweir {
642cdf0e10cSrcweir 	Reference<XPropertySet> xPropSet(rSection, uno::UNO_QUERY);
643cdf0e10cSrcweir 	if (xPropSet.is())
644cdf0e10cSrcweir 	{
645cdf0e10cSrcweir 		ExportStartOrEndRedline(xPropSet, bStart);
646cdf0e10cSrcweir 	}
647cdf0e10cSrcweir 	else
648cdf0e10cSrcweir 	{
649cdf0e10cSrcweir 		DBG_ERROR("XPropertySet expected");
650cdf0e10cSrcweir 	}
651cdf0e10cSrcweir }
652cdf0e10cSrcweir 
WriteComment(const OUString & rComment)653cdf0e10cSrcweir void XMLRedlineExport::WriteComment(const OUString& rComment)
654cdf0e10cSrcweir {
655cdf0e10cSrcweir 	if (rComment.getLength() > 0)
656cdf0e10cSrcweir 	{
657cdf0e10cSrcweir 		// iterate over all string-pieces separated by return (0x0a) and
658cdf0e10cSrcweir 		// put each inside a paragraph element.
659cdf0e10cSrcweir 		SvXMLTokenEnumerator aEnumerator(rComment, sal_Char(0x0a));
660cdf0e10cSrcweir 		OUString aSubString;
661cdf0e10cSrcweir 		while (aEnumerator.getNextToken(aSubString))
662cdf0e10cSrcweir 		{
663cdf0e10cSrcweir 			SvXMLElementExport aParagraph(
664cdf0e10cSrcweir 				rExport, XML_NAMESPACE_TEXT, XML_P, sal_True, sal_False);
665cdf0e10cSrcweir 			rExport.Characters(aSubString);
666cdf0e10cSrcweir 		}
667cdf0e10cSrcweir 	}
668cdf0e10cSrcweir }
669