LibreOffice Module framework (master) 1
saxnamespacefilter.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 <sal/config.h>
21
22#include <vector>
23
24#include <com/sun/star/xml/sax/SAXException.hpp>
25
27
29#include <rtl/ref.hxx>
30
31using namespace ::com::sun::star::xml::sax;
32using namespace ::com::sun::star::uno;
33
34namespace framework{
35
36SaxNamespaceFilter::SaxNamespaceFilter( Reference< XDocumentHandler > const & rSax1DocumentHandler ) :
37 xDocumentHandler( rSax1DocumentHandler )
38{
39}
40
42{
43}
44
45// XDocumentHandler
47{
48}
49
51{
52}
53
55 const OUString& rName, const Reference< XAttributeList > &xAttribs )
56{
57 XMLNamespaces aXMLNamespaces;
58 if ( !m_aNamespaceStack.empty() )
59 aXMLNamespaces = m_aNamespaceStack.top();
60
61 rtl::Reference<::comphelper::AttributeList> pNewList = new ::comphelper::AttributeList();
62
63 // examine all namespaces for this level
64 ::std::vector< sal_Int16 > aAttributeIndexes;
65 {
66 for ( sal_Int16 i=0; i< xAttribs->getLength(); i++ )
67 {
68 OUString aName = xAttribs->getNameByIndex( i );
69 if ( aName.startsWith( "xmlns" ) )
70 aXMLNamespaces.addNamespace( aName, xAttribs->getValueByIndex( i ));
71 else
72 aAttributeIndexes.push_back( i );
73 }
74 }
75
76 // current namespaces for this level
77 m_aNamespaceStack.push( aXMLNamespaces );
78
79 try
80 {
81 // apply namespaces to all remaining attributes
82 for (auto const& attributeIndex : aAttributeIndexes)
83 {
84 OUString aAttributeName = xAttribs->getNameByIndex(attributeIndex);
85 OUString aValue = xAttribs->getValueByIndex(attributeIndex);
86 OUString aNamespaceAttributeName = aXMLNamespaces.applyNSToAttributeName( aAttributeName );
87 pNewList->AddAttribute(aNamespaceAttributeName, aValue);
88 }
89 }
90 catch ( SAXException& e )
91 {
92 e.Message = getErrorLineString() + e.Message;
93 throw;
94 }
95
96 OUString aNamespaceElementName;
97
98 try
99 {
100 aNamespaceElementName = aXMLNamespaces.applyNSToElementName( rName );
101 }
102 catch ( SAXException& e )
103 {
104 e.Message = getErrorLineString() + e.Message;
105 throw;
106 }
107
108 xDocumentHandler->startElement( aNamespaceElementName, pNewList );
109}
110
111void SAL_CALL SaxNamespaceFilter::endElement(const OUString& aName)
112{
113 XMLNamespaces& aXMLNamespaces = m_aNamespaceStack.top();
114 OUString aNamespaceElementName;
115
116 try
117 {
118 aNamespaceElementName = aXMLNamespaces.applyNSToElementName( aName );
119 }
120 catch ( SAXException& e )
121 {
122 e.Message = getErrorLineString() + e.Message;
123 throw;
124 }
125
126 xDocumentHandler->endElement( aNamespaceElementName );
127 m_aNamespaceStack.pop();
128}
129
130void SAL_CALL SaxNamespaceFilter::characters(const OUString& aChars)
131{
132 xDocumentHandler->characters( aChars );
133}
134
135void SAL_CALL SaxNamespaceFilter::ignorableWhitespace(const OUString& aWhitespaces)
136{
137 xDocumentHandler->ignorableWhitespace( aWhitespaces );
138}
139
141 const OUString& aTarget, const OUString& aData)
142{
143 xDocumentHandler->processingInstruction( aTarget, aData );
144}
145
147 const Reference< XLocator > &xLocator)
148{
149 m_xLocator = xLocator;
150 xDocumentHandler->setDocumentLocator( xLocator );
151}
152
154{
155 if ( m_xLocator.is() )
156 return "Line: " + OUString::number( m_xLocator->getLineNumber() ) + " - ";
157 else
158 return OUString();
159}
160
161} // namespace
162
163/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void SAL_CALL startElement(const OUString &aName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttribs) override
css::uno::Reference< css::xml::sax::XDocumentHandler > xDocumentHandler
virtual void SAL_CALL endDocument() override
virtual ~SaxNamespaceFilter() override
css::uno::Reference< css::xml::sax::XLocator > m_xLocator
virtual void SAL_CALL processingInstruction(const OUString &aTarget, const OUString &aData) override
virtual void SAL_CALL setDocumentLocator(const css::uno::Reference< css::xml::sax::XLocator > &xLocator) override
virtual void SAL_CALL ignorableWhitespace(const OUString &aWhitespaces) override
virtual void SAL_CALL startDocument() override
SaxNamespaceFilter(css::uno::Reference< css::xml::sax::XDocumentHandler > const &rSax1DocumentHandler)
virtual void SAL_CALL endElement(const OUString &aName) override
virtual void SAL_CALL characters(const OUString &aChars) override
void addNamespace(const OUString &aName, const OUString &aValue)
OUString applyNSToAttributeName(const OUString &) const
OUString applyNSToElementName(const OUString &) const
OUString aName
constexpr OUStringLiteral aData
int i