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