LibreOffice Module xmloff (master)  1
DocumentTContext.cxx
Go to the documentation of this file.
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/xml/sax/SAXException.hpp>
21 #include <com/sun/star/xml/sax/XAttributeList.hpp>
22 #include <com/sun/star/beans/XPropertySetInfo.hpp>
23 #include <xmloff/namespacemap.hxx>
24 #include <xmloff/xmltoken.hxx>
25 #include <xmloff/xmlnamespace.hxx>
26 
27 #include "TransformerBase.hxx"
28 #include "MutableAttrList.hxx"
29 
30 #include "DocumentTContext.hxx"
31 
32 
33 using namespace ::xmloff::token;
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::xml::sax;
36 using namespace ::com::sun::star::beans;
37 
39  const OUString& rQName ) :
40  XMLTransformerContext( rImp, rQName )
41 {
42 }
43 
44 void XMLDocumentTransformerContext::StartElement( const Reference< XAttributeList >& rAttrList )
45 {
46  Reference< XAttributeList > xAttrList( rAttrList );
47 
48  bool bMimeFound = false;
49  OUString aClass;
50  OUString aClassQName(
51  GetTransformer().GetNamespaceMap().GetQNameByKey(
53 
54  XMLMutableAttributeList *pMutableAttrList = nullptr;
55  sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
56  for( sal_Int16 i=0; i < nAttrCount; i++ )
57  {
58  const OUString& rAttrName = xAttrList->getNameByIndex( i );
59  OUString aLocalName;
60  sal_uInt16 nPrefix =
62  &aLocalName );
63  if( XML_NAMESPACE_OFFICE == nPrefix &&
64  IsXMLToken( aLocalName, XML_MIMETYPE ) )
65  {
66  const OUString& rValue = xAttrList->getValueByIndex( i );
67  static const char * aTmp[] =
68  {
69  "application/vnd.oasis.openoffice.",
70  "application/x-vnd.oasis.openoffice.",
71  "application/vnd.oasis.opendocument.",
72  "application/x-vnd.oasis.document.",
73  nullptr
74  };
75  for (int k=0; aTmp[k]; k++)
76  {
77  OUString sTmpString = OUString::createFromAscii(aTmp[k]);
78  if( rValue.matchAsciiL( aTmp[k], sTmpString.getLength() ) )
79  {
80  aClass = rValue.copy( sTmpString.getLength() );
81  break;
82  }
83  }
84 
85  if( !pMutableAttrList )
86  {
87  pMutableAttrList = new XMLMutableAttributeList( xAttrList );
88  xAttrList = pMutableAttrList;
89  }
90  pMutableAttrList->SetValueByIndex( i, aClass );
91  pMutableAttrList->RenameAttributeByIndex(i, aClassQName );
92  bMimeFound = true;
93  break;
94  }
95  }
96 
97  if( !bMimeFound )
98  {
99  const Reference< XPropertySet > rPropSet =
101 
102  if( rPropSet.is() )
103  {
104  Reference< XPropertySetInfo > xPropSetInfo(
105  rPropSet->getPropertySetInfo() );
106  OUString aPropName("Class");
107  if( xPropSetInfo.is() && xPropSetInfo->hasPropertyByName( aPropName ) )
108  {
109  Any aAny = rPropSet->getPropertyValue( aPropName );
110  aAny >>= aClass;
111  }
112  }
113 
114  if( !aClass.isEmpty() )
115  {
116  if( !pMutableAttrList )
117  {
118  pMutableAttrList = new XMLMutableAttributeList( xAttrList );
119  xAttrList = pMutableAttrList;
120  }
121 
122  pMutableAttrList->AddAttribute( aClassQName, aClass );
123  }
124  }
126 }
127 
128 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3434
constexpr sal_uInt16 XML_NAMESPACE_OFFICE
const css::uno::Reference< css::beans::XPropertySet > & GetPropertySet() const
sal_uInt16 GetKeyByAttrName(const OUString &rAttrName, OUString *pPrefix, OUString *pLocalName, OUString *pNamespace) const
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
SvXMLNamespaceMap & GetNamespaceMap()
XMLDocumentTransformerContext(XMLTransformerBase &rTransformer, const OUString &rQName)
virtual sal_Int16 SAL_CALL getLength() override
int i
void SetValueByIndex(sal_Int16 i, const OUString &rValue)
void AddAttribute(const OUString &sName, const OUString &sValue)
void RenameAttributeByIndex(sal_Int16 i, const OUString &rNewName)
virtual void StartElement(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList)
XMLTransformerBase & GetTransformer()
virtual void StartElement(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3378
Handling of tokens in XML: