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