bump product version to 5.0.4.1
[LibreOffice.git] / xmloff / source / text / XMLIndexMarkExport.cxx
blob9004c7423fb6a49ef4e81a27470acfa74cb810f0
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 <tools/debug.hxx>
22 #include <rtl/ustring.hxx>
23 #include <rtl/ustrbuf.hxx>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/beans/XPropertySetInfo.hpp>
26 #include <sax/tools/converter.hxx>
27 #include <xmloff/xmltoken.hxx>
28 #include <xmloff/xmlnmspe.hxx>
29 #include <xmloff/xmlexp.hxx>
32 using namespace ::xmloff::token;
34 using ::com::sun::star::beans::XPropertySet;
35 using ::com::sun::star::beans::XPropertySetInfo;
36 using ::com::sun::star::uno::Reference;
37 using ::com::sun::star::uno::Any;
41 XMLIndexMarkExport::XMLIndexMarkExport(
42 SvXMLExport& rExp)
43 : sLevel("Level")
44 , sUserIndexName("UserIndexName")
45 , sPrimaryKey("PrimaryKey")
46 , sSecondaryKey("SecondaryKey")
47 , sDocumentIndexMark("DocumentIndexMark")
48 , sIsStart("IsStart")
49 , sIsCollapsed("IsCollapsed")
50 , sAlternativeText("AlternativeText")
51 , sTextReading("TextReading")
52 , sPrimaryKeyReading("PrimaryKeyReading")
53 , sSecondaryKeyReading("SecondaryKeyReading")
54 , sMainEntry("IsMainEntry")
55 , rExport(rExp)
59 const enum XMLTokenEnum lcl_pTocMarkNames[] =
60 { XML_TOC_MARK, XML_TOC_MARK_START, XML_TOC_MARK_END };
61 const enum XMLTokenEnum lcl_pUserIndexMarkName[] =
62 { XML_USER_INDEX_MARK,
63 XML_USER_INDEX_MARK_START, XML_USER_INDEX_MARK_END };
64 const enum XMLTokenEnum lcl_pAlphaIndexMarkName[] =
65 { XML_ALPHABETICAL_INDEX_MARK,
66 XML_ALPHABETICAL_INDEX_MARK_START,
67 XML_ALPHABETICAL_INDEX_MARK_END };
70 XMLIndexMarkExport::~XMLIndexMarkExport()
74 void XMLIndexMarkExport::ExportIndexMark(
75 const Reference<XPropertySet> & rPropSet,
76 bool bAutoStyles)
78 /// index marks have no styles!
79 if (!bAutoStyles)
81 const enum XMLTokenEnum * pElements = NULL;
82 sal_Int8 nElementNo = -1;
84 // get index mark
85 Any aAny;
86 aAny = rPropSet->getPropertyValue(sDocumentIndexMark);
87 Reference<XPropertySet> xIndexMarkPropSet;
88 aAny >>= xIndexMarkPropSet;
90 // common: handling of start, end, collapsed entries and
91 // alternative text
93 // collapsed/alternative text entry?
94 aAny = rPropSet->getPropertyValue(sIsCollapsed);
95 if (*static_cast<sal_Bool const *>(aAny.getValue()))
97 // collapsed entry: needs alternative text
98 nElementNo = 0;
100 aAny = xIndexMarkPropSet->getPropertyValue(sAlternativeText);
101 OUString sTmp;
102 aAny >>= sTmp;
103 DBG_ASSERT(!sTmp.isEmpty(),
104 "collapsed index mark without alternative text");
105 rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_STRING_VALUE, sTmp);
107 else
109 // start and end entries: has ID
110 aAny = rPropSet->getPropertyValue(sIsStart);
111 nElementNo = *static_cast<sal_Bool const *>(aAny.getValue()) ? 1 : 2;
113 // generate ID
114 OUStringBuffer sBuf;
115 GetID(sBuf, xIndexMarkPropSet);
116 rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_ID,
117 sBuf.makeStringAndClear());
120 // distinguish between TOC, user, alphab. index marks by
121 // asking for specific properties
122 // Export attributes for -mark-start and -mark elements,
123 // but not for -mark-end
124 Reference<XPropertySetInfo> xPropertySetInfo =
125 xIndexMarkPropSet->getPropertySetInfo();
126 if (xPropertySetInfo->hasPropertyByName(sUserIndexName))
128 // user index mark
129 pElements = lcl_pUserIndexMarkName;
130 if (nElementNo != 2)
132 ExportUserIndexMarkAttributes(xIndexMarkPropSet);
135 else if (xPropertySetInfo->hasPropertyByName(sPrimaryKey))
137 // alphabetical index mark
138 pElements = lcl_pAlphaIndexMarkName;
139 if (nElementNo != 2)
141 ExportAlphabeticalIndexMarkAttributes(xIndexMarkPropSet);
144 else
146 // table of content:
147 pElements = lcl_pTocMarkNames;
148 if (nElementNo != 2)
150 ExportTOCMarkAttributes(xIndexMarkPropSet);
154 // export element
155 DBG_ASSERT(pElements != NULL, "illegal element array");
156 DBG_ASSERT(nElementNo >= 0, "illegal name array index");
157 DBG_ASSERT(nElementNo <= 2, "illegal name array index");
159 if ((pElements != NULL) && (nElementNo != -1))
161 SvXMLElementExport aElem(rExport,
162 XML_NAMESPACE_TEXT,
163 pElements[nElementNo],
164 false, false);
169 void XMLIndexMarkExport::ExportTOCMarkAttributes(
170 const Reference<XPropertySet> & rPropSet)
172 // outline level
173 sal_Int16 nLevel = 0;
174 Any aAny = rPropSet->getPropertyValue(sLevel);
175 aAny >>= nLevel;
176 OUStringBuffer sBuf;
177 ::sax::Converter::convertNumber(sBuf, static_cast<sal_Int32>(nLevel + 1));
178 rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_OUTLINE_LEVEL,
179 sBuf.makeStringAndClear());
182 static void lcl_ExportPropertyString( SvXMLExport& rExport,
183 const Reference<XPropertySet> & rPropSet,
184 const OUString & sProperty,
185 XMLTokenEnum eToken,
186 Any& rAny )
188 rAny = rPropSet->getPropertyValue( sProperty );
190 OUString sValue;
191 if( rAny >>= sValue )
193 if( !sValue.isEmpty() )
195 rExport.AddAttribute( XML_NAMESPACE_TEXT, eToken, sValue );
200 static void lcl_ExportPropertyBool( SvXMLExport& rExport,
201 const Reference<XPropertySet> & rPropSet,
202 const OUString & sProperty,
203 XMLTokenEnum eToken,
204 Any& rAny )
206 rAny = rPropSet->getPropertyValue( sProperty );
208 bool bValue;
209 if( rAny >>= bValue )
211 if( bValue )
213 rExport.AddAttribute( XML_NAMESPACE_TEXT, eToken, XML_TRUE );
218 void XMLIndexMarkExport::ExportUserIndexMarkAttributes(
219 const Reference<XPropertySet> & rPropSet)
221 // name of user index
222 // (unless it's the default index; then it has no name)
223 Any aAny;
224 lcl_ExportPropertyString( rExport, rPropSet, sUserIndexName, XML_INDEX_NAME, aAny );
226 // additionally export outline level; just reuse ExportTOCMarkAttributes
227 ExportTOCMarkAttributes( rPropSet );
230 void XMLIndexMarkExport::ExportAlphabeticalIndexMarkAttributes(
231 const Reference<XPropertySet> & rPropSet)
233 // primary and secondary keys (if available)
234 Any aAny;
235 lcl_ExportPropertyString( rExport, rPropSet, sTextReading, XML_STRING_VALUE_PHONETIC, aAny );
236 lcl_ExportPropertyString( rExport, rPropSet, sPrimaryKey, XML_KEY1, aAny );
237 lcl_ExportPropertyString( rExport, rPropSet, sPrimaryKeyReading, XML_KEY1_PHONETIC, aAny );
238 lcl_ExportPropertyString( rExport, rPropSet, sSecondaryKey, XML_KEY2, aAny );
239 lcl_ExportPropertyString( rExport, rPropSet, sSecondaryKeyReading, XML_KEY2_PHONETIC, aAny );
240 lcl_ExportPropertyBool( rExport, rPropSet, sMainEntry, XML_MAIN_ENTRY, aAny );
243 void XMLIndexMarkExport::GetID(
244 OUStringBuffer& sBuf,
245 const Reference<XPropertySet> & rPropSet)
247 static const sal_Char sPrefix[] = "IMark";
249 // HACK: use address of object to form identifier
250 sal_Int64 nId = sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_uIntPtr>(rPropSet.get()));
251 sBuf.appendAscii(sPrefix, sizeof(sPrefix)-1);
252 sBuf.append(nId);
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */