nss: upgrade to release 3.73
[LibreOffice.git] / xmloff / source / text / XMLIndexMarkExport.cxx
blobb728c94120cc275a71378af65d3b39de0ffd8f75
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "XMLIndexMarkExport.hxx"
21 #include <o3tl/any.hxx>
22 #include <tools/debug.hxx>
23 #include <rtl/ustring.hxx>
24 #include <rtl/ustrbuf.hxx>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/beans/XPropertySetInfo.hpp>
27 #include <sax/tools/converter.hxx>
28 #include <xmloff/xmltoken.hxx>
29 #include <xmloff/xmlnamespace.hxx>
30 #include <xmloff/xmlexp.hxx>
33 using namespace ::xmloff::token;
35 using ::com::sun::star::beans::XPropertySet;
36 using ::com::sun::star::beans::XPropertySetInfo;
37 using ::com::sun::star::uno::Reference;
38 using ::com::sun::star::uno::Any;
40 XMLIndexMarkExport::XMLIndexMarkExport(
41 SvXMLExport& rExp)
42 : rExport(rExp)
46 const enum XMLTokenEnum lcl_pTocMarkNames[] =
47 { XML_TOC_MARK, XML_TOC_MARK_START, XML_TOC_MARK_END };
48 const enum XMLTokenEnum lcl_pUserIndexMarkName[] =
49 { XML_USER_INDEX_MARK,
50 XML_USER_INDEX_MARK_START, XML_USER_INDEX_MARK_END };
51 const enum XMLTokenEnum lcl_pAlphaIndexMarkName[] =
52 { XML_ALPHABETICAL_INDEX_MARK,
53 XML_ALPHABETICAL_INDEX_MARK_START,
54 XML_ALPHABETICAL_INDEX_MARK_END };
57 XMLIndexMarkExport::~XMLIndexMarkExport()
61 void XMLIndexMarkExport::ExportIndexMark(
62 const Reference<XPropertySet> & rPropSet,
63 bool bAutoStyles)
65 /// index marks have no styles!
66 if (bAutoStyles)
67 return;
69 const enum XMLTokenEnum * pElements = nullptr;
70 sal_Int8 nElementNo = -1;
72 // get index mark
73 Any aAny = rPropSet->getPropertyValue(gsDocumentIndexMark);
74 Reference<XPropertySet> xIndexMarkPropSet;
75 aAny >>= xIndexMarkPropSet;
77 // common: handling of start, end, collapsed entries and
78 // alternative text
80 // collapsed/alternative text entry?
81 aAny = rPropSet->getPropertyValue(gsIsCollapsed);
82 if (*o3tl::doAccess<bool>(aAny))
84 // collapsed entry: needs alternative text
85 nElementNo = 0;
87 aAny = xIndexMarkPropSet->getPropertyValue(gsAlternativeText);
88 OUString sTmp;
89 aAny >>= sTmp;
90 DBG_ASSERT(!sTmp.isEmpty(),
91 "collapsed index mark without alternative text");
92 rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_STRING_VALUE, sTmp);
94 else
96 // start and end entries: has ID
97 aAny = rPropSet->getPropertyValue(gsIsStart);
98 nElementNo = *o3tl::doAccess<bool>(aAny) ? 1 : 2;
100 // generate ID
101 OUStringBuffer sBuf;
102 GetID(sBuf, xIndexMarkPropSet);
103 rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_ID,
104 sBuf.makeStringAndClear());
107 // distinguish between TOC, user, alphab. index marks by
108 // asking for specific properties
109 // Export attributes for -mark-start and -mark elements,
110 // but not for -mark-end
111 Reference<XPropertySetInfo> xPropertySetInfo =
112 xIndexMarkPropSet->getPropertySetInfo();
113 if (xPropertySetInfo->hasPropertyByName(gsUserIndexName))
115 // user index mark
116 pElements = lcl_pUserIndexMarkName;
117 if (nElementNo != 2)
119 ExportUserIndexMarkAttributes(xIndexMarkPropSet);
122 else if (xPropertySetInfo->hasPropertyByName(gsPrimaryKey))
124 // alphabetical index mark
125 pElements = lcl_pAlphaIndexMarkName;
126 if (nElementNo != 2)
128 ExportAlphabeticalIndexMarkAttributes(xIndexMarkPropSet);
131 else
133 // table of content:
134 pElements = lcl_pTocMarkNames;
135 if (nElementNo != 2)
137 ExportTOCMarkAttributes(xIndexMarkPropSet);
141 // export element
142 DBG_ASSERT(pElements != nullptr, "illegal element array");
143 DBG_ASSERT(nElementNo >= 0, "illegal name array index");
144 DBG_ASSERT(nElementNo <= 2, "illegal name array index");
146 if ((pElements != nullptr) && (nElementNo != -1))
148 SvXMLElementExport aElem(rExport,
149 XML_NAMESPACE_TEXT,
150 pElements[nElementNo],
151 false, false);
156 void XMLIndexMarkExport::ExportTOCMarkAttributes(
157 const Reference<XPropertySet> & rPropSet)
159 // outline level
160 sal_Int16 nLevel = 0;
161 Any aAny = rPropSet->getPropertyValue(gsLevel);
162 aAny >>= nLevel;
163 rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_OUTLINE_LEVEL,
164 OUString::number(nLevel + 1));
167 static void lcl_ExportPropertyString( SvXMLExport& rExport,
168 const Reference<XPropertySet> & rPropSet,
169 const OUString & sProperty,
170 XMLTokenEnum eToken,
171 Any& rAny )
173 rAny = rPropSet->getPropertyValue( sProperty );
175 OUString sValue;
176 if( (rAny >>= sValue) && !sValue.isEmpty() )
178 rExport.AddAttribute( XML_NAMESPACE_TEXT, eToken, sValue );
182 static void lcl_ExportPropertyBool( SvXMLExport& rExport,
183 const Reference<XPropertySet> & rPropSet,
184 const OUString & sProperty,
185 XMLTokenEnum eToken,
186 Any& rAny )
188 rAny = rPropSet->getPropertyValue( sProperty );
190 bool bValue;
191 if( (rAny >>= bValue) && bValue )
193 rExport.AddAttribute( XML_NAMESPACE_TEXT, eToken, XML_TRUE );
197 void XMLIndexMarkExport::ExportUserIndexMarkAttributes(
198 const Reference<XPropertySet> & rPropSet)
200 // name of user index
201 // (unless it's the default index; then it has no name)
202 Any aAny;
203 lcl_ExportPropertyString( rExport, rPropSet, gsUserIndexName, XML_INDEX_NAME, aAny );
205 // additionally export outline level; just reuse ExportTOCMarkAttributes
206 ExportTOCMarkAttributes( rPropSet );
209 void XMLIndexMarkExport::ExportAlphabeticalIndexMarkAttributes(
210 const Reference<XPropertySet> & rPropSet)
212 // primary and secondary keys (if available)
213 Any aAny;
214 lcl_ExportPropertyString( rExport, rPropSet, gsTextReading, XML_STRING_VALUE_PHONETIC, aAny );
215 lcl_ExportPropertyString( rExport, rPropSet, gsPrimaryKey, XML_KEY1, aAny );
216 lcl_ExportPropertyString( rExport, rPropSet, gsPrimaryKeyReading, XML_KEY1_PHONETIC, aAny );
217 lcl_ExportPropertyString( rExport, rPropSet, gsSecondaryKey, XML_KEY2, aAny );
218 lcl_ExportPropertyString( rExport, rPropSet, gsSecondaryKeyReading, XML_KEY2_PHONETIC, aAny );
219 lcl_ExportPropertyBool( rExport, rPropSet, gsMainEntry, XML_MAIN_ENTRY, aAny );
222 void XMLIndexMarkExport::GetID(
223 OUStringBuffer& sBuf,
224 const Reference<XPropertySet> & rPropSet)
226 // HACK: use address of object to form identifier
227 sal_Int64 nId = sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_uIntPtr>(rPropSet.get()));
228 sBuf.append("IMark");
229 sBuf.append(nId);
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */