1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: saxnamespacefilter.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
34 /** Attention: stl headers must(!) be included at first. Otherwhise it can make trouble
35 with solaris headers ...
41 #include <xml/saxnamespacefilter.hxx>
43 #include <comphelper/attributelist.hxx>
45 #include <vcl/svapp.hxx>
46 #include <rtl/logfile.hxx>
48 using namespace ::com::sun::star::xml::sax
;
49 using namespace ::com::sun::star::uno
;
51 const ::rtl::OUString
aXMLAttributeNamespace( RTL_CONSTASCII_USTRINGPARAM( "xmlns" ));
52 const ::rtl::OUString
aXMLAttributeType( RTL_CONSTASCII_USTRINGPARAM( "CDATA" ));
57 SaxNamespaceFilter::SaxNamespaceFilter( Reference
< XDocumentHandler
>& rSax1DocumentHandler
) :
58 ThreadHelpBase( &Application::GetSolarMutex() ),
60 xDocumentHandler( rSax1DocumentHandler
),
65 SaxNamespaceFilter::~SaxNamespaceFilter()
70 void SAL_CALL
SaxNamespaceFilter::startDocument(void)
71 throw ( SAXException
, RuntimeException
)
75 void SAL_CALL
SaxNamespaceFilter::endDocument(void)
76 throw( SAXException
, RuntimeException
)
80 void SAL_CALL
SaxNamespaceFilter::startElement(
81 const rtl::OUString
& rName
, const Reference
< XAttributeList
> &xAttribs
)
82 throw( SAXException
, RuntimeException
)
84 XMLNamespaces aXMLNamespaces
;
85 if ( !m_aNamespaceStack
.empty() )
86 aXMLNamespaces
= m_aNamespaceStack
.top();
88 ::comphelper::AttributeList
* pNewList
= new ::comphelper::AttributeList();
90 // examine all namespaces for this level
91 ::std::vector
< sal_Int16
> aAttributeIndexes
;
93 for ( sal_Int16 i
=0; i
< xAttribs
->getLength(); i
++ )
95 ::rtl::OUString aName
= xAttribs
->getNameByIndex( i
);
96 if ( aName
.compareTo( aXMLAttributeNamespace
, aXMLAttributeNamespace
.getLength() ) == 0 )
97 aXMLNamespaces
.addNamespace( aName
, xAttribs
->getValueByIndex( i
));
99 aAttributeIndexes
.push_back( i
);
103 // current namespaces for this level
104 m_aNamespaceStack
.push( aXMLNamespaces
);
108 // apply namespaces to all remaing attributes
109 for ( ::std::vector
< sal_Int16
>::const_iterator
i(
110 aAttributeIndexes
.begin());
111 i
!= aAttributeIndexes
.end(); ++i
)
113 ::rtl::OUString aAttributeName
= xAttribs
->getNameByIndex( *i
);
114 ::rtl::OUString aValue
= xAttribs
->getValueByIndex( *i
);
115 ::rtl::OUString aNamespaceAttributeName
= aXMLNamespaces
.applyNSToAttributeName( aAttributeName
);
116 pNewList
->AddAttribute( aNamespaceAttributeName
, aXMLAttributeType
, aValue
);
119 catch ( SAXException
& e
)
121 e
.Message
= ::rtl::OUString( getErrorLineString() + e
.Message
);
125 ::rtl::OUString aNamespaceElementName
;
129 aNamespaceElementName
= aXMLNamespaces
.applyNSToElementName( rName
);
131 catch ( SAXException
& e
)
133 e
.Message
= ::rtl::OUString( getErrorLineString() + e
.Message
);
137 xDocumentHandler
->startElement( aNamespaceElementName
, pNewList
);
140 void SAL_CALL
SaxNamespaceFilter::endElement(const rtl::OUString
& aName
)
141 throw( SAXException
, RuntimeException
)
143 XMLNamespaces
& aXMLNamespaces
= m_aNamespaceStack
.top();
144 ::rtl::OUString aNamespaceElementName
;
148 aNamespaceElementName
= aXMLNamespaces
.applyNSToElementName( aName
);
150 catch ( SAXException
& e
)
152 e
.Message
= ::rtl::OUString( getErrorLineString() + e
.Message
);
156 xDocumentHandler
->endElement( aNamespaceElementName
);
157 m_aNamespaceStack
.pop();
160 void SAL_CALL
SaxNamespaceFilter::characters(const rtl::OUString
& aChars
)
161 throw( SAXException
, RuntimeException
)
163 xDocumentHandler
->characters( aChars
);
166 void SAL_CALL
SaxNamespaceFilter::ignorableWhitespace(const rtl::OUString
& aWhitespaces
)
167 throw( SAXException
, RuntimeException
)
169 xDocumentHandler
->ignorableWhitespace( aWhitespaces
);
172 void SAL_CALL
SaxNamespaceFilter::processingInstruction(
173 const rtl::OUString
& aTarget
, const rtl::OUString
& aData
)
174 throw( SAXException
, RuntimeException
)
176 xDocumentHandler
->processingInstruction( aTarget
, aData
);
179 void SAL_CALL
SaxNamespaceFilter::setDocumentLocator(
180 const Reference
< XLocator
> &xLocator
)
181 throw( SAXException
, RuntimeException
)
183 m_xLocator
= xLocator
;
184 xDocumentHandler
->setDocumentLocator( xLocator
);
187 ::rtl::OUString
SaxNamespaceFilter::getErrorLineString()
191 if ( m_xLocator
.is() )
193 snprintf( buffer
, sizeof(buffer
), "Line: %ld - ", static_cast<long>( m_xLocator
->getLineNumber() ));
194 return ::rtl::OUString::createFromAscii( buffer
);
197 return ::rtl::OUString();