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 36 : 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 36 : , rHelper(rHlp)
76 : {
77 36 : }
78 :
79 36 : void XMLFootnoteImportContext::StartElement(
80 : const Reference<XAttributeList> & xAttrList)
81 : {
82 : // create footnote
83 36 : Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),
84 36 : UNO_QUERY);
85 36 : if( xFactory.is() )
86 : {
87 : // create endnote or footnote
88 36 : bool bIsEndnote = false;
89 36 : sal_Int16 nLength = xAttrList->getLength();
90 72 : for(sal_Int16 nAttr1 = 0; nAttr1 < nLength; nAttr1++)
91 : {
92 72 : OUString sLocalName;
93 72 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
94 72 : GetKeyByAttrName( xAttrList->getNameByIndex(nAttr1),
95 144 : &sLocalName );
96 144 : if( XML_NAMESPACE_TEXT == nPrefix && IsXMLToken( sLocalName,
97 72 : XML_NOTE_CLASS ) )
98 : {
99 36 : const OUString& rValue = xAttrList->getValueByIndex( nAttr1 );
100 36 : if( IsXMLToken( rValue, XML_ENDNOTE ) )
101 10 : bIsEndnote = true;
102 36 : break;
103 : }
104 36 : }
105 :
106 36 : Reference<XInterface> xIfc = xFactory->createInstance(
107 : bIsEndnote ?
108 : OUString(sAPI_service_endnote) :
109 36 : OUString(sAPI_service_footnote) );
110 :
111 : // attach footnote to document
112 72 : Reference<XTextContent> xTextContent(xIfc, UNO_QUERY);
113 36 : rHelper.InsertTextContent(xTextContent);
114 :
115 : // process id attribute
116 108 : for(sal_Int16 nAttr2 = 0; nAttr2 < nLength; nAttr2++)
117 : {
118 72 : OUString sLocalName;
119 72 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
120 72 : GetKeyByAttrName( xAttrList->getNameByIndex(nAttr2),
121 144 : &sLocalName );
122 :
123 144 : if ( (XML_NAMESPACE_TEXT == nPrefix) &&
124 72 : IsXMLToken( sLocalName, XML_ID ) )
125 : {
126 : // get ID ...
127 36 : Reference<XPropertySet> xPropertySet(xTextContent, UNO_QUERY);
128 72 : Any aAny =xPropertySet->getPropertyValue(sPropertyReferenceId);
129 36 : sal_Int16 nID = 0;
130 36 : aAny >>= nID;
131 :
132 : // ... and insert into map
133 : rHelper.InsertFootnoteID(
134 36 : xAttrList->getValueByIndex(nAttr2),
135 108 : nID);
136 : }
137 72 : }
138 :
139 : // save old cursor and install new one
140 36 : xOldCursor = rHelper.GetCursor();
141 72 : Reference<XText> xText(xTextContent, UNO_QUERY);
142 36 : rHelper.SetCursor(xText->createTextCursor());
143 :
144 : // remember old list item and block (#89891#) and reset them
145 : // for the footnote
146 36 : rHelper.PushListContext();
147 36 : mbListContextPushed = true;
148 :
149 : // remember footnote (for CreateChildContext)
150 72 : Reference<XFootnote> xNote(xTextContent, UNO_QUERY);
151 72 : xFootnote = xNote;
152 36 : }
153 : // else: ignore footnote! Content will be merged into document.
154 36 : }
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 36 : void XMLFootnoteImportContext::EndElement()
163 : {
164 : // get rid of last dummy paragraph
165 36 : rHelper.DeleteParagraph();
166 :
167 : // reinstall old cursor
168 36 : rHelper.SetCursor(xOldCursor);
169 :
170 : // reinstall old list item
171 36 : if (mbListContextPushed) {
172 36 : rHelper.PopListContext();
173 : }
174 36 : }
175 :
176 :
177 72 : SvXMLImportContext *XMLFootnoteImportContext::CreateChildContext(
178 : sal_uInt16 p_nPrefix,
179 : const OUString& rLocalName,
180 : const Reference<XAttributeList> & xAttrList )
181 : {
182 72 : SvXMLImportContext* pContext = NULL;
183 :
184 72 : SvXMLTokenMap aTokenMap(aFootnoteChildTokenMap);
185 :
186 72 : 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 36 : sal_Int16 nLength = xAttrList->getLength();
194 36 : 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 36 : pContext = new SvXMLImportContext(GetImport(),
210 36 : p_nPrefix, rLocalName);
211 36 : break;
212 : }
213 :
214 : case XML_TOK_FTN_NOTE_BODY:
215 : // return footnote body
216 36 : pContext = new XMLFootnoteBodyImportContext(GetImport(),
217 36 : p_nPrefix, rLocalName);
218 36 : break;
219 : default:
220 : // default:
221 : pContext = SvXMLImportContext::CreateChildContext(p_nPrefix,
222 : rLocalName,
223 0 : xAttrList);
224 0 : break;
225 : }
226 :
227 72 : return pContext;
228 : }
229 :
230 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|