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