1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <rtl/ref.hxx>
21 #include <rtl/ustrbuf.hxx>
22 #include <com/sun/star/i18n/CharacterClassification.hpp>
23 #include <com/sun/star/i18n/UnicodeType.hpp>
24 #include <com/sun/star/util/MeasureUnit.hpp>
25 #include <sax/tools/converter.hxx>
26 #include <comphelper/processfactory.hxx>
27 #include <xmloff/nmspmap.hxx>
28 #include "xmloff/xmlnmspe.hxx"
29 #include "IgnoreTContext.hxx"
30 #include "RenameElemTContext.hxx"
31 #include "ProcAttrTContext.hxx"
32 #include "ProcAddAttrTContext.hxx"
33 #include "MergeElemTContext.hxx"
34 #include "CreateElemTContext.hxx"
35 #include "MutableAttrList.hxx"
36 #include "TransformerActions.hxx"
37 #include "ElemTransformerAction.hxx"
38 #include "PropertyActionsOOo.hxx"
39 #include "TransformerTokenMap.hxx"
41 #include "TransformerBase.hxx"
42 #include "TContextVector.hxx"
44 using namespace ::osl
;
45 using namespace ::xmloff::token
;
46 using namespace ::com::sun::star
;
47 using namespace ::com::sun::star::uno
;
48 using namespace ::com::sun::star::beans
;
49 using namespace ::com::sun::star::lang
;
50 using namespace ::com::sun::star::i18n
;
51 using namespace ::com::sun::star::xml::sax
;
55 bool lcl_ConvertAttr( OUString
& rOutAttribute
, sal_Int32 nParam
)
58 enum XMLTokenEnum eTokenToRename
=
59 static_cast< enum XMLTokenEnum
>( nParam
& 0xffff );
60 if( eTokenToRename
!= XML_TOKEN_INVALID
&&
61 IsXMLToken( rOutAttribute
, eTokenToRename
))
63 enum XMLTokenEnum eReplacementToken
=
64 static_cast< enum XMLTokenEnum
>( nParam
>> 16 );
65 rOutAttribute
= GetXMLToken( eReplacementToken
);
70 } // anonymous namespace
72 XMLTransformerContext
*XMLTransformerBase::CreateContext( sal_uInt16 nPrefix
,
73 const OUString
& rLocalName
, const OUString
& rQName
)
75 XMLTransformerActions::key_type
aKey( nPrefix
, rLocalName
);
76 XMLTransformerActions::const_iterator aIter
=
77 GetElemActions().find( aKey
);
79 if( !(aIter
== GetElemActions().end()) )
81 sal_uInt32 nActionType
= (*aIter
).second
.m_nActionType
;
82 if( (nActionType
& XML_ETACTION_USER_DEFINED
) != 0 )
84 XMLTransformerContext
*pContext
=
85 CreateUserDefinedContext( (*aIter
).second
,
87 OSL_ENSURE( pContext
&& !pContext
->IsPersistent(),
88 "unknown or not persistent action" );
94 case XML_ETACTION_COPY_CONTENT
:
95 return new XMLIgnoreTransformerContext( *this, rQName
, sal_False
,
97 case XML_ETACTION_COPY
:
98 return new XMLTransformerContext( *this, rQName
);
99 case XML_ETACTION_RENAME_ELEM
:
100 return new XMLRenameElemTransformerContext( *this, rQName
,
101 (*aIter
).second
.GetQNamePrefixFromParam1(),
102 (*aIter
).second
.GetQNameTokenFromParam1() );
103 case XML_ETACTION_RENAME_ELEM_ADD_ATTR
:
104 return new XMLRenameElemTransformerContext( *this, rQName
,
105 (*aIter
).second
.GetQNamePrefixFromParam1(),
106 (*aIter
).second
.GetQNameTokenFromParam1(),
107 (*aIter
).second
.GetQNamePrefixFromParam2(),
108 (*aIter
).second
.GetQNameTokenFromParam2(),
109 static_cast< XMLTokenEnum
>( (*aIter
).second
.m_nParam3
) );
110 case XML_ETACTION_RENAME_ELEM_PROC_ATTRS
:
111 return new XMLProcAttrTransformerContext( *this, rQName
,
112 (*aIter
).second
.GetQNamePrefixFromParam1(),
113 (*aIter
).second
.GetQNameTokenFromParam1(),
114 static_cast< sal_uInt16
>( (*aIter
).second
.m_nParam2
) );
115 case XML_ETACTION_RENAME_ELEM_ADD_PROC_ATTR
:
116 return new XMLProcAddAttrTransformerContext( *this, rQName
,
117 (*aIter
).second
.GetQNamePrefixFromParam1(),
118 (*aIter
).second
.GetQNameTokenFromParam1(),
119 static_cast< sal_uInt16
>(
120 (*aIter
).second
.m_nParam3
>> 16 ),
121 (*aIter
).second
.GetQNamePrefixFromParam2(),
122 (*aIter
).second
.GetQNameTokenFromParam2(),
123 static_cast< XMLTokenEnum
>(
124 (*aIter
).second
.m_nParam3
& 0xffff ) );
125 case XML_ETACTION_RENAME_ELEM_COND
:
127 const XMLTransformerContext
*pCurrent
= GetCurrentContext();
128 if( pCurrent
->HasQName(
129 (*aIter
).second
.GetQNamePrefixFromParam2(),
130 (*aIter
).second
.GetQNameTokenFromParam2() ) )
131 return new XMLRenameElemTransformerContext( *this, rQName
,
132 (*aIter
).second
.GetQNamePrefixFromParam1(),
133 (*aIter
).second
.GetQNameTokenFromParam1() );
136 case XML_ETACTION_RENAME_ELEM_PROC_ATTRS_COND
:
138 const XMLTransformerContext
*pCurrent
= GetCurrentContext();
139 if( pCurrent
->HasQName(
140 (*aIter
).second
.GetQNamePrefixFromParam3(),
141 (*aIter
).second
.GetQNameTokenFromParam3() ) )
142 return new XMLProcAttrTransformerContext( *this, rQName
,
143 (*aIter
).second
.GetQNamePrefixFromParam1(),
144 (*aIter
).second
.GetQNameTokenFromParam1(),
145 static_cast< sal_uInt16
>( (*aIter
).second
.m_nParam2
) );
147 return new XMLProcAttrTransformerContext( *this, rQName
,
148 static_cast< sal_uInt16
>( (*aIter
).second
.m_nParam2
) );
150 case XML_ETACTION_PROC_ATTRS
:
151 return new XMLProcAttrTransformerContext( *this, rQName
,
152 static_cast< sal_uInt16
>( (*aIter
).second
.m_nParam1
) );
153 case XML_ETACTION_PROC_ATTRS_COND
:
155 const XMLTransformerContext
*pCurrent
= GetCurrentContext();
156 if( pCurrent
->HasQName(
157 (*aIter
).second
.GetQNamePrefixFromParam1(),
158 (*aIter
).second
.GetQNameTokenFromParam1() ) )
159 return new XMLProcAttrTransformerContext( *this, rQName
,
160 static_cast< sal_uInt16
>( (*aIter
).second
.m_nParam2
) );
163 case XML_ETACTION_MOVE_ATTRS_TO_ELEMS
:
164 return new XMLCreateElemTransformerContext( *this, rQName
,
165 static_cast< sal_uInt16
>( (*aIter
).second
.m_nParam1
) );
166 case XML_ETACTION_MOVE_ELEMS_TO_ATTRS
:
167 return new XMLMergeElemTransformerContext( *this, rQName
,
168 static_cast< sal_uInt16
>( (*aIter
).second
.m_nParam1
) );
170 OSL_ENSURE( !this, "unknown action" );
175 // default is copying
176 return new XMLTransformerContext( *this, rQName
);
179 XMLTransformerActions
*XMLTransformerBase::GetUserDefinedActions( sal_uInt16
)
184 XMLTransformerBase::XMLTransformerBase( XMLTransformerActionInit
*pInit
,
185 ::xmloff::token::XMLTokenEnum
*pTKMapInit
)
187 m_pNamespaceMap( new SvXMLNamespaceMap
),
188 m_pReplaceNamespaceMap( new SvXMLNamespaceMap
),
189 m_pContexts( new XMLTransformerContextVector
),
190 m_pElemActions( new XMLTransformerActions( pInit
) ),
191 m_pTokenMap( new XMLTransformerTokenMap( pTKMapInit
) )
193 GetNamespaceMap().Add( GetXMLToken(XML_NP_XLINK
), GetXMLToken(XML_N_XLINK
), XML_NAMESPACE_XLINK
);
194 GetNamespaceMap().Add( GetXMLToken(XML_NP_DC
), GetXMLToken(XML_N_DC
), XML_NAMESPACE_DC
);
195 GetNamespaceMap().Add( GetXMLToken(XML_NP_MATH
), GetXMLToken(XML_N_MATH
), XML_NAMESPACE_MATH
);
196 GetNamespaceMap().Add( GetXMLToken(XML_NP_OOO
), GetXMLToken(XML_N_OOO
), XML_NAMESPACE_OOO
);
197 GetNamespaceMap().Add( GetXMLToken(XML_NP_DOM
), GetXMLToken(XML_N_DOM
), XML_NAMESPACE_DOM
);
198 GetNamespaceMap().Add( GetXMLToken(XML_NP_OOOW
), GetXMLToken(XML_N_OOOW
), XML_NAMESPACE_OOOW
);
199 GetNamespaceMap().Add( GetXMLToken(XML_NP_OOOC
), GetXMLToken(XML_N_OOOC
), XML_NAMESPACE_OOOC
);
202 XMLTransformerBase::~XMLTransformerBase() throw ()
206 delete m_pNamespaceMap
;
207 delete m_pReplaceNamespaceMap
;
209 delete m_pElemActions
;
213 void SAL_CALL
XMLTransformerBase::startDocument( void )
214 throw( SAXException
, RuntimeException
)
216 m_xHandler
->startDocument();
219 void SAL_CALL
XMLTransformerBase::endDocument( void )
220 throw( SAXException
, RuntimeException
)
222 m_xHandler
->endDocument();
225 void SAL_CALL
XMLTransformerBase::startElement( const OUString
& rName
,
226 const Reference
< XAttributeList
>& rAttrList
)
227 throw(SAXException
, RuntimeException
)
229 SvXMLNamespaceMap
*pRewindMap
= 0;
231 bool bRect
= rName
== "presentation:show-shape";
234 // Process namespace attributes. This must happen before creating the
235 // context, because namespace decaration apply to the element name itself.
236 XMLMutableAttributeList
*pMutableAttrList
= 0;
237 Reference
< XAttributeList
> xAttrList( rAttrList
);
238 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
239 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
241 const OUString
& rAttrName
= xAttrList
->getNameByIndex( i
);
242 if( ( rAttrName
.getLength() >= 5 ) &&
243 ( rAttrName
.startsWith( GetXMLToken(XML_XMLNS
) ) ) &&
244 ( rAttrName
.getLength() == 5 || ':' == rAttrName
[5] ) )
248 pRewindMap
= m_pNamespaceMap
;
249 m_pNamespaceMap
= new SvXMLNamespaceMap( *m_pNamespaceMap
);
251 const OUString
& rAttrValue
= xAttrList
->getValueByIndex( i
);
253 OUString
aPrefix( ( rAttrName
.getLength() == 5 )
255 : rAttrName
.copy( 6 ) );
256 // Add namespace, but only if it is known.
257 sal_uInt16 nKey
= m_pNamespaceMap
->AddIfKnown( aPrefix
, rAttrValue
);
258 // If namespace is unknown, try to match a name with similar
260 if( XML_NAMESPACE_UNKNOWN
== nKey
)
262 OUString
aTestName( rAttrValue
);
263 if( SvXMLNamespaceMap::NormalizeOasisURN( aTestName
) )
264 nKey
= m_pNamespaceMap
->AddIfKnown( aPrefix
, aTestName
);
266 // If that namespace is not known, too, add it as unknown
267 if( XML_NAMESPACE_UNKNOWN
== nKey
)
268 nKey
= m_pNamespaceMap
->Add( aPrefix
, rAttrValue
);
270 const OUString
& rRepName
= m_pReplaceNamespaceMap
->GetNameByKey( nKey
);
271 if( !rRepName
.isEmpty() )
273 if( !pMutableAttrList
)
275 pMutableAttrList
= new XMLMutableAttributeList( xAttrList
);
276 xAttrList
= pMutableAttrList
;
279 pMutableAttrList
->SetValueByIndex( i
, rRepName
);
284 // Get element's namespace and local name.
287 m_pNamespaceMap
->GetKeyByAttrName( rName
, &aLocalName
);
289 // If there are contexts already, call a CreateChildContext at the topmost
290 // context. Otherwise, create a default context.
291 ::rtl::Reference
< XMLTransformerContext
> xContext
;
292 if( !m_pContexts
->empty() )
294 xContext
= m_pContexts
->back()->CreateChildContext( nPrefix
,
301 xContext
= CreateContext( nPrefix
, aLocalName
, rName
);
304 OSL_ENSURE( xContext
.is(), "XMLTransformerBase::startElement: missing context" );
306 xContext
= new XMLTransformerContext( *this, rName
);
308 // Remember old namespace map.
310 xContext
->SetRewindMap( pRewindMap
);
312 // Push context on stack.
313 m_pContexts
->push_back( xContext
);
315 // Call a startElement at the new context.
316 xContext
->StartElement( xAttrList
);
319 void SAL_CALL
XMLTransformerBase::endElement( const OUString
&
320 #if OSL_DEBUG_LEVEL > 0
324 throw(SAXException
, RuntimeException
)
326 if( !m_pContexts
->empty() )
328 // Get topmost context
329 ::rtl::Reference
< XMLTransformerContext
> xContext
= m_pContexts
->back();
331 #if OSL_DEBUG_LEVEL > 0
332 OSL_ENSURE( xContext
->GetQName() == rName
,
333 "XMLTransformerBase::endElement: popped context has wrong lname" );
336 // Call a EndElement at the current context.
337 xContext
->EndElement();
339 // and remove it from the stack.
340 m_pContexts
->pop_back();
342 // Get a namespace map to rewind.
343 SvXMLNamespaceMap
*pRewindMap
= xContext
->GetRewindMap();
345 // Delete the current context.
348 // Rewind a namespace map.
351 delete m_pNamespaceMap
;
352 m_pNamespaceMap
= pRewindMap
;
357 void SAL_CALL
XMLTransformerBase::characters( const OUString
& rChars
)
358 throw(SAXException
, RuntimeException
)
360 if( !m_pContexts
->empty() )
362 m_pContexts
->back()->Characters( rChars
);
366 void SAL_CALL
XMLTransformerBase::ignorableWhitespace( const OUString
& rWhitespaces
)
367 throw(SAXException
, RuntimeException
)
369 m_xHandler
->ignorableWhitespace( rWhitespaces
);
372 void SAL_CALL
XMLTransformerBase::processingInstruction( const OUString
& rTarget
,
373 const OUString
& rData
)
374 throw(SAXException
, RuntimeException
)
376 m_xHandler
->processingInstruction( rTarget
, rData
);
379 void SAL_CALL
XMLTransformerBase::setDocumentLocator( const Reference
< XLocator
>& rLocator
)
380 throw(SAXException
, RuntimeException
)
382 m_xLocator
= rLocator
;
385 // XExtendedDocumentHandler
386 void SAL_CALL
XMLTransformerBase::startCDATA( void ) throw(SAXException
, RuntimeException
)
388 if( m_xExtHandler
.is() )
389 m_xExtHandler
->startCDATA();
392 void SAL_CALL
XMLTransformerBase::endCDATA( void ) throw(RuntimeException
)
394 if( m_xExtHandler
.is() )
395 m_xExtHandler
->endCDATA();
398 void SAL_CALL
XMLTransformerBase::comment( const OUString
& rComment
)
399 throw(SAXException
, RuntimeException
)
401 if( m_xExtHandler
.is() )
402 m_xExtHandler
->comment( rComment
);
405 void SAL_CALL
XMLTransformerBase::allowLineBreak( void )
406 throw(SAXException
, RuntimeException
)
408 if( m_xExtHandler
.is() )
409 m_xExtHandler
->allowLineBreak();
412 void SAL_CALL
XMLTransformerBase::unknown( const OUString
& rString
)
413 throw(SAXException
, RuntimeException
)
415 if( m_xExtHandler
.is() )
416 m_xExtHandler
->unknown( rString
);
420 void SAL_CALL
XMLTransformerBase::initialize( const Sequence
< Any
>& aArguments
)
421 throw(Exception
, RuntimeException
)
423 const sal_Int32 nAnyCount
= aArguments
.getLength();
424 const Any
* pAny
= aArguments
.getConstArray();
426 for( sal_Int32 nIndex
= 0; nIndex
< nAnyCount
; nIndex
++, pAny
++ )
428 // use isAssignableFrom instead of comparing the types to
429 // allow XExtendedDocumentHandler instead of XDocumentHandler (used in
430 // writeOasis2OOoLibraryElement in sfx2).
431 // The Any shift operator can't be used to query the type because it
432 // uses queryInterface, and the model also has a XPropertySet interface.
435 if( ::getCppuType( (const Reference
< XDocumentHandler
>*) 0 ).isAssignableFrom( pAny
->getValueType() ) )
436 m_xHandler
.set( *pAny
, UNO_QUERY
);
438 // property set to transport data across
439 if( ::getCppuType( (const Reference
< XPropertySet
>*) 0 ).isAssignableFrom( pAny
->getValueType() ) )
440 m_xPropSet
.set( *pAny
, UNO_QUERY
);
443 if( ::getCppuType( (const Reference
< ::com::sun::star::frame::XModel
>*) 0 ).isAssignableFrom( pAny
->getValueType() ) )
444 mxModel
.set( *pAny
, UNO_QUERY
);
447 if( m_xPropSet
.is() )
450 OUString sRelPath
, sName
;
451 Reference
< XPropertySetInfo
> xPropSetInfo
=
452 m_xPropSet
->getPropertySetInfo();
453 OUString
sPropName( "StreamRelPath" );
454 if( xPropSetInfo
->hasPropertyByName(sPropName
) )
456 aAny
= m_xPropSet
->getPropertyValue(sPropName
);
459 sPropName
= OUString( "StreamName" );
460 if( xPropSetInfo
->hasPropertyByName(sPropName
) )
462 aAny
= m_xPropSet
->getPropertyValue(sPropName
);
465 if( !sName
.isEmpty() )
467 m_aExtPathPrefix
= OUString( "../" );
469 // If there is a rel path within a package, then append
470 // additional '../'. If the rel path contains an ':', then it is
471 // an absolute URI (or invalid URI, because zip files don't
472 // permit ':'), and it will be ignored.
473 if( !sRelPath
.isEmpty() )
475 sal_Int32 nColPos
= sRelPath
.indexOf( ':' );
476 OSL_ENSURE( -1 == nColPos
,
477 "StreamRelPath contains ':', absolute URI?" );
481 OUString sTmp
= m_aExtPathPrefix
;
485 m_aExtPathPrefix
+= sTmp
;
486 nPos
= sRelPath
.indexOf( '/', nPos
+ 1 );
496 static sal_Int16
lcl_getUnit( const OUString
& rValue
)
498 if( rValue
.endsWithIgnoreAsciiCase( "cm" ) )
499 return util::MeasureUnit::CM
;
500 else if ( rValue
.endsWithIgnoreAsciiCase( "mm" ) )
501 return util::MeasureUnit::MM
;
503 return util::MeasureUnit::INCH
;
506 XMLMutableAttributeList
*XMLTransformerBase::ProcessAttrList(
507 Reference
< XAttributeList
>& rAttrList
, sal_uInt16 nActionMap
,
510 XMLMutableAttributeList
*pMutableAttrList
= 0;
511 XMLTransformerActions
*pActions
= GetUserDefinedActions( nActionMap
);
512 OSL_ENSURE( pActions
, "go no actions" );
515 sal_Int16 nAttrCount
= rAttrList
.is() ? rAttrList
->getLength() : 0;
516 for( sal_Int16 i
=0; i
< nAttrCount
; ++i
)
518 const OUString
& rAttrName
= rAttrList
->getNameByIndex( i
);
519 const OUString
& rAttrValue
= rAttrList
->getValueByIndex( i
);
521 sal_uInt16 nPrefix
= GetNamespaceMap().GetKeyByAttrName( rAttrName
,
524 XMLTransformerActions::key_type
aKey( nPrefix
, aLocalName
);
525 XMLTransformerActions::const_iterator aIter
=
526 pActions
->find( aKey
);
527 if( !(aIter
== pActions
->end() ) )
529 if( !pMutableAttrList
)
531 pMutableAttrList
= new XMLMutableAttributeList( rAttrList
,
533 rAttrList
= pMutableAttrList
;
536 sal_uInt32 nAction
= (*aIter
).second
.m_nActionType
;
537 sal_Bool bRename
= sal_False
;
540 case XML_ATACTION_RENAME
:
543 case XML_ATACTION_COPY
:
545 case XML_ATACTION_REMOVE
:
546 case XML_ATACTION_STYLE_DISPLAY_NAME
:
547 pMutableAttrList
->RemoveAttributeByIndex( i
);
551 case XML_ATACTION_RENAME_IN2INCH
:
553 case XML_ATACTION_IN2INCH
:
555 OUString
aAttrValue( rAttrValue
);
556 if( ReplaceSingleInWithInch( aAttrValue
) )
557 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
560 case XML_ATACTION_INS2INCHS
:
562 OUString
aAttrValue( rAttrValue
);
563 if( ReplaceInWithInch( aAttrValue
) )
564 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
567 case XML_ATACTION_RENAME_INCH2IN
:
569 case XML_ATACTION_INCH2IN
:
571 OUString
aAttrValue( rAttrValue
);
572 if( ReplaceSingleInchWithIn( aAttrValue
) )
573 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
576 case XML_ATACTION_INCHS2INS
:
578 OUString
aAttrValue( rAttrValue
);
579 if( ReplaceInchWithIn( aAttrValue
) )
580 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
583 case XML_ATACTION_TWIPS2IN
:
585 OUString
aAttrValue( rAttrValue
);
587 XMLTransformerBase::ReplaceSingleInchWithIn( aAttrValue
);
590 sal_Int16
const nDestUnit
= lcl_getUnit(aAttrValue
);
592 // convert twips value to inch
594 if (::sax::Converter::convertMeasure(nMeasure
,
595 aAttrValue
, util::MeasureUnit::MM_100TH
))
598 // #i13778#,#i36248# apply correct twip-to-1/100mm
599 nMeasure
= (sal_Int32
)( nMeasure
>= 0
600 ? ((nMeasure
*127+36)/72)
601 : ((nMeasure
*127-36)/72) );
603 OUStringBuffer aBuffer
;
604 ::sax::Converter::convertMeasure(aBuffer
,
605 nMeasure
, util::MeasureUnit::MM_100TH
,
607 aAttrValue
= aBuffer
.makeStringAndClear();
611 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
614 case XML_ATACTION_RENAME_DECODE_STYLE_NAME_REF
:
616 case XML_ATACTION_DECODE_STYLE_NAME
:
617 case XML_ATACTION_DECODE_STYLE_NAME_REF
:
619 OUString
aAttrValue( rAttrValue
);
620 if( DecodeStyleName(aAttrValue
) )
621 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
624 case XML_ATACTION_ENCODE_STYLE_NAME
:
626 OUString
aAttrValue( rAttrValue
);
627 if( EncodeStyleName(aAttrValue
) )
629 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
630 OUString
aNewAttrQName(
631 GetNamespaceMap().GetQNameByKey(
633 ::xmloff::token::GetXMLToken(
634 XML_DISPLAY_NAME
) ) );
635 pMutableAttrList
->AddAttribute( aNewAttrQName
,
640 case XML_ATACTION_RENAME_ENCODE_STYLE_NAME_REF
:
642 case XML_ATACTION_ENCODE_STYLE_NAME_REF
:
644 OUString
aAttrValue( rAttrValue
);
645 if( EncodeStyleName(aAttrValue
) )
646 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
649 case XML_ATACTION_RENAME_NEG_PERCENT
:
651 case XML_ATACTION_NEG_PERCENT
:
653 OUString
aAttrValue( rAttrValue
);
654 if( NegPercent( aAttrValue
) )
655 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
658 case XML_ATACTION_RENAME_ADD_NAMESPACE_PREFIX
:
660 case XML_ATACTION_ADD_NAMESPACE_PREFIX
:
662 OUString
aAttrValue( rAttrValue
);
663 sal_uInt16 nValPrefix
=
664 static_cast<sal_uInt16
>(
665 bRename
? (*aIter
).second
.m_nParam2
666 : (*aIter
).second
.m_nParam1
);
667 if( AddNamespacePrefix( aAttrValue
, nValPrefix
) )
668 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
671 case XML_ATACTION_ADD_APP_NAMESPACE_PREFIX
:
673 OUString
aAttrValue( rAttrValue
);
674 sal_uInt16 nValPrefix
=
675 static_cast<sal_uInt16
>((*aIter
).second
.m_nParam1
);
676 if( IsXMLToken( GetClass(), XML_SPREADSHEET
) )
677 nValPrefix
= XML_NAMESPACE_OOOC
;
678 else if( IsXMLToken( GetClass(), XML_TEXT
) )
679 nValPrefix
= XML_NAMESPACE_OOOW
;
680 if( AddNamespacePrefix( aAttrValue
, nValPrefix
) )
681 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
684 case XML_ATACTION_RENAME_REMOVE_NAMESPACE_PREFIX
:
686 case XML_ATACTION_REMOVE_NAMESPACE_PREFIX
:
688 OUString
aAttrValue( rAttrValue
);
689 sal_uInt16 nValPrefix
=
690 static_cast<sal_uInt16
>(
691 bRename
? (*aIter
).second
.m_nParam2
692 : (*aIter
).second
.m_nParam1
);
693 if( RemoveNamespacePrefix( aAttrValue
, nValPrefix
) )
694 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
697 case XML_ATACTION_REMOVE_ANY_NAMESPACE_PREFIX
:
699 OUString
aAttrValue( rAttrValue
);
700 if( RemoveNamespacePrefix( aAttrValue
) )
701 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
704 case XML_ATACTION_URI_OOO
:
706 OUString
aAttrValue( rAttrValue
);
707 if( ConvertURIToOASIS( aAttrValue
,
708 static_cast< sal_Bool
>((*aIter
).second
.m_nParam1
)))
709 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
712 case XML_ATACTION_URI_OASIS
:
714 OUString
aAttrValue( rAttrValue
);
715 if( ConvertURIToOOo( aAttrValue
,
716 static_cast< sal_Bool
>((*aIter
).second
.m_nParam1
)))
717 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
720 case XML_ATACTION_RENAME_ATTRIBUTE
:
722 OUString
aAttrValue( rAttrValue
);
723 RenameAttributeValue(
725 (*aIter
).second
.m_nParam1
,
726 (*aIter
).second
.m_nParam2
,
727 (*aIter
).second
.m_nParam3
);
728 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
731 case XML_ATACTION_RNG2ISO_DATETIME
:
733 OUString
aAttrValue( rAttrValue
);
734 if( ConvertRNGDateTimeToISO( aAttrValue
))
735 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
738 case XML_ATACTION_RENAME_RNG2ISO_DATETIME
:
740 OUString
aAttrValue( rAttrValue
);
741 if( ConvertRNGDateTimeToISO( aAttrValue
))
742 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
746 case XML_ATACTION_IN2TWIPS
:
748 OUString
aAttrValue( rAttrValue
);
749 XMLTransformerBase::ReplaceSingleInWithInch( aAttrValue
);
753 sal_Int16
const nDestUnit
= lcl_getUnit(aAttrValue
);
755 // convert inch value to twips and export as faked inch
757 if (::sax::Converter::convertMeasure(nMeasure
,
758 aAttrValue
, util::MeasureUnit::MM_100TH
))
761 // #i13778#,#i36248#/ apply correct 1/100mm-to-twip conversion
762 nMeasure
= (sal_Int32
)( nMeasure
>= 0
763 ? ((nMeasure
*72+63)/127)
764 : ((nMeasure
*72-63)/127) );
766 OUStringBuffer aBuffer
;
767 ::sax::Converter::convertMeasure( aBuffer
,
768 nMeasure
, util::MeasureUnit::MM_100TH
,
770 aAttrValue
= aBuffer
.makeStringAndClear();
774 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
777 case XML_ATACTION_SVG_WIDTH_HEIGHT_OOO
:
779 OUString
aAttrValue( rAttrValue
);
780 ReplaceSingleInchWithIn( aAttrValue
);
782 sal_Int16
const nDestUnit
= lcl_getUnit( aAttrValue
);
785 if (::sax::Converter::convertMeasure(nMeasure
,
786 aAttrValue
, util::MeasureUnit::MM_100TH
))
791 else if( nMeasure
< 0 )
795 OUStringBuffer aBuffer
;
796 ::sax::Converter::convertMeasure(aBuffer
, nMeasure
,
797 util::MeasureUnit::MM_100TH
, nDestUnit
);
798 aAttrValue
= aBuffer
.makeStringAndClear();
801 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
804 case XML_ATACTION_SVG_WIDTH_HEIGHT_OASIS
:
806 OUString
aAttrValue( rAttrValue
);
807 ReplaceSingleInWithInch( aAttrValue
);
809 sal_Int16
const nDestUnit
= lcl_getUnit( aAttrValue
);
812 if (::sax::Converter::convertMeasure(nMeasure
,
813 aAttrValue
, util::MeasureUnit::MM_100TH
))
818 else if( nMeasure
< 0 )
822 OUStringBuffer aBuffer
;
823 ::sax::Converter::convertMeasure(aBuffer
, nMeasure
,
824 util::MeasureUnit::MM_100TH
, nDestUnit
);
825 aAttrValue
= aBuffer
.makeStringAndClear();
828 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
831 case XML_ATACTION_DECODE_ID
:
833 const sal_Int32 nLen
= rAttrValue
.getLength();
834 OUStringBuffer aBuffer
;
837 for( pos
= 0; pos
< nLen
; pos
++ )
839 sal_Unicode c
= rAttrValue
[pos
];
840 if( (c
>= '0') && (c
<= '9') )
843 aBuffer
.append( (sal_Int32
)c
);
846 pMutableAttrList
->SetValueByIndex( i
, aBuffer
.makeStringAndClear() );
849 // #i50322# - special handling for the
850 // transparency of writer background graphics.
851 case XML_ATACTION_WRITER_BACK_GRAPHIC_TRANSPARENCY
:
853 // determine, if it's the transparency of a document style
854 XMLTransformerContext
* pFirstContext
= (*m_pContexts
)[0].get();
855 OUString aFirstContextLocalName
;
856 /* sal_uInt16 nFirstContextPrefix = */
857 GetNamespaceMap().GetKeyByAttrName( pFirstContext
->GetQName(),
858 &aFirstContextLocalName
);
859 bool bIsDocumentStyle(
860 ::xmloff::token::IsXMLToken( aFirstContextLocalName
,
861 XML_DOCUMENT_STYLES
) );
862 // no conversion of transparency value for document
863 // styles, because former OpenOffice.org version writes
864 // writes always a transparency value of 100% and doesn't
865 // read the value. Thus, it's intepreted as 0%
866 if ( !bIsDocumentStyle
)
868 OUString
aAttrValue( rAttrValue
);
869 NegPercent(aAttrValue
);
870 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
875 case XML_ATACTION_SHAPEID
:
877 OUString
sNewValue( "shape" );
878 sNewValue
+= rAttrValue
;
879 pMutableAttrList
->SetValueByIndex( i
, sNewValue
);
884 OSL_ENSURE( !this, "unknown action" );
890 OUString
aNewAttrQName(
891 GetNamespaceMap().GetQNameByKey(
892 (*aIter
).second
.GetQNamePrefixFromParam1(),
893 ::xmloff::token::GetXMLToken(
894 (*aIter
).second
.GetQNameTokenFromParam1()) ) );
895 pMutableAttrList
->RenameAttributeByIndex( i
,
902 return pMutableAttrList
;
905 sal_Bool
XMLTransformerBase::ReplaceSingleInchWithIn( OUString
& rValue
)
907 sal_Bool bRet
= sal_False
;
908 sal_Int32 nPos
= rValue
.getLength();
909 while( nPos
&& rValue
[nPos
-1] <= ' ' )
912 ('c'==rValue
[nPos
-2] || 'C'==rValue
[nPos
-2]) &&
913 ('h'==rValue
[nPos
-1] || 'H'==rValue
[nPos
-1]) )
915 rValue
=rValue
.copy( 0, nPos
-2 );
922 sal_Bool
XMLTransformerBase::ReplaceInchWithIn( OUString
& rValue
)
924 sal_Bool bRet
= sal_False
;
926 while( nPos
< rValue
.getLength()-3 )
928 sal_Unicode c
= rValue
[nPos
];
929 if( 'i'==c
|| 'I'==c
)
932 if( (c
>= '0' && c
<= '9') || '.' == c
)
935 if( 'n'==c
|| 'N'==c
)
938 if( 'c'==c
|| 'C'==c
)
941 if( 'h'==c
|| 'H'==c
)
943 rValue
= rValue
.replaceAt( nPos
,
944 4, GetXMLToken(XML_UNIT_INCH
) );
959 sal_Bool
XMLTransformerBase::ReplaceSingleInWithInch( OUString
& rValue
)
961 sal_Bool bRet
= sal_False
;
963 sal_Int32 nPos
= rValue
.getLength();
964 while( nPos
&& rValue
[nPos
-1] <= ' ' )
967 ('i'==rValue
[nPos
-2] ||
968 'I'==rValue
[nPos
-2]) &&
969 ('n'==rValue
[nPos
-1] ||
970 'N'==rValue
[nPos
-1]) )
973 rValue
= rValue
.replaceAt( nPos
, rValue
.getLength() - nPos
,
974 GetXMLToken(XML_INCH
) );
981 sal_Bool
XMLTransformerBase::ReplaceInWithInch( OUString
& rValue
)
983 sal_Bool bRet
= sal_False
;
985 while( nPos
< rValue
.getLength()-1 )
987 sal_Unicode c
= rValue
[nPos
];
988 if( 'i'==c
|| 'I'==c
)
991 if( (c
>= '0' && c
<= '9') || '.' == c
)
994 if( 'n'==c
|| 'N'==c
)
996 rValue
= rValue
.replaceAt( nPos
,
997 2, GetXMLToken(XML_INCH
) );
1010 sal_Bool
XMLTransformerBase::EncodeStyleName( OUString
& rName
) const
1012 static sal_Char aHexTab
[] = "0123456789abcdef";
1014 sal_Bool bEncoded
= sal_False
;
1016 sal_Int32 nLen
= rName
.getLength();
1017 OUStringBuffer
aBuffer( nLen
);
1019 for( sal_Int32 i
= 0; i
< nLen
; i
++ )
1021 sal_Unicode c
= rName
[i
];
1022 sal_Bool bValidChar
= sal_False
;
1026 (c
>= 0x0041 && c
<= 0x005a) ||
1027 (c
>= 0x0061 && c
<= 0x007a) ||
1028 (c
>= 0x00c0 && c
<= 0x00d6) ||
1029 (c
>= 0x00d8 && c
<= 0x00f6) ||
1030 (c
>= 0x00f8 && c
<= 0x00ff) ||
1031 ( i
> 0 && ( (c
>= 0x0030 && c
<= 0x0039) ||
1032 c
== 0x00b7 || c
== '-' || c
== '.') );
1036 if( (c
>= 0xf900U
&& c
<= 0xfffeU
) ||
1037 (c
>= 0x20ddU
&& c
<= 0x20e0U
))
1039 bValidChar
= sal_False
;
1041 else if( (c
>= 0x02bbU
&& c
<= 0x02c1U
) || c
== 0x0559 ||
1042 c
== 0x06e5 || c
== 0x06e6 )
1044 bValidChar
= sal_True
;
1046 else if( c
== 0x0387 )
1052 if( !xCharClass
.is() )
1054 const_cast < XMLTransformerBase
* >(this)
1055 ->xCharClass
= CharacterClassification::create( comphelper::getProcessComponentContext() );
1057 sal_Int16 nType
= xCharClass
->getType( rName
, i
);
1061 case UnicodeType::UPPERCASE_LETTER
: // Lu
1062 case UnicodeType::LOWERCASE_LETTER
: // Ll
1063 case UnicodeType::TITLECASE_LETTER
: // Lt
1064 case UnicodeType::OTHER_LETTER
: // Lo
1065 case UnicodeType::LETTER_NUMBER
: // Nl
1066 bValidChar
= sal_True
;
1068 case UnicodeType::NON_SPACING_MARK
: // Ms
1069 case UnicodeType::ENCLOSING_MARK
: // Me
1070 case UnicodeType::COMBINING_SPACING_MARK
: //Mc
1071 case UnicodeType::MODIFIER_LETTER
: // Lm
1072 case UnicodeType::DECIMAL_DIGIT_NUMBER
: // Nd
1080 aBuffer
.append( c
);
1084 aBuffer
.append( static_cast< sal_Unicode
>( '_' ) );
1086 aBuffer
.append( static_cast< sal_Unicode
>(
1087 aHexTab
[ (c
>> 12) & 0x0f ] ) );
1089 aBuffer
.append( static_cast< sal_Unicode
>(
1090 aHexTab
[ (c
>> 8) & 0x0f ] ) );
1092 aBuffer
.append( static_cast< sal_Unicode
>(
1093 aHexTab
[ (c
>> 4) & 0x0f ] ) );
1094 aBuffer
.append( static_cast< sal_Unicode
>(
1095 aHexTab
[ c
& 0x0f ] ) );
1096 aBuffer
.append( static_cast< sal_Unicode
>( '_' ) );
1097 bEncoded
= sal_True
;
1101 if( aBuffer
.getLength() > (1<<15)-1 )
1102 bEncoded
= sal_False
;
1105 rName
= aBuffer
.makeStringAndClear();
1109 sal_Bool
XMLTransformerBase::DecodeStyleName( OUString
& rName
)
1111 sal_Bool bEncoded
= sal_False
;
1113 sal_Int32 nLen
= rName
.getLength();
1114 OUStringBuffer
aBuffer( nLen
);
1116 sal_Bool bWithinHex
= sal_False
;
1117 sal_Unicode cEnc
= 0;
1118 for( sal_Int32 i
= 0; i
< nLen
; i
++ )
1120 sal_Unicode c
= rName
[i
];
1125 aBuffer
.append( cEnc
);
1130 bEncoded
= sal_True
;
1132 bWithinHex
= !bWithinHex
;
1134 else if( bWithinHex
)
1137 if( c
>= '0' && c
<= '9' )
1141 else if( c
>= 'a' && c
<= 'f' )
1143 cDigit
= c
- 'a' + 10;
1145 else if( c
>= 'A' && c
<= 'F' )
1147 cDigit
= c
- 'A' + 10;
1152 bEncoded
= sal_False
;
1155 cEnc
= (cEnc
<< 4) + cDigit
;
1159 aBuffer
.append( c
);
1164 rName
= aBuffer
.makeStringAndClear();
1168 sal_Bool
XMLTransformerBase::NegPercent( OUString
& rValue
)
1170 sal_Bool bRet
= sal_False
;
1171 sal_Bool bNeg
= sal_False
;
1175 sal_Int32 nLen
= rValue
.getLength();
1178 while( nPos
< nLen
&& sal_Unicode(' ') == rValue
[nPos
] )
1181 if( nPos
< nLen
&& sal_Unicode('-') == rValue
[nPos
] )
1188 while( nPos
< nLen
&&
1189 sal_Unicode('0') <= rValue
[nPos
] &&
1190 sal_Unicode('9') >= rValue
[nPos
] )
1192 // TODO: check overflow!
1194 nVal
+= (rValue
[nPos
] - sal_Unicode('0'));
1198 if( nPos
< nLen
&& sal_Unicode('.') == rValue
[nPos
] )
1202 while( nPos
< nLen
&&
1203 sal_Unicode('0') <= rValue
[nPos
] &&
1204 sal_Unicode('9') >= rValue
[nPos
] )
1206 // TODO: check overflow!
1208 nVal
+= ( static_cast<double>(rValue
[nPos
] - sal_Unicode('0')) / nDiv
);
1214 while( nPos
< nLen
&& sal_Unicode(' ') == rValue
[nPos
] )
1217 if( nPos
< nLen
&& sal_Unicode('%') == rValue
[nPos
] )
1223 sal_Int32 nIntVal
= 100 - static_cast<sal_Int32
>( nVal
);
1225 OUStringBuffer aNewValBuffer
;
1226 aNewValBuffer
.append( nIntVal
);
1227 aNewValBuffer
.append( sal_Unicode('%' ) );
1229 rValue
= aNewValBuffer
.makeStringAndClear();
1236 sal_Bool
XMLTransformerBase::AddNamespacePrefix( OUString
& rName
,
1237 sal_uInt16 nPrefix
) const
1239 rName
= GetNamespaceMap().GetQNameByKey( nPrefix
, rName
, sal_False
);
1243 sal_Bool
XMLTransformerBase::RemoveNamespacePrefix( OUString
& rName
,
1244 sal_uInt16 nPrefixOnly
) const
1246 OUString aLocalName
;
1247 sal_uInt16 nPrefix
=
1248 GetNamespaceMap()._GetKeyByAttrName( rName
, &aLocalName
, sal_False
);
1249 sal_Bool bRet
= XML_NAMESPACE_UNKNOWN
!= nPrefix
&&
1250 (USHRT_MAX
== nPrefixOnly
|| nPrefix
== nPrefixOnly
);
1257 sal_Bool
XMLTransformerBase::ConvertURIToOASIS( OUString
& rURI
,
1258 sal_Bool bSupportPackage
) const
1260 sal_Bool bRet
= sal_False
;
1261 if( !m_aExtPathPrefix
.isEmpty() && !rURI
.isEmpty() )
1263 sal_Bool bRel
= sal_False
;
1268 // for package URIs, the '#' has to be removed
1269 if( bSupportPackage
)
1271 rURI
= rURI
.copy( 1 );
1276 // no rel path; nothing to do
1279 // a rel path; to keep URI simple, remove './', if there
1281 if( rURI
.getLength() > 1 && '/' == rURI
[1] )
1283 rURI
= rURI
.copy( 2 );
1288 // check for a RFC2396 schema
1292 sal_Int32 nLen
= rURI
.getLength();
1293 while( nPos
< nLen
)
1295 switch( rURI
[nPos
] )
1298 // a relative path segement
1299 nPos
= nLen
; // leave loop
1304 nPos
= nLen
; // leave loop
1307 // we don't care about any other characters
1317 OUString
sTmp( m_aExtPathPrefix
);
1327 sal_Bool
XMLTransformerBase::ConvertURIToOOo( OUString
& rURI
,
1328 sal_Bool bSupportPackage
) const
1330 sal_Bool bRet
= sal_False
;
1331 if( !rURI
.isEmpty() )
1333 sal_Bool bPackage
= sal_False
;
1337 // no rel path; nothing to to
1341 if( rURI
.startsWith( m_aExtPathPrefix
) )
1343 // an external URI; remove '../'
1344 rURI
= rURI
.copy( m_aExtPathPrefix
.getLength() );
1349 bPackage
= sal_True
;
1353 // check for a RFC2396 schema
1355 bPackage
= sal_True
;
1357 sal_Int32 nLen
= rURI
.getLength();
1358 while( nPos
< nLen
)
1360 switch( rURI
[nPos
] )
1363 // a relative path segement within the package
1364 nPos
= nLen
; // leave loop
1368 bPackage
= sal_False
;
1369 nPos
= nLen
; // leave loop
1372 // we don't care about any other characters
1380 if( bPackage
&& bSupportPackage
)
1382 OUString
sTmp( OUString::valueOf( sal_Unicode( '#' ) ) );
1383 if( rURI
.startsWith( "./" ) )
1384 rURI
= rURI
.copy( 2 );
1394 sal_Bool
XMLTransformerBase::RenameAttributeValue(
1395 OUString
& rOutAttributeValue
,
1400 return ( lcl_ConvertAttr( rOutAttributeValue
, nParam1
) ||
1401 lcl_ConvertAttr( rOutAttributeValue
, nParam2
) ||
1402 lcl_ConvertAttr( rOutAttributeValue
, nParam3
) );
1406 bool XMLTransformerBase::ConvertRNGDateTimeToISO( OUString
& rDateTime
)
1408 if( !rDateTime
.isEmpty() &&
1409 rDateTime
.indexOf( sal_Unicode('.')) != -1 )
1411 rDateTime
= rDateTime
.replace( sal_Unicode('.'), sal_Unicode(','));
1418 XMLTokenEnum
XMLTransformerBase::GetToken( const OUString
& rStr
) const
1420 XMLTransformerTokenMap::const_iterator aIter
=
1421 m_pTokenMap
->find( rStr
);
1422 if( aIter
== m_pTokenMap
->end() )
1423 return XML_TOKEN_END
;
1425 return (*aIter
).second
;
1430 const XMLTransformerContext
*XMLTransformerBase::GetCurrentContext() const
1432 OSL_ENSURE( !m_pContexts
->empty(), "empty stack" );
1435 return m_pContexts
->empty() ? 0 : m_pContexts
->back().get();
1438 const XMLTransformerContext
*XMLTransformerBase::GetAncestorContext(
1439 sal_uInt32 n
) const
1441 XMLTransformerContextVector::size_type nSize
=
1442 m_pContexts
->size();
1443 XMLTransformerContextVector::size_type nPos
=
1444 static_cast<XMLTransformerContextVector::size_type
>( n
);
1446 OSL_ENSURE( nSize
>nPos
+2 , "invalid context" );
1448 return nSize
> nPos
+2 ? (*m_pContexts
)[nSize
-(nPos
+2)].get() : 0;
1451 bool XMLTransformerBase::isWriter() const
1453 Reference
< XServiceInfo
> xSI( mxModel
, UNO_QUERY
);
1455 ( xSI
->supportsService( OUString( "com.sun.star.text.TextDocument" ) ) ||
1456 xSI
->supportsService( OUString( "com.sun.star.text.WebDocument" ) ) ||
1457 xSI
->supportsService( OUString( "com.sun.star.text.GlobalDocument" ) ) );
1460 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */