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