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 <sax/tools/converter.hxx>
27 
28 #include <xmloff/xmlimp.hxx>
29 #include <xmloff/xmlnamespace.hxx>
30 #include <xmloff/namespacemap.hxx>
31 #include <xmloff/xmltoken.hxx>
32 
33 
34 using namespace ::xmloff::token;
35 
36 using ::com::sun::star::uno::Reference;
37 using ::com::sun::star::text::XTextCursor;
38 using namespace ::com::sun::star;
39 using ::com::sun::star::xml::sax::XAttributeList;
40 
41 
43  SvXMLImport& rImport,
44  sal_uInt16 nPrefix,
45  const OUString& rLocalName) :
46  SvXMLImportContext(rImport, nPrefix, rLocalName),
47  bMergeLastPara(true)
48 {
49 }
50 
52 {
53 }
54 
56  const Reference<XAttributeList> & xAttrList)
57 {
58  // process attributes: id
59  bool bHaveXmlId( false );
60  sal_Int16 nLength = xAttrList->getLength();
61  for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
62  {
63  OUString sLocalName;
64  sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
65  GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
66  &sLocalName );
67 
68  const OUString sValue = xAttrList->getValueByIndex(nAttr);
69  if (XML_NAMESPACE_XML == nPrefix)
70  {
71  if (IsXMLToken(sLocalName, XML_ID))
72  {
73  sID = sValue;
74  bHaveXmlId = true;
75  }
76  }
77  else if (XML_NAMESPACE_TEXT == nPrefix)
78  {
79  if (IsXMLToken(sLocalName, XML_ID))
80  {
81  if (!bHaveXmlId) { sID = sValue; }
82  }
83  else if( IsXMLToken( sLocalName, XML_MERGE_LAST_PARAGRAPH ) )
84  {
85  bool bTmp(false);
86  if (::sax::Converter::convertBool(bTmp, sValue))
87  {
88  bMergeLastPara = bTmp;
89  }
90  }
91  }
92  }
93 }
94 
96  sal_uInt16 nPrefix,
97  const OUString& rLocalName,
98  const Reference<XAttributeList> & /*xAttrList*/)
99 {
100  SvXMLImportContextRef xContext;
101 
102  if (XML_NAMESPACE_TEXT == nPrefix)
103  {
104  // from the ODF 1.2 standard :
105  // The <text:changed-region> element has the following child elements:
106  // <text:deletion>, <text:format-change> and <text:insertion>.
107  if ( IsXMLToken( rLocalName, XML_INSERTION ) ||
108  IsXMLToken( rLocalName, XML_DELETION ) ||
109  IsXMLToken( rLocalName, XML_FORMAT_CHANGE ) )
110  {
111  // create XMLChangeElementImportContext for all kinds of changes
112  xContext = new XMLChangeElementImportContext(
113  GetImport(), nPrefix, rLocalName,
114  IsXMLToken( rLocalName, XML_DELETION ),
115  *this);
116  }
117  // else: it may be a text element, see below
118  }
119 
120  if (!xContext)
121  {
122  // illegal element content! TODO: discard the redlines
123  // for the moment -> use text
124  // or default if text fail
125  }
126 
127  return xContext;
128 }
129 
131 {
132  // restore old XCursor (if necessary)
133  if (xOldCursor.is())
134  {
135  // delete last paragraph
136  // (one extra paragraph was inserted in the beginning)
139  rHelper->DeleteParagraph();
140 
141  GetImport().GetTextImport()->SetCursor(xOldCursor);
142  xOldCursor = nullptr;
143  }
144 }
145 
147  const OUString& rType,
148  const OUString& rAuthor,
149  const OUString& rComment,
150  const OUString& rDate)
151 {
152  util::DateTime aDateTime;
153  if (::sax::Converter::parseDateTime(aDateTime, rDate))
154  {
155  GetImport().GetTextImport()->RedlineAdd(
156  rType, sID, rAuthor, rComment, aDateTime, bMergeLastPara);
157  }
158 }
159 
161 {
162  // if we haven't already installed the redline cursor, do it now
163  if ( xOldCursor.is())
164  return;
165 
166  // get TextImportHelper and old Cursor
167  rtl::Reference<XMLTextImportHelper> rHelper(GetImport().GetTextImport());
168  Reference<XTextCursor> xCursor( rHelper->GetCursor() );
169 
170  // create Redline and new Cursor
171  Reference<XTextCursor> xNewCursor =
172  rHelper->RedlineCreateText(xCursor, sID);
173 
174  if (xNewCursor.is())
175  {
176  // save old cursor and install new one
177  xOldCursor = xCursor;
178  rHelper->SetCursor( xNewCursor );
179  }
180  // else: leave as is
181 }
182 
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3434
Import and elements contained in a element...
static bool convertBool(bool &rBool, const OUString &rString)
XMLChangedRegionImportContext(SvXMLImport &rImport, sal_uInt16 nPrefix, const OUString &rLocalName)
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:402
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
void SetChangeInfo(const OUString &rType, const OUString &rAuthor, const OUString &rComment, const OUString &rDate)
change info: To be called from change-info context
virtual void StartElement(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
StartElement is called after a context has been constructed and before an elements context is parsed...
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
constexpr sal_uInt16 XML_NAMESPACE_XML
static bool parseDateTime(css::util::DateTime &rDateTime, const OUString &rString)
constexpr sal_uInt16 XML_NAMESPACE_TEXT
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
virtual void EndElement() override
EndElement is called before a context will be destructed, but after an elements context has been pars...
Handling of tokens in XML:
sal_Int32 nLength
Definition: xmltoken.cxx:36