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>
27 #include <sax/tools/converter.hxx>
28 
29 #include <xmloff/xmlimp.hxx>
30 #include <xmloff/xmlnamespace.hxx>
31 #include <xmloff/namespacemap.hxx>
32 #include <xmloff/xmltoken.hxx>
33 
34 
35 using namespace ::xmloff::token;
36 
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;
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 
89 css::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,
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)
127  rHelper->DeleteParagraph();
128 
129  GetImport().GetTextImport()->SetCursor(xOldCursor);
130  xOldCursor = nullptr;
131  }
132 }
133 
135  const OUString& rType,
136  const OUString& rAuthor,
137  const OUString& rComment,
138  std::u16string_view rDate)
139 {
140  util::DateTime aDateTime;
141  if (::sax::Converter::parseDateTime(aDateTime, rDate))
142  {
143  GetImport().GetTextImport()->RedlineAdd(
144  rType, sID, rAuthor, rComment, aDateTime, bMergeLastPara);
145  }
146 }
147 
149 {
150  // if we haven't already installed the redline cursor, do it now
151  if ( xOldCursor.is())
152  return;
153 
154  // get TextImportHelper and old Cursor
155  rtl::Reference<XMLTextImportHelper> rHelper(GetImport().GetTextImport());
156  Reference<XTextCursor> xCursor( rHelper->GetCursor() );
157 
158  // create Redline and new Cursor
159  Reference<XTextCursor> xNewCursor =
160  rHelper->RedlineCreateText(xCursor, sID);
161 
162  if (xNewCursor.is())
163  {
164  // save old cursor and install new one
165  xOldCursor = xCursor;
166  rHelper->SetCursor( xNewCursor );
167  }
168  // else: leave as is
169 }
170 
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Import and elements contained in a element...
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:59
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
static bool parseDateTime(css::util::DateTime &rDateTime, std::u16string_view rString)
rtl::Reference< XMLTextImportHelper > const & GetTextImport()
Definition: xmlimp.hxx:600
void UseRedlineText()
create redline XText/XTextCursor on demand and register with XMLTextImportHelper
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...
bool bMergeLastPara
merge-last-paragraph flag
#define XMLOFF_WARN_UNKNOWN(area, rIter)
Definition: xmlictxt.hxx:113
void SetChangeInfo(const OUString &rType, const OUString &rAuthor, const OUString &rComment, std::u16string_view rDate)
change info: To be called from change-info context
static bool convertBool(bool &rBool, std::u16string_view rString)
static const OUString & getNameFromToken(sal_Int32 nToken)
Definition: xmlimp.cxx:1920
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
Handling of tokens in XML:
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97
#define XMLOFF_WARN_UNKNOWN_ELEMENT(area, token)
Definition: xmlictxt.hxx:119
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 ...
TEXT