LibreOffice Module xmloff (master)  1
xmlictxt.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/XAttributeList.hpp>
21 #include <xmloff/xmlimp.hxx>
22 #include <xmloff/xmlictxt.hxx>
23 #include <sax/fastattribs.hxx>
26 
27 using namespace ::com::sun::star;
28 
30  const OUString& rLName )
31  : mrImport(rImp)
32  , maLocalName(rLName)
33  , m_nRefCount(0)
34  , mnPrefix(nPrfx)
35  , mbPrefixAndLocalNameFilledIn(true)
36 {
37 }
38 
40  : mrImport(rImp)
41  , m_nRefCount(0)
42  , mnPrefix(0)
43  , mbPrefixAndLocalNameFilledIn(false)
44 {
45 }
46 
48 {
49 }
50 
52  const OUString& /*rLocalName*/,
53  const uno::Reference<xml::sax::XAttributeList>& )
54 {
55  return nullptr;
56 }
57 
58 void SvXMLImportContext::StartElement( const uno::Reference< xml::sax::XAttributeList >& )
59 {
60 }
61 
63 {
64 }
65 
66 void SvXMLImportContext::Characters( const OUString& )
67 {
68 }
69 
70 // css::xml::sax::XFastContextHandler:
71 void SAL_CALL SvXMLImportContext::startFastElement(sal_Int32 nElement, const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
72 {
73  mrImport.isFastContext = false;
74  const OUString& rPrefix = SvXMLImport::getNamespacePrefixFromToken(nElement, &GetImport().GetNamespaceMap());
75  const OUString& rLocalName = SvXMLImport::getNameFromToken( nElement );
76  startUnknownElement( SvXMLImport::aDefaultNamespace, (rPrefix.isEmpty())? rLocalName : rPrefix + SvXMLImport::aNamespaceSeparator + rLocalName, Attribs );
77 }
78 
79 void SAL_CALL SvXMLImportContext::startUnknownElement(const OUString & /*rNamespace*/, const OUString & rElementName,
80  const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
81 {
82  if ( mrImport.maAttrList.is() )
83  mrImport.maAttrList->Clear();
84  else
86 
87  mrImport.maNamespaceHandler->addNSDeclAttributes( mrImport.maAttrList );
88 
89  if ( Attribs.is() )
90  {
91  for( auto &it : sax_fastparser::castToFastAttributeList( Attribs ) )
92  {
93  sal_Int32 nToken = it.getToken();
94  const OUString& rAttrNamespacePrefix = SvXMLImport::getNamespacePrefixFromToken(nToken, &GetImport().GetNamespaceMap());
95  OUString sAttrName = SvXMLImport::getNameFromToken( nToken );
96  if ( !rAttrNamespacePrefix.isEmpty() )
97  sAttrName = rAttrNamespacePrefix + SvXMLImport::aNamespaceSeparator + sAttrName;
98 
99  mrImport.maAttrList->AddAttribute( sAttrName, "CDATA", it.toString() );
100  }
101 
102  const uno::Sequence< xml::Attribute > unknownAttribs = Attribs->getUnknownAttributes();
103  for ( const auto& rUnknownAttrib : unknownAttribs )
104  {
105  const OUString& rAttrValue = rUnknownAttrib.Value;
106  const OUString& rAttrName = rUnknownAttrib.Name;
107  // note: rAttrName is expected to be namespace-prefixed here
108  mrImport.maAttrList->AddAttribute( rAttrName, "CDATA", rAttrValue );
109  }
110  }
111  mrImport.startElement( rElementName, mrImport.maAttrList.get() );
112 }
113 
114 void SAL_CALL SvXMLImportContext::endFastElement(sal_Int32 nElement)
115 {
116  mrImport.isFastContext = false;
117  const OUString& rPrefix = SvXMLImport::getNamespacePrefixFromToken(nElement, &GetImport().GetNamespaceMap());
118  const OUString& rLocalName = SvXMLImport::getNameFromToken( nElement );
119  endUnknownElement( SvXMLImport::aDefaultNamespace, (rPrefix.isEmpty())? rLocalName : rPrefix + SvXMLImport::aNamespaceSeparator + rLocalName );
120 }
121 
122 void SAL_CALL SvXMLImportContext::endUnknownElement (const OUString & /*rNamespace*/, const OUString & rElementName)
123 {
124  mrImport.endElement( rElementName );
125 }
126 
127 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL SvXMLImportContext::createFastChildContext
128  (sal_Int32 Element, const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
129 {
130  // Call CreateFastContext only if it's the first element of the document
131  if ( mrImport.maFastContexts.empty() )
132  return mrImport.CreateFastContext( Element, Attribs );
133  return nullptr;
134 }
135 
136 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL SvXMLImportContext::createUnknownChildContext
137  (const OUString &, const OUString &, const uno::Reference< xml::sax::XFastAttributeList > &)
138 {
139  return this;
140 }
141 
142 void SAL_CALL SvXMLImportContext::characters(const OUString &rChars)
143 {
144  mrImport.Characters( rChars );
145 }
146 
147 // XInterface
148 css::uno::Any SAL_CALL SvXMLImportContext::queryInterface( const css::uno::Type& aType )
149 {
150  css::uno::Any a = ::cppu::queryInterface(
151  aType,
152  static_cast< XFastContextHandler* >(this),
153  static_cast< XTypeProvider* >(this),
154  static_cast< css::uno::XInterface* >(static_cast< XFastContextHandler* >(this)));
155 
156  return a;
157 }
158 
159 // XTypeProvider
160 css::uno::Sequence< css::uno::Type > SAL_CALL SvXMLImportContext::getTypes()
161 {
164 }
165 
166 css::uno::Sequence< sal_Int8 > SAL_CALL SvXMLImportContext::getImplementationId()
167 {
168  return css::uno::Sequence<sal_Int8>();
169 }
170 
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void Characters(const OUString &aChars)
Definition: xmlimp.cxx:827
SvXMLImport & mrImport
Definition: xmlictxt.hxx:50
virtual void SAL_CALL endUnknownElement(const OUString &Namespace, const OUString &Name) override
Definition: xmlictxt.cxx:122
virtual void EndElement()
EndElement is called before a context will be destructed, but after an elements context has been pars...
Definition: xmlictxt.cxx:62
virtual void SAL_CALL startFastElement(sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
Definition: xmlictxt.cxx:71
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
virtual void SAL_CALL endElement(const OUString &aName) override
Definition: xmlimp.cxx:768
virtual void StartElement(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList)
StartElement is called after a context has been constructed and before an elements context is parsed...
Definition: xmlictxt.cxx:58
virtual void Characters(const OUString &rChars)
This method is called for all characters that are contained in the current element.
Definition: xmlictxt.cxx:66
virtual void SAL_CALL startUnknownElement(const OUString &Namespace, const OUString &Name, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
Definition: xmlictxt.cxx:79
virtual ~SvXMLImportContext()
A contexts destructor does anything that is required if an element ends.
Definition: xmlictxt.cxx:47
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &aType) final override
Definition: xmlictxt.cxx:148
virtual void SAL_CALL startElement(const OUString &aName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttribs) override
Definition: xmlimp.cxx:720
uno_Any a
rtl::Reference< SvXMLImportFastNamespaceHandler > maNamespaceHandler
Definition: xmlimp.hxx:232
virtual css::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() final override
Definition: xmlictxt.cxx:166
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() final override
Definition: xmlictxt.cxx:160
bool isFastContext
Definition: xmlimp.hxx:230
static const OUStringLiteral aDefaultNamespace
Definition: xmlimp.hxx:538
static OUString getNamespacePrefixFromToken(sal_Int32 nToken, const SvXMLNamespaceMap *pMap)
Definition: xmlimp.cxx:2037
css::uno::Type const & get()
rtl::Reference< comphelper::AttributeList > maAttrList
Definition: xmlimp.hxx:233
SvXMLImportContext(SvXMLImport &rImport, sal_uInt16 nPrfx, const OUString &rLName)
A contexts constructor does anything that is required if an element starts.
Definition: xmlictxt.cxx:29
static const OUString & getNameFromToken(sal_Int32 nToken)
Definition: xmlimp.cxx:2022
DefTokenId nToken
virtual void SAL_CALL endFastElement(sal_Int32 Element) override
Definition: xmlictxt.cxx:114
virtual css::uno::Reference< XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
Definition: xmlictxt.cxx:128
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList)
Create a children element context.
Definition: xmlictxt.cxx:51
static const OUStringLiteral aNamespaceSeparator
Definition: xmlimp.hxx:539
virtual void SAL_CALL characters(const OUString &aChars) override
Definition: xmlictxt.cxx:142
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createUnknownChildContext(const OUString &Namespace, const OUString &Name, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
Definition: xmlictxt.cxx:137