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 2094 : 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 2094 : pAutoStylePool( pAutoStyleP )
67 : {
68 2094 : }
69 :
70 2094 : XMLStyleExport::~XMLStyleExport()
71 : {
72 2094 : }
73 :
74 436 : void XMLStyleExport::exportStyleAttributes( const Reference< XStyle >& )
75 : {
76 436 : }
77 :
78 436 : void XMLStyleExport::exportStyleContent( const Reference< XStyle >& )
79 : {
80 436 : }
81 :
82 10260 : 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 10260 : Reference< XPropertySet > xPropSet( rStyle, UNO_QUERY );
90 : Reference< XPropertySetInfo > xPropSetInfo =
91 20520 : xPropSet->getPropertySetInfo();
92 20520 : 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 10260 : if( xPropSetInfo->hasPropertyByName( sIsPhysical ) )
97 : {
98 9550 : aAny = xPropSet->getPropertyValue( sIsPhysical );
99 9550 : if( !*(sal_Bool *)aAny.getValue() )
100 8892 : return false;
101 : }
102 :
103 : // <style:style ...>
104 1368 : GetExport().CheckAttrList();
105 :
106 : // style:name="..."
107 2736 : OUString sName;
108 :
109 1368 : if(pPrefix)
110 84 : sName = *pPrefix;
111 1368 : sName += rStyle->getName();
112 :
113 1368 : bool bEncoded = false;
114 2736 : const OUString sEncodedStyleName(GetExport().EncodeStyleName( sName, &bEncoded ));
115 1368 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_NAME, sEncodedStyleName );
116 :
117 1368 : if( bEncoded )
118 332 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_DISPLAY_NAME,
119 332 : sName);
120 :
121 : // style:family="..."
122 1368 : if( !rXMLFamily.isEmpty() )
123 1368 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_FAMILY, rXMLFamily);
124 :
125 1368 : if ( xPropSetInfo->hasPropertyByName( "Hidden" ) )
126 : {
127 1368 : aAny = xPropSet->getPropertyValue( "Hidden" );
128 1368 : bool bHidden = false;
129 1368 : 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 2736 : OUString sParentString(rStyle->getParentStyle());
135 2736 : OUString sParent;
136 :
137 1368 : if(!sParentString.isEmpty())
138 : {
139 1070 : if(pPrefix)
140 48 : sParent = *pPrefix;
141 1070 : sParent += sParentString;
142 : }
143 : else
144 298 : sParent = sPoolStyleName;
145 :
146 1368 : if( !sParent.isEmpty() )
147 1070 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_PARENT_STYLE_NAME,
148 2140 : GetExport().EncodeStyleName( sParent ) );
149 :
150 : // style:next-style-name="..." (paragraph styles only)
151 1368 : if( xPropSetInfo->hasPropertyByName( sFollowStyle ) )
152 : {
153 504 : aAny = xPropSet->getPropertyValue( sFollowStyle );
154 504 : OUString sNextName;
155 504 : aAny >>= sNextName;
156 504 : if( sName != sNextName )
157 : {
158 66 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_NEXT_STYLE_NAME,
159 132 : GetExport().EncodeStyleName( sNextName ) );
160 504 : }
161 : }
162 :
163 : // style:auto-update="..." (SW only)
164 1368 : if( xPropSetInfo->hasPropertyByName( sIsAutoUpdate ) )
165 : {
166 534 : aAny = xPropSet->getPropertyValue( sIsAutoUpdate );
167 534 : if( *(sal_Bool *)aAny.getValue() )
168 0 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_AUTO_UPDATE,
169 0 : XML_TRUE );
170 : }
171 :
172 : // style:default-outline-level"..."
173 1368 : sal_Int32 nOutlineLevel = 0;
174 1368 : if( xPropSetInfo->hasPropertyByName( sOutlineLevel ) )
175 : {
176 504 : Reference< XPropertyState > xPropState( xPropSet, uno::UNO_QUERY );
177 504 : if( PropertyState_DIRECT_VALUE == xPropState->getPropertyState( sOutlineLevel ) )
178 : {
179 84 : aAny = xPropSet->getPropertyValue( sOutlineLevel );
180 84 : aAny >>= nOutlineLevel;
181 84 : if( nOutlineLevel > 0 )
182 : {
183 0 : OUStringBuffer sTmp;
184 0 : sTmp.append( static_cast<sal_Int32>(nOutlineLevel));
185 0 : GetExport().AddAttribute( XML_NAMESPACE_STYLE,
186 : XML_DEFAULT_OUTLINE_LEVEL,
187 0 : 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 168 : if ( ( GetExport().getExportFlags() & EXPORT_OASIS ) != 0 &&
195 84 : GetExport().getDefaultVersion() >= SvtSaveOptions::ODFVER_012 )
196 : {
197 84 : GetExport().AddAttribute( XML_NAMESPACE_STYLE,
198 : XML_DEFAULT_OUTLINE_LEVEL,
199 168 : OUString( "" ));
200 : }
201 : }
202 504 : }
203 : }
204 :
205 : // style:list-style-name="..." (SW paragarph styles only)
206 1368 : if( xPropSetInfo->hasPropertyByName( sNumberingStyleName ) )
207 : {
208 504 : Reference< XPropertyState > xPropState( xPropSet, uno::UNO_QUERY );
209 504 : if( PropertyState_DIRECT_VALUE ==
210 504 : xPropState->getPropertyState( sNumberingStyleName ) )
211 : {
212 0 : aAny = xPropSet->getPropertyValue( sNumberingStyleName );
213 0 : if( aAny.hasValue() )
214 : {
215 0 : OUString sListName;
216 0 : 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 0 : if ( sListName.isEmpty() )
223 : {
224 0 : GetExport().AddAttribute( XML_NAMESPACE_STYLE,
225 : XML_LIST_STYLE_NAME,
226 0 : sListName /* empty string */);
227 : }
228 : else
229 : {
230 : // Written OpenDocument file format doesn't fit to the created text document (#i69627#)
231 0 : bool bSuppressListStyle( false );
232 : {
233 0 : if ( !GetExport().writeOutlineStyleAsNormalListStyle() )
234 : {
235 : Reference< XChapterNumberingSupplier > xCNSupplier
236 0 : (GetExport().GetModel(), UNO_QUERY);
237 :
238 0 : OUString sOutlineName;
239 0 : if (xCNSupplier.is())
240 : {
241 : Reference< XIndexReplace > xNumRule
242 0 : ( xCNSupplier->getChapterNumberingRules() );
243 : DBG_ASSERT( xNumRule.is(), "no chapter numbering rules" );
244 :
245 0 : if (xNumRule.is())
246 : {
247 : Reference< XPropertySet > xNumRulePropSet
248 0 : (xNumRule, UNO_QUERY);
249 0 : xNumRulePropSet->getPropertyValue(
250 0 : OUString("Name") )
251 0 : >>= sOutlineName;
252 0 : bSuppressListStyle = ( sListName == sOutlineName );
253 0 : }
254 0 : }
255 : }
256 : }
257 :
258 0 : if ( !sListName.isEmpty() && !bSuppressListStyle )
259 : {
260 0 : GetExport().AddAttribute( XML_NAMESPACE_STYLE,
261 : XML_LIST_STYLE_NAME,
262 0 : GetExport().EncodeStyleName( sListName ) );
263 : }
264 0 : }
265 : }
266 : }
267 504 : else if( nOutlineLevel > 0 )
268 : {
269 :
270 0 : bool bNoInheritedListStyle( true );
271 :
272 0 : Reference<XStyle> xStyle( xPropState, UNO_QUERY );
273 0 : while ( xStyle.is() )
274 : {
275 0 : OUString aParentStyle( xStyle->getParentStyle() );
276 0 : if ( aParentStyle.isEmpty() || !xStyles->hasByName( aParentStyle ) )
277 : {
278 0 : break;
279 : }
280 : else
281 : {
282 0 : xPropState = Reference< XPropertyState >( xStyles->getByName( aParentStyle ), UNO_QUERY );
283 0 : if ( !xPropState.is() )
284 : {
285 0 : break;
286 : }
287 0 : if ( xPropState->getPropertyState( sNumberingStyleName ) == PropertyState_DIRECT_VALUE )
288 : {
289 0 : bNoInheritedListStyle = false;
290 0 : break;
291 : }
292 : else
293 : {
294 0 : xStyle = Reference<XStyle>( xPropState, UNO_QUERY );
295 : }
296 : }
297 0 : }
298 0 : if ( bNoInheritedListStyle )
299 0 : GetExport().AddAttribute( XML_NAMESPACE_STYLE,
300 : XML_LIST_STYLE_NAME,
301 0 : OUString( "" ));
302 504 : }
303 : }
304 :
305 : // style:pool-id="..." is not required any longer since we use
306 : // english style names only
307 1368 : exportStyleAttributes( rStyle );
308 :
309 : // TODO: style:help-file-name="..." and style:help-id="..." can neither
310 : // be modified by UI nor by API and that for, have not to be exported
311 : // currently.
312 :
313 : {
314 : // <style:style>
315 1368 : SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE, XML_STYLE,
316 1368 : true, true );
317 :
318 1368 : rPropMapper->SetStyleName( sName );
319 :
320 : // <style:properties>
321 : ::std::vector< XMLPropertyState > xPropStates =
322 2736 : rPropMapper->Filter( xPropSet, true );
323 1368 : rPropMapper->exportXML( GetExport(), xPropStates,
324 1368 : XML_EXPORT_FLAG_IGN_WS );
325 :
326 1368 : rPropMapper->SetStyleName( OUString() );
327 :
328 1368 : exportStyleContent( rStyle );
329 :
330 : // <script:events>, if they are supported by this style
331 2736 : Reference<XEventsSupplier> xEventsSupp(rStyle, UNO_QUERY);
332 2736 : GetExport().GetEventExport().Export(xEventsSupp);
333 : }
334 11628 : return true;
335 : }
336 :
337 322 : bool XMLStyleExport::exportDefaultStyle(
338 : const Reference< XPropertySet >& xPropSet,
339 : const OUString& rXMLFamily,
340 : const rtl::Reference < SvXMLExportPropertyMapper >& rPropMapper )
341 : {
342 : Reference< XPropertySetInfo > xPropSetInfo =
343 322 : xPropSet->getPropertySetInfo();
344 :
345 : // <style:default-style ...>
346 322 : GetExport().CheckAttrList();
347 :
348 : {
349 : // style:family="..."
350 322 : if( !rXMLFamily.isEmpty() )
351 322 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_FAMILY,
352 322 : rXMLFamily );
353 : // <style:style>
354 322 : SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE,
355 : XML_DEFAULT_STYLE,
356 322 : true, true );
357 : // <style:properties>
358 : ::std::vector< XMLPropertyState > xPropStates =
359 644 : rPropMapper->FilterDefaults( xPropSet );
360 322 : rPropMapper->exportXML( GetExport(), xPropStates,
361 644 : XML_EXPORT_FLAG_IGN_WS );
362 : }
363 322 : return true;
364 : }
365 :
366 296 : void XMLStyleExport::exportStyleFamily(
367 : const sal_Char *pFamily,
368 : const OUString& rXMLFamily,
369 : const rtl::Reference < SvXMLExportPropertyMapper >& rPropMapper,
370 : bool bUsed, sal_uInt16 nFamily, const OUString* pPrefix)
371 : {
372 296 : const OUString sFamily(OUString::createFromAscii(pFamily ));
373 : exportStyleFamily( sFamily, rXMLFamily, rPropMapper, bUsed, nFamily,
374 296 : pPrefix);
375 296 : }
376 :
377 350 : void XMLStyleExport::exportStyleFamily(
378 : const OUString& rFamily, const OUString& rXMLFamily,
379 : const rtl::Reference < SvXMLExportPropertyMapper >& rPropMapper,
380 : bool bUsed, sal_uInt16 nFamily, const OUString* pPrefix)
381 : {
382 : DBG_ASSERT( GetExport().GetModel().is(), "There is the model?" );
383 350 : Reference< XStyleFamiliesSupplier > xFamiliesSupp( GetExport().GetModel(), UNO_QUERY );
384 350 : if( !xFamiliesSupp.is() )
385 0 : return; // family not available in current model
386 :
387 628 : Reference< XNameAccess > xStyleCont;
388 :
389 628 : Reference< XNameAccess > xFamilies( xFamiliesSupp->getStyleFamilies() );
390 350 : if( xFamilies->hasByName( rFamily ) )
391 278 : xFamilies->getByName( rFamily ) >>= xStyleCont;
392 :
393 350 : if( !xStyleCont.is() )
394 72 : return;
395 :
396 : // If next styles are supported and used styles should be exported only,
397 : // the next style may be unused but has to be exported, too. In this case
398 : // the names of all exported styles are remembered.
399 556 : boost::scoped_ptr<std::set<OUString> > pExportedStyles(0);
400 278 : bool bFirstStyle = true;
401 :
402 556 : const uno::Sequence< OUString> aSeq = xStyleCont->getElementNames();
403 278 : const OUString* pIter = aSeq.getConstArray();
404 278 : const OUString* pEnd = pIter + aSeq.getLength();
405 11286 : for(;pIter != pEnd;++pIter)
406 : {
407 11008 : Reference< XStyle > xStyle;
408 : try
409 : {
410 11008 : xStyleCont->getByName( *pIter ) >>= xStyle;
411 : }
412 0 : catch(const lang::IndexOutOfBoundsException&)
413 : {
414 : // due to bugs in prior versions it is possible that
415 : // a binary file is missing some critical styles.
416 : // The only possible way to deal with this is to
417 : // not export them here and remain silent.
418 0 : continue;
419 : }
420 :
421 : DBG_ASSERT( xStyle.is(), "Style not found for export!" );
422 11008 : if( xStyle.is() )
423 : {
424 11008 : if( !bUsed || xStyle->isInUse() )
425 : {
426 : bool bExported = exportStyle( xStyle, rXMLFamily, rPropMapper,
427 10260 : xStyleCont,pPrefix );
428 10260 : if( bUsed && bFirstStyle && bExported )
429 : {
430 : // If this is the first style, find out whether next styles
431 : // are supported.
432 0 : Reference< XPropertySet > xPropSet( xStyle, UNO_QUERY );
433 : Reference< XPropertySetInfo > xPropSetInfo =
434 0 : xPropSet->getPropertySetInfo();
435 :
436 0 : if( xPropSetInfo->hasPropertyByName( sFollowStyle ) )
437 0 : pExportedStyles.reset(new std::set<OUString>());
438 0 : bFirstStyle = false;
439 : }
440 :
441 10260 : if( pExportedStyles && bExported )
442 : {
443 : // If next styles are supported, remember this style's name.
444 0 : pExportedStyles->insert( xStyle->getName() );
445 : }
446 : }
447 :
448 : // if an auto style pool is given, remember this style's name as a
449 : // style name that must not be used by automatic styles.
450 11008 : if( pAutoStylePool )
451 11008 : pAutoStylePool->RegisterName( nFamily, xStyle->getName() );
452 : }
453 11008 : }
454 :
455 278 : if( pExportedStyles )
456 : {
457 : // if next styles are supported, export all next styles that are
458 : // unused and that for, haven't been exported in the first loop.
459 0 : pIter = aSeq.getConstArray();
460 0 : for(;pIter != pEnd;++pIter)
461 : {
462 0 : Reference< XStyle > xStyle;
463 0 : xStyleCont->getByName( *pIter ) >>= xStyle;
464 :
465 : DBG_ASSERT( xStyle.is(), "Style not found for export!" );
466 0 : if( xStyle.is() )
467 : {
468 0 : Reference< XPropertySet > xPropSet( xStyle, UNO_QUERY );
469 0 : Reference< XPropertySetInfo > xPropSetInfo( xPropSet->getPropertySetInfo() );
470 :
471 : // styles that aren't existing really are ignored.
472 0 : if( xPropSetInfo->hasPropertyByName( sIsPhysical ) )
473 : {
474 0 : Any aAny( xPropSet->getPropertyValue( sIsPhysical ) );
475 0 : if( !*(sal_Bool *)aAny.getValue() )
476 0 : continue;
477 : }
478 :
479 0 : if( !xStyle->isInUse() )
480 0 : continue;
481 :
482 0 : if( !xPropSetInfo->hasPropertyByName( sFollowStyle ) )
483 : {
484 : DBG_ASSERT( sFollowStyle.isEmpty(), "no follow style???" );
485 0 : continue;
486 : }
487 :
488 0 : OUString sNextName;
489 0 : xPropSet->getPropertyValue( sFollowStyle ) >>= sNextName;
490 0 : OUString sTmp( sNextName );
491 : // if the next style hasn't been exported by now, export it now
492 : // and remember its name.
493 0 : if( xStyle->getName() != sNextName &&
494 0 : 0 == pExportedStyles->count( sTmp ) )
495 : {
496 0 : xStyleCont->getByName( sNextName ) >>= xStyle;
497 : DBG_ASSERT( xStyle.is(), "Style not found for export!" );
498 :
499 0 : if( xStyle.is() && exportStyle( xStyle, rXMLFamily, rPropMapper, xStyleCont, pPrefix ) )
500 0 : pExportedStyles->insert( sTmp );
501 0 : }
502 : }
503 0 : }
504 278 : }
505 : }
506 :
507 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|