1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "XMLChangedRegionImportContext.hxx"
21 #include "XMLChangeElementImportContext.hxx"
22 #include <com/sun/star/uno/Reference.h>
23 #include <com/sun/star/util/DateTime.hpp>
24 #include <com/sun/star/text/XTextCursor.hpp>
26 #include <sal/log.hxx>
27 #include <sax/tools/converter.hxx>
29 #include <xmloff/xmlimp.hxx>
30 #include <xmloff/xmlnamespace.hxx>
31 #include <xmloff/xmltoken.hxx>
34 using namespace ::xmloff::token
;
36 using ::com::sun::star::uno::Reference
;
37 using ::com::sun::star::text::XTextCursor
;
38 using namespace ::com::sun::star
;
41 XMLChangedRegionImportContext::XMLChangedRegionImportContext(SvXMLImport
& rImport
) :
42 SvXMLImportContext(rImport
),
47 XMLChangedRegionImportContext::~XMLChangedRegionImportContext()
51 void XMLChangedRegionImportContext::startFastElement(
52 sal_Int32
/*nElement*/,
53 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
55 // process attributes: id
56 bool bHaveXmlId( false );
57 for( auto& aIter
: sax_fastparser::castToFastAttributeList(xAttrList
) )
59 switch(aIter
.getToken())
61 case XML_ELEMENT(XML
, XML_ID
):
63 sID
= aIter
.toString();
67 case XML_ELEMENT(TEXT
, XML_ID
):
69 if (!bHaveXmlId
) { sID
= aIter
.toString(); }
72 case XML_ELEMENT(TEXT
, XML_MERGE_LAST_PARAGRAPH
):
75 if (::sax::Converter::convertBool(bTmp
, aIter
.toView()))
77 bMergeLastPara
= bTmp
;
82 XMLOFF_WARN_UNKNOWN("xmloff", aIter
);
87 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLChangedRegionImportContext::createFastChildContext(
88 sal_Int32 nElement
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& )
90 SvXMLImportContextRef xContext
;
92 // from the ODF 1.2 standard :
93 // The <text:changed-region> element has the following child elements:
94 // <text:deletion>, <text:format-change> and <text:insertion>.
95 if (nElement
== XML_ELEMENT(TEXT
, XML_INSERTION
) ||
96 nElement
== XML_ELEMENT(TEXT
, XML_DELETION
) ||
97 nElement
== XML_ELEMENT(TEXT
, XML_FORMAT_CHANGE
) )
99 // create XMLChangeElementImportContext for all kinds of changes
100 xContext
= new XMLChangeElementImportContext(
102 nElement
== XML_ELEMENT(TEXT
, XML_DELETION
),
104 SvXMLImport::getNameFromToken(nElement
));
107 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement
);
109 // illegal element content! TODO: discard the redlines
110 // for the moment -> use text
111 // or default if text fail
116 void XMLChangedRegionImportContext::endFastElement(sal_Int32
)
118 // restore old XCursor (if necessary)
121 // delete last paragraph
122 // (one extra paragraph was inserted in the beginning)
125 GetImport().GetTextImport()->DeleteParagraph();
127 catch (uno::Exception
const&)
128 { // cursor may be disposed - must reset to old cursor!
129 SAL_INFO("xmloff.text", "XMLChangedRegionImportContext: delete paragraph failed");
132 GetImport().GetTextImport()->SetCursor(xOldCursor
);
133 xOldCursor
= nullptr;
137 void XMLChangedRegionImportContext::SetChangeInfo(
138 const OUString
& rType
,
139 const OUString
& rAuthor
,
140 const OUString
& rComment
,
141 std::u16string_view rDate
,
142 const OUString
& rMovedID
)
144 util::DateTime aDateTime
;
145 if (::sax::Converter::parseDateTime(aDateTime
, rDate
))
147 GetImport().GetTextImport()->RedlineAdd(
148 rType
, sID
, rAuthor
, rComment
, aDateTime
, rMovedID
, bMergeLastPara
);
152 void XMLChangedRegionImportContext::UseRedlineText()
154 // if we haven't already installed the redline cursor, do it now
155 if ( xOldCursor
.is())
158 // get TextImportHelper and old Cursor
159 rtl::Reference
<XMLTextImportHelper
> rHelper(GetImport().GetTextImport());
160 Reference
<XTextCursor
> xCursor( rHelper
->GetCursor() );
162 // create Redline and new Cursor
163 Reference
<XTextCursor
> xNewCursor
=
164 rHelper
->RedlineCreateText(xCursor
, sID
);
168 // save old cursor and install new one
169 xOldCursor
= std::move(xCursor
);
170 rHelper
->SetCursor( xNewCursor
);
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */