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 ::rtl::OUString
;
45 using ::rtl::OUStringBuffer
;
46 using namespace ::osl
;
47 using namespace ::xmloff::token
;
48 using namespace ::com::sun::star
;
49 using namespace ::com::sun::star::uno
;
50 using namespace ::com::sun::star::beans
;
51 using namespace ::com::sun::star::lang
;
52 using namespace ::com::sun::star::i18n
;
53 using namespace ::com::sun::star::xml::sax
;
57 bool lcl_ConvertAttr( OUString
& rOutAttribute
, sal_Int32 nParam
)
60 enum XMLTokenEnum eTokenToRename
=
61 static_cast< enum XMLTokenEnum
>( nParam
& 0xffff );
62 if( eTokenToRename
!= XML_TOKEN_INVALID
&&
63 IsXMLToken( rOutAttribute
, eTokenToRename
))
65 enum XMLTokenEnum eReplacementToken
=
66 static_cast< enum XMLTokenEnum
>( nParam
>> 16 );
67 rOutAttribute
= GetXMLToken( eReplacementToken
);
72 } // anonymous namespace
74 XMLTransformerContext
*XMLTransformerBase::CreateContext( sal_uInt16 nPrefix
,
75 const OUString
& rLocalName
, const OUString
& rQName
)
77 XMLTransformerActions::key_type
aKey( nPrefix
, rLocalName
);
78 XMLTransformerActions::const_iterator aIter
=
79 GetElemActions().find( aKey
);
81 if( !(aIter
== GetElemActions().end()) )
83 sal_uInt32 nActionType
= (*aIter
).second
.m_nActionType
;
84 if( (nActionType
& XML_ETACTION_USER_DEFINED
) != 0 )
86 XMLTransformerContext
*pContext
=
87 CreateUserDefinedContext( (*aIter
).second
,
89 OSL_ENSURE( pContext
&& !pContext
->IsPersistent(),
90 "unknown or not persistent action" );
96 case XML_ETACTION_COPY_CONTENT
:
97 return new XMLIgnoreTransformerContext( *this, rQName
, sal_False
,
99 case XML_ETACTION_COPY
:
100 return new XMLTransformerContext( *this, rQName
);
101 case XML_ETACTION_RENAME_ELEM
:
102 return new XMLRenameElemTransformerContext( *this, rQName
,
103 (*aIter
).second
.GetQNamePrefixFromParam1(),
104 (*aIter
).second
.GetQNameTokenFromParam1() );
105 case XML_ETACTION_RENAME_ELEM_ADD_ATTR
:
106 return new XMLRenameElemTransformerContext( *this, rQName
,
107 (*aIter
).second
.GetQNamePrefixFromParam1(),
108 (*aIter
).second
.GetQNameTokenFromParam1(),
109 (*aIter
).second
.GetQNamePrefixFromParam2(),
110 (*aIter
).second
.GetQNameTokenFromParam2(),
111 static_cast< XMLTokenEnum
>( (*aIter
).second
.m_nParam3
) );
112 case XML_ETACTION_RENAME_ELEM_PROC_ATTRS
:
113 return new XMLProcAttrTransformerContext( *this, rQName
,
114 (*aIter
).second
.GetQNamePrefixFromParam1(),
115 (*aIter
).second
.GetQNameTokenFromParam1(),
116 static_cast< sal_uInt16
>( (*aIter
).second
.m_nParam2
) );
117 case XML_ETACTION_RENAME_ELEM_ADD_PROC_ATTR
:
118 return new XMLProcAddAttrTransformerContext( *this, rQName
,
119 (*aIter
).second
.GetQNamePrefixFromParam1(),
120 (*aIter
).second
.GetQNameTokenFromParam1(),
121 static_cast< sal_uInt16
>(
122 (*aIter
).second
.m_nParam3
>> 16 ),
123 (*aIter
).second
.GetQNamePrefixFromParam2(),
124 (*aIter
).second
.GetQNameTokenFromParam2(),
125 static_cast< XMLTokenEnum
>(
126 (*aIter
).second
.m_nParam3
& 0xffff ) );
127 case XML_ETACTION_RENAME_ELEM_COND
:
129 const XMLTransformerContext
*pCurrent
= GetCurrentContext();
130 if( pCurrent
->HasQName(
131 (*aIter
).second
.GetQNamePrefixFromParam2(),
132 (*aIter
).second
.GetQNameTokenFromParam2() ) )
133 return new XMLRenameElemTransformerContext( *this, rQName
,
134 (*aIter
).second
.GetQNamePrefixFromParam1(),
135 (*aIter
).second
.GetQNameTokenFromParam1() );
138 case XML_ETACTION_RENAME_ELEM_PROC_ATTRS_COND
:
140 const XMLTransformerContext
*pCurrent
= GetCurrentContext();
141 if( pCurrent
->HasQName(
142 (*aIter
).second
.GetQNamePrefixFromParam3(),
143 (*aIter
).second
.GetQNameTokenFromParam3() ) )
144 return new XMLProcAttrTransformerContext( *this, rQName
,
145 (*aIter
).second
.GetQNamePrefixFromParam1(),
146 (*aIter
).second
.GetQNameTokenFromParam1(),
147 static_cast< sal_uInt16
>( (*aIter
).second
.m_nParam2
) );
149 return new XMLProcAttrTransformerContext( *this, rQName
,
150 static_cast< sal_uInt16
>( (*aIter
).second
.m_nParam2
) );
152 case XML_ETACTION_PROC_ATTRS
:
153 return new XMLProcAttrTransformerContext( *this, rQName
,
154 static_cast< sal_uInt16
>( (*aIter
).second
.m_nParam1
) );
155 case XML_ETACTION_PROC_ATTRS_COND
:
157 const XMLTransformerContext
*pCurrent
= GetCurrentContext();
158 if( pCurrent
->HasQName(
159 (*aIter
).second
.GetQNamePrefixFromParam1(),
160 (*aIter
).second
.GetQNameTokenFromParam1() ) )
161 return new XMLProcAttrTransformerContext( *this, rQName
,
162 static_cast< sal_uInt16
>( (*aIter
).second
.m_nParam2
) );
165 case XML_ETACTION_MOVE_ATTRS_TO_ELEMS
:
166 return new XMLCreateElemTransformerContext( *this, rQName
,
167 static_cast< sal_uInt16
>( (*aIter
).second
.m_nParam1
) );
168 case XML_ETACTION_MOVE_ELEMS_TO_ATTRS
:
169 return new XMLMergeElemTransformerContext( *this, rQName
,
170 static_cast< sal_uInt16
>( (*aIter
).second
.m_nParam1
) );
172 OSL_ENSURE( !this, "unknown action" );
177 // default is copying
178 return new XMLTransformerContext( *this, rQName
);
181 XMLTransformerActions
*XMLTransformerBase::GetUserDefinedActions( sal_uInt16
)
186 XMLTransformerBase::XMLTransformerBase( XMLTransformerActionInit
*pInit
,
187 ::xmloff::token::XMLTokenEnum
*pTKMapInit
)
189 m_pNamespaceMap( new SvXMLNamespaceMap
),
190 m_pReplaceNamespaceMap( new SvXMLNamespaceMap
),
191 m_pContexts( new XMLTransformerContextVector
),
192 m_pElemActions( new XMLTransformerActions( pInit
) ),
193 m_pTokenMap( new XMLTransformerTokenMap( pTKMapInit
) )
195 GetNamespaceMap().Add( GetXMLToken(XML_NP_XLINK
), GetXMLToken(XML_N_XLINK
), XML_NAMESPACE_XLINK
);
196 GetNamespaceMap().Add( GetXMLToken(XML_NP_DC
), GetXMLToken(XML_N_DC
), XML_NAMESPACE_DC
);
197 GetNamespaceMap().Add( GetXMLToken(XML_NP_MATH
), GetXMLToken(XML_N_MATH
), XML_NAMESPACE_MATH
);
198 GetNamespaceMap().Add( GetXMLToken(XML_NP_OOO
), GetXMLToken(XML_N_OOO
), XML_NAMESPACE_OOO
);
199 GetNamespaceMap().Add( GetXMLToken(XML_NP_DOM
), GetXMLToken(XML_N_DOM
), XML_NAMESPACE_DOM
);
200 GetNamespaceMap().Add( GetXMLToken(XML_NP_OOOW
), GetXMLToken(XML_N_OOOW
), XML_NAMESPACE_OOOW
);
201 GetNamespaceMap().Add( GetXMLToken(XML_NP_OOOC
), GetXMLToken(XML_N_OOOC
), XML_NAMESPACE_OOOC
);
204 XMLTransformerBase::~XMLTransformerBase() throw ()
208 delete m_pNamespaceMap
;
209 delete m_pReplaceNamespaceMap
;
211 delete m_pElemActions
;
215 void SAL_CALL
XMLTransformerBase::startDocument( void )
216 throw( SAXException
, RuntimeException
)
218 m_xHandler
->startDocument();
221 void SAL_CALL
XMLTransformerBase::endDocument( void )
222 throw( SAXException
, RuntimeException
)
224 m_xHandler
->endDocument();
227 void SAL_CALL
XMLTransformerBase::startElement( const OUString
& rName
,
228 const Reference
< XAttributeList
>& rAttrList
)
229 throw(SAXException
, RuntimeException
)
231 SvXMLNamespaceMap
*pRewindMap
= 0;
233 bool bRect
= rName
== "presentation:show-shape";
236 // Process namespace attributes. This must happen before creating the
237 // context, because namespace decaration apply to the element name itself.
238 XMLMutableAttributeList
*pMutableAttrList
= 0;
239 Reference
< XAttributeList
> xAttrList( rAttrList
);
240 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
241 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
243 const OUString
& rAttrName
= xAttrList
->getNameByIndex( i
);
244 if( ( rAttrName
.getLength() >= 5 ) &&
245 ( rAttrName
.compareTo( GetXMLToken(XML_XMLNS
), 5 ) == 0 ) &&
246 ( rAttrName
.getLength() == 5 || ':' == rAttrName
[5] ) )
250 pRewindMap
= m_pNamespaceMap
;
251 m_pNamespaceMap
= new SvXMLNamespaceMap( *m_pNamespaceMap
);
253 const OUString
& rAttrValue
= xAttrList
->getValueByIndex( i
);
255 OUString
aPrefix( ( rAttrName
.getLength() == 5 )
257 : rAttrName
.copy( 6 ) );
258 // Add namespace, but only if it is known.
259 sal_uInt16 nKey
= m_pNamespaceMap
->AddIfKnown( aPrefix
, rAttrValue
);
260 // If namespace is unknown, try to match a name with similar
262 if( XML_NAMESPACE_UNKNOWN
== nKey
)
264 OUString
aTestName( rAttrValue
);
265 if( SvXMLNamespaceMap::NormalizeOasisURN( aTestName
) )
266 nKey
= m_pNamespaceMap
->AddIfKnown( aPrefix
, aTestName
);
268 // If that namespace is not known, too, add it as unknown
269 if( XML_NAMESPACE_UNKNOWN
== nKey
)
270 nKey
= m_pNamespaceMap
->Add( aPrefix
, rAttrValue
);
272 const OUString
& rRepName
= m_pReplaceNamespaceMap
->GetNameByKey( nKey
);
273 if( !rRepName
.isEmpty() )
275 if( !pMutableAttrList
)
277 pMutableAttrList
= new XMLMutableAttributeList( xAttrList
);
278 xAttrList
= pMutableAttrList
;
281 pMutableAttrList
->SetValueByIndex( i
, rRepName
);
286 // Get element's namespace and local name.
289 m_pNamespaceMap
->GetKeyByAttrName( rName
, &aLocalName
);
291 // If there are contexts already, call a CreateChildContext at the topmost
292 // context. Otherwise, create a default context.
293 ::rtl::Reference
< XMLTransformerContext
> xContext
;
294 if( !m_pContexts
->empty() )
296 xContext
= m_pContexts
->back()->CreateChildContext( nPrefix
,
303 xContext
= CreateContext( nPrefix
, aLocalName
, rName
);
306 OSL_ENSURE( xContext
.is(), "XMLTransformerBase::startElement: missing context" );
308 xContext
= new XMLTransformerContext( *this, rName
);
310 // Remeber old namespace map.
312 xContext
->SetRewindMap( pRewindMap
);
314 // Push context on stack.
315 m_pContexts
->push_back( xContext
);
317 // Call a startElement at the new context.
318 xContext
->StartElement( xAttrList
);
321 void SAL_CALL
XMLTransformerBase::endElement( const OUString
&
322 #if OSL_DEBUG_LEVEL > 0
326 throw(SAXException
, RuntimeException
)
328 if( !m_pContexts
->empty() )
330 // Get topmost context
331 ::rtl::Reference
< XMLTransformerContext
> xContext
= m_pContexts
->back();
333 #if OSL_DEBUG_LEVEL > 0
334 OSL_ENSURE( xContext
->GetQName() == rName
,
335 "XMLTransformerBase::endElement: popped context has wrong lname" );
338 // Call a EndElement at the current context.
339 xContext
->EndElement();
341 // and remove it from the stack.
342 m_pContexts
->pop_back();
344 // Get a namespace map to rewind.
345 SvXMLNamespaceMap
*pRewindMap
= xContext
->GetRewindMap();
347 // Delete the current context.
350 // Rewind a namespace map.
353 delete m_pNamespaceMap
;
354 m_pNamespaceMap
= pRewindMap
;
359 void SAL_CALL
XMLTransformerBase::characters( const OUString
& rChars
)
360 throw(SAXException
, RuntimeException
)
362 if( !m_pContexts
->empty() )
364 m_pContexts
->back()->Characters( rChars
);
368 void SAL_CALL
XMLTransformerBase::ignorableWhitespace( const OUString
& rWhitespaces
)
369 throw(SAXException
, RuntimeException
)
371 m_xHandler
->ignorableWhitespace( rWhitespaces
);
374 void SAL_CALL
XMLTransformerBase::processingInstruction( const OUString
& rTarget
,
375 const OUString
& rData
)
376 throw(SAXException
, RuntimeException
)
378 m_xHandler
->processingInstruction( rTarget
, rData
);
381 void SAL_CALL
XMLTransformerBase::setDocumentLocator( const Reference
< XLocator
>& rLocator
)
382 throw(SAXException
, RuntimeException
)
384 m_xLocator
= rLocator
;
387 // XExtendedDocumentHandler
388 void SAL_CALL
XMLTransformerBase::startCDATA( void ) throw(SAXException
, RuntimeException
)
390 if( m_xExtHandler
.is() )
391 m_xExtHandler
->startCDATA();
394 void SAL_CALL
XMLTransformerBase::endCDATA( void ) throw(RuntimeException
)
396 if( m_xExtHandler
.is() )
397 m_xExtHandler
->endCDATA();
400 void SAL_CALL
XMLTransformerBase::comment( const OUString
& rComment
)
401 throw(SAXException
, RuntimeException
)
403 if( m_xExtHandler
.is() )
404 m_xExtHandler
->comment( rComment
);
407 void SAL_CALL
XMLTransformerBase::allowLineBreak( void )
408 throw(SAXException
, RuntimeException
)
410 if( m_xExtHandler
.is() )
411 m_xExtHandler
->allowLineBreak();
414 void SAL_CALL
XMLTransformerBase::unknown( const OUString
& rString
)
415 throw(SAXException
, RuntimeException
)
417 if( m_xExtHandler
.is() )
418 m_xExtHandler
->unknown( rString
);
422 void SAL_CALL
XMLTransformerBase::initialize( const Sequence
< Any
>& aArguments
)
423 throw(Exception
, RuntimeException
)
425 const sal_Int32 nAnyCount
= aArguments
.getLength();
426 const Any
* pAny
= aArguments
.getConstArray();
428 for( sal_Int32 nIndex
= 0; nIndex
< nAnyCount
; nIndex
++, pAny
++ )
430 // use isAssignableFrom instead of comparing the types to
431 // allow XExtendedDocumentHandler instead of XDocumentHandler (used in
432 // writeOasis2OOoLibraryElement in sfx2).
433 // The Any shift operator can't be used to query the type because it
434 // uses queryInterface, and the model also has a XPropertySet interface.
437 if( ::getCppuType( (const Reference
< XDocumentHandler
>*) 0 ).isAssignableFrom( pAny
->getValueType() ) )
438 m_xHandler
.set( *pAny
, UNO_QUERY
);
440 // property set to transport data across
441 if( ::getCppuType( (const Reference
< XPropertySet
>*) 0 ).isAssignableFrom( pAny
->getValueType() ) )
442 m_xPropSet
.set( *pAny
, UNO_QUERY
);
445 if( ::getCppuType( (const Reference
< ::com::sun::star::frame::XModel
>*) 0 ).isAssignableFrom( pAny
->getValueType() ) )
446 mxModel
.set( *pAny
, UNO_QUERY
);
449 if( m_xPropSet
.is() )
452 OUString sRelPath
, sName
;
453 Reference
< XPropertySetInfo
> xPropSetInfo
=
454 m_xPropSet
->getPropertySetInfo();
455 OUString
sPropName( "StreamRelPath" );
456 if( xPropSetInfo
->hasPropertyByName(sPropName
) )
458 aAny
= m_xPropSet
->getPropertyValue(sPropName
);
461 sPropName
= OUString( "StreamName" );
462 if( xPropSetInfo
->hasPropertyByName(sPropName
) )
464 aAny
= m_xPropSet
->getPropertyValue(sPropName
);
467 if( !sName
.isEmpty() )
469 m_aExtPathPrefix
= OUString( "../" );
471 // If there is a rel path within a package, then append
472 // additional '../'. If the rel path contains an ':', then it is
473 // an absolute URI (or invalid URI, because zip files don't
474 // permit ':'), and it will be ignored.
475 if( !sRelPath
.isEmpty() )
477 sal_Int32 nColPos
= sRelPath
.indexOf( ':' );
478 OSL_ENSURE( -1 == nColPos
,
479 "StreamRelPath contains ':', absolute URI?" );
483 OUString sTmp
= m_aExtPathPrefix
;
487 m_aExtPathPrefix
+= sTmp
;
488 nPos
= sRelPath
.indexOf( '/', nPos
+ 1 );
498 static sal_Int16
lcl_getUnit( const OUString
& rValue
)
500 if( rValue
.endsWithIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM( "cm" ) ) )
501 return util::MeasureUnit::CM
;
502 else if ( rValue
.endsWithIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM( "mm" ) ) )
503 return util::MeasureUnit::MM
;
505 return util::MeasureUnit::INCH
;
508 XMLMutableAttributeList
*XMLTransformerBase::ProcessAttrList(
509 Reference
< XAttributeList
>& rAttrList
, sal_uInt16 nActionMap
,
512 XMLMutableAttributeList
*pMutableAttrList
= 0;
513 XMLTransformerActions
*pActions
= GetUserDefinedActions( nActionMap
);
514 OSL_ENSURE( pActions
, "go no actions" );
517 sal_Int16 nAttrCount
= rAttrList
.is() ? rAttrList
->getLength() : 0;
518 for( sal_Int16 i
=0; i
< nAttrCount
; ++i
)
520 const OUString
& rAttrName
= rAttrList
->getNameByIndex( i
);
521 const OUString
& rAttrValue
= rAttrList
->getValueByIndex( i
);
523 sal_uInt16 nPrefix
= GetNamespaceMap().GetKeyByAttrName( rAttrName
,
526 XMLTransformerActions::key_type
aKey( nPrefix
, aLocalName
);
527 XMLTransformerActions::const_iterator aIter
=
528 pActions
->find( aKey
);
529 if( !(aIter
== pActions
->end() ) )
531 if( !pMutableAttrList
)
533 pMutableAttrList
= new XMLMutableAttributeList( rAttrList
,
535 rAttrList
= pMutableAttrList
;
538 sal_uInt32 nAction
= (*aIter
).second
.m_nActionType
;
539 sal_Bool bRename
= sal_False
;
542 case XML_ATACTION_RENAME
:
545 case XML_ATACTION_COPY
:
547 case XML_ATACTION_REMOVE
:
548 case XML_ATACTION_STYLE_DISPLAY_NAME
:
549 pMutableAttrList
->RemoveAttributeByIndex( i
);
553 case XML_ATACTION_RENAME_IN2INCH
:
555 case XML_ATACTION_IN2INCH
:
557 OUString
aAttrValue( rAttrValue
);
558 if( ReplaceSingleInWithInch( aAttrValue
) )
559 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
562 case XML_ATACTION_INS2INCHS
:
564 OUString
aAttrValue( rAttrValue
);
565 if( ReplaceInWithInch( aAttrValue
) )
566 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
569 case XML_ATACTION_RENAME_INCH2IN
:
571 case XML_ATACTION_INCH2IN
:
573 OUString
aAttrValue( rAttrValue
);
574 if( ReplaceSingleInchWithIn( aAttrValue
) )
575 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
578 case XML_ATACTION_INCHS2INS
:
580 OUString
aAttrValue( rAttrValue
);
581 if( ReplaceInchWithIn( aAttrValue
) )
582 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
585 case XML_ATACTION_TWIPS2IN
:
587 OUString
aAttrValue( rAttrValue
);
589 XMLTransformerBase::ReplaceSingleInchWithIn( aAttrValue
);
592 sal_Int16
const nDestUnit
= lcl_getUnit(aAttrValue
);
594 // convert twips value to inch
596 if (::sax::Converter::convertMeasure(nMeasure
,
597 aAttrValue
, util::MeasureUnit::MM_100TH
))
600 // #i13778#,#i36248# apply correct twip-to-1/100mm
601 nMeasure
= (sal_Int32
)( nMeasure
>= 0
602 ? ((nMeasure
*127+36)/72)
603 : ((nMeasure
*127-36)/72) );
605 rtl::OUStringBuffer aBuffer
;
606 ::sax::Converter::convertMeasure(aBuffer
,
607 nMeasure
, util::MeasureUnit::MM_100TH
,
609 aAttrValue
= aBuffer
.makeStringAndClear();
613 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
616 case XML_ATACTION_RENAME_DECODE_STYLE_NAME_REF
:
618 case XML_ATACTION_DECODE_STYLE_NAME
:
619 case XML_ATACTION_DECODE_STYLE_NAME_REF
:
621 OUString
aAttrValue( rAttrValue
);
622 if( DecodeStyleName(aAttrValue
) )
623 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
626 case XML_ATACTION_ENCODE_STYLE_NAME
:
628 OUString
aAttrValue( rAttrValue
);
629 if( EncodeStyleName(aAttrValue
) )
631 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
632 OUString
aNewAttrQName(
633 GetNamespaceMap().GetQNameByKey(
635 ::xmloff::token::GetXMLToken(
636 XML_DISPLAY_NAME
) ) );
637 pMutableAttrList
->AddAttribute( aNewAttrQName
,
642 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
:
653 case XML_ATACTION_NEG_PERCENT
:
655 OUString
aAttrValue( rAttrValue
);
656 if( NegPercent( aAttrValue
) )
657 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
660 case XML_ATACTION_RENAME_ADD_NAMESPACE_PREFIX
:
662 case XML_ATACTION_ADD_NAMESPACE_PREFIX
:
664 OUString
aAttrValue( rAttrValue
);
665 sal_uInt16 nValPrefix
=
666 static_cast<sal_uInt16
>(
667 bRename
? (*aIter
).second
.m_nParam2
668 : (*aIter
).second
.m_nParam1
);
669 if( AddNamespacePrefix( aAttrValue
, nValPrefix
) )
670 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
673 case XML_ATACTION_ADD_APP_NAMESPACE_PREFIX
:
675 OUString
aAttrValue( rAttrValue
);
676 sal_uInt16 nValPrefix
=
677 static_cast<sal_uInt16
>((*aIter
).second
.m_nParam1
);
678 if( IsXMLToken( GetClass(), XML_SPREADSHEET
) )
679 nValPrefix
= XML_NAMESPACE_OOOC
;
680 else if( IsXMLToken( GetClass(), XML_TEXT
) )
681 nValPrefix
= XML_NAMESPACE_OOOW
;
682 if( AddNamespacePrefix( aAttrValue
, nValPrefix
) )
683 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
686 case XML_ATACTION_RENAME_REMOVE_NAMESPACE_PREFIX
:
688 case XML_ATACTION_REMOVE_NAMESPACE_PREFIX
:
690 OUString
aAttrValue( rAttrValue
);
691 sal_uInt16 nValPrefix
=
692 static_cast<sal_uInt16
>(
693 bRename
? (*aIter
).second
.m_nParam2
694 : (*aIter
).second
.m_nParam1
);
695 if( RemoveNamespacePrefix( aAttrValue
, nValPrefix
) )
696 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
699 case XML_ATACTION_REMOVE_ANY_NAMESPACE_PREFIX
:
701 OUString
aAttrValue( rAttrValue
);
702 if( RemoveNamespacePrefix( aAttrValue
) )
703 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
706 case XML_ATACTION_URI_OOO
:
708 OUString
aAttrValue( rAttrValue
);
709 if( ConvertURIToOASIS( aAttrValue
,
710 static_cast< sal_Bool
>((*aIter
).second
.m_nParam1
)))
711 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
714 case XML_ATACTION_URI_OASIS
:
716 OUString
aAttrValue( rAttrValue
);
717 if( ConvertURIToOOo( aAttrValue
,
718 static_cast< sal_Bool
>((*aIter
).second
.m_nParam1
)))
719 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
722 case XML_ATACTION_RENAME_ATTRIBUTE
:
724 OUString
aAttrValue( rAttrValue
);
725 RenameAttributeValue(
727 (*aIter
).second
.m_nParam1
,
728 (*aIter
).second
.m_nParam2
,
729 (*aIter
).second
.m_nParam3
);
730 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
733 case XML_ATACTION_RNG2ISO_DATETIME
:
735 OUString
aAttrValue( rAttrValue
);
736 if( ConvertRNGDateTimeToISO( aAttrValue
))
737 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
740 case XML_ATACTION_RENAME_RNG2ISO_DATETIME
:
742 OUString
aAttrValue( rAttrValue
);
743 if( ConvertRNGDateTimeToISO( aAttrValue
))
744 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
748 case XML_ATACTION_IN2TWIPS
:
750 OUString
aAttrValue( rAttrValue
);
751 XMLTransformerBase::ReplaceSingleInWithInch( aAttrValue
);
755 sal_Int16
const nDestUnit
= lcl_getUnit(aAttrValue
);
757 // convert inch value to twips and export as faked inch
759 if (::sax::Converter::convertMeasure(nMeasure
,
760 aAttrValue
, util::MeasureUnit::MM_100TH
))
763 // #i13778#,#i36248#/ apply correct 1/100mm-to-twip conversion
764 nMeasure
= (sal_Int32
)( nMeasure
>= 0
765 ? ((nMeasure
*72+63)/127)
766 : ((nMeasure
*72-63)/127) );
768 OUStringBuffer aBuffer
;
769 ::sax::Converter::convertMeasure( aBuffer
,
770 nMeasure
, util::MeasureUnit::MM_100TH
,
772 aAttrValue
= aBuffer
.makeStringAndClear();
776 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
779 case XML_ATACTION_SVG_WIDTH_HEIGHT_OOO
:
781 OUString
aAttrValue( rAttrValue
);
782 ReplaceSingleInchWithIn( aAttrValue
);
784 sal_Int16
const nDestUnit
= lcl_getUnit( aAttrValue
);
787 if (::sax::Converter::convertMeasure(nMeasure
,
788 aAttrValue
, util::MeasureUnit::MM_100TH
))
793 else if( nMeasure
< 0 )
797 OUStringBuffer aBuffer
;
798 ::sax::Converter::convertMeasure(aBuffer
, nMeasure
,
799 util::MeasureUnit::MM_100TH
, nDestUnit
);
800 aAttrValue
= aBuffer
.makeStringAndClear();
803 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
806 case XML_ATACTION_SVG_WIDTH_HEIGHT_OASIS
:
808 OUString
aAttrValue( rAttrValue
);
809 ReplaceSingleInWithInch( aAttrValue
);
811 sal_Int16
const nDestUnit
= lcl_getUnit( aAttrValue
);
814 if (::sax::Converter::convertMeasure(nMeasure
,
815 aAttrValue
, util::MeasureUnit::MM_100TH
))
820 else if( nMeasure
< 0 )
824 OUStringBuffer aBuffer
;
825 ::sax::Converter::convertMeasure(aBuffer
, nMeasure
,
826 util::MeasureUnit::MM_100TH
, nDestUnit
);
827 aAttrValue
= aBuffer
.makeStringAndClear();
830 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
833 case XML_ATACTION_DECODE_ID
:
835 const sal_Int32 nLen
= rAttrValue
.getLength();
836 OUStringBuffer aBuffer
;
839 for( pos
= 0; pos
< nLen
; pos
++ )
841 sal_Unicode c
= rAttrValue
[pos
];
842 if( (c
>= '0') && (c
<= '9') )
845 aBuffer
.append( (sal_Int32
)c
);
848 pMutableAttrList
->SetValueByIndex( i
, aBuffer
.makeStringAndClear() );
851 // #i50322# - special handling for the
852 // transparency of writer background graphics.
853 case XML_ATACTION_WRITER_BACK_GRAPHIC_TRANSPARENCY
:
855 // determine, if it's the transparency of a document style
856 XMLTransformerContext
* pFirstContext
= (*m_pContexts
)[0].get();
857 OUString aFirstContextLocalName
;
858 /* sal_uInt16 nFirstContextPrefix = */
859 GetNamespaceMap().GetKeyByAttrName( pFirstContext
->GetQName(),
860 &aFirstContextLocalName
);
861 bool bIsDocumentStyle(
862 ::xmloff::token::IsXMLToken( aFirstContextLocalName
,
863 XML_DOCUMENT_STYLES
) );
864 // no conversion of transparency value for document
865 // styles, because former OpenOffice.org version writes
866 // writes always a transparency value of 100% and doesn't
867 // read the value. Thus, it's intepreted as 0%
868 if ( !bIsDocumentStyle
)
870 OUString
aAttrValue( rAttrValue
);
871 NegPercent(aAttrValue
);
872 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
877 case XML_ATACTION_SHAPEID
:
879 OUString
sNewValue( "shape" );
880 sNewValue
+= rAttrValue
;
881 pMutableAttrList
->SetValueByIndex( i
, sNewValue
);
886 OSL_ENSURE( !this, "unknown action" );
892 OUString
aNewAttrQName(
893 GetNamespaceMap().GetQNameByKey(
894 (*aIter
).second
.GetQNamePrefixFromParam1(),
895 ::xmloff::token::GetXMLToken(
896 (*aIter
).second
.GetQNameTokenFromParam1()) ) );
897 pMutableAttrList
->RenameAttributeByIndex( i
,
904 return pMutableAttrList
;
907 sal_Bool
XMLTransformerBase::ReplaceSingleInchWithIn( OUString
& rValue
)
909 sal_Bool bRet
= sal_False
;
910 sal_Int32 nPos
= rValue
.getLength();
911 while( nPos
&& rValue
[nPos
-1] <= ' ' )
914 ('c'==rValue
[nPos
-2] || 'C'==rValue
[nPos
-2]) &&
915 ('h'==rValue
[nPos
-1] || 'H'==rValue
[nPos
-1]) )
917 rValue
=rValue
.copy( 0, nPos
-2 );
924 sal_Bool
XMLTransformerBase::ReplaceInchWithIn( OUString
& rValue
)
926 sal_Bool bRet
= sal_False
;
928 while( nPos
< rValue
.getLength()-3 )
930 sal_Unicode c
= rValue
[nPos
];
931 if( 'i'==c
|| 'I'==c
)
934 if( (c
>= '0' && c
<= '9') || '.' == c
)
937 if( 'n'==c
|| 'N'==c
)
940 if( 'c'==c
|| 'C'==c
)
943 if( 'h'==c
|| 'H'==c
)
945 rValue
= rValue
.replaceAt( nPos
,
946 4, GetXMLToken(XML_UNIT_INCH
) );
961 sal_Bool
XMLTransformerBase::ReplaceSingleInWithInch( OUString
& rValue
)
963 sal_Bool bRet
= sal_False
;
965 sal_Int32 nPos
= rValue
.getLength();
966 while( nPos
&& rValue
[nPos
-1] <= ' ' )
969 ('i'==rValue
[nPos
-2] ||
970 'I'==rValue
[nPos
-2]) &&
971 ('n'==rValue
[nPos
-1] ||
972 'N'==rValue
[nPos
-1]) )
975 rValue
= rValue
.replaceAt( nPos
, rValue
.getLength() - nPos
,
976 GetXMLToken(XML_INCH
) );
983 sal_Bool
XMLTransformerBase::ReplaceInWithInch( OUString
& rValue
)
985 sal_Bool bRet
= sal_False
;
987 while( nPos
< rValue
.getLength()-1 )
989 sal_Unicode c
= rValue
[nPos
];
990 if( 'i'==c
|| 'I'==c
)
993 if( (c
>= '0' && c
<= '9') || '.' == c
)
996 if( 'n'==c
|| 'N'==c
)
998 rValue
= rValue
.replaceAt( nPos
,
999 2, GetXMLToken(XML_INCH
) );
1012 sal_Bool
XMLTransformerBase::EncodeStyleName( OUString
& rName
) const
1014 static sal_Char aHexTab
[] = "0123456789abcdef";
1016 sal_Bool bEncoded
= sal_False
;
1018 sal_Int32 nLen
= rName
.getLength();
1019 OUStringBuffer
aBuffer( nLen
);
1021 for( sal_Int32 i
= 0; i
< nLen
; i
++ )
1023 sal_Unicode c
= rName
[i
];
1024 sal_Bool bValidChar
= sal_False
;
1028 (c
>= 0x0041 && c
<= 0x005a) ||
1029 (c
>= 0x0061 && c
<= 0x007a) ||
1030 (c
>= 0x00c0 && c
<= 0x00d6) ||
1031 (c
>= 0x00d8 && c
<= 0x00f6) ||
1032 (c
>= 0x00f8 && c
<= 0x00ff) ||
1033 ( i
> 0 && ( (c
>= 0x0030 && c
<= 0x0039) ||
1034 c
== 0x00b7 || c
== '-' || c
== '.') );
1038 if( (c
>= 0xf900U
&& c
<= 0xfffeU
) ||
1039 (c
>= 0x20ddU
&& c
<= 0x20e0U
))
1041 bValidChar
= sal_False
;
1043 else if( (c
>= 0x02bbU
&& c
<= 0x02c1U
) || c
== 0x0559 ||
1044 c
== 0x06e5 || c
== 0x06e6 )
1046 bValidChar
= sal_True
;
1048 else if( c
== 0x0387 )
1054 if( !xCharClass
.is() )
1056 const_cast < XMLTransformerBase
* >(this)
1057 ->xCharClass
= CharacterClassification::create( comphelper::getProcessComponentContext() );
1059 sal_Int16 nType
= xCharClass
->getType( rName
, i
);
1063 case UnicodeType::UPPERCASE_LETTER
: // Lu
1064 case UnicodeType::LOWERCASE_LETTER
: // Ll
1065 case UnicodeType::TITLECASE_LETTER
: // Lt
1066 case UnicodeType::OTHER_LETTER
: // Lo
1067 case UnicodeType::LETTER_NUMBER
: // Nl
1068 bValidChar
= sal_True
;
1070 case UnicodeType::NON_SPACING_MARK
: // Ms
1071 case UnicodeType::ENCLOSING_MARK
: // Me
1072 case UnicodeType::COMBINING_SPACING_MARK
: //Mc
1073 case UnicodeType::MODIFIER_LETTER
: // Lm
1074 case UnicodeType::DECIMAL_DIGIT_NUMBER
: // Nd
1082 aBuffer
.append( c
);
1086 aBuffer
.append( static_cast< sal_Unicode
>( '_' ) );
1088 aBuffer
.append( static_cast< sal_Unicode
>(
1089 aHexTab
[ (c
>> 12) & 0x0f ] ) );
1091 aBuffer
.append( static_cast< sal_Unicode
>(
1092 aHexTab
[ (c
>> 8) & 0x0f ] ) );
1094 aBuffer
.append( static_cast< sal_Unicode
>(
1095 aHexTab
[ (c
>> 4) & 0x0f ] ) );
1096 aBuffer
.append( static_cast< sal_Unicode
>(
1097 aHexTab
[ c
& 0x0f ] ) );
1098 aBuffer
.append( static_cast< sal_Unicode
>( '_' ) );
1099 bEncoded
= sal_True
;
1103 if( aBuffer
.getLength() > (1<<15)-1 )
1104 bEncoded
= sal_False
;
1107 rName
= aBuffer
.makeStringAndClear();
1111 sal_Bool
XMLTransformerBase::DecodeStyleName( OUString
& rName
)
1113 sal_Bool bEncoded
= sal_False
;
1115 sal_Int32 nLen
= rName
.getLength();
1116 OUStringBuffer
aBuffer( nLen
);
1118 sal_Bool bWithinHex
= sal_False
;
1119 sal_Unicode cEnc
= 0;
1120 for( sal_Int32 i
= 0; i
< nLen
; i
++ )
1122 sal_Unicode c
= rName
[i
];
1127 aBuffer
.append( cEnc
);
1132 bEncoded
= sal_True
;
1134 bWithinHex
= !bWithinHex
;
1136 else if( bWithinHex
)
1139 if( c
>= '0' && c
<= '9' )
1143 else if( c
>= 'a' && c
<= 'f' )
1145 cDigit
= c
- 'a' + 10;
1147 else if( c
>= 'A' && c
<= 'F' )
1149 cDigit
= c
- 'A' + 10;
1154 bEncoded
= sal_False
;
1157 cEnc
= (cEnc
<< 4) + cDigit
;
1161 aBuffer
.append( c
);
1166 rName
= aBuffer
.makeStringAndClear();
1170 sal_Bool
XMLTransformerBase::NegPercent( OUString
& rValue
)
1172 sal_Bool bRet
= sal_False
;
1173 sal_Bool bNeg
= sal_False
;
1177 sal_Int32 nLen
= rValue
.getLength();
1180 while( nPos
< nLen
&& sal_Unicode(' ') == rValue
[nPos
] )
1183 if( nPos
< nLen
&& sal_Unicode('-') == rValue
[nPos
] )
1190 while( nPos
< nLen
&&
1191 sal_Unicode('0') <= rValue
[nPos
] &&
1192 sal_Unicode('9') >= rValue
[nPos
] )
1194 // TODO: check overflow!
1196 nVal
+= (rValue
[nPos
] - sal_Unicode('0'));
1200 if( nPos
< nLen
&& sal_Unicode('.') == rValue
[nPos
] )
1204 while( nPos
< nLen
&&
1205 sal_Unicode('0') <= rValue
[nPos
] &&
1206 sal_Unicode('9') >= rValue
[nPos
] )
1208 // TODO: check overflow!
1210 nVal
+= ( static_cast<double>(rValue
[nPos
] - sal_Unicode('0')) / nDiv
);
1216 while( nPos
< nLen
&& sal_Unicode(' ') == rValue
[nPos
] )
1219 if( nPos
< nLen
&& sal_Unicode('%') == rValue
[nPos
] )
1225 sal_Int32 nIntVal
= 100 - static_cast<sal_Int32
>( nVal
);
1227 OUStringBuffer aNewValBuffer
;
1228 aNewValBuffer
.append( nIntVal
);
1229 aNewValBuffer
.append( sal_Unicode('%' ) );
1231 rValue
= aNewValBuffer
.makeStringAndClear();
1238 sal_Bool
XMLTransformerBase::AddNamespacePrefix( ::rtl::OUString
& rName
,
1239 sal_uInt16 nPrefix
) const
1241 rName
= GetNamespaceMap().GetQNameByKey( nPrefix
, rName
, sal_False
);
1245 sal_Bool
XMLTransformerBase::RemoveNamespacePrefix( ::rtl::OUString
& rName
,
1246 sal_uInt16 nPrefixOnly
) const
1248 OUString aLocalName
;
1249 sal_uInt16 nPrefix
=
1250 GetNamespaceMap()._GetKeyByAttrName( rName
, &aLocalName
, sal_False
);
1251 sal_Bool bRet
= XML_NAMESPACE_UNKNOWN
!= nPrefix
&&
1252 (USHRT_MAX
== nPrefixOnly
|| nPrefix
== nPrefixOnly
);
1259 sal_Bool
XMLTransformerBase::ConvertURIToOASIS( ::rtl::OUString
& rURI
,
1260 sal_Bool bSupportPackage
) const
1262 sal_Bool bRet
= sal_False
;
1263 if( !m_aExtPathPrefix
.isEmpty() && !rURI
.isEmpty() )
1265 sal_Bool bRel
= sal_False
;
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 sal_Bool
XMLTransformerBase::ConvertURIToOOo( ::rtl::OUString
& rURI
,
1330 sal_Bool bSupportPackage
) const
1332 sal_Bool bRet
= sal_False
;
1333 if( !rURI
.isEmpty() )
1335 sal_Bool bPackage
= sal_False
;
1339 // no rel path; nothing to to
1343 if( 0 == rURI
.compareTo( m_aExtPathPrefix
,
1344 m_aExtPathPrefix
.getLength() ) )
1346 // an external URI; remove '../'
1347 rURI
= rURI
.copy( m_aExtPathPrefix
.getLength() );
1352 bPackage
= sal_True
;
1356 // check for a RFC2396 schema
1358 bPackage
= sal_True
;
1360 sal_Int32 nLen
= rURI
.getLength();
1361 while( nPos
< nLen
)
1363 switch( rURI
[nPos
] )
1366 // a relative path segement within the package
1367 nPos
= nLen
; // leave loop
1371 bPackage
= sal_False
;
1372 nPos
= nLen
; // leave loop
1375 // we don't care about any other characters
1383 if( bPackage
&& bSupportPackage
)
1385 OUString
sTmp( OUString::valueOf( sal_Unicode( '#' ) ) );
1386 if( 0 == rURI
.compareToAscii( "./", 2 ) )
1387 rURI
= rURI
.copy( 2 );
1397 sal_Bool
XMLTransformerBase::RenameAttributeValue(
1398 OUString
& rOutAttributeValue
,
1403 return ( lcl_ConvertAttr( rOutAttributeValue
, nParam1
) ||
1404 lcl_ConvertAttr( rOutAttributeValue
, nParam2
) ||
1405 lcl_ConvertAttr( rOutAttributeValue
, nParam3
) );
1409 bool XMLTransformerBase::ConvertRNGDateTimeToISO( ::rtl::OUString
& rDateTime
)
1411 if( !rDateTime
.isEmpty() &&
1412 rDateTime
.indexOf( sal_Unicode('.')) != -1 )
1414 rDateTime
= rDateTime
.replace( sal_Unicode('.'), sal_Unicode(','));
1421 XMLTokenEnum
XMLTransformerBase::GetToken( const OUString
& rStr
) const
1423 XMLTransformerTokenMap::const_iterator aIter
=
1424 m_pTokenMap
->find( rStr
);
1425 if( aIter
== m_pTokenMap
->end() )
1426 return XML_TOKEN_END
;
1428 return (*aIter
).second
;
1433 const XMLTransformerContext
*XMLTransformerBase::GetCurrentContext() const
1435 OSL_ENSURE( !m_pContexts
->empty(), "empty stack" );
1438 return m_pContexts
->empty() ? 0 : m_pContexts
->back().get();
1441 const XMLTransformerContext
*XMLTransformerBase::GetAncestorContext(
1442 sal_uInt32 n
) const
1444 XMLTransformerContextVector::size_type nSize
=
1445 m_pContexts
->size();
1446 XMLTransformerContextVector::size_type nPos
=
1447 static_cast<XMLTransformerContextVector::size_type
>( n
);
1449 OSL_ENSURE( nSize
>nPos
+2 , "invalid context" );
1451 return nSize
> nPos
+2 ? (*m_pContexts
)[nSize
-(nPos
+2)].get() : 0;
1454 bool XMLTransformerBase::isWriter() const
1456 Reference
< XServiceInfo
> xSI( mxModel
, UNO_QUERY
);
1458 ( xSI
->supportsService( OUString( "com.sun.star.text.TextDocument" ) ) ||
1459 xSI
->supportsService( OUString( "com.sun.star.text.WebDocument" ) ) ||
1460 xSI
->supportsService( OUString( "com.sun.star.text.GlobalDocument" ) ) );
1463 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */