summaryrefslogtreecommitdiffstats
path: root/xmloff/source/text/XMLRedlineExport.cxx
blob: 731260c9b37404f7d98faed3f8cea1cddfc54cc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "XMLRedlineExport.hxx"
#include <tools/debug.hxx>
#include <tools/solar.h>
#include <rtl/ustring.hxx>
#include <rtl/ustrbuf.hxx>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/beans/UnknownPropertyException.hpp>
#include <com/sun/star/container/XEnumerationAccess.hpp>

#include <com/sun/star/container/XEnumeration.hpp>
#include <com/sun/star/document/XRedlinesSupplier.hpp>
#include <com/sun/star/text/XText.hpp>
#include <com/sun/star/text/XTextContent.hpp>
#include <com/sun/star/text/XTextSection.hpp>
#include <com/sun/star/util/DateTime.hpp>

#include <sax/tools/converter.hxx>

#include <xmloff/xmltoken.hxx>
#include <xmloff/xmlnmspe.hxx>
#include <xmloff/xmlexp.hxx>
#include <xmloff/xmluconv.hxx>

using namespace ::com::sun::star;
using namespace ::xmloff::token;

using ::com::sun::star::beans::PropertyValue;
using ::com::sun::star::beans::XPropertySet;
using ::com::sun::star::beans::UnknownPropertyException;
using ::com::sun::star::document::XRedlinesSupplier;
using ::com::sun::star::container::XEnumerationAccess;
using ::com::sun::star::container::XEnumeration;
using ::com::sun::star::text::XText;
using ::com::sun::star::text::XTextContent;
using ::com::sun::star::text::XTextSection;
using ::com::sun::star::uno::Any;
using ::com::sun::star::uno::Reference;
using ::com::sun::star::uno::Sequence;
using ::std::list;


XMLRedlineExport::XMLRedlineExport(SvXMLExport& rExp)
:   sDelete("Delete")
,   sDeletion(GetXMLToken(XML_DELETION))
,   sFormat("Format")
,   sFormatChange(GetXMLToken(XML_FORMAT_CHANGE))
,   sInsert("Insert")
,   sInsertion(GetXMLToken(XML_INSERTION))
,   sIsCollapsed("IsCollapsed")
,   sIsStart("IsStart")
,   sRedlineAuthor("RedlineAuthor")
,   sRedlineComment("RedlineComment")
,   sRedlineDateTime("RedlineDateTime")
,   sRedlineSuccessorData("RedlineSuccessorData")
,   sRedlineText("RedlineText")
,   sRedlineType("RedlineType")
,   sRedlineUndoType("RedlineUndoType")
,   sRedlineUndoStart("RedlineUndoStart")
,   sRedlineUndoEnd("RedlineUndoEnd")
,   sUnknownChange("UnknownChange")
,   sStartRedline("StartRedline")
,   sEndRedline("EndRedline")
,   sRedlineIdentifier("RedlineIdentifier")
,   sIsInHeaderFooter("IsInHeaderFooter")
,   sRecordChanges("RecordChanges")
,   sMergeLastPara("MergeLastPara")
,   sChangePrefix("ct")
,   rExport(rExp)
,   pCurrentChangesList(nullptr)
{
}


XMLRedlineExport::~XMLRedlineExport()
{
    // delete changes lists
    for( ChangesMapType::iterator aIter = aChangeMap.begin();
         aIter != aChangeMap.end();
         ++aIter )
    {
        delete aIter->second;
    }
    aChangeMap.clear();
}

void XMLRedlineExport::ExportUndoChange(
    const Reference<XPropertySet> & rPropSet,
    sal_uInt32 nParaIdx,
    bool bAutoStyle)
{
    if (!bAutoStyle)
    {
        ExportUndoChangeInfo(rPropSet, nParaIdx);
    }
}

void XMLRedlineExport::SetCurrentXText(
    const Reference<XText> & rText)
{
    if (rText.is())
    {
        // look for appropriate list in map; use the found one, or create new
        ChangesMapType::iterator aIter = aChangeMap.find(rText);
        if (aIter == aChangeMap.end())
        {
            ChangesListType* pList = new ChangesListType;
            aChangeMap[rText] = pList;
            pCurrentChangesList = pList;
        }
        else
            pCurrentChangesList = aIter->second;
    }
    else
    {
        // don't record changes
        SetCurrentXText();
    }
}

void XMLRedlineExport::SetCurrentXText()
{
    pCurrentChangesList = nullptr;
}

void XMLRedlineExport::ExportUndoChangedRegion(
    const Reference<XPropertySet> & rPropSet, sal_uInt32 nParaIdx)
{
    {
        Any aAny = rPropSet->getPropertyValue(sRedlineType);
        OUString sType;
        aAny >>= sType;

        sal_uInt32 nCharStart, nCharEnd;
        sal_uInt32 nTextDelCount = rExport.GetTextParagraphExport()->getTextDelCount();
        rPropSet->getPropertyValue(sRedlineUndoStart) >>= nCharStart;
        rPropSet->getPropertyValue(sRedlineUndoEnd) >>= nCharEnd;
        nCharStart -= nTextDelCount;
        nCharEnd -= nTextDelCount;

        OUString sUndoType;
        aAny = rPropSet->getPropertyValue(sRedlineUndoType);
        aAny >>= sUndoType;

        if( sUndoType == "paragraph" )
        {
            nParaIdx++;
            rExport.AddAttribute(XML_NAMESPACE_C, XML_START, "/" + rtl::OUString::number(nParaIdx));
            rExport.AddAttribute(XML_NAMESPACE_DC, XML_TYPE, XML_PARAGRAPH);
        }
        else
        {
            rExport.AddAttribute(XML_NAMESPACE_C, XML_START, "/" + rtl::OUString::number(nParaIdx) + "/" + rtl::OUString::number(nCharStart));
            if( sType == sInsert || sType == sFormat )
                rExport.AddAttribute(XML_NAMESPACE_C, XML_END, "/" + rtl::OUString::number(nParaIdx) + "/" + rtl::OUString::number(nCharEnd));
            else
                rExport.GetTextParagraphExport()->setTextDelCount( rExport.GetTextParagraphExport()->getTextDelCount() + nCharEnd - nCharStart + 1 );
            if( sType == sFormat )
                rExport.AddAttribute(XML_NAMESPACE_DC, XML_TYPE, XML_FORMAT_CHANGE);
            else
                rExport.AddAttribute(XML_NAMESPACE_DC, XML_TYPE, XML_TEXT);
        }
        SvXMLElementExport aChange(rExport, XML_NAMESPACE_TEXT,
                                ConvertTypeName(sType), true, true);

        // get XText from the redline and export (if the XText exists)
        aAny = rPropSet->getPropertyValue(sRedlineText);
        Reference<XText> xText;
        aAny >>= xText;
        if (xText.is())
        {
            rExport.GetTextParagraphExport()->exportText(xText);
            // default parameters: bProgress, bExportParagraph ???
        }
    }
}

const OUString XMLRedlineExport::ConvertTypeName(
    const OUString& sApiName)
{
    if (sApiName == sDelete)
    {
        return sInsertion;
    }
    else if (sApiName == sInsert)
    {
        return sDeletion;
    }
    else if (sApiName == sFormat)
    {
        return sFormatChange;
    }
    else
    {
        OSL_FAIL("unknown redline type");
        return sUnknownChange;
    }
}

void XMLRedlineExport::ExportUndoChangeInfo(
    const Reference<XPropertySet> & rPropSet, sal_uInt32 nParaIdx)
{
    Any aAny = rPropSet->getPropertyValue(sIsCollapsed);
    bool bCollapsed = *static_cast<sal_Bool const *>(aAny.getValue());
    aAny = rPropSet->getPropertyValue(sIsStart);
    bool bStart = *static_cast<sal_Bool const *>(aAny.getValue());
    if( bCollapsed || bStart )
    {
        aAny = rPropSet->getPropertyValue(sRedlineAuthor);
        OUString sAuthor;
        aAny >>= sAuthor;
        if (!sAuthor.isEmpty())
        {
            rExport.AddAttribute(XML_NAMESPACE_DC, XML_CREATOR, sAuthor);
        }

        aAny = rPropSet->getPropertyValue(sRedlineDateTime);
        util::DateTime aDateTime;
        aAny >>= aDateTime;
        OUStringBuffer sBuf;
        ::sax::Converter::convertDateTime(sBuf, aDateTime, nullptr);
        rExport.AddAttribute(XML_NAMESPACE_DC, XML_DATE, sBuf.makeStringAndClear());

        SvXMLElementExport aChange(rExport, XML_NAMESPACE_OFFICE,
                                    XML_CHANGE, true, true);
        ExportUndoChangedRegion(rPropSet, nParaIdx);
    }
}

void XMLRedlineExport::ExportStartOrEndRedline(
    const Reference<XPropertySet> & rPropSet,
    bool bStart)
{
    if( ! rPropSet.is() )
        return;

    // get appropriate (start or end) property
    Any aAny;
    try
    {
        aAny = rPropSet->getPropertyValue(bStart ? sStartRedline : sEndRedline);
    }
    catch(const UnknownPropertyException&)
    {
        // If we don't have the property, there's nothing to do.
        return;
    }

    Sequence<PropertyValue> aValues;
    aAny >>= aValues;
    const PropertyValue* pValues = aValues.getConstArray();

    // seek for redline properties
    bool bIsCollapsed = false;
    bool bIsStart = true;
    OUString sId;
    bool bIdOK = false; // have we seen an ID?
    sal_Int32 nLength = aValues.getLength();
    for(sal_Int32 i = 0; i < nLength; i++)
    {
        if (sRedlineIdentifier.equals(pValues[i].Name))
        {
            pValues[i].Value >>= sId;
            bIdOK = true;
        }
        else if (sIsCollapsed.equals(pValues[i].Name))
        {
            bIsCollapsed = *static_cast<sal_Bool const *>(pValues[i].Value.getValue());
        }
        else if (sIsStart.equals(pValues[i].Name))
        {
            bIsStart = *static_cast<sal_Bool const *>(pValues[i].Value.getValue());
        }
    }

    if( bIdOK )
    {
        DBG_ASSERT( !sId.isEmpty(), "Redlines must have IDs" );

        // TODO: use GetRedlineID or eliminate that function
        OUStringBuffer sBuffer(sChangePrefix);
        sBuffer.append(sId);

        rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_CHANGE_ID,
                             sBuffer.makeStringAndClear());

        // export the element
        // (whitespace because we're not inside paragraphs)
        SvXMLElementExport aChangeElem(
            rExport, XML_NAMESPACE_TEXT,
            bIsCollapsed ? XML_CHANGE :
                ( bIsStart ? XML_CHANGE_START : XML_CHANGE_END ),
            true, true);
    }
}

void XMLRedlineExport::ExportStartOrEndRedline(
    const Reference<XTextContent> & rContent,
    bool bStart)
{
    Reference<XPropertySet> xPropSet(rContent, uno::UNO_QUERY);
    if (xPropSet.is())
    {
        ExportStartOrEndRedline(xPropSet, bStart);
    }
    else
    {
        OSL_FAIL("XPropertySet expected");
    }
}

void XMLRedlineExport::ExportStartOrEndRedline(
    const Reference<XTextSection> & rSection,
    bool bStart)
{
    Reference<XPropertySet> xPropSet(rSection, uno::UNO_QUERY);
    if (xPropSet.is())
    {
        ExportStartOrEndRedline(xPropSet, bStart);
    }
    else
    {
        OSL_FAIL("XPropertySet expected");
    }
}

void XMLRedlineExport::WriteComment(const OUString& rComment)
{
    if (!rComment.isEmpty())
    {
        // iterate over all string-pieces separated by return (0x0a) and
        // put each inside a paragraph element.
        SvXMLTokenEnumerator aEnumerator(rComment, sal_Char(0x0a));
        OUString aSubString;
        while (aEnumerator.getNextToken(aSubString))
        {
            SvXMLElementExport aParagraph(
                rExport, XML_NAMESPACE_TEXT, XML_P, true, false);
            rExport.Characters(aSubString);
        }
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */