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/namespacemap.hxx>
32 #include <xmloff/xmltoken.hxx>
35 using namespace ::xmloff::token
;
37 using ::com::sun::star::uno::Reference
;
38 using ::com::sun::star::text::XTextCursor
;
39 using namespace ::com::sun::star
;
40 using ::com::sun::star::xml::sax::XAttributeList
;
43 XMLChangedRegionImportContext::XMLChangedRegionImportContext(SvXMLImport
& rImport
) :
44 SvXMLImportContext(rImport
),
49 XMLChangedRegionImportContext::~XMLChangedRegionImportContext()
53 void XMLChangedRegionImportContext::startFastElement(
54 sal_Int32
/*nElement*/,
55 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
57 // process attributes: id
58 bool bHaveXmlId( false );
59 for( auto& aIter
: sax_fastparser::castToFastAttributeList(xAttrList
) )
61 switch(aIter
.getToken())
63 case XML_ELEMENT(XML
, XML_ID
):
65 sID
= aIter
.toString();
69 case XML_ELEMENT(TEXT
, XML_ID
):
71 if (!bHaveXmlId
) { sID
= aIter
.toString(); }
74 case XML_ELEMENT(TEXT
, XML_MERGE_LAST_PARAGRAPH
):
77 if (::sax::Converter::convertBool(bTmp
, aIter
.toView()))
79 bMergeLastPara
= bTmp
;
84 XMLOFF_WARN_UNKNOWN("xmloff", aIter
);
89 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLChangedRegionImportContext::createFastChildContext(
90 sal_Int32 nElement
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& )
92 SvXMLImportContextRef xContext
;
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
) )
101 // create XMLChangeElementImportContext for all kinds of changes
102 xContext
= new XMLChangeElementImportContext(
104 nElement
== XML_ELEMENT(TEXT
, XML_DELETION
),
106 SvXMLImport::getNameFromToken(nElement
));
109 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement
);
111 // illegal element content! TODO: discard the redlines
112 // for the moment -> use text
113 // or default if text fail
118 void XMLChangedRegionImportContext::endFastElement(sal_Int32
)
120 // restore old XCursor (if necessary)
123 // delete last paragraph
124 // (one extra paragraph was inserted in the beginning)
125 rtl::Reference
<XMLTextImportHelper
> rHelper
=
126 GetImport().GetTextImport();
127 rHelper
->DeleteParagraph();
129 GetImport().GetTextImport()->SetCursor(xOldCursor
);
130 xOldCursor
= nullptr;
134 void XMLChangedRegionImportContext::SetChangeInfo(
135 const OUString
& rType
,
136 const OUString
& rAuthor
,
137 const OUString
& rComment
,
138 std::u16string_view rDate
)
140 util::DateTime aDateTime
;
141 if (::sax::Converter::parseDateTime(aDateTime
, rDate
))
143 GetImport().GetTextImport()->RedlineAdd(
144 rType
, sID
, rAuthor
, rComment
, aDateTime
, bMergeLastPara
);
148 void XMLChangedRegionImportContext::UseRedlineText()
150 // if we haven't already installed the redline cursor, do it now
151 if ( xOldCursor
.is())
154 // get TextImportHelper and old Cursor
155 rtl::Reference
<XMLTextImportHelper
> rHelper(GetImport().GetTextImport());
156 Reference
<XTextCursor
> xCursor( rHelper
->GetCursor() );
158 // create Redline and new Cursor
159 Reference
<XTextCursor
> xNewCursor
=
160 rHelper
->RedlineCreateText(xCursor
, sID
);
164 // save old cursor and install new one
165 xOldCursor
= xCursor
;
166 rHelper
->SetCursor( xNewCursor
);
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */