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;
40using ::com::sun::star::xml::sax::XAttributeList;
41
42
44 SvXMLImportContext(rImport),
45 bMergeLastPara(true)
46{
47}
48
50{
51}
52
54 sal_Int32 /*nElement*/,
55 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
56{
57 // process attributes: id
58 bool bHaveXmlId( false );
59 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
60 {
61 switch(aIter.getToken())
62 {
63 case XML_ELEMENT(XML, XML_ID):
64 {
65 sID = aIter.toString();
66 bHaveXmlId = true;
67 break;
68 }
69 case XML_ELEMENT(TEXT, XML_ID):
70 {
71 if (!bHaveXmlId) { sID = aIter.toString(); }
72 break;
73 }
75 {
76 bool bTmp(false);
77 if (::sax::Converter::convertBool(bTmp, aIter.toView()))
78 {
79 bMergeLastPara = bTmp;
80 }
81 break;
82 }
83 default:
84 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
85 }
86 }
87}
88
89css::uno::Reference< css::xml::sax::XFastContextHandler > XMLChangedRegionImportContext::createFastChildContext(
90 sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& )
91{
92 SvXMLImportContextRef xContext;
93
94 // from the ODF 1.2 standard :
95 // The <text:changed-region> element has the following child elements:
96 // <text:deletion>, <text:format-change> and <text:insertion>.
97 if (nElement == XML_ELEMENT(TEXT, XML_INSERTION) ||
98 nElement == XML_ELEMENT(TEXT, XML_DELETION) ||
99 nElement == XML_ELEMENT(TEXT, XML_FORMAT_CHANGE) )
100 {
101 // create XMLChangeElementImportContext for all kinds of changes
102 xContext = new XMLChangeElementImportContext(
103 GetImport(),
104 nElement == XML_ELEMENT(TEXT, XML_DELETION),
105 *this,
106 SvXMLImport::getNameFromToken(nElement));
107 }
108 else
109 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
110
111 // illegal element content! TODO: discard the redlines
112 // for the moment -> use text
113 // or default if text fail
114
115 return xContext;
116}
117
119{
120 // restore old XCursor (if necessary)
121 if (xOldCursor.is())
122 {
123 // delete last paragraph
124 // (one extra paragraph was inserted in the beginning)
125 try
126 {
127 GetImport().GetTextImport()->DeleteParagraph();
128 }
129 catch (uno::Exception const&)
130 { // cursor may be disposed - must reset to old cursor!
131 SAL_INFO("xmloff.text", "XMLChangedRegionImportContext: delete paragraph failed");
132 }
133
134 GetImport().GetTextImport()->SetCursor(xOldCursor);
135 xOldCursor = nullptr;
136 }
137}
138
140 const OUString& rType,
141 const OUString& rAuthor,
142 const OUString& rComment,
143 std::u16string_view rDate)
144{
145 util::DateTime aDateTime;
146 if (::sax::Converter::parseDateTime(aDateTime, rDate))
147 {
148 GetImport().GetTextImport()->RedlineAdd(
149 rType, sID, rAuthor, rComment, aDateTime, bMergeLastPara);
150 }
151}
152
154{
155 // if we haven't already installed the redline cursor, do it now
156 if ( xOldCursor.is())
157 return;
158
159 // get TextImportHelper and old Cursor
160 rtl::Reference<XMLTextImportHelper> rHelper(GetImport().GetTextImport());
161 Reference<XTextCursor> xCursor( rHelper->GetCursor() );
162
163 // create Redline and new Cursor
164 Reference<XTextCursor> xNewCursor =
165 rHelper->RedlineCreateText(xCursor, sID);
166
167 if (xNewCursor.is())
168 {
169 // save old cursor and install new one
170 xOldCursor = xCursor;
171 rHelper->SetCursor( xNewCursor );
172 }
173 // else: leave as is
174}
175
176/* 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:2276
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