| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 11 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 |
* contributor license agreements. See the NOTICE file distributed |
| 13 |
* with this work for additional information regarding copyright |
| 14 |
* ownership. The ASF licenses this file to you under the Apache |
| 15 |
* License, Version 2.0 (the "License"); you may not use this file |
| 16 |
* except in compliance with the License. You may obtain a copy of |
| 17 |
* the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <rtl/ref.hxx> |
| 21 |
#include <rtl/ustrbuf.hxx> |
| 22 |
#include <tools/UnitConversion.hxx> |
| 23 |
#include <osl/diagnose.h> |
| 24 |
#include <o3tl/numeric.hxx> |
| 25 |
#include <com/sun/star/i18n/CharacterClassification.hpp> |
| 26 |
#include <com/sun/star/i18n/UnicodeType.hpp> |
| 27 |
#include <com/sun/star/util/MeasureUnit.hpp> |
| 28 |
#include <sax/tools/converter.hxx> |
| 29 |
#include <comphelper/processfactory.hxx> |
| 30 |
#include <xmloff/namespacemap.hxx> |
| 31 |
#include <xmloff/xmlnamespace.hxx> |
| 32 |
#include "IgnoreTContext.hxx" |
| 33 |
#include "RenameElemTContext.hxx" |
| 34 |
#include "ProcAttrTContext.hxx" |
| 35 |
#include "ProcAddAttrTContext.hxx" |
| 36 |
#include "MergeElemTContext.hxx" |
| 37 |
#include "CreateElemTContext.hxx" |
| 38 |
#include "MutableAttrList.hxx" |
| 39 |
#include "TransformerActions.hxx" |
| 40 |
#include "ElemTransformerAction.hxx" |
| 41 |
#include "PropertyActionsOOo.hxx" |
| 42 |
#include "TransformerTokenMap.hxx" |
| 43 |
|
| 44 |
#include "TransformerBase.hxx" |
| 45 |
#include <xmloff/xmlimp.hxx> |
| 46 |
|
| 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; |
| 54 |
|
| 55 |
namespace |
| 56 |
{ |
| 57 |
bool lcl_ConvertAttr( OUString & rOutAttribute, sal_Int32 nParam ) |
| 58 |
{ |
| 59 |
bool bResult = false; |
| 60 |
enum XMLTokenEnum eTokenToRename = |
| 61 |
static_cast< enum XMLTokenEnum >( nParam & 0xffff ); |
| 62 |
if( eTokenToRename != XML_TOKEN_INVALID && |
| 63 |
IsXMLToken( rOutAttribute, eTokenToRename )) |
| 64 |
{ |
| 65 |
enum XMLTokenEnum eReplacementToken = |
| 66 |
static_cast< enum XMLTokenEnum >( nParam >> 16 ); |
| 67 |
rOutAttribute = GetXMLToken( eReplacementToken ); |
| 68 |
bResult = true; |
| 69 |
} |
| 70 |
return bResult; |
| 71 |
} |
| 72 |
} // anonymous namespace |
| 73 |
|
| 74 |
XMLTransformerContext *XMLTransformerBase::CreateContext( sal_uInt16 nPrefix, |
| 75 |
const OUString& rLocalName, const OUString& rQName ) |
| 76 |
{ |
| 77 |
XMLTransformerActions::key_type aKey( nPrefix, rLocalName ); |
| 78 |
XMLTransformerActions::const_iterator aIter = |
| 79 |
GetElemActions().find( aKey ); |
| 80 |
|
| 81 |
if( aIter != GetElemActions().end() ) |
| 82 |
{ |
| 83 |
sal_uInt32 nActionType = (*aIter).second.m_nActionType; |
| 84 |
if( (nActionType & XML_ETACTION_USER_DEFINED) != 0 ) |
| 85 |
{ |
| 86 |
XMLTransformerContext *pContext = |
| 87 |
CreateUserDefinedContext( (*aIter).second, |
| 88 |
rQName ); |
| 89 |
OSL_ENSURE( pContext && !pContext->IsPersistent(), |
| 90 |
"unknown or not persistent action" ); |
| 91 |
return pContext; |
| 92 |
} |
| 93 |
|
| 94 |
switch( nActionType ) |
| 95 |
{ |
| 96 |
case XML_ETACTION_COPY_CONTENT: |
| 97 |
return new XMLIgnoreTransformerContext( *this, rQName, false, |
| 98 |
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_PROC_ATTRS_COND: |
| 128 |
{ |
| 129 |
const XMLTransformerContext *pCurrent = GetCurrentContext(); |
| 130 |
if( pCurrent->HasQName( |
| 131 |
(*aIter).second.GetQNamePrefixFromParam3(), |
| 132 |
(*aIter).second.GetQNameTokenFromParam3() ) ) |
| 133 |
return new XMLProcAttrTransformerContext( *this, rQName, |
| 134 |
(*aIter).second.GetQNamePrefixFromParam1(), |
| 135 |
(*aIter).second.GetQNameTokenFromParam1(), |
| 136 |
static_cast< sal_uInt16 >( (*aIter).second.m_nParam2 ) ); |
| 137 |
else |
| 138 |
return new XMLProcAttrTransformerContext( *this, rQName, |
| 139 |
static_cast< sal_uInt16 >( (*aIter).second.m_nParam2 ) ); |
| 140 |
} |
| 141 |
case XML_ETACTION_PROC_ATTRS: |
| 142 |
return new XMLProcAttrTransformerContext( *this, rQName, |
| 143 |
static_cast< sal_uInt16 >( (*aIter).second.m_nParam1 ) ); |
| 144 |
case XML_ETACTION_PROC_ATTRS_COND: |
| 145 |
{ |
| 146 |
const XMLTransformerContext *pCurrent = GetCurrentContext(); |
| 147 |
if( pCurrent->HasQName( |
| 148 |
(*aIter).second.GetQNamePrefixFromParam1(), |
| 149 |
(*aIter).second.GetQNameTokenFromParam1() ) ) |
| 150 |
return new XMLProcAttrTransformerContext( *this, rQName, |
| 151 |
static_cast< sal_uInt16 >( (*aIter).second.m_nParam2 ) ); |
| 152 |
} |
| 153 |
break; |
| 154 |
case XML_ETACTION_MOVE_ATTRS_TO_ELEMS: |
| 155 |
return new XMLCreateElemTransformerContext( *this, rQName, |
| 156 |
static_cast< sal_uInt16 >( (*aIter).second.m_nParam1 ) ); |
| 157 |
case XML_ETACTION_MOVE_ELEMS_TO_ATTRS: |
| 158 |
return new XMLMergeElemTransformerContext( *this, rQName, |
| 159 |
static_cast< sal_uInt16 >( (*aIter).second.m_nParam1 ) ); |
| 160 |
default: |
| 161 |
OSL_ENSURE( false, "unknown action" ); |
| 162 |
break; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
// default is copying |
| 167 |
return new XMLTransformerContext( *this, rQName ); |
| 168 |
} |
| 169 |
|
| 170 |
XMLTransformerActions *XMLTransformerBase::GetUserDefinedActions( sal_uInt16 ) |
| 171 |
{ |
| 172 |
return nullptr; |
| 173 |
} |
| 174 |
|
| 175 |
XMLTransformerBase::XMLTransformerBase( XMLTransformerActionInit const *pInit, |
| 176 |
::xmloff::token::XMLTokenEnum const *pTKMapInit ) |
| 177 |
noexcept : |
| 178 |
m_pNamespaceMap( new SvXMLNamespaceMap ), |
| 179 |
m_ElemActions( pInit ), |
| 180 |
m_TokenMap( pTKMapInit ) |
| 181 |
{ |
| 182 |
GetNamespaceMap().Add( GetXMLToken(XML_NP_XLINK), GetXMLToken(XML_N_XLINK), XML_NAMESPACE_XLINK ); |
| 183 |
GetNamespaceMap().Add( GetXMLToken(XML_NP_DC), GetXMLToken(XML_N_DC), XML_NAMESPACE_DC ); |
| 184 |
GetNamespaceMap().Add( GetXMLToken(XML_NP_MATH), GetXMLToken(XML_N_MATH), XML_NAMESPACE_MATH ); |
| 185 |
GetNamespaceMap().Add( GetXMLToken(XML_NP_OOO), GetXMLToken(XML_N_OOO), XML_NAMESPACE_OOO ); |
| 186 |
GetNamespaceMap().Add( GetXMLToken(XML_NP_DOM), GetXMLToken(XML_N_DOM), XML_NAMESPACE_DOM ); |
| 187 |
GetNamespaceMap().Add( GetXMLToken(XML_NP_OOOW), GetXMLToken(XML_N_OOOW), XML_NAMESPACE_OOOW ); |
| 188 |
GetNamespaceMap().Add( GetXMLToken(XML_NP_OOOC), GetXMLToken(XML_N_OOOC), XML_NAMESPACE_OOOC ); |
| 189 |
} |
| 190 |
|
| 191 |
XMLTransformerBase::~XMLTransformerBase() noexcept |
| 192 |
{ |
| 193 |
} |
| 194 |
|
| 195 |
void SAL_CALL XMLTransformerBase::startDocument() |
| 196 |
{ |
| 197 |
m_xHandler->startDocument(); |
| 198 |
} |
| 199 |
|
| 200 |
void SAL_CALL XMLTransformerBase::endDocument() |
| 201 |
{ |
| 202 |
m_xHandler->endDocument(); |
| 203 |
} |
| 204 |
|
| 205 |
void SAL_CALL XMLTransformerBase::startElement( const OUString& rName, |
| 206 |
const Reference< XAttributeList >& rAttrList ) |
| 207 |
{ |
| 208 |
std::unique_ptr<SvXMLNamespaceMap> pRewindMap; |
| 209 |
|
| 210 |
// Process namespace attributes. This must happen before creating the |
| 211 |
// context, because namespace declaration apply to the element name itself. |
| 212 |
rtl::Reference<XMLMutableAttributeList> pMutableAttrList; |
| 213 |
Reference< XAttributeList > xAttrList( rAttrList ); |
| 214 |
sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0; |
| 215 |
for( sal_Int16 i=0; i < nAttrCount; i++ ) |
| 216 |
{ |
| 217 |
const OUString aAttrName = xAttrList->getNameByIndex( i ); |
| 218 |
if( ( aAttrName.getLength() >= 5 ) && |
| 219 |
( aAttrName.startsWith( GetXMLToken(XML_XMLNS) ) ) && |
| 220 |
( aAttrName.getLength() == 5 || ':' == aAttrName[5] ) ) |
| 221 |
{ |
| 222 |
if( !pRewindMap ) |
| 223 |
{ |
| 224 |
pRewindMap = std::move(m_pNamespaceMap); |
| 225 |
m_pNamespaceMap.reset( new SvXMLNamespaceMap( *pRewindMap ) ); |
| 226 |
} |
| 227 |
const OUString aAttrValue = xAttrList->getValueByIndex( i ); |
| 228 |
|
| 229 |
OUString aPrefix( ( aAttrName.getLength() == 5 ) |
| 230 |
? OUString() |
| 231 |
: aAttrName.copy( 6 ) ); |
| 232 |
// Add namespace, but only if it is known. |
| 233 |
sal_uInt16 nKey = m_pNamespaceMap->AddIfKnown( aPrefix, aAttrValue ); |
| 234 |
// If namespace is unknown, try to match a name with similar |
| 235 |
// TC Id and version |
| 236 |
if( XML_NAMESPACE_UNKNOWN == nKey ) |
| 237 |
{ |
| 238 |
OUString aTestName( aAttrValue ); |
| 239 |
if( SvXMLNamespaceMap::NormalizeOasisURN( aTestName ) ) |
| 240 |
nKey = m_pNamespaceMap->AddIfKnown( aPrefix, aTestName ); |
| 241 |
} |
| 242 |
// If that namespace is not known, too, add it as unknown |
| 243 |
if( XML_NAMESPACE_UNKNOWN == nKey ) |
| 244 |
nKey = m_pNamespaceMap->Add( aPrefix, aAttrValue ); |
| 245 |
|
| 246 |
const OUString& rRepName = m_vReplaceNamespaceMap.GetNameByKey( nKey ); |
| 247 |
if( !rRepName.isEmpty() ) |
| 248 |
{ |
| 249 |
if( !pMutableAttrList ) |
| 250 |
{ |
| 251 |
pMutableAttrList = new XMLMutableAttributeList( xAttrList ); |
| 252 |
xAttrList = pMutableAttrList; |
| 253 |
} |
| 254 |
|
| 255 |
pMutableAttrList->SetValueByIndex( i, rRepName ); |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
// Get element's namespace and local name. |
| 261 |
OUString aLocalName; |
| 262 |
sal_uInt16 nPrefix = |
| 263 |
m_pNamespaceMap->GetKeyByAttrName( rName, &aLocalName ); |
| 264 |
|
| 265 |
// If there are contexts already, call a CreateChildContext at the topmost |
| 266 |
// context. Otherwise, create a default context. |
| 267 |
::rtl::Reference < XMLTransformerContext > xContext; |
| 268 |
if( !m_vContexts.empty() ) |
| 269 |
{ |
| 270 |
xContext = m_vContexts.back()->CreateChildContext( nPrefix, |
| 271 |
aLocalName, |
| 272 |
rName, |
| 273 |
xAttrList ); |
| 274 |
} |
| 275 |
else |
| 276 |
{ |
| 277 |
xContext = CreateContext( nPrefix, aLocalName, rName ); |
| 278 |
} |
| 279 |
|
| 280 |
OSL_ENSURE( xContext.is(), "XMLTransformerBase::startElement: missing context" ); |
| 281 |
if( !xContext.is() ) |
| 282 |
xContext = new XMLTransformerContext( *this, rName ); |
| 283 |
|
| 284 |
// Remember old namespace map. |
| 285 |
if( pRewindMap ) |
| 286 |
xContext->PutRewindMap( std::move(pRewindMap) ); |
| 287 |
|
| 288 |
// Push context on stack. |
| 289 |
m_vContexts.push_back( xContext ); |
| 290 |
|
| 291 |
// Call a startElement at the new context. |
| 292 |
xContext->StartElement( xAttrList ); |
| 293 |
} |
| 294 |
|
| 295 |
void SAL_CALL XMLTransformerBase::endElement( const OUString& |
| 296 |
#if OSL_DEBUG_LEVEL > 0 |
| 297 |
rName |
| 298 |
#endif |
| 299 |
) |
| 300 |
{ |
| 301 |
if( m_vContexts.empty() ) |
| 302 |
return; |
| 303 |
|
| 304 |
// Get topmost context |
| 305 |
::rtl::Reference< XMLTransformerContext > xContext = m_vContexts.back(); |
| 306 |
|
| 307 |
#if OSL_DEBUG_LEVEL > 0 |
| 308 |
OSL_ENSURE( xContext->GetQName() == rName, |
| 309 |
"XMLTransformerBase::endElement: popped context has wrong lname" ); |
| 310 |
#endif |
| 311 |
|
| 312 |
// Call a EndElement at the current context. |
| 313 |
xContext->EndElement(); |
| 314 |
|
| 315 |
// and remove it from the stack. |
| 316 |
m_vContexts.pop_back(); |
| 317 |
|
| 318 |
// Get a namespace map to rewind. |
| 319 |
std::unique_ptr<SvXMLNamespaceMap> pRewindMap = xContext->TakeRewindMap(); |
| 320 |
|
| 321 |
// Delete the current context. |
| 322 |
xContext = nullptr; |
| 323 |
|
| 324 |
// Rewind a namespace map. |
| 325 |
if( pRewindMap ) |
| 326 |
{ |
| 327 |
m_pNamespaceMap = std::move( pRewindMap ); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
void SAL_CALL XMLTransformerBase::characters( const OUString& rChars ) |
| 332 |
{ |
| 333 |
if( !m_vContexts.empty() ) |
| 334 |
{ |
| 335 |
m_vContexts.back()->Characters( rChars ); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
void SAL_CALL XMLTransformerBase::ignorableWhitespace( const OUString& rWhitespaces ) |
| 340 |
{ |
| 341 |
m_xHandler->ignorableWhitespace( rWhitespaces ); |
| 342 |
} |
| 343 |
|
| 344 |
void SAL_CALL XMLTransformerBase::processingInstruction( const OUString& rTarget, |
| 345 |
const OUString& rData ) |
| 346 |
{ |
| 347 |
m_xHandler->processingInstruction( rTarget, rData ); |
| 348 |
} |
| 349 |
|
| 350 |
void SAL_CALL XMLTransformerBase::setDocumentLocator( const Reference< XLocator >& ) |
| 351 |
{ |
| 352 |
} |
| 353 |
|
| 354 |
// XExtendedDocumentHandler |
| 355 |
void SAL_CALL XMLTransformerBase::startCDATA() |
| 356 |
{ |
| 357 |
} |
| 358 |
|
| 359 |
void SAL_CALL XMLTransformerBase::endCDATA() |
| 360 |
{ |
| 361 |
} |
| 362 |
|
| 363 |
void SAL_CALL XMLTransformerBase::comment( const OUString& /*rComment*/ ) |
| 364 |
{ |
| 365 |
} |
| 366 |
|
| 367 |
void SAL_CALL XMLTransformerBase::allowLineBreak() |
| 368 |
{ |
| 369 |
} |
| 370 |
|
| 371 |
void SAL_CALL XMLTransformerBase::unknown( const OUString& /*rString*/ ) |
| 372 |
{ |
| 373 |
} |
| 374 |
|
| 375 |
// XInitialize |
| 376 |
void SAL_CALL XMLTransformerBase::initialize( const Sequence< Any >& aArguments ) |
| 377 |
{ |
| 378 |
for( const auto& rArgument : aArguments ) |
| 379 |
{ |
| 380 |
// use isAssignableFrom instead of comparing the types to |
| 381 |
// allow XExtendedDocumentHandler instead of XDocumentHandler (used in |
| 382 |
// writeOasis2OOoLibraryElement in sfx2). |
| 383 |
// The Any shift operator can't be used to query the type because it |
| 384 |
// uses queryInterface, and the model also has a XPropertySet interface. |
| 385 |
|
| 386 |
css::uno::Reference< XFastDocumentHandler > xFastHandler; |
| 387 |
if( (rArgument >>= xFastHandler) && xFastHandler ) |
| 388 |
{ |
| 389 |
SvXMLImport *pFastHandler = static_cast<SvXMLImport*>( xFastHandler.get() ); |
| 390 |
m_xHandler.set( new SvXMLLegacyToFastDocHandler( pFastHandler ) ); |
| 391 |
} |
| 392 |
// document handler |
| 393 |
else if( cppu::UnoType<XDocumentHandler>::get().isAssignableFrom( rArgument.getValueType() ) ) |
| 394 |
{ |
| 395 |
m_xHandler.set( rArgument, UNO_QUERY ); |
| 396 |
} |
| 397 |
// property set to transport data across |
| 398 |
else if( cppu::UnoType<XPropertySet>::get().isAssignableFrom( rArgument.getValueType() ) ) |
| 399 |
m_xPropSet.set( rArgument, UNO_QUERY ); |
| 400 |
// xmodel |
| 401 |
else if( cppu::UnoType<css::frame::XModel>::get().isAssignableFrom( rArgument.getValueType() ) ) |
| 402 |
mxModel.set( rArgument, UNO_QUERY ); |
| 403 |
} |
| 404 |
|
| 405 |
if( m_xPropSet.is() ) |
| 406 |
{ |
| 407 |
Any aAny; |
| 408 |
OUString sRelPath, sName; |
| 409 |
Reference< XPropertySetInfo > xPropSetInfo = |
| 410 |
m_xPropSet->getPropertySetInfo(); |
| 411 |
OUString sPropName( u"StreamRelPath"_ustr ); |
| 412 |
if( xPropSetInfo->hasPropertyByName(sPropName) ) |
| 413 |
{ |
| 414 |
aAny = m_xPropSet->getPropertyValue(sPropName); |
| 415 |
aAny >>= sRelPath; |
| 416 |
} |
| 417 |
sPropName = "StreamName"; |
| 418 |
if( xPropSetInfo->hasPropertyByName(sPropName) ) |
| 419 |
{ |
| 420 |
aAny = m_xPropSet->getPropertyValue(sPropName); |
| 421 |
aAny >>= sName; |
| 422 |
} |
| 423 |
if( !sName.isEmpty() ) |
| 424 |
{ |
| 425 |
m_aExtPathPrefix = "../"; |
| 426 |
|
| 427 |
// If there is a rel path within a package, then append |
| 428 |
// additional '../'. If the rel path contains an ':', then it is |
| 429 |
// an absolute URI (or invalid URI, because zip files don't |
| 430 |
// permit ':'), and it will be ignored. |
| 431 |
if( !sRelPath.isEmpty() ) |
| 432 |
{ |
| 433 |
sal_Int32 nColPos = sRelPath.indexOf( ':' ); |
| 434 |
OSL_ENSURE( -1 == nColPos, |
| 435 |
"StreamRelPath contains ':', absolute URI?" ); |
| 436 |
|
| 437 |
if( -1 == nColPos ) |
| 438 |
{ |
| 439 |
OUString sTmp = m_aExtPathPrefix; |
| 440 |
sal_Int32 nPos = 0; |
| 441 |
do |
| 442 |
{ |
| 443 |
m_aExtPathPrefix += sTmp; |
| 444 |
nPos = sRelPath.indexOf( '/', nPos + 1 ); |
| 445 |
} |
| 446 |
while( -1 != nPos ); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
assert(m_xHandler.is()); // can't do anything without that |
| 454 |
} |
| 455 |
|
| 456 |
static sal_Int16 lcl_getUnit( std::u16string_view rValue ) |
| 457 |
{ |
| 458 |
if( o3tl::endsWithIgnoreAsciiCase( rValue, "cm" ) ) |
| 459 |
return util::MeasureUnit::CM; |
| 460 |
else if ( o3tl::endsWithIgnoreAsciiCase( rValue, "mm" ) ) |
| 461 |
return util::MeasureUnit::MM; |
| 462 |
else |
| 463 |
return util::MeasureUnit::INCH; |
| 464 |
} |
| 465 |
|
| 466 |
XMLMutableAttributeList *XMLTransformerBase::ProcessAttrList( |
| 467 |
Reference< XAttributeList >& rAttrList, sal_uInt16 nActionMap, |
| 468 |
bool bClone ) |
| 469 |
{ |
| 470 |
rtl::Reference<XMLMutableAttributeList> pMutableAttrList; |
| 471 |
XMLTransformerActions *pActions = GetUserDefinedActions( nActionMap ); |
| 472 |
OSL_ENSURE( pActions, "go no actions" ); |
| 473 |
if( pActions ) |
| 474 |
{ |
| 475 |
sal_Int16 nAttrCount = rAttrList.is() ? rAttrList->getLength() : 0; |
| 476 |
for( sal_Int16 i=0; i < nAttrCount; ++i ) |
| 477 |
{ |
| 478 |
const OUString aAttrName = rAttrList->getNameByIndex( i ); |
| 479 |
const OUString aAttrValue = rAttrList->getValueByIndex( i ); |
| 480 |
OUString aLocalName; |
| 481 |
sal_uInt16 nPrefix = GetNamespaceMap().GetKeyByAttrName( aAttrName, |
| 482 |
&aLocalName ); |
| 483 |
|
| 484 |
XMLTransformerActions::key_type aKey( nPrefix, aLocalName ); |
| 485 |
XMLTransformerActions::const_iterator aIter = |
| 486 |
pActions->find( aKey ); |
| 487 |
if( aIter != pActions->end() ) |
| 488 |
{ |
| 489 |
if( !pMutableAttrList ) |
| 490 |
{ |
| 491 |
pMutableAttrList = new XMLMutableAttributeList( rAttrList, |
| 492 |
bClone ); |
| 493 |
rAttrList = pMutableAttrList; |
| 494 |
} |
| 495 |
|
| 496 |
sal_uInt32 nAction = (*aIter).second.m_nActionType; |
| 497 |
bool bRename = false; |
| 498 |
switch( nAction ) |
| 499 |
{ |
| 500 |
case XML_ATACTION_RENAME: |
| 501 |
bRename = true; |
| 502 |
break; |
| 503 |
case XML_ATACTION_COPY: |
| 504 |
break; |
| 505 |
case XML_ATACTION_REMOVE: |
| 506 |
case XML_ATACTION_STYLE_DISPLAY_NAME: |
| 507 |
pMutableAttrList->RemoveAttributeByIndex( i ); |
| 508 |
--i; |
| 509 |
--nAttrCount; |
| 510 |
break; |
| 511 |
case XML_ATACTION_RENAME_IN2INCH: |
| 512 |
bRename = true; |
| 513 |
[[fallthrough]]; |
| 514 |
case XML_ATACTION_IN2INCH: |
| 515 |
{ |
| 516 |
OUString aAttrValue2( aAttrValue ); |
| 517 |
if( ReplaceSingleInWithInch( aAttrValue2 ) ) |
| 518 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 519 |
} |
| 520 |
break; |
| 521 |
case XML_ATACTION_INS2INCHS: |
| 522 |
{ |
| 523 |
OUString aAttrValue2( aAttrValue ); |
| 524 |
if( ReplaceInWithInch( aAttrValue2 ) ) |
| 525 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 526 |
} |
| 527 |
break; |
| 528 |
case XML_ATACTION_RENAME_INCH2IN: |
| 529 |
bRename = true; |
| 530 |
[[fallthrough]]; |
| 531 |
case XML_ATACTION_INCH2IN: |
| 532 |
{ |
| 533 |
OUString aAttrValue2( aAttrValue ); |
| 534 |
if( ReplaceSingleInchWithIn( aAttrValue2 ) ) |
| 535 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 536 |
} |
| 537 |
break; |
| 538 |
case XML_ATACTION_INCHS2INS: |
| 539 |
{ |
| 540 |
OUString aAttrValue2( aAttrValue ); |
| 541 |
if( ReplaceInchWithIn( aAttrValue2 ) ) |
| 542 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 543 |
} |
| 544 |
break; |
| 545 |
case XML_ATACTION_TWIPS2IN: |
| 546 |
{ |
| 547 |
OUString aAttrValue2( aAttrValue ); |
| 548 |
|
| 549 |
XMLTransformerBase::ReplaceSingleInchWithIn( aAttrValue2 ); |
| 550 |
if( isWriter() ) |
| 551 |
{ |
| 552 |
sal_Int16 const nDestUnit = lcl_getUnit(aAttrValue2); |
| 553 |
|
| 554 |
// convert twips value to inch |
| 555 |
sal_Int32 nMeasure; |
| 556 |
if (::sax::Converter::convertMeasure(nMeasure, |
| 557 |
aAttrValue2)) |
| 558 |
{ |
| 559 |
nMeasure = static_cast<sal_Int32>(convertTwipToMm100(nMeasure)); |
| 560 |
|
| 561 |
OUStringBuffer aBuffer; |
| 562 |
::sax::Converter::convertMeasure(aBuffer, |
| 563 |
nMeasure, util::MeasureUnit::MM_100TH, |
| 564 |
nDestUnit ); |
| 565 |
aAttrValue2 = aBuffer.makeStringAndClear(); |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 570 |
} |
| 571 |
break; |
| 572 |
case XML_ATACTION_RENAME_DECODE_STYLE_NAME_REF: |
| 573 |
bRename = true; |
| 574 |
[[fallthrough]]; |
| 575 |
case XML_ATACTION_DECODE_STYLE_NAME: |
| 576 |
case XML_ATACTION_DECODE_STYLE_NAME_REF: |
| 577 |
{ |
| 578 |
OUString aAttrValue2( aAttrValue ); |
| 579 |
if( DecodeStyleName(aAttrValue2) ) |
| 580 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 581 |
} |
| 582 |
break; |
| 583 |
case XML_ATACTION_ENCODE_STYLE_NAME: |
| 584 |
{ |
| 585 |
OUString aAttrValue2( aAttrValue ); |
| 586 |
if( EncodeStyleName(aAttrValue2) ) |
| 587 |
{ |
| 588 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 589 |
OUString aNewAttrQName( |
| 590 |
GetNamespaceMap().GetQNameByKey( |
| 591 |
nPrefix, |
| 592 |
::xmloff::token::GetXMLToken( |
| 593 |
XML_DISPLAY_NAME ) ) ); |
| 594 |
pMutableAttrList->AddAttribute( aNewAttrQName, |
| 595 |
aAttrValue ); |
| 596 |
} |
| 597 |
} |
| 598 |
break; |
| 599 |
case XML_ATACTION_RENAME_ENCODE_STYLE_NAME_REF: |
| 600 |
bRename = true; |
| 601 |
[[fallthrough]]; |
| 602 |
case XML_ATACTION_ENCODE_STYLE_NAME_REF: |
| 603 |
{ |
| 604 |
OUString aAttrValue2( aAttrValue ); |
| 605 |
if( EncodeStyleName(aAttrValue2) ) |
| 606 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 607 |
} |
| 608 |
break; |
| 609 |
case XML_ATACTION_RENAME_NEG_PERCENT: |
| 610 |
bRename = true; |
| 611 |
[[fallthrough]]; |
| 612 |
case XML_ATACTION_NEG_PERCENT: |
| 613 |
{ |
| 614 |
OUString aAttrValue2( aAttrValue ); |
| 615 |
if( NegPercent( aAttrValue2 ) ) |
| 616 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 617 |
} |
| 618 |
break; |
| 619 |
case XML_ATACTION_RENAME_ADD_NAMESPACE_PREFIX: |
| 620 |
bRename = true; |
| 621 |
[[fallthrough]]; |
| 622 |
case XML_ATACTION_ADD_NAMESPACE_PREFIX: |
| 623 |
{ |
| 624 |
OUString aAttrValue2( aAttrValue ); |
| 625 |
sal_uInt16 nValPrefix = |
| 626 |
static_cast<sal_uInt16>( |
| 627 |
bRename ? (*aIter).second.m_nParam2 |
| 628 |
: (*aIter).second.m_nParam1); |
| 629 |
AddNamespacePrefix( aAttrValue2, nValPrefix ); |
| 630 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 631 |
} |
| 632 |
break; |
| 633 |
case XML_ATACTION_ADD_APP_NAMESPACE_PREFIX: |
| 634 |
{ |
| 635 |
OUString aAttrValue2( aAttrValue ); |
| 636 |
sal_uInt16 nValPrefix = |
| 637 |
static_cast<sal_uInt16>((*aIter).second.m_nParam1); |
| 638 |
if( IsXMLToken( GetClass(), XML_SPREADSHEET ) ) |
| 639 |
nValPrefix = XML_NAMESPACE_OOOC; |
| 640 |
else if( IsXMLToken( GetClass(), XML_TEXT ) ) |
| 641 |
nValPrefix = XML_NAMESPACE_OOOW; |
| 642 |
AddNamespacePrefix( aAttrValue2, nValPrefix ); |
| 643 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 644 |
} |
| 645 |
break; |
| 646 |
case XML_ATACTION_RENAME_REMOVE_NAMESPACE_PREFIX: |
| 647 |
bRename = true; |
| 648 |
[[fallthrough]]; |
| 649 |
case XML_ATACTION_REMOVE_NAMESPACE_PREFIX: |
| 650 |
{ |
| 651 |
OUString aAttrValue2( aAttrValue ); |
| 652 |
sal_uInt16 nValPrefix = |
| 653 |
static_cast<sal_uInt16>( |
| 654 |
bRename ? (*aIter).second.m_nParam2 |
| 655 |
: (*aIter).second.m_nParam1); |
| 656 |
if( RemoveNamespacePrefix( aAttrValue2, nValPrefix ) ) |
| 657 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 658 |
} |
| 659 |
break; |
| 660 |
case XML_ATACTION_REMOVE_ANY_NAMESPACE_PREFIX: |
| 661 |
{ |
| 662 |
OUString aAttrValue2( aAttrValue ); |
| 663 |
if( RemoveNamespacePrefix( aAttrValue2 ) ) |
| 664 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 665 |
} |
| 666 |
break; |
| 667 |
case XML_ATACTION_URI_OOO: |
| 668 |
{ |
| 669 |
OUString aAttrValue2( aAttrValue ); |
| 670 |
if( ConvertURIToOASIS( aAttrValue2, |
| 671 |
static_cast< bool >((*aIter).second.m_nParam1))) |
| 672 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 673 |
} |
| 674 |
break; |
| 675 |
case XML_ATACTION_URI_OASIS: |
| 676 |
{ |
| 677 |
OUString aAttrValue2( aAttrValue ); |
| 678 |
if( ConvertURIToOOo( aAttrValue2, |
| 679 |
static_cast< bool >((*aIter).second.m_nParam1))) |
| 680 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 681 |
} |
| 682 |
break; |
| 683 |
case XML_ATACTION_RENAME_ATTRIBUTE: |
| 684 |
{ |
| 685 |
OUString aAttrValue2( aAttrValue ); |
| 686 |
RenameAttributeValue( |
| 687 |
aAttrValue2, |
| 688 |
(*aIter).second.m_nParam1, |
| 689 |
(*aIter).second.m_nParam2, |
| 690 |
(*aIter).second.m_nParam3 ); |
| 691 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 692 |
} |
| 693 |
break; |
| 694 |
case XML_ATACTION_RNG2ISO_DATETIME: |
| 695 |
{ |
| 696 |
OUString aAttrValue2( aAttrValue ); |
| 697 |
if( ConvertRNGDateTimeToISO( aAttrValue2 )) |
| 698 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 699 |
} |
| 700 |
break; |
| 701 |
case XML_ATACTION_RENAME_RNG2ISO_DATETIME: |
| 702 |
{ |
| 703 |
OUString aAttrValue2( aAttrValue ); |
| 704 |
if( ConvertRNGDateTimeToISO( aAttrValue2 )) |
| 705 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 706 |
bRename = true; |
| 707 |
} |
| 708 |
break; |
| 709 |
case XML_ATACTION_IN2TWIPS: |
| 710 |
{ |
| 711 |
OUString aAttrValue2( aAttrValue ); |
| 712 |
XMLTransformerBase::ReplaceSingleInWithInch( aAttrValue2 ); |
| 713 |
|
| 714 |
if( isWriter() ) |
| 715 |
{ |
| 716 |
sal_Int16 const nDestUnit = lcl_getUnit(aAttrValue2); |
| 717 |
|
| 718 |
// convert inch value to twips and export as faked inch |
| 719 |
sal_Int32 nMeasure; |
| 720 |
if (::sax::Converter::convertMeasure(nMeasure, |
| 721 |
aAttrValue2)) |
| 722 |
{ |
| 723 |
nMeasure = o3tl::toTwips(nMeasure, o3tl::Length::mm100); |
| 724 |
|
| 725 |
OUStringBuffer aBuffer; |
| 726 |
::sax::Converter::convertMeasure( aBuffer, |
| 727 |
nMeasure, util::MeasureUnit::MM_100TH, |
| 728 |
nDestUnit ); |
| 729 |
aAttrValue2 = aBuffer.makeStringAndClear(); |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 734 |
} |
| 735 |
break; |
| 736 |
case XML_ATACTION_SVG_WIDTH_HEIGHT_OOO: |
| 737 |
{ |
| 738 |
OUString aAttrValue2( aAttrValue ); |
| 739 |
ReplaceSingleInchWithIn( aAttrValue2 ); |
| 740 |
|
| 741 |
sal_Int16 const nDestUnit = lcl_getUnit( aAttrValue2 ); |
| 742 |
|
| 743 |
sal_Int32 nMeasure; |
| 744 |
if (::sax::Converter::convertMeasure(nMeasure, |
| 745 |
aAttrValue2)) |
| 746 |
{ |
| 747 |
|
| 748 |
if( nMeasure > 0 ) |
| 749 |
nMeasure -= 1; |
| 750 |
else if( nMeasure < 0 ) |
| 751 |
nMeasure += 1; |
| 752 |
|
| 753 |
|
| 754 |
OUStringBuffer aBuffer; |
| 755 |
::sax::Converter::convertMeasure(aBuffer, nMeasure, |
| 756 |
util::MeasureUnit::MM_100TH, nDestUnit); |
| 757 |
aAttrValue2 = aBuffer.makeStringAndClear(); |
| 758 |
} |
| 759 |
|
| 760 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 761 |
} |
| 762 |
break; |
| 763 |
case XML_ATACTION_SVG_WIDTH_HEIGHT_OASIS: |
| 764 |
{ |
| 765 |
OUString aAttrValue2( aAttrValue ); |
| 766 |
ReplaceSingleInWithInch( aAttrValue2 ); |
| 767 |
|
| 768 |
sal_Int16 const nDestUnit = lcl_getUnit( aAttrValue2 ); |
| 769 |
|
| 770 |
sal_Int32 nMeasure; |
| 771 |
if (::sax::Converter::convertMeasure(nMeasure, |
| 772 |
aAttrValue2)) |
| 773 |
{ |
| 774 |
|
| 775 |
if( nMeasure > 0 ) |
| 776 |
nMeasure += 1; |
| 777 |
else if( nMeasure < 0 ) |
| 778 |
nMeasure -= 1; |
| 779 |
|
| 780 |
|
| 781 |
OUStringBuffer aBuffer; |
| 782 |
::sax::Converter::convertMeasure(aBuffer, nMeasure, |
| 783 |
util::MeasureUnit::MM_100TH, nDestUnit ); |
| 784 |
aAttrValue2 = aBuffer.makeStringAndClear(); |
| 785 |
} |
| 786 |
|
| 787 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 788 |
} |
| 789 |
break; |
| 790 |
case XML_ATACTION_DECODE_ID: |
| 791 |
{ |
| 792 |
const sal_Int32 nLen = aAttrValue.getLength(); |
| 793 |
OUStringBuffer aBuffer; |
| 794 |
|
| 795 |
sal_Int32 pos; |
| 796 |
for( pos = 0; pos < nLen; pos++ ) |
| 797 |
{ |
| 798 |
sal_Unicode c = aAttrValue[pos]; |
| 799 |
if( (c >= '0') && (c <= '9') ) |
| 800 |
aBuffer.append( c ); |
| 801 |
else |
| 802 |
aBuffer.append( static_cast<sal_Int32>(c) ); |
| 803 |
} |
| 804 |
|
| 805 |
pMutableAttrList->SetValueByIndex( i, aBuffer.makeStringAndClear() ); |
| 806 |
} |
| 807 |
break; |
| 808 |
// #i50322# - special handling for the |
| 809 |
// transparency of writer background graphics. |
| 810 |
case XML_ATACTION_WRITER_BACK_GRAPHIC_TRANSPARENCY: |
| 811 |
{ |
| 812 |
// determine, if it's the transparency of a document style |
| 813 |
XMLTransformerContext* pFirstContext = m_vContexts[0].get(); |
| 814 |
OUString aFirstContextLocalName; |
| 815 |
/* sal_uInt16 nFirstContextPrefix = */ |
| 816 |
GetNamespaceMap().GetKeyByAttrName( pFirstContext->GetQName(), |
| 817 |
&aFirstContextLocalName ); |
| 818 |
bool bIsDocumentStyle( |
| 819 |
::xmloff::token::IsXMLToken( aFirstContextLocalName, |
| 820 |
XML_DOCUMENT_STYLES ) ); |
| 821 |
// no conversion of transparency value for document |
| 822 |
// styles, because former OpenOffice.org version writes |
| 823 |
// writes always a transparency value of 100% and doesn't |
| 824 |
// read the value. Thus, it's interpreted as 0% |
| 825 |
if ( !bIsDocumentStyle ) |
| 826 |
{ |
| 827 |
OUString aAttrValue2( aAttrValue ); |
| 828 |
NegPercent(aAttrValue2); |
| 829 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 830 |
} |
| 831 |
bRename = true; |
| 832 |
} |
| 833 |
break; |
| 834 |
case XML_ATACTION_SHAPEID: |
| 835 |
{ |
| 836 |
OUString sNewValue = "shape" + aAttrValue; |
| 837 |
pMutableAttrList->SetValueByIndex( i, sNewValue ); |
| 838 |
break; |
| 839 |
} |
| 840 |
|
| 841 |
default: |
| 842 |
OSL_ENSURE( false, "unknown action" ); |
| 843 |
break; |
| 844 |
} |
| 845 |
|
| 846 |
if( bRename ) |
| 847 |
{ |
| 848 |
OUString aNewAttrQName( |
| 849 |
GetNamespaceMap().GetQNameByKey( |
| 850 |
(*aIter).second.GetQNamePrefixFromParam1(), |
| 851 |
::xmloff::token::GetXMLToken( |
| 852 |
(*aIter).second.GetQNameTokenFromParam1()) ) ); |
| 853 |
pMutableAttrList->RenameAttributeByIndex( i, |
| 854 |
aNewAttrQName ); |
| 855 |
} |
| 856 |
} |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
return pMutableAttrList.get(); |
| 861 |
} |
| 862 |
|
| 863 |
bool XMLTransformerBase::ReplaceSingleInchWithIn( OUString& rValue ) |
| 864 |
{ |
| 865 |
bool bRet = false; |
| 866 |
sal_Int32 nPos = rValue.getLength(); |
| 867 |
while( nPos && rValue[nPos-1] <= ' ' ) |
| 868 |
--nPos; |
| 869 |
if( nPos > 2 && |
| 870 |
('c'==rValue[nPos-2] || 'C'==rValue[nPos-2]) && |
| 871 |
('h'==rValue[nPos-1] || 'H'==rValue[nPos-1]) ) |
| 872 |
{ |
| 873 |
rValue =rValue.copy( 0, nPos-2 ); |
| 874 |
bRet = true; |
| 875 |
} |
| 876 |
|
| 877 |
return bRet; |
| 878 |
} |
| 879 |
|
| 880 |
bool XMLTransformerBase::ReplaceInchWithIn( OUString& rValue ) |
| 881 |
{ |
| 882 |
bool bRet = false; |
| 883 |
sal_Int32 nPos = 1; |
| 884 |
while( nPos < rValue.getLength()-3 ) |
| 885 |
{ |
| 886 |
sal_Unicode c = rValue[nPos]; |
| 887 |
if( 'i'==c || 'I'==c ) |
| 888 |
{ |
| 889 |
c = rValue[nPos-1]; |
| 890 |
if( (c >= '0' && c <= '9') || '.' == c ) |
| 891 |
{ |
| 892 |
c = rValue[nPos+1]; |
| 893 |
if( 'n'==c || 'N'==c ) |
| 894 |
{ |
| 895 |
c = rValue[nPos+2]; |
| 896 |
if( 'c'==c || 'C'==c ) |
| 897 |
{ |
| 898 |
c = rValue[nPos+3]; |
| 899 |
if( 'h'==c || 'H'==c ) |
| 900 |
{ |
| 901 |
rValue = rValue.replaceAt( nPos, |
| 902 |
4, GetXMLToken(XML_IN) ); |
| 903 |
nPos += 2; |
| 904 |
bRet = true; |
| 905 |
continue; |
| 906 |
} |
| 907 |
} |
| 908 |
} |
| 909 |
} |
| 910 |
} |
| 911 |
++nPos; |
| 912 |
} |
| 913 |
|
| 914 |
return bRet; |
| 915 |
} |
| 916 |
|
| 917 |
bool XMLTransformerBase::ReplaceSingleInWithInch( OUString& rValue ) |
| 918 |
{ |
| 919 |
bool bRet = false; |
| 920 |
|
| 921 |
sal_Int32 nPos = rValue.getLength(); |
| 922 |
while( nPos && rValue[nPos-1] <= ' ' ) |
| 923 |
--nPos; |
| 924 |
if( nPos > 2 && |
| 925 |
('i'==rValue[nPos-2] || |
| 926 |
'I'==rValue[nPos-2]) && |
| 927 |
('n'==rValue[nPos-1] || |
| 928 |
'N'==rValue[nPos-1]) ) |
| 929 |
{ |
| 930 |
nPos -= 2; |
| 931 |
rValue = rValue.replaceAt( nPos, rValue.getLength() - nPos, |
| 932 |
GetXMLToken(XML_INCH) ); |
| 933 |
bRet = true; |
| 934 |
} |
| 935 |
|
| 936 |
return bRet; |
| 937 |
} |
| 938 |
|
| 939 |
bool XMLTransformerBase::ReplaceInWithInch( OUString& rValue ) |
| 940 |
{ |
| 941 |
bool bRet = false; |
| 942 |
sal_Int32 nPos = 1; |
| 943 |
while( nPos < rValue.getLength()-1 ) |
| 944 |
{ |
| 945 |
sal_Unicode c = rValue[nPos]; |
| 946 |
if( 'i'==c || 'I'==c ) |
| 947 |
{ |
| 948 |
c = rValue[nPos-1]; |
| 949 |
if( (c >= '0' && c <= '9') || '.' == c ) |
| 950 |
{ |
| 951 |
c = rValue[nPos+1]; |
| 952 |
if( 'n'==c || 'N'==c ) |
| 953 |
{ |
| 954 |
rValue = rValue.replaceAt( nPos, |
| 955 |
2, GetXMLToken(XML_INCH) ); |
| 956 |
nPos += 4; |
| 957 |
bRet = true; |
| 958 |
continue; |
| 959 |
} |
| 960 |
} |
| 961 |
} |
| 962 |
++nPos; |
| 963 |
} |
| 964 |
|
| 965 |
return bRet; |
| 966 |
} |
| 967 |
|
| 968 |
bool XMLTransformerBase::EncodeStyleName( OUString& rName ) const |
| 969 |
{ |
| 970 |
static const char aHexTab[] = "0123456789abcdef"; |
| 971 |
|
| 972 |
bool bEncoded = false; |
| 973 |
|
| 974 |
sal_Int32 nLen = rName.getLength(); |
| 975 |
OUStringBuffer aBuffer( nLen ); |
| 976 |
|
| 977 |
for( sal_Int32 i = 0; i < nLen; i++ ) |
| 978 |
{ |
| 979 |
sal_Unicode c = rName[i]; |
| 980 |
bool bValidChar = false; |
| 981 |
if( c < 0x00ffU ) |
| 982 |
{ |
| 983 |
bValidChar = |
| 984 |
(c >= 0x0041 && c <= 0x005a) || |
| 985 |
(c >= 0x0061 && c <= 0x007a) || |
| 986 |
(c >= 0x00c0 && c <= 0x00d6) || |
| 987 |
(c >= 0x00d8 && c <= 0x00f6) || |
| 988 |
(c >= 0x00f8 && c <= 0x00ff) || |
| 989 |
( i > 0 && ( (c >= 0x0030 && c <= 0x0039) || |
| 990 |
c == 0x00b7 || c == '-' || c == '.') ); |
| 991 |
} |
| 992 |
else |
| 993 |
{ |
| 994 |
if( (c >= 0xf900U && c <= 0xfffeU) || |
| 995 |
(c >= 0x20ddU && c <= 0x20e0U)) |
| 996 |
{ |
| 997 |
bValidChar = false; |
| 998 |
} |
| 999 |
else if( (c >= 0x02bbU && c <= 0x02c1U) || c == 0x0559 || |
| 1000 |
c == 0x06e5 || c == 0x06e6 ) |
| 1001 |
{ |
| 1002 |
bValidChar = true; |
| 1003 |
} |
| 1004 |
else if( c == 0x0387 ) |
| 1005 |
{ |
| 1006 |
bValidChar = i > 0; |
| 1007 |
} |
| 1008 |
else |
| 1009 |
{ |
| 1010 |
if( !xCharClass.is() ) |
| 1011 |
{ |
| 1012 |
const_cast < XMLTransformerBase * >(this) |
| 1013 |
->xCharClass = CharacterClassification::create( comphelper::getProcessComponentContext() ); |
| 1014 |
} |
| 1015 |
sal_Int16 nType = xCharClass->getType( rName, i ); |
| 1016 |
|
| 1017 |
switch( nType ) |
| 1018 |
{ |
| 1019 |
case UnicodeType::UPPERCASE_LETTER: // Lu |
| 1020 |
case UnicodeType::LOWERCASE_LETTER: // Ll |
| 1021 |
case UnicodeType::TITLECASE_LETTER: // Lt |
| 1022 |
case UnicodeType::OTHER_LETTER: // Lo |
| 1023 |
case UnicodeType::LETTER_NUMBER: // Nl |
| 1024 |
bValidChar = true; |
| 1025 |
break; |
| 1026 |
case UnicodeType::NON_SPACING_MARK: // Ms |
| 1027 |
case UnicodeType::ENCLOSING_MARK: // Me |
| 1028 |
case UnicodeType::COMBINING_SPACING_MARK: //Mc |
| 1029 |
case UnicodeType::MODIFIER_LETTER: // Lm |
| 1030 |
case UnicodeType::DECIMAL_DIGIT_NUMBER: // Nd |
| 1031 |
bValidChar = i > 0; |
| 1032 |
break; |
| 1033 |
} |
| 1034 |
} |
| 1035 |
} |
| 1036 |
if( bValidChar ) |
| 1037 |
{ |
| 1038 |
aBuffer.append( c ); |
| 1039 |
} |
| 1040 |
else |
| 1041 |
{ |
| 1042 |
aBuffer.append( '_' ); |
| 1043 |
if( c > 0x0fff ) |
| 1044 |
aBuffer.append( static_cast< sal_Unicode >( |
| 1045 |
aHexTab[ (c >> 12) & 0x0f ] ) ); |
| 1046 |
if( c > 0x00ff ) |
| 1047 |
aBuffer.append( static_cast< sal_Unicode >( |
| 1048 |
aHexTab[ (c >> 8) & 0x0f ] ) ); |
| 1049 |
if( c > 0x000f ) |
| 1050 |
aBuffer.append( static_cast< sal_Unicode >( |
| 1051 |
aHexTab[ (c >> 4) & 0x0f ] ) ); |
| 1052 |
aBuffer.append( |
| 1053 |
OUString::number(static_cast< sal_Unicode >( aHexTab[ c & 0x0f ] ) ) |
| 1054 |
+ "_" ); |
| 1055 |
bEncoded = true; |
| 1056 |
} |
| 1057 |
} |
| 1058 |
|
| 1059 |
if( aBuffer.getLength() > (1<<15)-1 ) |
| 1060 |
bEncoded = false; |
| 1061 |
|
| 1062 |
if( bEncoded ) |
| 1063 |
rName = aBuffer.makeStringAndClear(); |
| 1064 |
return bEncoded; |
| 1065 |
} |
| 1066 |
|
| 1067 |
bool XMLTransformerBase::DecodeStyleName( OUString& rName ) |
| 1068 |
{ |
| 1069 |
bool bEncoded = false; |
| 1070 |
|
| 1071 |
sal_Int32 nLen = rName.getLength(); |
| 1072 |
OUStringBuffer aBuffer( nLen ); |
| 1073 |
|
| 1074 |
bool bWithinHex = false; |
| 1075 |
sal_Unicode cEnc = 0; |
| 1076 |
for( sal_Int32 i = 0; i < nLen; i++ ) |
| 1077 |
{ |
| 1078 |
sal_Unicode c = rName[i]; |
| 1079 |
if( '_' == c ) |
| 1080 |
{ |
| 1081 |
if( bWithinHex ) |
| 1082 |
{ |
| 1083 |
aBuffer.append( cEnc ); |
| 1084 |
cEnc = 0; |
| 1085 |
} |
| 1086 |
else |
| 1087 |
{ |
| 1088 |
bEncoded = true; |
| 1089 |
} |
| 1090 |
bWithinHex = !bWithinHex; |
| 1091 |
} |
| 1092 |
else if( bWithinHex ) |
| 1093 |
{ |
| 1094 |
int nValue = o3tl::convertToHex<int>(c); |
| 1095 |
if (nValue == -1) |
| 1096 |
{ |
| 1097 |
// error |
| 1098 |
bEncoded = false; |
| 1099 |
break; |
| 1100 |
} |
| 1101 |
cEnc = (cEnc << 4) + nValue; |
| 1102 |
} |
| 1103 |
else |
| 1104 |
{ |
| 1105 |
aBuffer.append( c ); |
| 1106 |
} |
| 1107 |
} |
| 1108 |
|
| 1109 |
if( bEncoded ) |
| 1110 |
rName = aBuffer.makeStringAndClear(); |
| 1111 |
return bEncoded; |
| 1112 |
} |
| 1113 |
|
| 1114 |
bool XMLTransformerBase::NegPercent( OUString& rValue ) |
| 1115 |
{ |
| 1116 |
bool bRet = false; |
| 1117 |
bool bNeg = false; |
| 1118 |
double nVal = 0; |
| 1119 |
|
| 1120 |
sal_Int32 nPos = 0; |
| 1121 |
sal_Int32 nLen = rValue.getLength(); |
| 1122 |
|
| 1123 |
// skip white space |
| 1124 |
while( nPos < nLen && ' ' == rValue[nPos] ) |
| 1125 |
nPos++; |
| 1126 |
|
| 1127 |
if( nPos < nLen && '-' == rValue[nPos] ) |
| 1128 |
{ |
| 1129 |
bNeg = true; |
| 1130 |
nPos++; |
| 1131 |
} |
| 1132 |
|
| 1133 |
// get number |
| 1134 |
while( nPos < nLen && |
| 1135 |
'0' <= rValue[nPos] && |
| 1136 |
'9' >= rValue[nPos] ) |
| 1137 |
{ |
| 1138 |
// TODO: check overflow! |
| 1139 |
nVal *= 10; |
| 1140 |
nVal += (rValue[nPos] - '0'); |
| 1141 |
nPos++; |
| 1142 |
} |
| 1143 |
if( nPos < nLen && '.' == rValue[nPos] ) |
| 1144 |
{ |
| 1145 |
nPos++; |
| 1146 |
double nDiv = 1.; |
| 1147 |
|
| 1148 |
while( nPos < nLen && |
| 1149 |
'0' <= rValue[nPos] && |
| 1150 |
'9' >= rValue[nPos] ) |
| 1151 |
{ |
| 1152 |
// TODO: check overflow! |
| 1153 |
nDiv *= 10; |
| 1154 |
nVal += ( static_cast<double>(rValue[nPos] - '0') / nDiv ); |
| 1155 |
nPos++; |
| 1156 |
} |
| 1157 |
} |
| 1158 |
|
| 1159 |
// skip white space |
| 1160 |
while( nPos < nLen && ' ' == rValue[nPos] ) |
| 1161 |
nPos++; |
| 1162 |
|
| 1163 |
if( nPos < nLen && '%' == rValue[nPos] ) |
| 1164 |
{ |
| 1165 |
if( bNeg ) |
| 1166 |
nVal = -nVal; |
| 1167 |
nVal += .5; |
| 1168 |
|
| 1169 |
sal_Int32 nIntVal = 100 - static_cast<sal_Int32>( nVal ); |
| 1170 |
|
| 1171 |
rValue = OUString::number(nIntVal) + "%"; |
| 1172 |
|
| 1173 |
bRet = true; |
| 1174 |
} |
| 1175 |
|
| 1176 |
return bRet; |
| 1177 |
} |
| 1178 |
|
| 1179 |
void XMLTransformerBase::AddNamespacePrefix( OUString& rName, |
| 1180 |
sal_uInt16 nPrefix ) const |
| 1181 |
{ |
| 1182 |
rName = GetNamespaceMap().GetQNameByKey( nPrefix, rName, false ); |
| 1183 |
} |
| 1184 |
|
| 1185 |
bool XMLTransformerBase::RemoveNamespacePrefix( OUString& rName, |
| 1186 |
sal_uInt16 nPrefixOnly ) const |
| 1187 |
{ |
| 1188 |
OUString aLocalName; |
| 1189 |
sal_uInt16 nPrefix = |
| 1190 |
GetNamespaceMap().GetKeyByAttrValueQName(rName, &aLocalName); |
| 1191 |
bool bRet = XML_NAMESPACE_UNKNOWN != nPrefix && |
| 1192 |
(USHRT_MAX == nPrefixOnly || nPrefix == nPrefixOnly); |
| 1193 |
if( bRet ) |
| 1194 |
rName = aLocalName; |
| 1195 |
|
| 1196 |
return bRet; |
| 1197 |
} |
| 1198 |
|
| 1199 |
bool XMLTransformerBase::ConvertURIToOASIS( OUString& rURI, |
| 1200 |
bool bSupportPackage ) const |
| 1201 |
{ |
| 1202 |
bool bRet = false; |
| 1203 |
if( !m_aExtPathPrefix.isEmpty() && !rURI.isEmpty() ) |
| 1204 |
{ |
| 1205 |
bool bRel = false; |
| 1206 |
switch( rURI[0] ) |
| 1207 |
{ |
| 1208 |
case '#': |
| 1209 |
// no rel path, but |
| 1210 |
// for package URIs, the '#' has to be removed |
| 1211 |
if( bSupportPackage ) |
| 1212 |
{ |
| 1213 |
rURI = rURI.copy( 1 ); |
| 1214 |
bRet = true; |
| 1215 |
} |
| 1216 |
break; |
| 1217 |
case '/': |
| 1218 |
// no rel path; nothing to do |
| 1219 |
break; |
| 1220 |
case '.': |
| 1221 |
// a rel path; to keep URI simple, remove './', if there |
| 1222 |
bRel = true; |
| 1223 |
if( rURI.getLength() > 1 && '/' == rURI[1] ) |
| 1224 |
{ |
| 1225 |
rURI = rURI.copy( 2 ); |
| 1226 |
bRet = true; |
| 1227 |
} |
| 1228 |
break; |
| 1229 |
default: |
| 1230 |
// check for a RFC2396 schema |
| 1231 |
{ |
| 1232 |
bRel = true; |
| 1233 |
sal_Int32 nPos = 1; |
| 1234 |
sal_Int32 nLen = rURI.getLength(); |
| 1235 |
while( nPos < nLen ) |
| 1236 |
{ |
| 1237 |
switch( rURI[nPos] ) |
| 1238 |
{ |
| 1239 |
case '/': |
| 1240 |
// a relative path segment |
| 1241 |
nPos = nLen; // leave loop |
| 1242 |
break; |
| 1243 |
case ':': |
| 1244 |
// a schema |
| 1245 |
bRel = false; |
| 1246 |
nPos = nLen; // leave loop |
| 1247 |
break; |
| 1248 |
default: |
| 1249 |
// we don't care about any other characters |
| 1250 |
break; |
| 1251 |
} |
| 1252 |
++nPos; |
| 1253 |
} |
| 1254 |
} |
| 1255 |
} |
| 1256 |
|
| 1257 |
if( bRel ) |
| 1258 |
{ |
| 1259 |
rURI = m_aExtPathPrefix + rURI; |
| 1260 |
bRet = true; |
| 1261 |
} |
| 1262 |
} |
| 1263 |
|
| 1264 |
return bRet; |
| 1265 |
} |
| 1266 |
|
| 1267 |
bool XMLTransformerBase::ConvertURIToOOo( OUString& rURI, |
| 1268 |
bool bSupportPackage ) const |
| 1269 |
{ |
| 1270 |
bool bRet = false; |
| 1271 |
if( !rURI.isEmpty() ) |
| 1272 |
{ |
| 1273 |
bool bPackage = false; |
| 1274 |
switch( rURI[0] ) |
| 1275 |
{ |
| 1276 |
case '/': |
| 1277 |
// no rel path; nothing to do |
| 1278 |
break; |
| 1279 |
case '.': |
| 1280 |
// a rel path |
| 1281 |
if( rURI.startsWith( m_aExtPathPrefix ) ) |
| 1282 |
{ |
| 1283 |
// an external URI; remove '../' |
| 1284 |
rURI = rURI.copy( m_aExtPathPrefix.getLength() ); |
| 1285 |
bRet = true; |
| 1286 |
} |
| 1287 |
else |
| 1288 |
{ |
| 1289 |
bPackage = true; |
| 1290 |
} |
| 1291 |
break; |
| 1292 |
default: |
| 1293 |
// check for a RFC2396 schema |
| 1294 |
{ |
| 1295 |
bPackage = true; |
| 1296 |
sal_Int32 nPos = 1; |
| 1297 |
sal_Int32 nLen = rURI.getLength(); |
| 1298 |
while( nPos < nLen ) |
| 1299 |
{ |
| 1300 |
switch( rURI[nPos] ) |
| 1301 |
{ |
| 1302 |
case '/': |
| 1303 |
// a relative path segment within the package |
| 1304 |
nPos = nLen - 1; // leave loop |
| 1305 |
break; |
| 1306 |
case ':': |
| 1307 |
// a schema |
| 1308 |
bPackage = false; |
| 1309 |
nPos = nLen - 1; // leave loop |
| 1310 |
break; |
| 1311 |
default: |
| 1312 |
// we don't care about any other characters |
| 1313 |
break; |
| 1314 |
} |
| 1315 |
++nPos; |
| 1316 |
} |
| 1317 |
} |
| 1318 |
} |
| 1319 |
|
| 1320 |
if( bPackage && bSupportPackage ) |
| 1321 |
{ |
| 1322 |
if( rURI.startsWith( "./" ) ) |
| 1323 |
rURI = rURI.copy( 2 ); |
| 1324 |
rURI = "#" + rURI; |
| 1325 |
bRet = true; |
| 1326 |
} |
| 1327 |
} |
| 1328 |
|
| 1329 |
return bRet; |
| 1330 |
} |
| 1331 |
|
| 1332 |
bool XMLTransformerBase::RenameAttributeValue( |
| 1333 |
OUString& rOutAttributeValue, |
| 1334 |
sal_Int32 nParam1, |
| 1335 |
sal_Int32 nParam2, |
| 1336 |
sal_Int32 nParam3 ) |
| 1337 |
{ |
| 1338 |
return ( lcl_ConvertAttr( rOutAttributeValue, nParam1) || |
| 1339 |
lcl_ConvertAttr( rOutAttributeValue, nParam2) || |
| 1340 |
lcl_ConvertAttr( rOutAttributeValue, nParam3) ); |
| 1341 |
} |
| 1342 |
|
| 1343 |
// static |
| 1344 |
bool XMLTransformerBase::ConvertRNGDateTimeToISO( OUString& rDateTime ) |
| 1345 |
{ |
| 1346 |
if( !rDateTime.isEmpty() && |
| 1347 |
rDateTime.indexOf( '.' ) != -1 ) |
| 1348 |
{ |
| 1349 |
rDateTime = rDateTime.replace( '.', ','); |
| 1350 |
return true; |
| 1351 |
} |
| 1352 |
|
| 1353 |
return false; |
| 1354 |
} |
| 1355 |
|
| 1356 |
XMLTokenEnum XMLTransformerBase::GetToken( const OUString& rStr ) const |
| 1357 |
{ |
| 1358 |
XMLTransformerTokenMap::const_iterator aIter = |
| 1359 |
m_TokenMap.find( rStr ); |
| 1360 |
if( aIter == m_TokenMap.end() ) |
| 1361 |
return XML_TOKEN_END; |
| 1362 |
else |
| 1363 |
return (*aIter).second; |
| 1364 |
} |
| 1365 |
|
| 1366 |
|
| 1367 |
const XMLTransformerContext *XMLTransformerBase::GetCurrentContext() const |
| 1368 |
{ |
| 1369 |
OSL_ENSURE( !m_vContexts.empty(), "empty stack" ); |
| 1370 |
|
| 1371 |
|
| 1372 |
return m_vContexts.empty() ? nullptr : m_vContexts.back().get(); |
| 1373 |
} |
| 1374 |
|
| 1375 |
const XMLTransformerContext *XMLTransformerBase::GetAncestorContext( |
| 1376 |
sal_uInt32 n ) const |
| 1377 |
{ |
| 1378 |
auto nSize = m_vContexts.size(); |
| 1379 |
|
| 1380 |
OSL_ENSURE( nSize > n + 2 , "invalid context" ); |
| 1381 |
|
| 1382 |
return nSize > n + 2 ? m_vContexts[nSize - (n + 2)].get() : nullptr; |
| 1383 |
} |
| 1384 |
|
| 1385 |
bool XMLTransformerBase::isWriter() const |
| 1386 |
{ |
| 1387 |
Reference< XServiceInfo > xSI( mxModel, UNO_QUERY ); |
| 1388 |
return xSI.is() && |
| 1389 |
( xSI->supportsService(u"com.sun.star.text.TextDocument"_ustr) || |
| 1390 |
xSI->supportsService(u"com.sun.star.text.WebDocument"_ustr) || |
| 1391 |
xSI->supportsService(u"com.sun.star.text.GlobalDocument"_ustr) ); |
| 1392 |
} |
| 1393 |
|
| 1394 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |