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 128 : XMLStyleExport::XMLStyleExport(
55 : SvXMLExport& rExp,
56 : const OUString& rPoolStyleName,
57 : SvXMLAutoStylePoolP *pAutoStyleP ) :
58 : rExport( rExp ),
59 : sIsPhysical( "IsPhysical" ),
60 : sIsAutoUpdate( "IsAutoUpdate" ),
61 : sFollowStyle( "FollowStyle" ),
62 : sNumberingStyleName( "NumberingStyleName" ),
63 : sOutlineLevel( "OutlineLevel" ),
64 : sPoolStyleName( rPoolStyleName ),
65 128 : pAutoStylePool( pAutoStyleP )
66 : {
67 128 : }
68 :
69 128 : XMLStyleExport::~XMLStyleExport()
70 : {
71 128 : }
72 :
73 124 : void XMLStyleExport::exportStyleAttributes( const Reference< XStyle >& )
74 : {
75 124 : }
76 :
77 124 : void XMLStyleExport::exportStyleContent( const Reference< XStyle >& )
78 : {
79 124 : }
80 :
81 2737 : 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 2737 : Reference< XPropertySet > xPropSet( rStyle, UNO_QUERY );
89 : Reference< XPropertySetInfo > xPropSetInfo =
90 5474 : xPropSet->getPropertySetInfo();
91 5474 : 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 2737 : if( xPropSetInfo->hasPropertyByName( sIsPhysical ) )
96 : {
97 2565 : aAny = xPropSet->getPropertyValue( sIsPhysical );
98 2565 : if( !*(sal_Bool *)aAny.getValue() )
99 2383 : return sal_False;
100 : }
101 :
102 : // <style:style ...>
103 354 : GetExport().CheckAttrList();
104 :
105 : // style:name="..."
106 708 : OUString sName;
107 :
108 354 : if(pPrefix)
109 28 : sName = *pPrefix;
110 354 : sName += rStyle->getName();
111 :
112 354 : sal_Bool bEncoded = sal_False;
113 708 : const OUString sEncodedStyleName(GetExport().EncodeStyleName( sName, &bEncoded ));
114 354 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_NAME, sEncodedStyleName );
115 :
116 354 : if( bEncoded )
117 96 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_DISPLAY_NAME,
118 96 : sName);
119 :
120 : // style:family="..."
121 354 : if( !rXMLFamily.isEmpty() )
122 354 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_FAMILY, rXMLFamily);
123 :
124 354 : if ( xPropSetInfo->hasPropertyByName( "Hidden" ) )
125 : {
126 354 : aAny = xPropSet->getPropertyValue( "Hidden" );
127 354 : sal_Bool bHidden = sal_False;
128 354 : 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 708 : OUString sParentString(rStyle->getParentStyle());
134 708 : OUString sParent;
135 :
136 354 : if(!sParentString.isEmpty())
137 : {
138 277 : if(pPrefix)
139 16 : sParent = *pPrefix;
140 277 : sParent += sParentString;
141 : }
142 : else
143 77 : sParent = sPoolStyleName;
144 :
145 354 : if( !sParent.isEmpty() )
146 277 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_PARENT_STYLE_NAME,
147 554 : GetExport().EncodeStyleName( sParent ) );
148 :
149 : // style:next-style-name="..." (paragraph styles only)
150 354 : if( xPropSetInfo->hasPropertyByName( sFollowStyle ) )
151 : {
152 130 : aAny = xPropSet->getPropertyValue( sFollowStyle );
153 130 : OUString sNextName;
154 130 : aAny >>= sNextName;
155 130 : if( sName != sNextName )
156 : {
157 15 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_NEXT_STYLE_NAME,
158 30 : GetExport().EncodeStyleName( sNextName ) );
159 130 : }
160 : }
161 :
162 : // style:auto-update="..." (SW only)
163 354 : if( xPropSetInfo->hasPropertyByName( sIsAutoUpdate ) )
164 : {
165 137 : aAny = xPropSet->getPropertyValue( sIsAutoUpdate );
166 137 : 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 354 : sal_Int32 nOutlineLevel = 0;
173 354 : if( xPropSetInfo->hasPropertyByName( sOutlineLevel ) )
174 : {
175 130 : Reference< XPropertyState > xPropState( xPropSet, uno::UNO_QUERY );
176 130 : if( PropertyState_DIRECT_VALUE == xPropState->getPropertyState( sOutlineLevel ) )
177 : {
178 34 : aAny = xPropSet->getPropertyValue( sOutlineLevel );
179 34 : aAny >>= nOutlineLevel;
180 34 : 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 68 : if ( ( GetExport().getExportFlags() & EXPORT_OASIS ) != 0 &&
194 34 : GetExport().getDefaultVersion() >= SvtSaveOptions::ODFVER_012 )
195 : {
196 34 : GetExport().AddAttribute( XML_NAMESPACE_STYLE,
197 : XML_DEFAULT_OUTLINE_LEVEL,
198 68 : OUString( "" ));
199 : }
200 : }
201 130 : }
202 : }
203 :
204 : // style:list-style-name="..." (SW paragarph styles only)
205 354 : if( xPropSetInfo->hasPropertyByName( sNumberingStyleName ) )
206 : {
207 130 : Reference< XPropertyState > xPropState( xPropSet, uno::UNO_QUERY );
208 130 : if( PropertyState_DIRECT_VALUE ==
209 130 : 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("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 130 : 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 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 : /////////////////////////////////////////////////
299 0 : if ( bNoInheritedListStyle )
300 0 : GetExport().AddAttribute( XML_NAMESPACE_STYLE,
301 : XML_LIST_STYLE_NAME,
302 0 : OUString( "" ));
303 130 : }
304 : }
305 :
306 : // style:pool-id="..." is not required any longer since we use
307 : // english style names only
308 354 : 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 354 : SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE, XML_STYLE,
317 354 : sal_True, sal_True );
318 :
319 354 : rPropMapper->SetStyleName( sName );
320 :
321 : // <style:properties>
322 : ::std::vector< XMLPropertyState > xPropStates =
323 708 : rPropMapper->Filter( xPropSet );
324 354 : rPropMapper->exportXML( GetExport(), xPropStates,
325 354 : XML_EXPORT_FLAG_IGN_WS );
326 :
327 354 : rPropMapper->SetStyleName( OUString() );
328 :
329 354 : exportStyleContent( rStyle );
330 :
331 : // <script:events>, if they are supported by this style
332 708 : Reference<XEventsSupplier> xEventsSupp(rStyle, UNO_QUERY);
333 708 : GetExport().GetEventExport().Export(xEventsSupp);
334 : }
335 3091 : return sal_True;
336 : }
337 :
338 80 : sal_Bool XMLStyleExport::exportDefaultStyle(
339 : const Reference< XPropertySet >& xPropSet,
340 : const OUString& rXMLFamily,
341 : const UniReference < SvXMLExportPropertyMapper >& rPropMapper )
342 : {
343 : Reference< XPropertySetInfo > xPropSetInfo =
344 80 : xPropSet->getPropertySetInfo();
345 :
346 160 : Any aAny;
347 :
348 : // <style:default-style ...>
349 80 : GetExport().CheckAttrList();
350 :
351 : {
352 : // style:family="..."
353 80 : if( !rXMLFamily.isEmpty() )
354 80 : GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_FAMILY,
355 80 : rXMLFamily );
356 : // <style:style>
357 80 : SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE,
358 : XML_DEFAULT_STYLE,
359 80 : sal_True, sal_True );
360 : // <style:properties>
361 : ::std::vector< XMLPropertyState > xPropStates =
362 160 : rPropMapper->FilterDefaults( xPropSet );
363 80 : rPropMapper->exportXML( GetExport(), xPropStates,
364 160 : XML_EXPORT_FLAG_IGN_WS );
365 : }
366 160 : return sal_True;
367 : }
368 :
369 78 : 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 78 : const OUString sFamily(OUString::createFromAscii(pFamily ));
376 : exportStyleFamily( sFamily, rXMLFamily, rPropMapper, bUsed, nFamily,
377 78 : pPrefix);
378 78 : }
379 :
380 88 : 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 88 : Reference< XStyleFamiliesSupplier > xFamiliesSupp( GetExport().GetModel(), UNO_QUERY );
387 88 : if( !xFamiliesSupp.is() )
388 0 : return; // family not available in current model
389 :
390 158 : Reference< XNameAccess > xStyleCont;
391 :
392 158 : Reference< XNameAccess > xFamilies( xFamiliesSupp->getStyleFamilies() );
393 88 : if( xFamilies->hasByName( rFamily ) )
394 70 : xFamilies->getByName( rFamily ) >>= xStyleCont;
395 :
396 88 : if( !xStyleCont.is() )
397 18 : return;
398 :
399 : // If next styles are supported and used styles should be exported only,
400 : // the next style may be unused but has to be exported, too. In this case
401 : // the names of all exported styles are remembered.
402 140 : boost::scoped_ptr<std::set<OUString> > pExportedStyles(0);
403 70 : sal_Bool bFirstStyle = sal_True;
404 :
405 140 : const uno::Sequence< OUString> aSeq = xStyleCont->getElementNames();
406 70 : const OUString* pIter = aSeq.getConstArray();
407 70 : const OUString* pEnd = pIter + aSeq.getLength();
408 3011 : for(;pIter != pEnd;++pIter)
409 : {
410 2941 : Reference< XStyle > xStyle;
411 : try
412 : {
413 2941 : xStyleCont->getByName( *pIter ) >>= xStyle;
414 : }
415 0 : catch(const lang::IndexOutOfBoundsException&)
416 : {
417 : // due to bugs in prior versions it is possible that
418 : // a binary file is missing some critical styles.
419 : // The only possible way to deal with this is to
420 : // not export them here and remain silent.
421 0 : continue;
422 : }
423 :
424 : DBG_ASSERT( xStyle.is(), "Style not found for export!" );
425 2941 : if( xStyle.is() )
426 : {
427 2941 : if( !bUsed || xStyle->isInUse() )
428 : {
429 : sal_Bool bExported = exportStyle( xStyle, rXMLFamily, rPropMapper,
430 2737 : xStyleCont,pPrefix );
431 2737 : if( bUsed && bFirstStyle && bExported )
432 : {
433 : // If this is the first style, find out whether next styles
434 : // are supported.
435 0 : Reference< XPropertySet > xPropSet( xStyle, UNO_QUERY );
436 : Reference< XPropertySetInfo > xPropSetInfo =
437 0 : xPropSet->getPropertySetInfo();
438 :
439 0 : if( xPropSetInfo->hasPropertyByName( sFollowStyle ) )
440 0 : pExportedStyles.reset(new std::set<OUString>());
441 0 : bFirstStyle = sal_False;
442 : }
443 :
444 2737 : if( pExportedStyles && bExported )
445 : {
446 : // If next styles are supported, remember this style's name.
447 0 : pExportedStyles->insert( xStyle->getName() );
448 : }
449 : }
450 :
451 : // if an auto style pool is given, remember this style's name as a
452 : // style name that must not be used by automatic styles.
453 2941 : if( pAutoStylePool )
454 2941 : pAutoStylePool->RegisterName( nFamily, xStyle->getName() );
455 : }
456 2941 : }
457 :
458 70 : if( pExportedStyles )
459 : {
460 : // if next styles are supported, export all next styles that are
461 : // unused and that for, haven't been exported in the first loop.
462 0 : pIter = aSeq.getConstArray();
463 0 : for(;pIter != pEnd;++pIter)
464 : {
465 0 : Reference< XStyle > xStyle;
466 0 : xStyleCont->getByName( *pIter ) >>= xStyle;
467 :
468 : DBG_ASSERT( xStyle.is(), "Style not found for export!" );
469 0 : if( xStyle.is() )
470 : {
471 0 : Reference< XPropertySet > xPropSet( xStyle, UNO_QUERY );
472 0 : Reference< XPropertySetInfo > xPropSetInfo( xPropSet->getPropertySetInfo() );
473 :
474 : // styles that aren't existing realy are ignored.
475 0 : if( xPropSetInfo->hasPropertyByName( sIsPhysical ) )
476 : {
477 0 : Any aAny( xPropSet->getPropertyValue( sIsPhysical ) );
478 0 : if( !*(sal_Bool *)aAny.getValue() )
479 0 : continue;
480 : }
481 :
482 0 : if( !xStyle->isInUse() )
483 0 : continue;
484 :
485 0 : if( !xPropSetInfo->hasPropertyByName( sFollowStyle ) )
486 : {
487 : DBG_ASSERT( sFollowStyle.isEmpty(), "no follow style???" );
488 0 : continue;
489 : }
490 :
491 0 : OUString sNextName;
492 0 : xPropSet->getPropertyValue( sFollowStyle ) >>= sNextName;
493 0 : OUString sTmp( sNextName );
494 : // if the next style hasn't been exported by now, export it now
495 : // and remember its name.
496 0 : if( xStyle->getName() != sNextName &&
497 0 : 0 == pExportedStyles->count( sTmp ) )
498 : {
499 0 : xStyleCont->getByName( sNextName ) >>= xStyle;
500 : DBG_ASSERT( xStyle.is(), "Style not found for export!" );
501 :
502 0 : if( xStyle.is() && exportStyle( xStyle, rXMLFamily, rPropMapper, xStyleCont, pPrefix ) )
503 0 : pExportedStyles->insert( sTmp );
504 0 : }
505 : }
506 0 : }
507 70 : }
508 : }
509 :
510 :
511 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|