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 "XMLIndexTOCContext.hxx"
21 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
22 : #include <com/sun/star/lang/IllegalArgumentException.hpp>
23 : #include <com/sun/star/uno/XInterface.hpp>
24 : #include <com/sun/star/text/XTextContent.hpp>
25 : #include <com/sun/star/text/XTextSection.hpp>
26 : #include <com/sun/star/text/XRelativeTextContentInsert.hpp>
27 : #include <com/sun/star/beans/XPropertySet.hpp>
28 : #include <sax/tools/converter.hxx>
29 : #include "XMLIndexTOCSourceContext.hxx"
30 : #include "XMLIndexObjectSourceContext.hxx"
31 : #include "XMLIndexAlphabeticalSourceContext.hxx"
32 : #include "XMLIndexUserSourceContext.hxx"
33 : #include "XMLIndexBibliographySourceContext.hxx"
34 : #include "XMLIndexTableSourceContext.hxx"
35 : #include "XMLIndexIllustrationSourceContext.hxx"
36 : #include "XMLIndexBodyContext.hxx"
37 : #include <xmloff/xmlictxt.hxx>
38 : #include <xmloff/xmlimp.hxx>
39 : #include <xmloff/txtimp.hxx>
40 : #include <xmloff/nmspmap.hxx>
41 : #include "xmloff/xmlnmspe.hxx"
42 : #include <xmloff/xmltoken.hxx>
43 : #include <xmloff/prstylei.hxx>
44 : #include "xmloff/xmlerror.hxx"
45 : #include <xmloff/xmluconv.hxx>
46 : #include <rtl/ustring.hxx>
47 :
48 :
49 : using namespace ::com::sun::star::uno;
50 : using namespace ::com::sun::star::text;
51 : using namespace ::xmloff::token;
52 :
53 : using ::rtl::OUString;
54 : using ::com::sun::star::beans::XPropertySet;
55 : using ::com::sun::star::uno::Reference;
56 : using ::com::sun::star::xml::sax::XAttributeList;
57 : using ::com::sun::star::lang::XMultiServiceFactory;
58 : using ::com::sun::star::lang::IllegalArgumentException;
59 :
60 :
61 0 : TYPEINIT1(XMLIndexTOCContext, SvXMLImportContext);
62 :
63 : static const sal_Char* aIndexServiceMap[] =
64 : {
65 : "com.sun.star.text.ContentIndex",
66 : "com.sun.star.text.DocumentIndex",
67 : "com.sun.star.text.TableIndex",
68 : "com.sun.star.text.ObjectIndex",
69 : "com.sun.star.text.Bibliography",
70 : "com.sun.star.text.UserIndex",
71 : "com.sun.star.text.IllustrationsIndex"
72 : };
73 :
74 : static const XMLTokenEnum aIndexSourceElementMap[] =
75 : {
76 : XML_TABLE_OF_CONTENT_SOURCE,
77 : XML_ALPHABETICAL_INDEX_SOURCE,
78 : XML_TABLE_INDEX_SOURCE,
79 : XML_OBJECT_INDEX_SOURCE,
80 : XML_BIBLIOGRAPHY_SOURCE,
81 : XML_USER_INDEX_SOURCE,
82 : XML_ILLUSTRATION_INDEX_SOURCE
83 : };
84 :
85 : SvXMLEnumMapEntry const aIndexTypeMap[] =
86 : {
87 : { XML_TABLE_OF_CONTENT, TEXT_INDEX_TOC },
88 : { XML_ALPHABETICAL_INDEX, TEXT_INDEX_ALPHABETICAL },
89 : { XML_TABLE_INDEX, TEXT_INDEX_TABLE },
90 : { XML_OBJECT_INDEX, TEXT_INDEX_OBJECT },
91 : { XML_BIBLIOGRAPHY, TEXT_INDEX_BIBLIOGRAPHY },
92 : { XML_USER_INDEX, TEXT_INDEX_USER },
93 : { XML_ILLUSTRATION_INDEX, TEXT_INDEX_ILLUSTRATION },
94 : { XML_TOKEN_INVALID, 0 }
95 : };
96 :
97 :
98 0 : XMLIndexTOCContext::XMLIndexTOCContext(
99 : SvXMLImport& rImport,
100 : sal_uInt16 nPrfx,
101 : const OUString& rLocalName )
102 : : SvXMLImportContext(rImport, nPrfx, rLocalName)
103 : , sTitle(RTL_CONSTASCII_USTRINGPARAM("Title"))
104 : , sIsProtected(RTL_CONSTASCII_USTRINGPARAM("IsProtected"))
105 : , sName(RTL_CONSTASCII_USTRINGPARAM("Name"))
106 0 : , bValid(sal_False)
107 : {
108 0 : if (XML_NAMESPACE_TEXT == nPrfx)
109 : {
110 : sal_uInt16 nTmp;
111 0 : if (SvXMLUnitConverter::convertEnum(nTmp, rLocalName, aIndexTypeMap))
112 : {
113 : // check for array index:
114 : OSL_ENSURE(nTmp < (SAL_N_ELEMENTS(aIndexServiceMap)), "index out of range");
115 : OSL_ENSURE(SAL_N_ELEMENTS(aIndexServiceMap) ==
116 : SAL_N_ELEMENTS(aIndexSourceElementMap),
117 : "service and source element maps must be same size");
118 :
119 0 : eIndexType = static_cast<IndexTypeEnum>(nTmp);
120 0 : bValid = sal_True;
121 : }
122 : }
123 0 : }
124 :
125 0 : XMLIndexTOCContext::~XMLIndexTOCContext()
126 : {
127 0 : }
128 :
129 0 : void XMLIndexTOCContext::StartElement(
130 : const Reference<XAttributeList> & xAttrList)
131 : {
132 0 : if (bValid)
133 : {
134 : // find text:style-name attribute and set section style
135 : // find text:protected and set value
136 : // find text:name and set value (if not empty)
137 0 : sal_Int16 nCount = xAttrList->getLength();
138 0 : sal_Bool bProtected = sal_False;
139 0 : OUString sIndexName;
140 0 : OUString sXmlId;
141 0 : XMLPropStyleContext* pStyle(NULL);
142 0 : for(sal_Int16 nAttr = 0; nAttr < nCount; nAttr++)
143 : {
144 0 : OUString sLocalName;
145 0 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
146 0 : GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
147 0 : &sLocalName );
148 0 : if ( XML_NAMESPACE_TEXT == nPrefix)
149 : {
150 0 : if ( IsXMLToken( sLocalName, XML_STYLE_NAME ) )
151 : {
152 0 : pStyle = GetImport().GetTextImport()->FindSectionStyle(
153 0 : xAttrList->getValueByIndex(nAttr));
154 : }
155 0 : else if ( IsXMLToken( sLocalName, XML_PROTECTED ) )
156 : {
157 0 : bool bTmp(false);
158 0 : if (::sax::Converter::convertBool(
159 0 : bTmp, xAttrList->getValueByIndex(nAttr)))
160 : {
161 0 : bProtected = bTmp;
162 : }
163 : }
164 0 : else if ( IsXMLToken( sLocalName, XML_NAME ) )
165 : {
166 0 : sIndexName = xAttrList->getValueByIndex(nAttr);
167 : }
168 : }
169 0 : else if ( XML_NAMESPACE_XML == nPrefix)
170 : {
171 0 : if ( IsXMLToken( sLocalName, XML_ID ) )
172 : {
173 0 : sXmlId = xAttrList->getValueByIndex(nAttr);
174 : }
175 : }
176 0 : }
177 :
178 : // create table of content (via MultiServiceFactory)
179 0 : Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),
180 0 : UNO_QUERY);
181 0 : if( xFactory.is() )
182 : {
183 : Reference<XInterface> xIfc =
184 0 : xFactory->createInstance(
185 0 : OUString::createFromAscii(aIndexServiceMap[eIndexType]));
186 0 : if( xIfc.is() )
187 : {
188 : // get Property set
189 0 : Reference<XPropertySet> xPropSet(xIfc, UNO_QUERY);
190 0 : xTOCPropertySet = xPropSet;
191 :
192 : // insert section
193 : // a) insert section
194 : // The inserted index consists of an empty paragraph
195 : // only, as well as an empty paragraph *after* the index
196 : // b) insert marker after index, and put Cursor inside of the
197 : // index
198 :
199 : // preliminaries
200 : #ifndef DBG_UTIL
201 0 : OUString sMarker(RTL_CONSTASCII_USTRINGPARAM(" "));
202 : #else
203 : OUString sMarker(RTL_CONSTASCII_USTRINGPARAM("Y"));
204 : #endif
205 : UniReference<XMLTextImportHelper> rImport =
206 0 : GetImport().GetTextImport();
207 :
208 : // a) insert index
209 0 : Reference<XTextContent> xTextContent(xIfc, UNO_QUERY);
210 : try
211 : {
212 0 : GetImport().GetTextImport()->InsertTextContent(
213 0 : xTextContent);
214 : }
215 0 : catch(const IllegalArgumentException& e)
216 : {
217 : // illegal argument? Then we can't accept indices here!
218 0 : Sequence<OUString> aSeq(1);
219 0 : aSeq[0] = GetLocalName();
220 0 : GetImport().SetError(
221 : XMLERROR_FLAG_ERROR | XMLERROR_NO_INDEX_ALLOWED_HERE,
222 0 : aSeq, e.Message, NULL );
223 :
224 : // set bValid to false, and return prematurely
225 0 : bValid = false;
226 0 : return;
227 : }
228 :
229 : // xml:id for RDF metadata
230 0 : GetImport().SetXmlId(xIfc, sXmlId);
231 :
232 : // b) insert marker and move cursor
233 0 : rImport->InsertString(sMarker);
234 0 : rImport->GetCursor()->goLeft(2, sal_False);
235 0 : }
236 : }
237 :
238 : // finally, check for redlines that should start at
239 : // the section start node
240 0 : if( bValid )
241 0 : GetImport().GetTextImport()->
242 0 : RedlineAdjustStartNodeCursor(sal_True);
243 :
244 0 : if (pStyle != NULL)
245 : {
246 0 : pStyle->FillPropertySet( xTOCPropertySet );
247 : }
248 :
249 0 : Any aAny;
250 0 : aAny.setValue( &bProtected, ::getBooleanCppuType() );
251 0 : xTOCPropertySet->setPropertyValue( sIsProtected, aAny );
252 :
253 0 : if (!sIndexName.isEmpty())
254 : {
255 0 : aAny <<= sIndexName;
256 0 : xTOCPropertySet->setPropertyValue( sName, aAny );
257 0 : }
258 : }
259 : }
260 :
261 0 : void XMLIndexTOCContext::EndElement()
262 : {
263 : // complete import of index by removing the markers (if the index
264 : // was actually inserted, that is)
265 0 : if( bValid )
266 : {
267 : // preliminaries
268 0 : OUString sEmpty;
269 0 : UniReference<XMLTextImportHelper> rHelper= GetImport().GetTextImport();
270 :
271 : // get rid of last paragraph (unless it's the only paragraph)
272 0 : rHelper->GetCursor()->goRight(1, sal_False);
273 0 : if( xBodyContextRef.Is() &&
274 0 : ((XMLIndexBodyContext*)&xBodyContextRef)->HasContent() )
275 : {
276 0 : rHelper->GetCursor()->goLeft(1, sal_True);
277 0 : rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
278 0 : sEmpty, sal_True);
279 : }
280 :
281 : // and delete second marker
282 0 : rHelper->GetCursor()->goRight(1, sal_True);
283 0 : rHelper->GetText()->insertString(rHelper->GetCursorAsRange(),
284 0 : sEmpty, sal_True);
285 :
286 : // check for Redlines on our end node
287 0 : GetImport().GetTextImport()->RedlineAdjustStartNodeCursor(sal_False);
288 : }
289 0 : }
290 :
291 0 : SvXMLImportContext* XMLIndexTOCContext::CreateChildContext(
292 : sal_uInt16 nPrefix,
293 : const OUString& rLocalName,
294 : const Reference<XAttributeList> & xAttrList )
295 : {
296 0 : SvXMLImportContext* pContext = NULL;
297 :
298 0 : if (bValid)
299 : {
300 0 : if (XML_NAMESPACE_TEXT == nPrefix)
301 : {
302 0 : if ( IsXMLToken( rLocalName, XML_INDEX_BODY ) )
303 : {
304 0 : pContext = new XMLIndexBodyContext(GetImport(), nPrefix,
305 0 : rLocalName);
306 0 : if ( !xBodyContextRef.Is() ||
307 0 : !((XMLIndexBodyContext*)&xBodyContextRef)->HasContent() )
308 : {
309 0 : xBodyContextRef = pContext;
310 : }
311 : }
312 0 : else if (IsXMLToken(rLocalName, aIndexSourceElementMap[eIndexType]))
313 : {
314 : // instantiate source context for the appropriate index type
315 0 : switch (eIndexType)
316 : {
317 : case TEXT_INDEX_TOC:
318 : pContext = new XMLIndexTOCSourceContext(
319 0 : GetImport(), nPrefix, rLocalName, xTOCPropertySet);
320 0 : break;
321 :
322 : case TEXT_INDEX_OBJECT:
323 : pContext = new XMLIndexObjectSourceContext(
324 0 : GetImport(), nPrefix, rLocalName, xTOCPropertySet);
325 0 : break;
326 :
327 : case TEXT_INDEX_ALPHABETICAL:
328 : pContext = new XMLIndexAlphabeticalSourceContext(
329 0 : GetImport(), nPrefix, rLocalName, xTOCPropertySet);
330 0 : break;
331 :
332 : case TEXT_INDEX_USER:
333 : pContext = new XMLIndexUserSourceContext(
334 0 : GetImport(), nPrefix, rLocalName, xTOCPropertySet);
335 0 : break;
336 :
337 : case TEXT_INDEX_BIBLIOGRAPHY:
338 : pContext = new XMLIndexBibliographySourceContext(
339 0 : GetImport(), nPrefix, rLocalName, xTOCPropertySet);
340 0 : break;
341 :
342 : case TEXT_INDEX_TABLE:
343 : pContext = new XMLIndexTableSourceContext(
344 0 : GetImport(), nPrefix, rLocalName, xTOCPropertySet);
345 0 : break;
346 :
347 : case TEXT_INDEX_ILLUSTRATION:
348 : pContext = new XMLIndexIllustrationSourceContext(
349 0 : GetImport(), nPrefix, rLocalName, xTOCPropertySet);
350 0 : break;
351 :
352 : default:
353 : OSL_FAIL("index type not implemented");
354 0 : break;
355 : }
356 : }
357 : // else: ignore
358 : }
359 : // else: no text: namespace -> ignore
360 : }
361 : // else: not valid -> ignore
362 :
363 : // default: ignore
364 0 : if (pContext == NULL)
365 : {
366 : pContext = SvXMLImportContext::CreateChildContext(nPrefix, rLocalName,
367 0 : xAttrList);
368 : }
369 :
370 0 : return pContext;
371 : }
372 :
373 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|