| 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 <com/sun/star/beans/XPropertySetInfo.hpp> |
| 21 |
#include <xmloff/namespacemap.hxx> |
| 22 |
#include <xmloff/xmltoken.hxx> |
| 23 |
#include <xmloff/xmlnamespace.hxx> |
| 24 |
|
| 25 |
#include "TransformerBase.hxx" |
| 26 |
#include "MutableAttrList.hxx" |
| 27 |
|
| 28 |
#include "DocumentTContext.hxx" |
| 29 |
|
| 30 |
|
| 31 |
using namespace ::xmloff::token; |
| 32 |
using namespace ::com::sun::star::uno; |
| 33 |
using namespace ::com::sun::star::xml::sax; |
| 34 |
using namespace ::com::sun::star::beans; |
| 35 |
|
| 36 |
XMLDocumentTransformerContext::XMLDocumentTransformerContext( XMLTransformerBase& rImp, |
| 37 |
const OUString& rQName ) : |
| 38 |
XMLTransformerContext( rImp, rQName ) |
| 39 |
{ |
| 40 |
} |
| 41 |
|
| 42 |
void XMLDocumentTransformerContext::StartElement( const Reference< XAttributeList >& rAttrList ) |
| 43 |
{ |
| 44 |
Reference< XAttributeList > xAttrList( rAttrList ); |
| 45 |
|
| 46 |
bool bMimeFound = false; |
| 47 |
OUString aClass; |
| 48 |
OUString aClassQName( |
| 49 |
GetTransformer().GetNamespaceMap().GetQNameByKey( |
| 50 |
XML_NAMESPACE_OFFICE, GetXMLToken(XML_CLASS ) ) ); |
| 51 |
|
| 52 |
rtl::Reference<XMLMutableAttributeList> pMutableAttrList; |
| 53 |
sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0; |
| 54 |
for( sal_Int16 i=0; i < nAttrCount; i++ ) |
| 55 |
{ |
| 56 |
const OUString aAttrName = xAttrList->getNameByIndex( i ); |
| 57 |
OUString aLocalName; |
| 58 |
sal_uInt16 nPrefix = |
| 59 |
GetTransformer().GetNamespaceMap().GetKeyByAttrName( aAttrName, |
| 60 |
&aLocalName ); |
| 61 |
if( XML_NAMESPACE_OFFICE == nPrefix && |
| 62 |
IsXMLToken( aLocalName, XML_MIMETYPE ) ) |
| 63 |
{ |
| 64 |
const OUString aValue = xAttrList->getValueByIndex( i ); |
| 65 |
static constexpr std::string_view aTmp[] |
| 66 |
{ |
| 67 |
"application/vnd.oasis.openoffice.", |
| 68 |
"application/x-vnd.oasis.openoffice.", |
| 69 |
"application/vnd.oasis.opendocument.", |
| 70 |
"application/x-vnd.oasis.document." |
| 71 |
}; |
| 72 |
for (const auto & rPrefix : aTmp) |
| 73 |
{ |
| 74 |
if( aValue.matchAsciiL( rPrefix.data(), rPrefix.size() ) ) |
| 75 |
{ |
| 76 |
aClass = aValue.copy( rPrefix.size() ); |
| 77 |
break; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if( !pMutableAttrList ) |
| 82 |
{ |
| 83 |
pMutableAttrList = new XMLMutableAttributeList( xAttrList ); |
| 84 |
xAttrList = pMutableAttrList; |
| 85 |
} |
| 86 |
pMutableAttrList->SetValueByIndex( i, aClass ); |
| 87 |
pMutableAttrList->RenameAttributeByIndex(i, aClassQName ); |
| 88 |
bMimeFound = true; |
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if( !bMimeFound ) |
| 94 |
{ |
| 95 |
const Reference< XPropertySet > rPropSet = |
| 96 |
GetTransformer().GetPropertySet(); |
| 97 |
|
| 98 |
if( rPropSet.is() ) |
| 99 |
{ |
| 100 |
Reference< XPropertySetInfo > xPropSetInfo( |
| 101 |
rPropSet->getPropertySetInfo() ); |
| 102 |
OUString aPropName(u"Class"_ustr); |
| 103 |
if( xPropSetInfo.is() && xPropSetInfo->hasPropertyByName( aPropName ) ) |
| 104 |
{ |
| 105 |
Any aAny = rPropSet->getPropertyValue( aPropName ); |
| 106 |
aAny >>= aClass; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if( !aClass.isEmpty() ) |
| 111 |
{ |
| 112 |
if( !pMutableAttrList ) |
| 113 |
{ |
| 114 |
pMutableAttrList = new XMLMutableAttributeList( xAttrList ); |
| 115 |
xAttrList = pMutableAttrList; |
| 116 |
} |
| 117 |
|
| 118 |
pMutableAttrList->AddAttribute( aClassQName, aClass ); |
| 119 |
} |
| 120 |
} |
| 121 |
XMLTransformerContext::StartElement( xAttrList ); |
| 122 |
} |
| 123 |
|
| 124 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |