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 "XMLFootnoteImportContext.hxx"
21 :
22 : #include <rtl/ustring.hxx>
23 : #include <xmloff/xmlimp.hxx>
24 : #include <xmloff/txtimp.hxx>
25 : #include <xmloff/nmspmap.hxx>
26 : #include <xmloff/xmlnmspe.hxx>
27 : #include <xmloff/xmltoken.hxx>
28 :
29 : #include "XMLFootnoteBodyImportContext.hxx"
30 : #include "XMLTextListBlockContext.hxx"
31 : #include "XMLTextListItemContext.hxx"
32 :
33 : #include <com/sun/star/xml/sax/XAttributeList.hpp>
34 : #include <com/sun/star/text/XTextContent.hpp>
35 : #include <com/sun/star/beans/XPropertySet.hpp>
36 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 : #include <com/sun/star/text/XFootnote.hpp>
38 :
39 :
40 :
41 : using namespace ::com::sun::star::uno;
42 : using namespace ::com::sun::star::text;
43 : using namespace ::com::sun::star::lang;
44 : using namespace ::com::sun::star::beans;
45 : using namespace ::com::sun::star::xml::sax;
46 : using namespace ::xmloff::token;
47 :
48 0 : TYPEINIT1(XMLFootnoteImportContext, SvXMLImportContext);
49 :
50 : const sal_Char sAPI_service_footnote[] = "com.sun.star.text.Footnote";
51 : const sal_Char sAPI_service_endnote[] = "com.sun.star.text.Endnote";
52 :
53 : enum XMLFootnoteChildToken {
54 : XML_TOK_FTN_NOTE_CITATION,
55 : XML_TOK_FTN_NOTE_BODY
56 : };
57 :
58 : static const SvXMLTokenMapEntry aFootnoteChildTokenMap[] =
59 : {
60 : { XML_NAMESPACE_TEXT, XML_NOTE_CITATION,
61 : XML_TOK_FTN_NOTE_CITATION },
62 : { XML_NAMESPACE_TEXT, XML_NOTE_BODY, XML_TOK_FTN_NOTE_BODY },
63 : XML_TOKEN_MAP_END
64 : };
65 :
66 :
67 23 : XMLFootnoteImportContext::XMLFootnoteImportContext(
68 : SvXMLImport& rImport,
69 : XMLTextImportHelper& rHlp,
70 : sal_uInt16 nPrfx,
71 : const OUString& rLocalName )
72 : : SvXMLImportContext(rImport, nPrfx, rLocalName)
73 : , sPropertyReferenceId("ReferenceId")
74 : , mbListContextPushed(false)
75 23 : , rHelper(rHlp)
76 : {
77 23 : }
78 :
79 23 : void XMLFootnoteImportContext::StartElement(
80 : const Reference<XAttributeList> & xAttrList)
81 : {
82 : // create footnote
83 23 : Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),
84 23 : UNO_QUERY);
85 23 : if( xFactory.is() )
86 : {
87 : // create endnote or footnote
88 23 : bool bIsEndnote = false;
89 23 : sal_Int16 nLength = xAttrList->getLength();
90 46 : for(sal_Int16 nAttr1 = 0; nAttr1 < nLength; nAttr1++)
91 : {
92 46 : OUString sLocalName;
93 46 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
94 46 : GetKeyByAttrName( xAttrList->getNameByIndex(nAttr1),
95 92 : &sLocalName );
96 92 : if( XML_NAMESPACE_TEXT == nPrefix && IsXMLToken( sLocalName,
97 46 : XML_NOTE_CLASS ) )
98 : {
99 23 : const OUString& rValue = xAttrList->getValueByIndex( nAttr1 );
100 23 : if( IsXMLToken( rValue, XML_ENDNOTE ) )
101 8 : bIsEndnote = true;
102 23 : break;
103 : }
104 23 : }
105 :
106 23 : Reference<XInterface> xIfc = xFactory->createInstance(
107 : bIsEndnote ?
108 : OUString(sAPI_service_endnote) :
109 23 : OUString(sAPI_service_footnote) );
110 :
111 : // attach footnote to document
112 46 : Reference<XTextContent> xTextContent(xIfc, UNO_QUERY);
113 23 : rHelper.InsertTextContent(xTextContent);
114 :
115 : // process id attribute
116 69 : for(sal_Int16 nAttr2 = 0; nAttr2 < nLength; nAttr2++)
117 : {
118 46 : OUString sLocalName;
119 46 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
120 46 : GetKeyByAttrName( xAttrList->getNameByIndex(nAttr2),
121 92 : &sLocalName );
122 :
123 92 : if ( (XML_NAMESPACE_TEXT == nPrefix) &&
124 46 : IsXMLToken( sLocalName, XML_ID ) )
125 : {
126 : // get ID ...
127 23 : Reference<XPropertySet> xPropertySet(xTextContent, UNO_QUERY);
128 46 : Any aAny =xPropertySet->getPropertyValue(sPropertyReferenceId);
129 23 : sal_Int16 nID = 0;
130 23 : aAny >>= nID;
131 :
132 : // ... and insert into map
133 : rHelper.InsertFootnoteID(
134 23 : xAttrList->getValueByIndex(nAttr2),
135 69 : nID);
136 : }
137 46 : }
138 :
139 : // save old cursor and install new one
140 23 : xOldCursor = rHelper.GetCursor();
141 46 : Reference<XText> xText(xTextContent, UNO_QUERY);
142 23 : rHelper.SetCursor(xText->createTextCursor());
143 :
144 : // remember old list item and block (#89891#) and reset them
145 : // for the footnote
146 23 : rHelper.PushListContext();
147 23 : mbListContextPushed = true;
148 :
149 : // remember footnote (for CreateChildContext)
150 46 : Reference<XFootnote> xNote(xTextContent, UNO_QUERY);
151 46 : xFootnote = xNote;
152 23 : }
153 : // else: ignore footnote! Content will be merged into document.
154 23 : }
155 :
156 0 : void XMLFootnoteImportContext::Characters(const OUString&)
157 : {
158 : // ignore characters! Text must be contained in paragraphs!
159 : // rHelper.InsertString(rString);
160 0 : }
161 :
162 23 : void XMLFootnoteImportContext::EndElement()
163 : {
164 : // get rid of last dummy paragraph
165 23 : rHelper.DeleteParagraph();
166 :
167 : // reinstall old cursor
168 23 : rHelper.SetCursor(xOldCursor);
169 :
170 : // reinstall old list item
171 23 : if (mbListContextPushed) {
172 23 : rHelper.PopListContext();
173 : }
174 23 : }
175 :
176 :
177 46 : SvXMLImportContext *XMLFootnoteImportContext::CreateChildContext(
178 : sal_uInt16 p_nPrefix,
179 : const OUString& rLocalName,
180 : const Reference<XAttributeList> & xAttrList )
181 : {
182 46 : SvXMLImportContext* pContext = NULL;
183 :
184 46 : SvXMLTokenMap aTokenMap(aFootnoteChildTokenMap);
185 :
186 46 : switch(aTokenMap.Get(p_nPrefix, rLocalName))
187 : {
188 : case XML_TOK_FTN_NOTE_CITATION:
189 : {
190 : // little hack: we only care for one attribute of the citation
191 : // element. We handle that here, and then return a
192 : // default context.
193 23 : sal_Int16 nLength = xAttrList->getLength();
194 23 : for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
195 : {
196 0 : OUString sLocalName;
197 0 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
198 0 : GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
199 0 : &sLocalName );
200 :
201 0 : if ( (nPrefix == XML_NAMESPACE_TEXT) &&
202 0 : IsXMLToken( sLocalName, XML_LABEL ) )
203 : {
204 0 : xFootnote->setLabel(xAttrList->getValueByIndex(nAttr));
205 : }
206 0 : }
207 :
208 : // ignore content: return default context
209 23 : pContext = new SvXMLImportContext(GetImport(),
210 23 : p_nPrefix, rLocalName);
211 23 : break;
212 : }
213 :
214 : case XML_TOK_FTN_NOTE_BODY:
215 : // return footnote body
216 23 : pContext = new XMLFootnoteBodyImportContext(GetImport(),
217 23 : p_nPrefix, rLocalName);
218 23 : break;
219 : default:
220 : // default:
221 : pContext = SvXMLImportContext::CreateChildContext(p_nPrefix,
222 : rLocalName,
223 0 : xAttrList);
224 0 : break;
225 : }
226 :
227 46 : return pContext;
228 : }
229 :
230 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|