LibreOffice Module xmloff (master) 1
XMLChangedRegionImportContext.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
22#include <com/sun/star/uno/Reference.h>
23#include <com/sun/star/util/DateTime.hpp>
24#include <com/sun/star/text/XTextCursor.hpp>
25
26#include <sal/log.hxx>
28
29#include <xmloff/xmlimp.hxx>
32#include <xmloff/xmltoken.hxx>
33
34
35using namespace ::xmloff::token;
36
37using ::com::sun::star::uno::Reference;
38using ::com::sun::star::text::XTextCursor;
39using namespace ::com::sun::star;
40
41
43 SvXMLImportContext(rImport),
44 bMergeLastPara(true)
45{
46}
47
49{
50}
51
53 sal_Int32 /*nElement*/,
54 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
55{
56 // process attributes: id
57 bool bHaveXmlId( false );
58 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
59 {
60 switch(aIter.getToken())
61 {
62 case XML_ELEMENT(XML, XML_ID):
63 {
64 sID = aIter.toString();
65 bHaveXmlId = true;
66 break;
67 }
68 case XML_ELEMENT(TEXT, XML_ID):
69 {
70 if (!bHaveXmlId) { sID = aIter.toString(); }
71 break;
72 }
74 {
75 bool bTmp(false);
76 if (::sax::Converter::convertBool(bTmp, aIter.toView()))
77 {
78 bMergeLastPara = bTmp;
79 }
80 break;
81 }
82 default:
83 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
84 }
85 }
86}
87
88css::uno::Reference< css::xml::sax::XFastContextHandler > XMLChangedRegionImportContext::createFastChildContext(
89 sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
90{
91 SvXMLImportContextRef xContext;
92
93 // from the ODF 1.2 standard :
94 // The <text:changed-region> element has the following child elements:
95 // <text:deletion>, <text:format-change> and <text:insertion>.
96 if (nElement == XML_ELEMENT(TEXT, XML_INSERTION) ||
97 nElement == XML_ELEMENT(TEXT, XML_DELETION) ||
98 nElement == XML_ELEMENT(TEXT, XML_FORMAT_CHANGE) )
99 {
100 // create XMLChangeElementImportContext for all kinds of changes
101 xContext = new XMLChangeElementImportContext(
102 GetImport(),
103 nElement == XML_ELEMENT(TEXT, XML_DELETION),
104 *this,
105 SvXMLImport::getNameFromToken(nElement));
106 }
107 else
108 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
109
110 // illegal element content! TODO: discard the redlines
111 // for the moment -> use text
112 // or default if text fail
113
114 return xContext;
115}
116
118{
119 // restore old XCursor (if necessary)
120 if (xOldCursor.is())
121 {
122 // delete last paragraph
123 // (one extra paragraph was inserted in the beginning)
124 try
125 {
126 GetImport().GetTextImport()->DeleteParagraph();
127 }
128 catch (uno::Exception const&)
129 { // cursor may be disposed - must reset to old cursor!
130 SAL_INFO("xmloff.text", "XMLChangedRegionImportContext: delete paragraph failed");
131 }
132
133 GetImport().GetTextImport()->SetCursor(xOldCursor);
134 xOldCursor = nullptr;
135 }
136}
137
139 const OUString& rType,
140 const OUString& rAuthor,
141 const OUString& rComment,
142 std::u16string_view rDate)
143{
144 util::DateTime aDateTime;
145 if (::sax::Converter::parseDateTime(aDateTime, rDate))
146 {
147 GetImport().GetTextImport()->RedlineAdd(
148 rType, sID, rAuthor, rComment, aDateTime, bMergeLastPara);
149 }
150}
151
153{
154 // if we haven't already installed the redline cursor, do it now
155 if ( xOldCursor.is())
156 return;
157
158 // get TextImportHelper and old Cursor
159 rtl::Reference<XMLTextImportHelper> rHelper(GetImport().GetTextImport());
160 Reference<XTextCursor> xCursor( rHelper->GetCursor() );
161
162 // create Redline and new Cursor
163 Reference<XTextCursor> xNewCursor =
164 rHelper->RedlineCreateText(xCursor, sID);
165
166 if (xNewCursor.is())
167 {
168 // save old cursor and install new one
169 xOldCursor = xCursor;
170 rHelper->SetCursor( xNewCursor );
171 }
172 // else: leave as is
173}
174
175/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
This class deliberately does not support XWeak, to improve performance when loading large documents.
Definition: xmlictxt.hxx:48
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
Import <text:deletion> and <text:insertion> elements contained in a <text:changed-region> element.
void UseRedlineText()
create redline XText/XTextCursor on demand and register with XMLTextImportHelper
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
css::uno::Reference< css::text::XTextCursor > xOldCursor
if we replace the current XTextCursor/XText by the ones for the redline, we remember the old cursor h...
void SetChangeInfo(const OUString &rType, const OUString &rAuthor, const OUString &rComment, std::u16string_view rDate)
change info: To be called from change-info context
bool bMergeLastPara
merge-last-paragraph flag
static bool parseDateTime(css::util::DateTime &rDateTime, std::u16string_view rString)
static bool convertBool(bool &rBool, std::u16string_view rString)
#define SAL_INFO(area, stream)
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
Handling of tokens in XML:
@ XML_MERGE_LAST_PARAGRAPH
Definition: xmltoken.hxx:2281
TEXT
#define XMLOFF_WARN_UNKNOWN_ELEMENT(area, token)
Definition: xmlictxt.hxx:120
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:114
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97