Line data Source code
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 :
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>
25 :
26 : #include <sax/tools/converter.hxx>
27 :
28 : #include <xmloff/xmlimp.hxx>
29 : #include "xmloff/xmlnmspe.hxx"
30 : #include <xmloff/nmspmap.hxx>
31 : #include <xmloff/xmltoken.hxx>
32 :
33 :
34 : using namespace ::xmloff::token;
35 :
36 : using ::rtl::OUString;
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 :
43 :
44 0 : TYPEINIT1(XMLChangedRegionImportContext, SvXMLImportContext);
45 :
46 0 : XMLChangedRegionImportContext::XMLChangedRegionImportContext(
47 : SvXMLImport& rImport,
48 : sal_uInt16 nPrefix,
49 : const OUString& rLocalName) :
50 : SvXMLImportContext(rImport, nPrefix, rLocalName),
51 0 : bMergeLastPara(sal_True)
52 : {
53 0 : }
54 :
55 0 : XMLChangedRegionImportContext::~XMLChangedRegionImportContext()
56 : {
57 0 : }
58 :
59 0 : void XMLChangedRegionImportContext::StartElement(
60 : const Reference<XAttributeList> & xAttrList)
61 : {
62 : // process attributes: id
63 0 : bool bHaveXmlId( false );
64 0 : sal_Int16 nLength = xAttrList->getLength();
65 0 : for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
66 : {
67 0 : OUString sLocalName;
68 0 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
69 0 : GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
70 0 : &sLocalName );
71 :
72 0 : const OUString sValue = xAttrList->getValueByIndex(nAttr);
73 0 : if (XML_NAMESPACE_XML == nPrefix)
74 : {
75 0 : if (IsXMLToken(sLocalName, XML_ID))
76 : {
77 0 : sID = sValue;
78 0 : bHaveXmlId = true;
79 : }
80 : }
81 0 : else if (XML_NAMESPACE_TEXT == nPrefix)
82 : {
83 0 : if (IsXMLToken(sLocalName, XML_ID))
84 : {
85 0 : if (!bHaveXmlId) { sID = sValue; }
86 : }
87 0 : else if( IsXMLToken( sLocalName, XML_MERGE_LAST_PARAGRAPH ) )
88 : {
89 0 : bool bTmp(false);
90 0 : if (::sax::Converter::convertBool(bTmp, sValue))
91 : {
92 0 : bMergeLastPara = bTmp;
93 : }
94 : }
95 : }
96 0 : }
97 0 : }
98 :
99 0 : SvXMLImportContext* XMLChangedRegionImportContext::CreateChildContext(
100 : sal_uInt16 nPrefix,
101 : const OUString& rLocalName,
102 : const Reference<XAttributeList> & xAttrList)
103 : {
104 0 : SvXMLImportContext* pContext = NULL;
105 :
106 0 : if (XML_NAMESPACE_TEXT == nPrefix)
107 : {
108 0 : if ( IsXMLToken( rLocalName, XML_INSERTION ) ||
109 0 : IsXMLToken( rLocalName, XML_DELETION ) ||
110 0 : IsXMLToken( rLocalName, XML_FORMAT_CHANGE ) )
111 : {
112 : // create XMLChangeElementImportContext for all kinds of changes
113 : pContext = new XMLChangeElementImportContext(
114 0 : GetImport(), nPrefix, rLocalName,
115 0 : IsXMLToken( rLocalName, XML_DELETION ),
116 0 : *this);
117 : }
118 : // else: it may be a text element, see below
119 : }
120 :
121 0 : if (NULL == pContext)
122 : {
123 : pContext = SvXMLImportContext::CreateChildContext(nPrefix, rLocalName,
124 0 : xAttrList);
125 :
126 : // was it a text element? If not, use default!
127 0 : if (NULL == pContext)
128 : {
129 : pContext = SvXMLImportContext::CreateChildContext(
130 0 : nPrefix, rLocalName, xAttrList);
131 : }
132 : }
133 :
134 0 : return pContext;
135 : }
136 :
137 0 : void XMLChangedRegionImportContext::EndElement()
138 : {
139 : // restore old XCursor (if necessary)
140 0 : if (xOldCursor.is())
141 : {
142 : // delete last paragraph
143 : // (one extra paragraph was inserted in the beginning)
144 : UniReference<XMLTextImportHelper> rHelper =
145 0 : GetImport().GetTextImport();
146 0 : rHelper->DeleteParagraph();
147 :
148 0 : GetImport().GetTextImport()->SetCursor(xOldCursor);
149 0 : xOldCursor = NULL;
150 : }
151 0 : }
152 :
153 0 : void XMLChangedRegionImportContext::SetChangeInfo(
154 : const OUString& rType,
155 : const OUString& rAuthor,
156 : const OUString& rComment,
157 : const OUString& rDate)
158 : {
159 0 : util::DateTime aDateTime;
160 0 : if (::sax::Converter::convertDateTime(aDateTime, rDate))
161 : {
162 0 : GetImport().GetTextImport()->RedlineAdd(
163 0 : rType, sID, rAuthor, rComment, aDateTime, bMergeLastPara);
164 : }
165 0 : }
166 :
167 0 : void XMLChangedRegionImportContext::UseRedlineText()
168 : {
169 : // if we haven't already installed the redline cursor, do it now
170 0 : if (! xOldCursor.is())
171 : {
172 : // get TextImportHelper and old Cursor
173 0 : UniReference<XMLTextImportHelper> rHelper(GetImport().GetTextImport());
174 0 : Reference<XTextCursor> xCursor( rHelper->GetCursor() );
175 :
176 : // create Redline and new Cursor
177 : Reference<XTextCursor> xNewCursor =
178 0 : rHelper->RedlineCreateText(xCursor, sID);
179 :
180 0 : if (xNewCursor.is())
181 : {
182 : // save old cursor and install new one
183 0 : xOldCursor = xCursor;
184 0 : rHelper->SetCursor( xNewCursor );
185 0 : }
186 : // else: leave as is
187 : }
188 0 : }
189 :
190 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|