Update ooo320-m1
[ooovba.git] / xmloff / source / text / XMLIndexMarkExport.cxx
blob1c3a200dae9f30ab5660357a79046c5585b6ca62
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XMLIndexMarkExport.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
33 #include "XMLIndexMarkExport.hxx"
34 #include <tools/debug.hxx>
35 #include <rtl/ustring.hxx>
36 #include <rtl/ustrbuf.hxx>
37 #include <com/sun/star/beans/XPropertySet.hpp>
38 #include <com/sun/star/beans/XPropertySetInfo.hpp>
39 #include <xmloff/xmltoken.hxx>
40 #include "xmlnmspe.hxx"
41 #include <xmloff/xmlexp.hxx>
42 #include <xmloff/xmluconv.hxx>
45 using namespace ::xmloff::token;
47 using ::rtl::OUString;
48 using ::rtl::OUStringBuffer;
49 using ::com::sun::star::beans::XPropertySet;
50 using ::com::sun::star::beans::XPropertySetInfo;
51 using ::com::sun::star::uno::Reference;
52 using ::com::sun::star::uno::Any;
56 XMLIndexMarkExport::XMLIndexMarkExport(
57 SvXMLExport& rExp,
58 XMLTextParagraphExport& rParaExp)
59 : sLevel(RTL_CONSTASCII_USTRINGPARAM("Level"))
60 , sUserIndexName(RTL_CONSTASCII_USTRINGPARAM("UserIndexName"))
61 , sPrimaryKey(RTL_CONSTASCII_USTRINGPARAM("PrimaryKey"))
62 , sSecondaryKey(RTL_CONSTASCII_USTRINGPARAM("SecondaryKey"))
63 , sDocumentIndexMark(RTL_CONSTASCII_USTRINGPARAM("DocumentIndexMark"))
64 , sIsStart(RTL_CONSTASCII_USTRINGPARAM("IsStart"))
65 , sIsCollapsed(RTL_CONSTASCII_USTRINGPARAM("IsCollapsed"))
66 , sAlternativeText(RTL_CONSTASCII_USTRINGPARAM("AlternativeText"))
67 , sTextReading(RTL_CONSTASCII_USTRINGPARAM("TextReading"))
68 , sPrimaryKeyReading(RTL_CONSTASCII_USTRINGPARAM("PrimaryKeyReading"))
69 , sSecondaryKeyReading(RTL_CONSTASCII_USTRINGPARAM("SecondaryKeyReading"))
70 , sMainEntry(RTL_CONSTASCII_USTRINGPARAM("IsMainEntry"))
71 , rExport(rExp)
72 , rParaExport(rParaExp)
76 const enum XMLTokenEnum lcl_pTocMarkNames[] =
77 { XML_TOC_MARK, XML_TOC_MARK_START, XML_TOC_MARK_END };
78 const enum XMLTokenEnum lcl_pUserIndexMarkName[] =
79 { XML_USER_INDEX_MARK,
80 XML_USER_INDEX_MARK_START, XML_USER_INDEX_MARK_END };
81 const enum XMLTokenEnum lcl_pAlphaIndexMarkName[] =
82 { XML_ALPHABETICAL_INDEX_MARK,
83 XML_ALPHABETICAL_INDEX_MARK_START,
84 XML_ALPHABETICAL_INDEX_MARK_END };
87 XMLIndexMarkExport::~XMLIndexMarkExport()
91 void XMLIndexMarkExport::ExportIndexMark(
92 const Reference<XPropertySet> & rPropSet,
93 sal_Bool bAutoStyles)
95 /// index marks have no styles!
96 if (!bAutoStyles)
98 const enum XMLTokenEnum * pElements = NULL;
99 sal_Int8 nElementNo = -1;
101 // get index mark
102 Any aAny;
103 aAny = rPropSet->getPropertyValue(sDocumentIndexMark);
104 Reference<XPropertySet> xIndexMarkPropSet;
105 aAny >>= xIndexMarkPropSet;
107 // common: handling of start, end, collapsed entries and
108 // alternative text
110 // collapsed/alternative text entry?
111 aAny = rPropSet->getPropertyValue(sIsCollapsed);
112 if (*(sal_Bool *)aAny.getValue())
114 // collapsed entry: needs alternative text
115 nElementNo = 0;
117 aAny = xIndexMarkPropSet->getPropertyValue(sAlternativeText);
118 OUString sTmp;
119 aAny >>= sTmp;
120 DBG_ASSERT(sTmp.getLength() > 0,
121 "collapsed index mark without alternative text");
122 rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_STRING_VALUE, sTmp);
124 else
126 // start and end entries: has ID
127 aAny = rPropSet->getPropertyValue(sIsStart);
128 nElementNo = *(sal_Bool *)aAny.getValue() ? 1 : 2;
130 // generate ID
131 OUStringBuffer sBuf;
132 GetID(sBuf, xIndexMarkPropSet);
133 rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_ID,
134 sBuf.makeStringAndClear());
137 // distinguish between TOC, user, alphab. index marks by
138 // asking for specific properties
139 // Export attributes for -mark-start and -mark elements,
140 // but not for -mark-end
141 Reference<XPropertySetInfo> xPropertySetInfo =
142 xIndexMarkPropSet->getPropertySetInfo();
143 if (xPropertySetInfo->hasPropertyByName(sUserIndexName))
145 // user index mark
146 pElements = lcl_pUserIndexMarkName;
147 if (nElementNo != 2)
149 ExportUserIndexMarkAttributes(xIndexMarkPropSet);
152 else if (xPropertySetInfo->hasPropertyByName(sPrimaryKey))
154 // alphabetical index mark
155 pElements = lcl_pAlphaIndexMarkName;
156 if (nElementNo != 2)
158 ExportAlphabeticalIndexMarkAttributes(xIndexMarkPropSet);
161 else
163 // table of content:
164 pElements = lcl_pTocMarkNames;
165 if (nElementNo != 2)
167 ExportTOCMarkAttributes(xIndexMarkPropSet);
171 // export element
172 DBG_ASSERT(pElements != NULL, "illegal element array");
173 DBG_ASSERT(nElementNo >= 0, "illegal name array index");
174 DBG_ASSERT(nElementNo <= 2, "illegal name array index");
176 if ((pElements != NULL) && (nElementNo != -1))
178 SvXMLElementExport aElem(rExport,
179 XML_NAMESPACE_TEXT,
180 pElements[nElementNo],
181 sal_False, sal_False);
186 void XMLIndexMarkExport::ExportTOCMarkAttributes(
187 const Reference<XPropertySet> & rPropSet)
189 // outline level
190 sal_Int16 nLevel = 0;
191 Any aAny = rPropSet->getPropertyValue(sLevel);
192 aAny >>= nLevel;
193 OUStringBuffer sBuf;
194 SvXMLUnitConverter::convertNumber(sBuf, (sal_Int32)nLevel + 1);
195 rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_OUTLINE_LEVEL,
196 sBuf.makeStringAndClear());
199 void lcl_ExportPropertyString( SvXMLExport& rExport,
200 const Reference<XPropertySet> & rPropSet,
201 const OUString sProperty,
202 XMLTokenEnum eToken,
203 Any& rAny )
205 rAny = rPropSet->getPropertyValue( sProperty );
207 OUString sValue;
208 if( rAny >>= sValue )
210 if( sValue.getLength() > 0 )
212 rExport.AddAttribute( XML_NAMESPACE_TEXT, eToken, sValue );
217 void lcl_ExportPropertyBool( SvXMLExport& rExport,
218 const Reference<XPropertySet> & rPropSet,
219 const OUString sProperty,
220 XMLTokenEnum eToken,
221 Any& rAny )
223 rAny = rPropSet->getPropertyValue( sProperty );
225 sal_Bool bValue = sal_Bool();
226 if( rAny >>= bValue )
228 if( bValue )
230 rExport.AddAttribute( XML_NAMESPACE_TEXT, eToken, XML_TRUE );
235 void XMLIndexMarkExport::ExportUserIndexMarkAttributes(
236 const Reference<XPropertySet> & rPropSet)
238 // name of user index
239 // (unless it's the default index; then it has no name)
240 Any aAny;
241 lcl_ExportPropertyString( rExport, rPropSet, sUserIndexName, XML_INDEX_NAME, aAny );
243 // additionally export outline level; just reuse ExportTOCMarkAttributes
244 ExportTOCMarkAttributes( rPropSet );
247 void XMLIndexMarkExport::ExportAlphabeticalIndexMarkAttributes(
248 const Reference<XPropertySet> & rPropSet)
250 // primary and secondary keys (if available)
251 Any aAny;
252 lcl_ExportPropertyString( rExport, rPropSet, sTextReading, XML_STRING_VALUE_PHONETIC, aAny );
253 lcl_ExportPropertyString( rExport, rPropSet, sPrimaryKey, XML_KEY1, aAny );
254 lcl_ExportPropertyString( rExport, rPropSet, sPrimaryKeyReading, XML_KEY1_PHONETIC, aAny );
255 lcl_ExportPropertyString( rExport, rPropSet, sSecondaryKey, XML_KEY2, aAny );
256 lcl_ExportPropertyString( rExport, rPropSet, sSecondaryKeyReading, XML_KEY2_PHONETIC, aAny );
257 lcl_ExportPropertyBool( rExport, rPropSet, sMainEntry, XML_MAIN_ENTRY, aAny );
260 void XMLIndexMarkExport::GetID(
261 OUStringBuffer& sBuf,
262 const Reference<XPropertySet> & rPropSet)
264 static const sal_Char sPrefix[] = "IMark";
266 // HACK: use address of object to form identifier
267 sal_Int64 nId = sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_uIntPtr>(rPropSet.get()));
268 sBuf.appendAscii(sPrefix, sizeof(sPrefix)-1);
269 sBuf.append(nId);