Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
30 :
31 :
32 : using namespace ::xmloff::token;
33 :
34 : using ::rtl::OUString;
35 : using ::rtl::OUStringBuffer;
36 : using ::com::sun::star::beans::XPropertySet;
37 : using ::com::sun::star::beans::XPropertySetInfo;
38 : using ::com::sun::star::uno::Reference;
39 : using ::com::sun::star::uno::Any;
40 :
41 :
42 :
43 8 : XMLIndexMarkExport::XMLIndexMarkExport(
44 : SvXMLExport& rExp)
45 : : sLevel(RTL_CONSTASCII_USTRINGPARAM("Level"))
46 : , sUserIndexName(RTL_CONSTASCII_USTRINGPARAM("UserIndexName"))
47 : , sPrimaryKey(RTL_CONSTASCII_USTRINGPARAM("PrimaryKey"))
48 : , sSecondaryKey(RTL_CONSTASCII_USTRINGPARAM("SecondaryKey"))
49 : , sDocumentIndexMark(RTL_CONSTASCII_USTRINGPARAM("DocumentIndexMark"))
50 : , sIsStart(RTL_CONSTASCII_USTRINGPARAM("IsStart"))
51 : , sIsCollapsed(RTL_CONSTASCII_USTRINGPARAM("IsCollapsed"))
52 : , sAlternativeText(RTL_CONSTASCII_USTRINGPARAM("AlternativeText"))
53 : , sTextReading(RTL_CONSTASCII_USTRINGPARAM("TextReading"))
54 : , sPrimaryKeyReading(RTL_CONSTASCII_USTRINGPARAM("PrimaryKeyReading"))
55 : , sSecondaryKeyReading(RTL_CONSTASCII_USTRINGPARAM("SecondaryKeyReading"))
56 : , sMainEntry(RTL_CONSTASCII_USTRINGPARAM("IsMainEntry"))
57 8 : , rExport(rExp)
58 : {
59 8 : }
60 :
61 : const enum XMLTokenEnum lcl_pTocMarkNames[] =
62 : { XML_TOC_MARK, XML_TOC_MARK_START, XML_TOC_MARK_END };
63 : const enum XMLTokenEnum lcl_pUserIndexMarkName[] =
64 : { XML_USER_INDEX_MARK,
65 : XML_USER_INDEX_MARK_START, XML_USER_INDEX_MARK_END };
66 : const enum XMLTokenEnum lcl_pAlphaIndexMarkName[] =
67 : { XML_ALPHABETICAL_INDEX_MARK,
68 : XML_ALPHABETICAL_INDEX_MARK_START,
69 : XML_ALPHABETICAL_INDEX_MARK_END };
70 :
71 :
72 8 : XMLIndexMarkExport::~XMLIndexMarkExport()
73 : {
74 8 : }
75 :
76 0 : void XMLIndexMarkExport::ExportIndexMark(
77 : const Reference<XPropertySet> & rPropSet,
78 : sal_Bool bAutoStyles)
79 : {
80 : /// index marks have no styles!
81 0 : if (!bAutoStyles)
82 : {
83 0 : const enum XMLTokenEnum * pElements = NULL;
84 0 : sal_Int8 nElementNo = -1;
85 :
86 : // get index mark
87 0 : Any aAny;
88 0 : aAny = rPropSet->getPropertyValue(sDocumentIndexMark);
89 0 : Reference<XPropertySet> xIndexMarkPropSet;
90 0 : aAny >>= xIndexMarkPropSet;
91 :
92 : // common: handling of start, end, collapsed entries and
93 : // alternative text
94 :
95 : // collapsed/alternative text entry?
96 0 : aAny = rPropSet->getPropertyValue(sIsCollapsed);
97 0 : if (*(sal_Bool *)aAny.getValue())
98 : {
99 : // collapsed entry: needs alternative text
100 0 : nElementNo = 0;
101 :
102 0 : aAny = xIndexMarkPropSet->getPropertyValue(sAlternativeText);
103 0 : OUString sTmp;
104 0 : aAny >>= sTmp;
105 : DBG_ASSERT(!sTmp.isEmpty(),
106 : "collapsed index mark without alternative text");
107 0 : rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_STRING_VALUE, sTmp);
108 : }
109 : else
110 : {
111 : // start and end entries: has ID
112 0 : aAny = rPropSet->getPropertyValue(sIsStart);
113 0 : nElementNo = *(sal_Bool *)aAny.getValue() ? 1 : 2;
114 :
115 : // generate ID
116 0 : OUStringBuffer sBuf;
117 0 : GetID(sBuf, xIndexMarkPropSet);
118 : rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_ID,
119 0 : sBuf.makeStringAndClear());
120 : }
121 :
122 : // distinguish between TOC, user, alphab. index marks by
123 : // asking for specific properties
124 : // Export attributes for -mark-start and -mark elements,
125 : // but not for -mark-end
126 : Reference<XPropertySetInfo> xPropertySetInfo =
127 0 : xIndexMarkPropSet->getPropertySetInfo();
128 0 : if (xPropertySetInfo->hasPropertyByName(sUserIndexName))
129 : {
130 : // user index mark
131 0 : pElements = lcl_pUserIndexMarkName;
132 0 : if (nElementNo != 2)
133 : {
134 0 : ExportUserIndexMarkAttributes(xIndexMarkPropSet);
135 : }
136 : }
137 0 : else if (xPropertySetInfo->hasPropertyByName(sPrimaryKey))
138 : {
139 : // alphabetical index mark
140 0 : pElements = lcl_pAlphaIndexMarkName;
141 0 : if (nElementNo != 2)
142 : {
143 0 : ExportAlphabeticalIndexMarkAttributes(xIndexMarkPropSet);
144 : }
145 : }
146 : else
147 : {
148 : // table of content:
149 0 : pElements = lcl_pTocMarkNames;
150 0 : if (nElementNo != 2)
151 : {
152 0 : ExportTOCMarkAttributes(xIndexMarkPropSet);
153 : }
154 : }
155 :
156 : // export element
157 : DBG_ASSERT(pElements != NULL, "illegal element array");
158 : DBG_ASSERT(nElementNo >= 0, "illegal name array index");
159 : DBG_ASSERT(nElementNo <= 2, "illegal name array index");
160 :
161 0 : if ((pElements != NULL) && (nElementNo != -1))
162 : {
163 : SvXMLElementExport aElem(rExport,
164 : XML_NAMESPACE_TEXT,
165 0 : pElements[nElementNo],
166 0 : sal_False, sal_False);
167 0 : }
168 : }
169 0 : }
170 :
171 0 : void XMLIndexMarkExport::ExportTOCMarkAttributes(
172 : const Reference<XPropertySet> & rPropSet)
173 : {
174 : // outline level
175 0 : sal_Int16 nLevel = 0;
176 0 : Any aAny = rPropSet->getPropertyValue(sLevel);
177 0 : aAny >>= nLevel;
178 0 : OUStringBuffer sBuf;
179 0 : ::sax::Converter::convertNumber(sBuf, static_cast<sal_Int32>(nLevel + 1));
180 : rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_OUTLINE_LEVEL,
181 0 : sBuf.makeStringAndClear());
182 0 : }
183 :
184 0 : static void lcl_ExportPropertyString( SvXMLExport& rExport,
185 : const Reference<XPropertySet> & rPropSet,
186 : const OUString & sProperty,
187 : XMLTokenEnum eToken,
188 : Any& rAny )
189 : {
190 0 : rAny = rPropSet->getPropertyValue( sProperty );
191 :
192 0 : OUString sValue;
193 0 : if( rAny >>= sValue )
194 : {
195 0 : if( !sValue.isEmpty() )
196 : {
197 0 : rExport.AddAttribute( XML_NAMESPACE_TEXT, eToken, sValue );
198 : }
199 0 : }
200 0 : }
201 :
202 0 : static void lcl_ExportPropertyBool( SvXMLExport& rExport,
203 : const Reference<XPropertySet> & rPropSet,
204 : const OUString & sProperty,
205 : XMLTokenEnum eToken,
206 : Any& rAny )
207 : {
208 0 : rAny = rPropSet->getPropertyValue( sProperty );
209 :
210 0 : sal_Bool bValue = sal_Bool();
211 0 : if( rAny >>= bValue )
212 : {
213 0 : if( bValue )
214 : {
215 0 : rExport.AddAttribute( XML_NAMESPACE_TEXT, eToken, XML_TRUE );
216 : }
217 : }
218 0 : }
219 :
220 0 : void XMLIndexMarkExport::ExportUserIndexMarkAttributes(
221 : const Reference<XPropertySet> & rPropSet)
222 : {
223 : // name of user index
224 : // (unless it's the default index; then it has no name)
225 0 : Any aAny;
226 0 : lcl_ExportPropertyString( rExport, rPropSet, sUserIndexName, XML_INDEX_NAME, aAny );
227 :
228 : // additionally export outline level; just reuse ExportTOCMarkAttributes
229 0 : ExportTOCMarkAttributes( rPropSet );
230 0 : }
231 :
232 0 : void XMLIndexMarkExport::ExportAlphabeticalIndexMarkAttributes(
233 : const Reference<XPropertySet> & rPropSet)
234 : {
235 : // primary and secondary keys (if available)
236 0 : Any aAny;
237 0 : lcl_ExportPropertyString( rExport, rPropSet, sTextReading, XML_STRING_VALUE_PHONETIC, aAny );
238 0 : lcl_ExportPropertyString( rExport, rPropSet, sPrimaryKey, XML_KEY1, aAny );
239 0 : lcl_ExportPropertyString( rExport, rPropSet, sPrimaryKeyReading, XML_KEY1_PHONETIC, aAny );
240 0 : lcl_ExportPropertyString( rExport, rPropSet, sSecondaryKey, XML_KEY2, aAny );
241 0 : lcl_ExportPropertyString( rExport, rPropSet, sSecondaryKeyReading, XML_KEY2_PHONETIC, aAny );
242 0 : lcl_ExportPropertyBool( rExport, rPropSet, sMainEntry, XML_MAIN_ENTRY, aAny );
243 0 : }
244 :
245 0 : void XMLIndexMarkExport::GetID(
246 : OUStringBuffer& sBuf,
247 : const Reference<XPropertySet> & rPropSet)
248 : {
249 : static const sal_Char sPrefix[] = "IMark";
250 :
251 : // HACK: use address of object to form identifier
252 0 : sal_Int64 nId = sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_uIntPtr>(rPropSet.get()));
253 0 : sBuf.appendAscii(sPrefix, sizeof(sPrefix)-1);
254 0 : sBuf.append(nId);
255 0 : }
256 :
257 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|