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