LibreOffice Module xmloff (master)  1
XMLIndexMarkExport.cxx
Go to the documentation of this file.
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 "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>
31 
32 
33 using namespace ::xmloff::token;
34 
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;
39 
41  SvXMLExport& rExp)
42 : rExport(rExp)
43 {
44 }
45 
55 
56 
58 {
59 }
60 
62  const Reference<XPropertySet> & rPropSet,
63  bool bAutoStyles)
64 {
66  if (bAutoStyles)
67  return;
68 
69  const enum XMLTokenEnum * pElements = nullptr;
70  sal_Int8 nElementNo = -1;
71 
72  // get index mark
73  Any aAny = rPropSet->getPropertyValue(gsDocumentIndexMark);
74  Reference<XPropertySet> xIndexMarkPropSet;
75  aAny >>= xIndexMarkPropSet;
76 
77  // common: handling of start, end, collapsed entries and
78  // alternative text
79 
80  // collapsed/alternative text entry?
81  aAny = rPropSet->getPropertyValue(gsIsCollapsed);
82  if (*o3tl::doAccess<bool>(aAny))
83  {
84  // collapsed entry: needs alternative text
85  nElementNo = 0;
86 
87  aAny = xIndexMarkPropSet->getPropertyValue(gsAlternativeText);
88  OUString sTmp;
89  aAny >>= sTmp;
90  DBG_ASSERT(!sTmp.isEmpty(),
91  "collapsed index mark without alternative text");
93  }
94  else
95  {
96  // start and end entries: has ID
97  aAny = rPropSet->getPropertyValue(gsIsStart);
98  nElementNo = *o3tl::doAccess<bool>(aAny) ? 1 : 2;
99 
100  // generate ID
101  OUStringBuffer sBuf;
102  GetID(sBuf, xIndexMarkPropSet);
104  sBuf.makeStringAndClear());
105  }
106 
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))
114  {
115  // user index mark
116  pElements = lcl_pUserIndexMarkName;
117  if (nElementNo != 2)
118  {
119  ExportUserIndexMarkAttributes(xIndexMarkPropSet);
120  }
121  }
122  else if (xPropertySetInfo->hasPropertyByName(gsPrimaryKey))
123  {
124  // alphabetical index mark
125  pElements = lcl_pAlphaIndexMarkName;
126  if (nElementNo != 2)
127  {
128  ExportAlphabeticalIndexMarkAttributes(xIndexMarkPropSet);
129  }
130  }
131  else
132  {
133  // table of content:
134  pElements = lcl_pTocMarkNames;
135  if (nElementNo != 2)
136  {
137  ExportTOCMarkAttributes(xIndexMarkPropSet);
138  }
139  }
140 
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");
145 
146  if ((pElements != nullptr) && (nElementNo != -1))
147  {
150  pElements[nElementNo],
151  false, false);
152  }
153 
154 }
155 
157  const Reference<XPropertySet> & rPropSet)
158 {
159  // outline level
160  sal_Int16 nLevel = 0;
161  Any aAny = rPropSet->getPropertyValue(gsLevel);
162  aAny >>= nLevel;
164  OUString::number(nLevel + 1));
165 }
166 
168  const Reference<XPropertySet> & rPropSet,
169  const OUString & sProperty,
171  Any& rAny )
172 {
173  rAny = rPropSet->getPropertyValue( sProperty );
174 
175  OUString sValue;
176  if( (rAny >>= sValue) && !sValue.isEmpty() )
177  {
178  rExport.AddAttribute( XML_NAMESPACE_TEXT, eToken, sValue );
179  }
180 }
181 
183  const Reference<XPropertySet> & rPropSet,
184  const OUString & sProperty,
186  Any& rAny )
187 {
188  rAny = rPropSet->getPropertyValue( sProperty );
189 
190  bool bValue;
191  if( (rAny >>= bValue) && bValue )
192  {
193  rExport.AddAttribute( XML_NAMESPACE_TEXT, eToken, XML_TRUE );
194  }
195 }
196 
198  const Reference<XPropertySet> & rPropSet)
199 {
200  // name of user index
201  // (unless it's the default index; then it has no name)
202  Any aAny;
204 
205  // additionally export outline level; just reuse ExportTOCMarkAttributes
206  ExportTOCMarkAttributes( rPropSet );
207 }
208 
210  const Reference<XPropertySet> & rPropSet)
211 {
212  // primary and secondary keys (if available)
213  Any aAny;
220 }
221 
223  OUStringBuffer& sBuf,
224  const Reference<XPropertySet> & rPropSet)
225 {
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);
230 }
231 
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void lcl_ExportPropertyString(SvXMLExport &rExport, const Reference< XPropertySet > &rPropSet, const OUString &sProperty, XMLTokenEnum eToken, Any &rAny)
void ExportUserIndexMarkAttributes(const css::uno::Reference< css::beans::XPropertySet > &rPropSet)
export attributes of user index marks
static constexpr OUStringLiteral gsSecondaryKey
void ExportTOCMarkAttributes(const css::uno::Reference< css::beans::XPropertySet > &rPropSet)
export attributes of table-of-content index marks
signed char sal_Int8
sal_Int16 nId
void ExportIndexMark(const css::uno::Reference< css::beans::XPropertySet > &rPropSet, bool bAutoStyles)
export by the property set of its text portion.
static constexpr OUStringLiteral gsMainEntry
static constexpr OUStringLiteral gsLevel
void ExportAlphabeticalIndexMarkAttributes(const css::uno::Reference< css::beans::XPropertySet > &rPropSet)
export attributes of alphabetical index marks
void AddAttribute(sal_uInt16 nPrefix, const char *pName, const OUString &rValue)
Definition: xmlexp.cxx:909
static constexpr OUStringLiteral gsPrimaryKey
XMLTokenEnum
The enumeration of all XML tokens.
Definition: xmltoken.hxx:49
static constexpr OUStringLiteral gsPrimaryKeyReading
#define DBG_ASSERT(sCon, aError)
static void GetID(OUStringBuffer &sBuffer, const css::uno::Reference< css::beans::XPropertySet > &rPropSet)
create a numerical ID for this index mark (represented by its properties)
constexpr sal_uInt16 XML_NAMESPACE_TEXT
static constexpr OUStringLiteral gsDocumentIndexMark
static constexpr OUStringLiteral gsTextReading
static constexpr OUStringLiteral gsIsStart
Handling of tokens in XML:
XMLIndexMarkExport(SvXMLExport &rExp)
static constexpr OUStringLiteral gsIsCollapsed
enum XMLTokenEnum lcl_pAlphaIndexMarkName[]
static constexpr OUStringLiteral gsUserIndexName
XMLTokenEnum eToken
Definition: xmltoken.cxx:40
static constexpr OUStringLiteral gsSecondaryKeyReading
css::uno::Any const SvXMLExport & rExport
Definition: ImageStyle.hxx:38
static constexpr OUStringLiteral gsAlternativeText
static void lcl_ExportPropertyBool(SvXMLExport &rExport, const Reference< XPropertySet > &rPropSet, const OUString &sProperty, XMLTokenEnum eToken, Any &rAny)
enum XMLTokenEnum lcl_pTocMarkNames[]
enum XMLTokenEnum lcl_pUserIndexMarkName[]