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