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 <sax/tools/converter.hxx>
28 #include <xmloff/xmlimp.hxx>
29 #include <xmloff/xmlnmspe.hxx>
30 #include <xmloff/nmspmap.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
;
39 using ::com::sun::star::xml::sax::XAttributeList
;
43 TYPEINIT1(XMLChangedRegionImportContext
, SvXMLImportContext
);
45 XMLChangedRegionImportContext::XMLChangedRegionImportContext(
48 const OUString
& rLocalName
) :
49 SvXMLImportContext(rImport
, nPrefix
, rLocalName
),
54 XMLChangedRegionImportContext::~XMLChangedRegionImportContext()
58 void XMLChangedRegionImportContext::StartElement(
59 const Reference
<XAttributeList
> & xAttrList
)
61 // process attributes: id
62 bool bHaveXmlId( false );
63 sal_Int16 nLength
= xAttrList
->getLength();
64 for(sal_Int16 nAttr
= 0; nAttr
< nLength
; nAttr
++)
67 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
68 GetKeyByAttrName( xAttrList
->getNameByIndex(nAttr
),
71 const OUString sValue
= xAttrList
->getValueByIndex(nAttr
);
72 if (XML_NAMESPACE_XML
== nPrefix
)
74 if (IsXMLToken(sLocalName
, XML_ID
))
80 else if (XML_NAMESPACE_TEXT
== nPrefix
)
82 if (IsXMLToken(sLocalName
, XML_ID
))
84 if (!bHaveXmlId
) { sID
= sValue
; }
86 else if( IsXMLToken( sLocalName
, XML_MERGE_LAST_PARAGRAPH
) )
89 if (::sax::Converter::convertBool(bTmp
, sValue
))
91 bMergeLastPara
= bTmp
;
98 SvXMLImportContext
* XMLChangedRegionImportContext::CreateChildContext(
100 const OUString
& rLocalName
,
101 const Reference
<XAttributeList
> & xAttrList
)
103 SvXMLImportContext
* pContext
= NULL
;
105 if (XML_NAMESPACE_TEXT
== nPrefix
)
107 // from the ODF 1.2 standard :
108 // The <text:changed-region> element has the following child elements:
109 // <text:deletion>, <text:format-change> and <text:insertion>.
110 if ( IsXMLToken( rLocalName
, XML_INSERTION
) ||
111 IsXMLToken( rLocalName
, XML_DELETION
) ||
112 IsXMLToken( rLocalName
, XML_FORMAT_CHANGE
) )
114 // create XMLChangeElementImportContext for all kinds of changes
115 pContext
= new XMLChangeElementImportContext(
116 GetImport(), nPrefix
, rLocalName
,
117 IsXMLToken( rLocalName
, XML_DELETION
),
120 // else: it may be a text element, see below
123 if (NULL
== pContext
)
125 // illegal element content! TODO: discard the redlines
126 // for the moment -> use text
128 pContext
= SvXMLImportContext::CreateChildContext(nPrefix
, rLocalName
,
131 // or default if text fail
132 if (NULL
== pContext
)
134 pContext
= SvXMLImportContext::CreateChildContext(
135 nPrefix
, rLocalName
, xAttrList
);
142 void XMLChangedRegionImportContext::EndElement()
144 // restore old XCursor (if necessary)
147 // delete last paragraph
148 // (one extra paragraph was inserted in the beginning)
149 rtl::Reference
<XMLTextImportHelper
> rHelper
=
150 GetImport().GetTextImport();
151 rHelper
->DeleteParagraph();
153 GetImport().GetTextImport()->SetCursor(xOldCursor
);
158 void XMLChangedRegionImportContext::SetChangeInfo(
159 const OUString
& rType
,
160 const OUString
& rAuthor
,
161 const OUString
& rComment
,
162 const OUString
& rDate
)
164 util::DateTime aDateTime
;
165 if (::sax::Converter::parseDateTime(aDateTime
, 0, rDate
))
167 GetImport().GetTextImport()->RedlineAdd(
168 rType
, sID
, rAuthor
, rComment
, aDateTime
, bMergeLastPara
);
172 void XMLChangedRegionImportContext::UseRedlineText()
174 // if we haven't already installed the redline cursor, do it now
175 if (! xOldCursor
.is())
177 // get TextImportHelper and old Cursor
178 rtl::Reference
<XMLTextImportHelper
> rHelper(GetImport().GetTextImport());
179 Reference
<XTextCursor
> xCursor( rHelper
->GetCursor() );
181 // create Redline and new Cursor
182 Reference
<XTextCursor
> xNewCursor
=
183 rHelper
->RedlineCreateText(xCursor
, sID
);
187 // save old cursor and install new one
188 xOldCursor
= xCursor
;
189 rHelper
->SetCursor( xNewCursor
);
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */