1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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(
44 , sUserIndexName("UserIndexName")
45 , sPrimaryKey("PrimaryKey")
46 , sSecondaryKey("SecondaryKey")
47 , sDocumentIndexMark("DocumentIndexMark")
49 , sIsCollapsed("IsCollapsed")
50 , sAlternativeText("AlternativeText")
51 , sTextReading("TextReading")
52 , sPrimaryKeyReading("PrimaryKeyReading")
53 , sSecondaryKeyReading("SecondaryKeyReading")
54 , sMainEntry("IsMainEntry")
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
,
78 /// index marks have no styles!
81 const enum XMLTokenEnum
* pElements
= NULL
;
82 sal_Int8 nElementNo
= -1;
86 aAny
= rPropSet
->getPropertyValue(sDocumentIndexMark
);
87 Reference
<XPropertySet
> xIndexMarkPropSet
;
88 aAny
>>= xIndexMarkPropSet
;
90 // common: handling of start, end, collapsed entries and
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
100 aAny
= xIndexMarkPropSet
->getPropertyValue(sAlternativeText
);
103 DBG_ASSERT(!sTmp
.isEmpty(),
104 "collapsed index mark without alternative text");
105 rExport
.AddAttribute(XML_NAMESPACE_TEXT
, XML_STRING_VALUE
, sTmp
);
109 // start and end entries: has ID
110 aAny
= rPropSet
->getPropertyValue(sIsStart
);
111 nElementNo
= *static_cast<sal_Bool
const *>(aAny
.getValue()) ? 1 : 2;
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
))
129 pElements
= lcl_pUserIndexMarkName
;
132 ExportUserIndexMarkAttributes(xIndexMarkPropSet
);
135 else if (xPropertySetInfo
->hasPropertyByName(sPrimaryKey
))
137 // alphabetical index mark
138 pElements
= lcl_pAlphaIndexMarkName
;
141 ExportAlphabeticalIndexMarkAttributes(xIndexMarkPropSet
);
147 pElements
= lcl_pTocMarkNames
;
150 ExportTOCMarkAttributes(xIndexMarkPropSet
);
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
,
163 pElements
[nElementNo
],
169 void XMLIndexMarkExport::ExportTOCMarkAttributes(
170 const Reference
<XPropertySet
> & rPropSet
)
173 sal_Int16 nLevel
= 0;
174 Any aAny
= rPropSet
->getPropertyValue(sLevel
);
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
,
188 rAny
= rPropSet
->getPropertyValue( sProperty
);
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
,
206 rAny
= rPropSet
->getPropertyValue( sProperty
);
209 if( rAny
>>= 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)
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)
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);
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */