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