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 <tools/debug.hxx>
21 : #include <xmloff/nmspmap.hxx>
22 : #include <xmloff/xmlnmspe.hxx>
23 : #include <xmloff/xmltoken.hxx>
24 : #include <xmloff/xmluconv.hxx>
25 : #include <xmloff/attrlist.hxx>
26 : #include <xmloff/xmlprmap.hxx>
27 : #include <xmloff/xmlexppr.hxx>
28 : #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
29 : #include <com/sun/star/frame/XModel.hpp>
30 : #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
31 : #include <com/sun/star/style/XStyle.hpp>
32 : #include <com/sun/star/container/XNameContainer.hpp>
33 : #include <com/sun/star/beans/XPropertySet.hpp>
34 : #include <com/sun/star/beans/XPropertyState.hpp>
35 : #include <com/sun/star/document/XEventsSupplier.hpp>
36 : #include <com/sun/star/text/XChapterNumberingSupplier.hpp>
37 : #include <xmloff/xmlaustp.hxx>
38 : #include <xmloff/styleexp.hxx>
39 : #include <xmloff/xmlexp.hxx>
40 : #include <xmloff/XMLEventExport.hxx>
41 : #include <xmloff/maptype.hxx>
42 : #include <set>
43 : #include <boost/scoped_ptr.hpp>
44 :
45 : using namespace ::com::sun::star;
46 : using namespace ::com::sun::star::uno;
47 : using namespace ::com::sun::star::style;
48 : using namespace ::com::sun::star::container;
49 : using namespace ::com::sun::star::beans;
50 : using namespace ::com::sun::star::text;
51 : using namespace ::xmloff::token;
52 :
53 : using ::com::sun::star::document::XEventsSupplier;
54 :
55 1209 : XMLStyleExport::XMLStyleExport(
56 : SvXMLExport& rExp,
57 : const OUString& rPoolStyleName,
58 : SvXMLAutoStylePoolP *pAutoStyleP ) :
59 : rExport( rExp ),
60 : sIsPhysical( "IsPhysical" ),
61 : sIsAutoUpdate( "IsAutoUpdate" ),
62 : sFollowStyle( "FollowStyle" ),
63 : sNumberingStyleName( "NumberingStyleName" ),
64 : sOutlineLevel( "OutlineLevel" ),
65 : sPoolStyleName( rPoolStyleName ),
66 1209 : pAutoStylePool( pAutoStyleP )
67 : {
68 1209 : }
69 :
70 1209 : XMLStyleExport::~XMLStyleExport()
71 : {
72 1209 : }
73 :
74 338 : void XMLStyleExport::exportStyleAttributes( const Reference< XStyle >& )
75 : {
76 338 : }
77 :
78 338 : void XMLStyleExport::exportStyleContent( const Reference< XStyle >& )
79 : {
80 338 : }
81 :
82 12261 : bool XMLStyleExport::exportStyle(
83 : const Reference< XStyle >& rStyle,
84 : const OUString& rXMLFamily,
85 : const rtl::Reference < SvXMLExportPropertyMapper >& rPropMapper,
86 : const Reference< XNameAccess >& xStyles,
87 : const OUString* pPrefix )
88 : {
89 12261 : Reference< XPropertySet > xPropSet( rStyle, UNO_QUERY );
90 : Reference< XPropertySetInfo > xPropSetInfo =
91 24522 : xPropSet->getPropertySetInfo();
92 24522 : Any aAny;
93 :
94 : // Don't export styles that aren't existing really. This may be the
95 : // case for StarOffice Writer's pool styles.
96 12261 : if( xPropSetInfo->hasPropertyByName( sIsPhysical ) )
97 : {
98 11761 : aAny = xPropSet->getPropertyValue( sIsPhysical );
99 11761 : if( !*static_cast<sal_Bool const *>(aAny.getValue()) )
100 10763 : return false;
101 : }
102 :
103 : // <style:style ...>
104 1498 : GetExport().CheckAttrList();
105 :
106 : // style:name="..."
107 2996 : OUString sName;
108 :
109 1498 : if(pPrefix)
110 98 : sName = *pPrefix;
111 1498 : sName += rStyle->getName();
112 :
113 1498 : bool bEncoded = false;
114 2996 : const OUString sEncodedStyleName(GetExport().EncodeStyleName( sName, &bEncoded ));
115 1498 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_NAME, sEncodedStyleName );
116 :
117 1498 : if( bEncoded )
118 478 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_DISPLAY_NAME,
119 478 : sName);
120 :
121 : // style:family="..."
122 1498 : if( !rXMLFamily.isEmpty() )
123 1498 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_FAMILY, rXMLFamily);
124 :
125 1498 : if ( xPropSetInfo->hasPropertyByName( "Hidden" ) )
126 : {
127 1498 : aAny = xPropSet->getPropertyValue( "Hidden" );
128 1498 : bool bHidden = false;
129 1498 : if ( ( aAny >>= bHidden ) && bHidden && GetExport( ).getDefaultVersion( ) == SvtSaveOptions::ODFVER_LATEST )
130 0 : GetExport( ).AddAttribute( XML_NAMESPACE_STYLE, XML_HIDDEN, "true" );
131 : }
132 :
133 : // style:parent-style-name="..."
134 2996 : OUString sParentString(rStyle->getParentStyle());
135 2996 : OUString sParent;
136 :
137 1498 : if(!sParentString.isEmpty())
138 : {
139 1097 : if(pPrefix)
140 56 : sParent = *pPrefix;
141 1097 : sParent += sParentString;
142 : }
143 : else
144 401 : sParent = sPoolStyleName;
145 :
146 1498 : if( !sParent.isEmpty() )
147 1097 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_PARENT_STYLE_NAME,
148 2194 : GetExport().EncodeStyleName( sParent ) );
149 :
150 : // style:next-style-name="..." (paragraph styles only)
151 1498 : if( xPropSetInfo->hasPropertyByName( sFollowStyle ) )
152 : {
153 745 : aAny = xPropSet->getPropertyValue( sFollowStyle );
154 745 : OUString sNextName;
155 745 : aAny >>= sNextName;
156 745 : if( sName != sNextName )
157 : {
158 164 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_NEXT_STYLE_NAME,
159 328 : GetExport().EncodeStyleName( sNextName ) );
160 745 : }
161 : }
162 :
163 : // style:auto-update="..." (SW only)
164 1498 : if( xPropSetInfo->hasPropertyByName( sIsAutoUpdate ) )
165 : {
166 776 : aAny = xPropSet->getPropertyValue( sIsAutoUpdate );
167 776 : if( *static_cast<sal_Bool const *>(aAny.getValue()) )
168 2 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_AUTO_UPDATE,
169 2 : XML_TRUE );
170 : }
171 :
172 : // style:default-outline-level"..."
173 1498 : sal_Int32 nOutlineLevel = 0;
174 1498 : if( xPropSetInfo->hasPropertyByName( sOutlineLevel ) )
175 : {
176 745 : Reference< XPropertyState > xPropState( xPropSet, uno::UNO_QUERY );
177 745 : if( PropertyState_DIRECT_VALUE == xPropState->getPropertyState( sOutlineLevel ) )
178 : {
179 108 : aAny = xPropSet->getPropertyValue( sOutlineLevel );
180 108 : aAny >>= nOutlineLevel;
181 108 : if( nOutlineLevel > 0 )
182 : {
183 18 : OUStringBuffer sTmp;
184 18 : sTmp.append( static_cast<sal_Int32>(nOutlineLevel));
185 18 : GetExport().AddAttribute( XML_NAMESPACE_STYLE,
186 : XML_DEFAULT_OUTLINE_LEVEL,
187 36 : sTmp.makeStringAndClear() );
188 : }
189 : else
190 : {
191 : /* Empty value for style:default-outline-level does exist
192 : since ODF 1.2. Thus, suppress its export for former versions. (#i104889#)
193 : */
194 243 : if ( ( GetExport().getExportFlags() & SvXMLExportFlags::OASIS ) &&
195 153 : GetExport().getDefaultVersion() >= SvtSaveOptions::ODFVER_012 )
196 : {
197 63 : GetExport().AddAttribute( XML_NAMESPACE_STYLE,
198 : XML_DEFAULT_OUTLINE_LEVEL,
199 126 : OUString( "" ));
200 : }
201 : }
202 745 : }
203 : }
204 :
205 : // style:list-style-name="..." (SW paragarph styles only)
206 1498 : if( xPropSetInfo->hasPropertyByName( sNumberingStyleName ) )
207 : {
208 745 : Reference< XPropertyState > xPropState( xPropSet, uno::UNO_QUERY );
209 745 : if( PropertyState_DIRECT_VALUE ==
210 745 : xPropState->getPropertyState( sNumberingStyleName ) )
211 : {
212 32 : aAny = xPropSet->getPropertyValue( sNumberingStyleName );
213 32 : if( aAny.hasValue() )
214 : {
215 32 : OUString sListName;
216 32 : aAny >>= sListName;
217 :
218 : /* An direct set empty list style has to be written. Otherwise,
219 : this information is lost and causes an error, if the parent
220 : style has a list style set. (#i69523#)
221 : */
222 32 : if ( sListName.isEmpty() )
223 : {
224 6 : GetExport().AddAttribute( XML_NAMESPACE_STYLE,
225 : XML_LIST_STYLE_NAME,
226 6 : sListName /* empty string */);
227 : }
228 : else
229 : {
230 : // Written OpenDocument file format doesn't fit to the created text document (#i69627#)
231 26 : bool bSuppressListStyle( false );
232 : {
233 26 : if ( !GetExport().writeOutlineStyleAsNormalListStyle() )
234 : {
235 : Reference< XChapterNumberingSupplier > xCNSupplier
236 26 : (GetExport().GetModel(), UNO_QUERY);
237 :
238 52 : OUString sOutlineName;
239 26 : if (xCNSupplier.is())
240 : {
241 : Reference< XIndexReplace > xNumRule
242 26 : ( xCNSupplier->getChapterNumberingRules() );
243 : assert(xNumRule.is());
244 :
245 : Reference< XPropertySet > xNumRulePropSet
246 52 : (xNumRule, UNO_QUERY);
247 26 : xNumRulePropSet->getPropertyValue("Name")
248 26 : >>= sOutlineName;
249 52 : bSuppressListStyle = sListName == sOutlineName;
250 26 : }
251 : }
252 : }
253 :
254 26 : if ( !sListName.isEmpty() && !bSuppressListStyle )
255 : {
256 14 : GetExport().AddAttribute( XML_NAMESPACE_STYLE,
257 : XML_LIST_STYLE_NAME,
258 28 : GetExport().EncodeStyleName( sListName ) );
259 : }
260 32 : }
261 : }
262 : }
263 713 : else if( nOutlineLevel > 0 )
264 : {
265 :
266 2 : bool bNoInheritedListStyle( true );
267 :
268 2 : Reference<XStyle> xStyle( xPropState, UNO_QUERY );
269 6 : while ( xStyle.is() )
270 : {
271 4 : OUString aParentStyle( xStyle->getParentStyle() );
272 4 : if ( aParentStyle.isEmpty() || !xStyles->hasByName( aParentStyle ) )
273 : {
274 2 : break;
275 : }
276 : else
277 : {
278 2 : xPropState = Reference< XPropertyState >( xStyles->getByName( aParentStyle ), UNO_QUERY );
279 2 : if ( !xPropState.is() )
280 : {
281 0 : break;
282 : }
283 2 : if ( xPropState->getPropertyState( sNumberingStyleName ) == PropertyState_DIRECT_VALUE )
284 : {
285 0 : bNoInheritedListStyle = false;
286 0 : break;
287 : }
288 : else
289 : {
290 2 : xStyle = Reference<XStyle>( xPropState, UNO_QUERY );
291 : }
292 : }
293 2 : }
294 2 : if ( bNoInheritedListStyle )
295 2 : GetExport().AddAttribute( XML_NAMESPACE_STYLE,
296 : XML_LIST_STYLE_NAME,
297 4 : OUString( "" ));
298 745 : }
299 : }
300 :
301 : // style:pool-id="..." is not required any longer since we use
302 : // english style names only
303 1498 : exportStyleAttributes( rStyle );
304 :
305 : // TODO: style:help-file-name="..." and style:help-id="..." can neither
306 : // be modified by UI nor by API and that for, have not to be exported
307 : // currently.
308 :
309 : {
310 : // <style:style>
311 1498 : SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE, XML_STYLE,
312 1498 : true, true );
313 :
314 1498 : rPropMapper->SetStyleName( sName );
315 :
316 : // <style:properties>
317 : ::std::vector< XMLPropertyState > xPropStates =
318 2996 : rPropMapper->Filter( xPropSet, true );
319 1498 : rPropMapper->exportXML( GetExport(), xPropStates,
320 1498 : SvXmlExportFlags::IGN_WS );
321 :
322 1498 : rPropMapper->SetStyleName( OUString() );
323 :
324 1498 : exportStyleContent( rStyle );
325 :
326 : // <script:events>, if they are supported by this style
327 2996 : Reference<XEventsSupplier> xEventsSupp(rStyle, UNO_QUERY);
328 2996 : GetExport().GetEventExport().Export(xEventsSupp);
329 : }
330 13759 : return true;
331 : }
332 :
333 347 : bool XMLStyleExport::exportDefaultStyle(
334 : const Reference< XPropertySet >& xPropSet,
335 : const OUString& rXMLFamily,
336 : const rtl::Reference < SvXMLExportPropertyMapper >& rPropMapper )
337 : {
338 : Reference< XPropertySetInfo > xPropSetInfo =
339 347 : xPropSet->getPropertySetInfo();
340 :
341 : // <style:default-style ...>
342 347 : GetExport().CheckAttrList();
343 :
344 : {
345 : // style:family="..."
346 347 : if( !rXMLFamily.isEmpty() )
347 347 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_FAMILY,
348 347 : rXMLFamily );
349 : // <style:style>
350 347 : SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE,
351 : XML_DEFAULT_STYLE,
352 347 : true, true );
353 : // <style:properties>
354 : ::std::vector< XMLPropertyState > xPropStates =
355 694 : rPropMapper->FilterDefaults( xPropSet );
356 347 : rPropMapper->exportXML( GetExport(), xPropStates,
357 694 : SvXmlExportFlags::IGN_WS );
358 : }
359 347 : return true;
360 : }
361 :
362 333 : void XMLStyleExport::exportStyleFamily(
363 : const sal_Char *pFamily,
364 : const OUString& rXMLFamily,
365 : const rtl::Reference < SvXMLExportPropertyMapper >& rPropMapper,
366 : bool bUsed, sal_uInt16 nFamily, const OUString* pPrefix)
367 : {
368 333 : const OUString sFamily(OUString::createFromAscii(pFamily ));
369 : exportStyleFamily( sFamily, rXMLFamily, rPropMapper, bUsed, nFamily,
370 333 : pPrefix);
371 333 : }
372 :
373 369 : void XMLStyleExport::exportStyleFamily(
374 : const OUString& rFamily, const OUString& rXMLFamily,
375 : const rtl::Reference < SvXMLExportPropertyMapper >& rPropMapper,
376 : bool bUsed, sal_uInt16 nFamily, const OUString* pPrefix)
377 : {
378 : assert(GetExport().GetModel().is());
379 369 : Reference< XStyleFamiliesSupplier > xFamiliesSupp( GetExport().GetModel(), UNO_QUERY );
380 369 : if( !xFamiliesSupp.is() )
381 0 : return; // family not available in current model
382 :
383 654 : Reference< XNameAccess > xStyleCont;
384 :
385 654 : Reference< XNameAccess > xFamilies( xFamiliesSupp->getStyleFamilies() );
386 369 : if( xFamilies->hasByName( rFamily ) )
387 285 : xFamilies->getByName( rFamily ) >>= xStyleCont;
388 :
389 369 : if( !xStyleCont.is() )
390 84 : return;
391 :
392 : // If next styles are supported and used styles should be exported only,
393 : // the next style may be unused but has to be exported, too. In this case
394 : // the names of all exported styles are remembered.
395 570 : boost::scoped_ptr<std::set<OUString> > pExportedStyles(0);
396 285 : bool bFirstStyle = true;
397 :
398 570 : const uno::Sequence< OUString> aSeq = xStyleCont->getElementNames();
399 285 : const OUString* pIter = aSeq.getConstArray();
400 285 : const OUString* pEnd = pIter + aSeq.getLength();
401 13056 : for(;pIter != pEnd;++pIter)
402 : {
403 12771 : Reference< XStyle > xStyle;
404 : try
405 : {
406 12771 : xStyleCont->getByName( *pIter ) >>= xStyle;
407 : }
408 0 : catch(const lang::IndexOutOfBoundsException&)
409 : {
410 : // due to bugs in prior versions it is possible that
411 : // a binary file is missing some critical styles.
412 : // The only possible way to deal with this is to
413 : // not export them here and remain silent.
414 0 : continue;
415 : }
416 :
417 : assert(xStyle.is());
418 12771 : if (!bUsed || xStyle->isInUse())
419 : {
420 : bool bExported = exportStyle( xStyle, rXMLFamily, rPropMapper,
421 12261 : xStyleCont,pPrefix );
422 12261 : if (bUsed && bFirstStyle && bExported)
423 : {
424 : // If this is the first style, find out whether next styles
425 : // are supported.
426 0 : Reference< XPropertySet > xPropSet( xStyle, UNO_QUERY );
427 : Reference< XPropertySetInfo > xPropSetInfo =
428 0 : xPropSet->getPropertySetInfo();
429 :
430 0 : if (xPropSetInfo->hasPropertyByName( sFollowStyle ))
431 0 : pExportedStyles.reset(new std::set<OUString>());
432 0 : bFirstStyle = false;
433 : }
434 :
435 12261 : if (pExportedStyles && bExported)
436 : {
437 : // If next styles are supported, remember this style's name.
438 0 : pExportedStyles->insert( xStyle->getName() );
439 : }
440 : }
441 :
442 : // if an auto style pool is given, remember this style's name as a
443 : // style name that must not be used by automatic styles.
444 12771 : if (pAutoStylePool)
445 12771 : pAutoStylePool->RegisterName( nFamily, xStyle->getName() );
446 12771 : }
447 :
448 285 : if( pExportedStyles )
449 : {
450 : // if next styles are supported, export all next styles that are
451 : // unused and that for, haven't been exported in the first loop.
452 0 : pIter = aSeq.getConstArray();
453 0 : for(;pIter != pEnd;++pIter)
454 : {
455 0 : Reference< XStyle > xStyle;
456 0 : xStyleCont->getByName( *pIter ) >>= xStyle;
457 :
458 : assert(xStyle.is());
459 :
460 0 : Reference< XPropertySet > xPropSet( xStyle, UNO_QUERY );
461 0 : Reference< XPropertySetInfo > xPropSetInfo( xPropSet->getPropertySetInfo() );
462 :
463 : // styles that aren't existing really are ignored.
464 0 : if (xPropSetInfo->hasPropertyByName( sIsPhysical ))
465 : {
466 0 : Any aAny( xPropSet->getPropertyValue( sIsPhysical ) );
467 0 : if (!*static_cast<sal_Bool const *>(aAny.getValue()))
468 0 : continue;
469 : }
470 :
471 0 : if (!xStyle->isInUse())
472 0 : continue;
473 :
474 0 : if (!xPropSetInfo->hasPropertyByName( sFollowStyle ))
475 : {
476 0 : continue;
477 : }
478 :
479 0 : OUString sNextName;
480 0 : xPropSet->getPropertyValue( sFollowStyle ) >>= sNextName;
481 0 : OUString sTmp( sNextName );
482 : // if the next style hasn't been exported by now, export it now
483 : // and remember its name.
484 0 : if (xStyle->getName() != sNextName &&
485 0 : 0 == pExportedStyles->count( sTmp ))
486 : {
487 0 : xStyleCont->getByName( sNextName ) >>= xStyle;
488 : assert(xStyle.is());
489 :
490 0 : if (exportStyle(xStyle, rXMLFamily, rPropMapper, xStyleCont, pPrefix))
491 0 : pExportedStyles->insert( sTmp );
492 : }
493 0 : }
494 285 : }
495 : }
496 :
497 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|