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