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 <osl/diagnose.h>
23 #include <com/sun/star/i18n/CharacterClassification.hpp>
24 #include <com/sun/star/i18n/UnicodeType.hpp>
25 #include <com/sun/star/util/MeasureUnit.hpp>
26 #include <sax/tools/converter.hxx>
27 #include <comphelper/processfactory.hxx>
28 #include <xmloff/nmspmap.hxx>
29 #include <xmloff/xmlnmspe.hxx>
30 #include "IgnoreTContext.hxx"
31 #include "RenameElemTContext.hxx"
32 #include "ProcAttrTContext.hxx"
33 #include "ProcAddAttrTContext.hxx"
34 #include "MergeElemTContext.hxx"
35 #include "CreateElemTContext.hxx"
36 #include "MutableAttrList.hxx"
37 #include "TransformerActions.hxx"
38 #include "ElemTransformerAction.hxx"
39 #include "PropertyActionsOOo.hxx"
40 #include "TransformerTokenMap.hxx"
42 #include "TransformerBase.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
, 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( false, "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_pElemActions( new XMLTransformerActions( pInit
) ),
190 m_pTokenMap( new XMLTransformerTokenMap( pTKMapInit
) )
192 GetNamespaceMap().Add( GetXMLToken(XML_NP_XLINK
), GetXMLToken(XML_N_XLINK
), XML_NAMESPACE_XLINK
);
193 GetNamespaceMap().Add( GetXMLToken(XML_NP_DC
), GetXMLToken(XML_N_DC
), XML_NAMESPACE_DC
);
194 GetNamespaceMap().Add( GetXMLToken(XML_NP_MATH
), GetXMLToken(XML_N_MATH
), XML_NAMESPACE_MATH
);
195 GetNamespaceMap().Add( GetXMLToken(XML_NP_OOO
), GetXMLToken(XML_N_OOO
), XML_NAMESPACE_OOO
);
196 GetNamespaceMap().Add( GetXMLToken(XML_NP_DOM
), GetXMLToken(XML_N_DOM
), XML_NAMESPACE_DOM
);
197 GetNamespaceMap().Add( GetXMLToken(XML_NP_OOOW
), GetXMLToken(XML_N_OOOW
), XML_NAMESPACE_OOOW
);
198 GetNamespaceMap().Add( GetXMLToken(XML_NP_OOOC
), GetXMLToken(XML_N_OOOC
), XML_NAMESPACE_OOOC
);
201 XMLTransformerBase::~XMLTransformerBase() throw ()
203 delete m_pNamespaceMap
;
204 delete m_pReplaceNamespaceMap
;
205 delete m_pElemActions
;
209 void SAL_CALL
XMLTransformerBase::startDocument()
210 throw( SAXException
, RuntimeException
, std::exception
)
212 m_xHandler
->startDocument();
215 void SAL_CALL
XMLTransformerBase::endDocument()
216 throw( SAXException
, RuntimeException
, std::exception
)
218 m_xHandler
->endDocument();
221 void SAL_CALL
XMLTransformerBase::startElement( const OUString
& rName
,
222 const Reference
< XAttributeList
>& rAttrList
)
223 throw(SAXException
, RuntimeException
, std::exception
)
225 SvXMLNamespaceMap
*pRewindMap
= nullptr;
227 bool bRect
= rName
== "presentation:show-shape";
230 // Process namespace attributes. This must happen before creating the
231 // context, because namespace decaration apply to the element name itself.
232 XMLMutableAttributeList
*pMutableAttrList
= nullptr;
233 Reference
< XAttributeList
> xAttrList( rAttrList
);
234 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
235 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
237 const OUString
& rAttrName
= xAttrList
->getNameByIndex( i
);
238 if( ( rAttrName
.getLength() >= 5 ) &&
239 ( rAttrName
.startsWith( GetXMLToken(XML_XMLNS
) ) ) &&
240 ( rAttrName
.getLength() == 5 || ':' == rAttrName
[5] ) )
244 pRewindMap
= m_pNamespaceMap
;
245 m_pNamespaceMap
= new SvXMLNamespaceMap( *m_pNamespaceMap
);
247 const OUString
& rAttrValue
= xAttrList
->getValueByIndex( i
);
249 OUString
aPrefix( ( rAttrName
.getLength() == 5 )
251 : rAttrName
.copy( 6 ) );
252 // Add namespace, but only if it is known.
253 sal_uInt16 nKey
= m_pNamespaceMap
->AddIfKnown( aPrefix
, rAttrValue
);
254 // If namespace is unknown, try to match a name with similar
256 if( XML_NAMESPACE_UNKNOWN
== nKey
)
258 OUString
aTestName( rAttrValue
);
259 if( SvXMLNamespaceMap::NormalizeOasisURN( aTestName
) )
260 nKey
= m_pNamespaceMap
->AddIfKnown( aPrefix
, aTestName
);
262 // If that namespace is not known, too, add it as unknown
263 if( XML_NAMESPACE_UNKNOWN
== nKey
)
264 nKey
= m_pNamespaceMap
->Add( aPrefix
, rAttrValue
);
266 const OUString
& rRepName
= m_pReplaceNamespaceMap
->GetNameByKey( nKey
);
267 if( !rRepName
.isEmpty() )
269 if( !pMutableAttrList
)
271 pMutableAttrList
= new XMLMutableAttributeList( xAttrList
);
272 xAttrList
= pMutableAttrList
;
275 pMutableAttrList
->SetValueByIndex( i
, rRepName
);
280 // Get element's namespace and local name.
283 m_pNamespaceMap
->GetKeyByAttrName( rName
, &aLocalName
);
285 // If there are contexts already, call a CreateChildContext at the topmost
286 // context. Otherwise, create a default context.
287 ::rtl::Reference
< XMLTransformerContext
> xContext
;
288 if( !m_pContexts
.empty() )
290 xContext
= m_pContexts
.back()->CreateChildContext( nPrefix
,
297 xContext
= CreateContext( nPrefix
, aLocalName
, rName
);
300 OSL_ENSURE( xContext
.is(), "XMLTransformerBase::startElement: missing context" );
302 xContext
= new XMLTransformerContext( *this, rName
);
304 // Remember old namespace map.
306 xContext
->PutRewindMap( pRewindMap
);
308 // Push context on stack.
309 m_pContexts
.push_back( xContext
);
311 // Call a startElement at the new context.
312 xContext
->StartElement( xAttrList
);
315 void SAL_CALL
XMLTransformerBase::endElement( const OUString
&
316 #if OSL_DEBUG_LEVEL > 0
320 throw(SAXException
, RuntimeException
, std::exception
)
322 if( !m_pContexts
.empty() )
324 // Get topmost context
325 ::rtl::Reference
< XMLTransformerContext
> xContext
= m_pContexts
.back();
327 #if OSL_DEBUG_LEVEL > 0
328 OSL_ENSURE( xContext
->GetQName() == rName
,
329 "XMLTransformerBase::endElement: popped context has wrong lname" );
332 // Call a EndElement at the current context.
333 xContext
->EndElement();
335 // and remove it from the stack.
336 m_pContexts
.pop_back();
338 // Get a namespace map to rewind.
339 SvXMLNamespaceMap
*pRewindMap
= xContext
->TakeRewindMap();
341 // Delete the current context.
344 // Rewind a namespace map.
347 delete m_pNamespaceMap
;
348 m_pNamespaceMap
= pRewindMap
;
353 void SAL_CALL
XMLTransformerBase::characters( const OUString
& rChars
)
354 throw(SAXException
, RuntimeException
, std::exception
)
356 if( !m_pContexts
.empty() )
358 m_pContexts
.back()->Characters( rChars
);
362 void SAL_CALL
XMLTransformerBase::ignorableWhitespace( const OUString
& rWhitespaces
)
363 throw(SAXException
, RuntimeException
, std::exception
)
365 m_xHandler
->ignorableWhitespace( rWhitespaces
);
368 void SAL_CALL
XMLTransformerBase::processingInstruction( const OUString
& rTarget
,
369 const OUString
& rData
)
370 throw(SAXException
, RuntimeException
, std::exception
)
372 m_xHandler
->processingInstruction( rTarget
, rData
);
375 void SAL_CALL
XMLTransformerBase::setDocumentLocator( const Reference
< XLocator
>& rLocator
)
376 throw(SAXException
, RuntimeException
, std::exception
)
378 m_xLocator
= rLocator
;
381 // XExtendedDocumentHandler
382 void SAL_CALL
XMLTransformerBase::startCDATA() throw(SAXException
, RuntimeException
, std::exception
)
384 if( m_xExtHandler
.is() )
385 m_xExtHandler
->startCDATA();
388 void SAL_CALL
XMLTransformerBase::endCDATA() throw(RuntimeException
, std::exception
)
390 if( m_xExtHandler
.is() )
391 m_xExtHandler
->endCDATA();
394 void SAL_CALL
XMLTransformerBase::comment( const OUString
& rComment
)
395 throw(SAXException
, RuntimeException
, std::exception
)
397 if( m_xExtHandler
.is() )
398 m_xExtHandler
->comment( rComment
);
401 void SAL_CALL
XMLTransformerBase::allowLineBreak()
402 throw(SAXException
, RuntimeException
, std::exception
)
404 if( m_xExtHandler
.is() )
405 m_xExtHandler
->allowLineBreak();
408 void SAL_CALL
XMLTransformerBase::unknown( const OUString
& rString
)
409 throw(SAXException
, RuntimeException
, std::exception
)
411 if( m_xExtHandler
.is() )
412 m_xExtHandler
->unknown( rString
);
416 void SAL_CALL
XMLTransformerBase::initialize( const Sequence
< Any
>& aArguments
)
417 throw(Exception
, RuntimeException
, std::exception
)
419 const sal_Int32 nAnyCount
= aArguments
.getLength();
420 const Any
* pAny
= aArguments
.getConstArray();
422 for( sal_Int32 nIndex
= 0; nIndex
< nAnyCount
; nIndex
++, pAny
++ )
424 // use isAssignableFrom instead of comparing the types to
425 // allow XExtendedDocumentHandler instead of XDocumentHandler (used in
426 // writeOasis2OOoLibraryElement in sfx2).
427 // The Any shift operator can't be used to query the type because it
428 // uses queryInterface, and the model also has a XPropertySet interface.
431 if( cppu::UnoType
<XDocumentHandler
>::get().isAssignableFrom( pAny
->getValueType() ) )
432 m_xHandler
.set( *pAny
, UNO_QUERY
);
434 // property set to transport data across
435 if( cppu::UnoType
<XPropertySet
>::get().isAssignableFrom( pAny
->getValueType() ) )
436 m_xPropSet
.set( *pAny
, UNO_QUERY
);
439 if( cppu::UnoType
<css::frame::XModel
>::get().isAssignableFrom( pAny
->getValueType() ) )
440 mxModel
.set( *pAny
, UNO_QUERY
);
443 if( m_xPropSet
.is() )
446 OUString sRelPath
, sName
;
447 Reference
< XPropertySetInfo
> xPropSetInfo
=
448 m_xPropSet
->getPropertySetInfo();
449 OUString
sPropName( "StreamRelPath" );
450 if( xPropSetInfo
->hasPropertyByName(sPropName
) )
452 aAny
= m_xPropSet
->getPropertyValue(sPropName
);
455 sPropName
= "StreamName";
456 if( xPropSetInfo
->hasPropertyByName(sPropName
) )
458 aAny
= m_xPropSet
->getPropertyValue(sPropName
);
461 if( !sName
.isEmpty() )
463 m_aExtPathPrefix
= "../";
465 // If there is a rel path within a package, then append
466 // additional '../'. If the rel path contains an ':', then it is
467 // an absolute URI (or invalid URI, because zip files don't
468 // permit ':'), and it will be ignored.
469 if( !sRelPath
.isEmpty() )
471 sal_Int32 nColPos
= sRelPath
.indexOf( ':' );
472 OSL_ENSURE( -1 == nColPos
,
473 "StreamRelPath contains ':', absolute URI?" );
477 OUString sTmp
= m_aExtPathPrefix
;
481 m_aExtPathPrefix
+= sTmp
;
482 nPos
= sRelPath
.indexOf( '/', nPos
+ 1 );
491 assert(m_xHandler
.is()); // can't do anything without that
494 static sal_Int16
lcl_getUnit( const OUString
& rValue
)
496 if( rValue
.endsWithIgnoreAsciiCase( "cm" ) )
497 return util::MeasureUnit::CM
;
498 else if ( rValue
.endsWithIgnoreAsciiCase( "mm" ) )
499 return util::MeasureUnit::MM
;
501 return util::MeasureUnit::INCH
;
504 XMLMutableAttributeList
*XMLTransformerBase::ProcessAttrList(
505 Reference
< XAttributeList
>& rAttrList
, sal_uInt16 nActionMap
,
508 XMLMutableAttributeList
*pMutableAttrList
= nullptr;
509 XMLTransformerActions
*pActions
= GetUserDefinedActions( nActionMap
);
510 OSL_ENSURE( pActions
, "go no actions" );
513 sal_Int16 nAttrCount
= rAttrList
.is() ? rAttrList
->getLength() : 0;
514 for( sal_Int16 i
=0; i
< nAttrCount
; ++i
)
516 const OUString
& rAttrName
= rAttrList
->getNameByIndex( i
);
517 const OUString
& rAttrValue
= rAttrList
->getValueByIndex( i
);
519 sal_uInt16 nPrefix
= GetNamespaceMap().GetKeyByAttrName( rAttrName
,
522 XMLTransformerActions::key_type
aKey( nPrefix
, aLocalName
);
523 XMLTransformerActions::const_iterator aIter
=
524 pActions
->find( aKey
);
525 if( !(aIter
== pActions
->end() ) )
527 if( !pMutableAttrList
)
529 pMutableAttrList
= new XMLMutableAttributeList( rAttrList
,
531 rAttrList
= pMutableAttrList
;
534 sal_uInt32 nAction
= (*aIter
).second
.m_nActionType
;
535 bool bRename
= false;
538 case XML_ATACTION_RENAME
:
541 case XML_ATACTION_COPY
:
543 case XML_ATACTION_REMOVE
:
544 case XML_ATACTION_STYLE_DISPLAY_NAME
:
545 pMutableAttrList
->RemoveAttributeByIndex( i
);
549 case XML_ATACTION_RENAME_IN2INCH
:
552 case XML_ATACTION_IN2INCH
:
554 OUString
aAttrValue( rAttrValue
);
555 if( ReplaceSingleInWithInch( aAttrValue
) )
556 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
559 case XML_ATACTION_INS2INCHS
:
561 OUString
aAttrValue( rAttrValue
);
562 if( ReplaceInWithInch( aAttrValue
) )
563 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
566 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
,
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
:
617 case XML_ATACTION_DECODE_STYLE_NAME
:
618 case XML_ATACTION_DECODE_STYLE_NAME_REF
:
620 OUString
aAttrValue( rAttrValue
);
621 if( DecodeStyleName(aAttrValue
) )
622 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
625 case XML_ATACTION_ENCODE_STYLE_NAME
:
627 OUString
aAttrValue( rAttrValue
);
628 if( EncodeStyleName(aAttrValue
) )
630 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
631 OUString
aNewAttrQName(
632 GetNamespaceMap().GetQNameByKey(
634 ::xmloff::token::GetXMLToken(
635 XML_DISPLAY_NAME
) ) );
636 pMutableAttrList
->AddAttribute( aNewAttrQName
,
641 case XML_ATACTION_RENAME_ENCODE_STYLE_NAME_REF
:
644 case XML_ATACTION_ENCODE_STYLE_NAME_REF
:
646 OUString
aAttrValue( rAttrValue
);
647 if( EncodeStyleName(aAttrValue
) )
648 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
651 case XML_ATACTION_RENAME_NEG_PERCENT
:
654 case XML_ATACTION_NEG_PERCENT
:
656 OUString
aAttrValue( rAttrValue
);
657 if( NegPercent( aAttrValue
) )
658 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
661 case XML_ATACTION_RENAME_ADD_NAMESPACE_PREFIX
:
664 case XML_ATACTION_ADD_NAMESPACE_PREFIX
:
666 OUString
aAttrValue( rAttrValue
);
667 sal_uInt16 nValPrefix
=
668 static_cast<sal_uInt16
>(
669 bRename
? (*aIter
).second
.m_nParam2
670 : (*aIter
).second
.m_nParam1
);
671 if( AddNamespacePrefix( aAttrValue
, nValPrefix
) )
672 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
675 case XML_ATACTION_ADD_APP_NAMESPACE_PREFIX
:
677 OUString
aAttrValue( rAttrValue
);
678 sal_uInt16 nValPrefix
=
679 static_cast<sal_uInt16
>((*aIter
).second
.m_nParam1
);
680 if( IsXMLToken( GetClass(), XML_SPREADSHEET
) )
681 nValPrefix
= XML_NAMESPACE_OOOC
;
682 else if( IsXMLToken( GetClass(), XML_TEXT
) )
683 nValPrefix
= XML_NAMESPACE_OOOW
;
684 if( AddNamespacePrefix( aAttrValue
, nValPrefix
) )
685 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
688 case XML_ATACTION_RENAME_REMOVE_NAMESPACE_PREFIX
:
691 case XML_ATACTION_REMOVE_NAMESPACE_PREFIX
:
693 OUString
aAttrValue( rAttrValue
);
694 sal_uInt16 nValPrefix
=
695 static_cast<sal_uInt16
>(
696 bRename
? (*aIter
).second
.m_nParam2
697 : (*aIter
).second
.m_nParam1
);
698 if( RemoveNamespacePrefix( aAttrValue
, nValPrefix
) )
699 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
702 case XML_ATACTION_REMOVE_ANY_NAMESPACE_PREFIX
:
704 OUString
aAttrValue( rAttrValue
);
705 if( RemoveNamespacePrefix( aAttrValue
) )
706 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
709 case XML_ATACTION_URI_OOO
:
711 OUString
aAttrValue( rAttrValue
);
712 if( ConvertURIToOASIS( aAttrValue
,
713 static_cast< bool >((*aIter
).second
.m_nParam1
)))
714 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
717 case XML_ATACTION_URI_OASIS
:
719 OUString
aAttrValue( rAttrValue
);
720 if( ConvertURIToOOo( aAttrValue
,
721 static_cast< bool >((*aIter
).second
.m_nParam1
)))
722 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
725 case XML_ATACTION_RENAME_ATTRIBUTE
:
727 OUString
aAttrValue( rAttrValue
);
728 RenameAttributeValue(
730 (*aIter
).second
.m_nParam1
,
731 (*aIter
).second
.m_nParam2
,
732 (*aIter
).second
.m_nParam3
);
733 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
736 case XML_ATACTION_RNG2ISO_DATETIME
:
738 OUString
aAttrValue( rAttrValue
);
739 if( ConvertRNGDateTimeToISO( aAttrValue
))
740 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
743 case XML_ATACTION_RENAME_RNG2ISO_DATETIME
:
745 OUString
aAttrValue( rAttrValue
);
746 if( ConvertRNGDateTimeToISO( aAttrValue
))
747 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
751 case XML_ATACTION_IN2TWIPS
:
753 OUString
aAttrValue( rAttrValue
);
754 XMLTransformerBase::ReplaceSingleInWithInch( aAttrValue
);
758 sal_Int16
const nDestUnit
= lcl_getUnit(aAttrValue
);
760 // convert inch value to twips and export as faked inch
762 if (::sax::Converter::convertMeasure(nMeasure
,
766 // #i13778#,#i36248#/ apply correct 1/100mm-to-twip conversion
767 nMeasure
= (sal_Int32
)( nMeasure
>= 0
768 ? ((nMeasure
*72+63)/127)
769 : ((nMeasure
*72-63)/127) );
771 OUStringBuffer aBuffer
;
772 ::sax::Converter::convertMeasure( aBuffer
,
773 nMeasure
, util::MeasureUnit::MM_100TH
,
775 aAttrValue
= aBuffer
.makeStringAndClear();
779 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
782 case XML_ATACTION_SVG_WIDTH_HEIGHT_OOO
:
784 OUString
aAttrValue( rAttrValue
);
785 ReplaceSingleInchWithIn( aAttrValue
);
787 sal_Int16
const nDestUnit
= lcl_getUnit( aAttrValue
);
790 if (::sax::Converter::convertMeasure(nMeasure
,
796 else if( nMeasure
< 0 )
800 OUStringBuffer aBuffer
;
801 ::sax::Converter::convertMeasure(aBuffer
, nMeasure
,
802 util::MeasureUnit::MM_100TH
, nDestUnit
);
803 aAttrValue
= aBuffer
.makeStringAndClear();
806 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
809 case XML_ATACTION_SVG_WIDTH_HEIGHT_OASIS
:
811 OUString
aAttrValue( rAttrValue
);
812 ReplaceSingleInWithInch( aAttrValue
);
814 sal_Int16
const nDestUnit
= lcl_getUnit( aAttrValue
);
817 if (::sax::Converter::convertMeasure(nMeasure
,
823 else if( nMeasure
< 0 )
827 OUStringBuffer aBuffer
;
828 ::sax::Converter::convertMeasure(aBuffer
, nMeasure
,
829 util::MeasureUnit::MM_100TH
, nDestUnit
);
830 aAttrValue
= aBuffer
.makeStringAndClear();
833 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
836 case XML_ATACTION_DECODE_ID
:
838 const sal_Int32 nLen
= rAttrValue
.getLength();
839 OUStringBuffer aBuffer
;
842 for( pos
= 0; pos
< nLen
; pos
++ )
844 sal_Unicode c
= rAttrValue
[pos
];
845 if( (c
>= '0') && (c
<= '9') )
848 aBuffer
.append( (sal_Int32
)c
);
851 pMutableAttrList
->SetValueByIndex( i
, aBuffer
.makeStringAndClear() );
854 // #i50322# - special handling for the
855 // transparency of writer background graphics.
856 case XML_ATACTION_WRITER_BACK_GRAPHIC_TRANSPARENCY
:
858 // determine, if it's the transparency of a document style
859 XMLTransformerContext
* pFirstContext
= m_pContexts
[0].get();
860 OUString aFirstContextLocalName
;
861 /* sal_uInt16 nFirstContextPrefix = */
862 GetNamespaceMap().GetKeyByAttrName( pFirstContext
->GetQName(),
863 &aFirstContextLocalName
);
864 bool bIsDocumentStyle(
865 ::xmloff::token::IsXMLToken( aFirstContextLocalName
,
866 XML_DOCUMENT_STYLES
) );
867 // no conversion of transparency value for document
868 // styles, because former OpenOffice.org version writes
869 // writes always a transparency value of 100% and doesn't
870 // read the value. Thus, it's intepreted as 0%
871 if ( !bIsDocumentStyle
)
873 OUString
aAttrValue( rAttrValue
);
874 NegPercent(aAttrValue
);
875 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
880 case XML_ATACTION_SHAPEID
:
882 OUString
sNewValue( "shape" );
883 sNewValue
+= rAttrValue
;
884 pMutableAttrList
->SetValueByIndex( i
, sNewValue
);
889 OSL_ENSURE( false, "unknown action" );
895 OUString
aNewAttrQName(
896 GetNamespaceMap().GetQNameByKey(
897 (*aIter
).second
.GetQNamePrefixFromParam1(),
898 ::xmloff::token::GetXMLToken(
899 (*aIter
).second
.GetQNameTokenFromParam1()) ) );
900 pMutableAttrList
->RenameAttributeByIndex( i
,
907 return pMutableAttrList
;
910 bool XMLTransformerBase::ReplaceSingleInchWithIn( OUString
& rValue
)
913 sal_Int32 nPos
= rValue
.getLength();
914 while( nPos
&& rValue
[nPos
-1] <= ' ' )
917 ('c'==rValue
[nPos
-2] || 'C'==rValue
[nPos
-2]) &&
918 ('h'==rValue
[nPos
-1] || 'H'==rValue
[nPos
-1]) )
920 rValue
=rValue
.copy( 0, nPos
-2 );
927 bool XMLTransformerBase::ReplaceInchWithIn( OUString
& rValue
)
931 while( nPos
< rValue
.getLength()-3 )
933 sal_Unicode c
= rValue
[nPos
];
934 if( 'i'==c
|| 'I'==c
)
937 if( (c
>= '0' && c
<= '9') || '.' == c
)
940 if( 'n'==c
|| 'N'==c
)
943 if( 'c'==c
|| 'C'==c
)
946 if( 'h'==c
|| 'H'==c
)
948 rValue
= rValue
.replaceAt( nPos
,
949 4, GetXMLToken(XML_UNIT_INCH
) );
964 bool XMLTransformerBase::ReplaceSingleInWithInch( OUString
& rValue
)
968 sal_Int32 nPos
= rValue
.getLength();
969 while( nPos
&& rValue
[nPos
-1] <= ' ' )
972 ('i'==rValue
[nPos
-2] ||
973 'I'==rValue
[nPos
-2]) &&
974 ('n'==rValue
[nPos
-1] ||
975 'N'==rValue
[nPos
-1]) )
978 rValue
= rValue
.replaceAt( nPos
, rValue
.getLength() - nPos
,
979 GetXMLToken(XML_INCH
) );
986 bool XMLTransformerBase::ReplaceInWithInch( OUString
& rValue
)
990 while( nPos
< rValue
.getLength()-1 )
992 sal_Unicode c
= rValue
[nPos
];
993 if( 'i'==c
|| 'I'==c
)
996 if( (c
>= '0' && c
<= '9') || '.' == c
)
999 if( 'n'==c
|| 'N'==c
)
1001 rValue
= rValue
.replaceAt( nPos
,
1002 2, GetXMLToken(XML_INCH
) );
1015 bool XMLTransformerBase::EncodeStyleName( OUString
& rName
) const
1017 static const sal_Char aHexTab
[] = "0123456789abcdef";
1019 bool bEncoded
= false;
1021 sal_Int32 nLen
= rName
.getLength();
1022 OUStringBuffer
aBuffer( nLen
);
1024 for( sal_Int32 i
= 0; i
< nLen
; i
++ )
1026 sal_Unicode c
= rName
[i
];
1027 bool bValidChar
= false;
1031 (c
>= 0x0041 && c
<= 0x005a) ||
1032 (c
>= 0x0061 && c
<= 0x007a) ||
1033 (c
>= 0x00c0 && c
<= 0x00d6) ||
1034 (c
>= 0x00d8 && c
<= 0x00f6) ||
1035 (c
>= 0x00f8 && c
<= 0x00ff) ||
1036 ( i
> 0 && ( (c
>= 0x0030 && c
<= 0x0039) ||
1037 c
== 0x00b7 || c
== '-' || c
== '.') );
1041 if( (c
>= 0xf900U
&& c
<= 0xfffeU
) ||
1042 (c
>= 0x20ddU
&& c
<= 0x20e0U
))
1046 else if( (c
>= 0x02bbU
&& c
<= 0x02c1U
) || c
== 0x0559 ||
1047 c
== 0x06e5 || c
== 0x06e6 )
1051 else if( c
== 0x0387 )
1057 if( !xCharClass
.is() )
1059 const_cast < XMLTransformerBase
* >(this)
1060 ->xCharClass
= CharacterClassification::create( comphelper::getProcessComponentContext() );
1062 sal_Int16 nType
= xCharClass
->getType( rName
, i
);
1066 case UnicodeType::UPPERCASE_LETTER
: // Lu
1067 case UnicodeType::LOWERCASE_LETTER
: // Ll
1068 case UnicodeType::TITLECASE_LETTER
: // Lt
1069 case UnicodeType::OTHER_LETTER
: // Lo
1070 case UnicodeType::LETTER_NUMBER
: // Nl
1073 case UnicodeType::NON_SPACING_MARK
: // Ms
1074 case UnicodeType::ENCLOSING_MARK
: // Me
1075 case UnicodeType::COMBINING_SPACING_MARK
: //Mc
1076 case UnicodeType::MODIFIER_LETTER
: // Lm
1077 case UnicodeType::DECIMAL_DIGIT_NUMBER
: // Nd
1085 aBuffer
.append( c
);
1089 aBuffer
.append( '_' );
1091 aBuffer
.append( static_cast< sal_Unicode
>(
1092 aHexTab
[ (c
>> 12) & 0x0f ] ) );
1094 aBuffer
.append( static_cast< sal_Unicode
>(
1095 aHexTab
[ (c
>> 8) & 0x0f ] ) );
1097 aBuffer
.append( static_cast< sal_Unicode
>(
1098 aHexTab
[ (c
>> 4) & 0x0f ] ) );
1099 aBuffer
.append( static_cast< sal_Unicode
>(
1100 aHexTab
[ c
& 0x0f ] ) );
1101 aBuffer
.append( '_' );
1106 if( aBuffer
.getLength() > (1<<15)-1 )
1110 rName
= aBuffer
.makeStringAndClear();
1114 bool XMLTransformerBase::DecodeStyleName( OUString
& rName
)
1116 bool bEncoded
= false;
1118 sal_Int32 nLen
= rName
.getLength();
1119 OUStringBuffer
aBuffer( nLen
);
1121 bool bWithinHex
= false;
1122 sal_Unicode cEnc
= 0;
1123 for( sal_Int32 i
= 0; i
< nLen
; i
++ )
1125 sal_Unicode c
= rName
[i
];
1130 aBuffer
.append( cEnc
);
1137 bWithinHex
= !bWithinHex
;
1139 else if( bWithinHex
)
1142 if( c
>= '0' && c
<= '9' )
1146 else if( c
>= 'a' && c
<= 'f' )
1148 cDigit
= c
- 'a' + 10;
1150 else if( c
>= 'A' && c
<= 'F' )
1152 cDigit
= c
- 'A' + 10;
1160 cEnc
= (cEnc
<< 4) + cDigit
;
1164 aBuffer
.append( c
);
1169 rName
= aBuffer
.makeStringAndClear();
1173 bool XMLTransformerBase::NegPercent( OUString
& rValue
)
1180 sal_Int32 nLen
= rValue
.getLength();
1183 while( nPos
< nLen
&& ' ' == rValue
[nPos
] )
1186 if( nPos
< nLen
&& '-' == rValue
[nPos
] )
1193 while( nPos
< nLen
&&
1194 '0' <= rValue
[nPos
] &&
1195 '9' >= rValue
[nPos
] )
1197 // TODO: check overflow!
1199 nVal
+= (rValue
[nPos
] - '0');
1202 if( nPos
< nLen
&& '.' == rValue
[nPos
] )
1207 while( nPos
< nLen
&&
1208 '0' <= rValue
[nPos
] &&
1209 '9' >= rValue
[nPos
] )
1211 // TODO: check overflow!
1213 nVal
+= ( static_cast<double>(rValue
[nPos
] - '0') / nDiv
);
1219 while( nPos
< nLen
&& ' ' == rValue
[nPos
] )
1222 if( nPos
< nLen
&& '%' == rValue
[nPos
] )
1228 sal_Int32 nIntVal
= 100 - static_cast<sal_Int32
>( nVal
);
1230 rValue
= OUString::number(nIntVal
) + "%";
1238 bool XMLTransformerBase::AddNamespacePrefix( OUString
& rName
,
1239 sal_uInt16 nPrefix
) const
1241 rName
= GetNamespaceMap().GetQNameByKey( nPrefix
, rName
, false );
1245 bool XMLTransformerBase::RemoveNamespacePrefix( OUString
& rName
,
1246 sal_uInt16 nPrefixOnly
) const
1248 OUString aLocalName
;
1249 sal_uInt16 nPrefix
=
1250 GetNamespaceMap().GetKeyByAttrName_( rName
, &aLocalName
);
1251 bool bRet
= XML_NAMESPACE_UNKNOWN
!= nPrefix
&&
1252 (USHRT_MAX
== nPrefixOnly
|| nPrefix
== nPrefixOnly
);
1259 bool XMLTransformerBase::ConvertURIToOASIS( OUString
& rURI
,
1260 bool bSupportPackage
) const
1263 if( !m_aExtPathPrefix
.isEmpty() && !rURI
.isEmpty() )
1270 // for package URIs, the '#' has to be removed
1271 if( bSupportPackage
)
1273 rURI
= rURI
.copy( 1 );
1278 // no rel path; nothing to do
1281 // a rel path; to keep URI simple, remove './', if there
1283 if( rURI
.getLength() > 1 && '/' == rURI
[1] )
1285 rURI
= rURI
.copy( 2 );
1290 // check for a RFC2396 schema
1294 sal_Int32 nLen
= rURI
.getLength();
1295 while( nPos
< nLen
)
1297 switch( rURI
[nPos
] )
1300 // a relative path segement
1301 nPos
= nLen
; // leave loop
1306 nPos
= nLen
; // leave loop
1309 // we don't care about any other characters
1319 OUString
sTmp( m_aExtPathPrefix
);
1329 bool XMLTransformerBase::ConvertURIToOOo( OUString
& rURI
,
1330 bool bSupportPackage
) const
1333 if( !rURI
.isEmpty() )
1335 bool bPackage
= false;
1339 // no rel path; nothing to do
1343 if( rURI
.startsWith( m_aExtPathPrefix
) )
1345 // an external URI; remove '../'
1346 rURI
= rURI
.copy( m_aExtPathPrefix
.getLength() );
1355 // check for a RFC2396 schema
1359 sal_Int32 nLen
= rURI
.getLength();
1360 while( nPos
< nLen
)
1362 switch( rURI
[nPos
] )
1365 // a relative path segement within the package
1366 nPos
= nLen
; // leave loop
1371 nPos
= nLen
; // leave loop
1374 // we don't care about any other characters
1382 if( bPackage
&& bSupportPackage
)
1384 OUString
sTmp( '#' );
1385 if( rURI
.startsWith( "./" ) )
1386 rURI
= rURI
.copy( 2 );
1396 bool XMLTransformerBase::RenameAttributeValue(
1397 OUString
& rOutAttributeValue
,
1402 return ( lcl_ConvertAttr( rOutAttributeValue
, nParam1
) ||
1403 lcl_ConvertAttr( rOutAttributeValue
, nParam2
) ||
1404 lcl_ConvertAttr( rOutAttributeValue
, nParam3
) );
1408 bool XMLTransformerBase::ConvertRNGDateTimeToISO( OUString
& rDateTime
)
1410 if( !rDateTime
.isEmpty() &&
1411 rDateTime
.indexOf( '.' ) != -1 )
1413 rDateTime
= rDateTime
.replace( '.', ',');
1420 XMLTokenEnum
XMLTransformerBase::GetToken( const OUString
& rStr
) const
1422 XMLTransformerTokenMap::const_iterator aIter
=
1423 m_pTokenMap
->find( rStr
);
1424 if( aIter
== m_pTokenMap
->end() )
1425 return XML_TOKEN_END
;
1427 return (*aIter
).second
;
1431 const XMLTransformerContext
*XMLTransformerBase::GetCurrentContext() const
1433 OSL_ENSURE( !m_pContexts
.empty(), "empty stack" );
1436 return m_pContexts
.empty() ? nullptr : m_pContexts
.back().get();
1439 const XMLTransformerContext
*XMLTransformerBase::GetAncestorContext(
1440 sal_uInt32 n
) const
1442 auto nSize
= m_pContexts
.size();
1444 OSL_ENSURE( nSize
> n
+ 2 , "invalid context" );
1446 return nSize
> n
+ 2 ? m_pContexts
[nSize
- (n
+ 2)].get() : nullptr;
1449 bool XMLTransformerBase::isWriter() const
1451 Reference
< XServiceInfo
> xSI( mxModel
, UNO_QUERY
);
1453 ( xSI
->supportsService("com.sun.star.text.TextDocument") ||
1454 xSI
->supportsService("com.sun.star.text.WebDocument") ||
1455 xSI
->supportsService("com.sun.star.text.GlobalDocument") );
1458 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */