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