Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : /** Attention: stl headers must(!) be included at first. Otherwhise it can make trouble
31 : : with solaris headers ...
32 : : */
33 : : #include <vector>
34 : :
35 : : #include <stdio.h>
36 : :
37 : : #include <xml/saxnamespacefilter.hxx>
38 : :
39 : : #include <comphelper/attributelist.hxx>
40 : :
41 : : #include <vcl/svapp.hxx>
42 : : #include <rtl/logfile.hxx>
43 : :
44 : : using namespace ::com::sun::star::xml::sax;
45 : : using namespace ::com::sun::star::uno;
46 : :
47 : : namespace framework{
48 : :
49 : :
50 : 1553 : SaxNamespaceFilter::SaxNamespaceFilter( Reference< XDocumentHandler >& rSax1DocumentHandler ) :
51 : 1553 : ThreadHelpBase( &Application::GetSolarMutex() ),
52 : : m_xLocator( 0 ),
53 : : xDocumentHandler( rSax1DocumentHandler ),
54 : : m_nDepth( 0 ),
55 : : m_aXMLAttributeNamespace( RTL_CONSTASCII_USTRINGPARAM( "xmlns" )),
56 [ + - ][ + - ]: 1553 : m_aXMLAttributeType( RTL_CONSTASCII_USTRINGPARAM( "CDATA" ))
[ + - ][ + - ]
[ + - ][ + - ]
57 : : {
58 : 1553 : }
59 : :
60 [ + - ]: 1553 : SaxNamespaceFilter::~SaxNamespaceFilter()
61 : : {
62 [ - + ]: 3106 : }
63 : :
64 : : // XDocumentHandler
65 : 1553 : void SAL_CALL SaxNamespaceFilter::startDocument(void)
66 : : throw ( SAXException, RuntimeException )
67 : : {
68 : 1553 : }
69 : :
70 : 1553 : void SAL_CALL SaxNamespaceFilter::endDocument(void)
71 : : throw( SAXException, RuntimeException )
72 : : {
73 : 1553 : }
74 : :
75 : 41584 : void SAL_CALL SaxNamespaceFilter::startElement(
76 : : const rtl::OUString& rName, const Reference< XAttributeList > &xAttribs )
77 : : throw( SAXException, RuntimeException )
78 : : {
79 [ + - ]: 41584 : XMLNamespaces aXMLNamespaces;
80 [ + - ][ + + ]: 41584 : if ( !m_aNamespaceStack.empty() )
81 [ + - ][ + - ]: 41172 : aXMLNamespaces = m_aNamespaceStack.top();
82 : :
83 [ + - ]: 41584 : ::comphelper::AttributeList* pNewList = new ::comphelper::AttributeList();
84 : :
85 : : // examine all namespaces for this level
86 [ + - ]: 41584 : ::std::vector< sal_Int16 > aAttributeIndexes;
87 : : {
88 [ + - ][ + - ]: 87406 : for ( sal_Int16 i=0; i< xAttribs->getLength(); i++ )
[ + + ]
89 : : {
90 [ + - ][ + - ]: 45822 : ::rtl::OUString aName = xAttribs->getNameByIndex( i );
91 [ + + ]: 45822 : if ( aName.compareTo( m_aXMLAttributeNamespace, m_aXMLAttributeNamespace.getLength() ) == 0 )
92 [ + - ][ + - ]: 728 : aXMLNamespaces.addNamespace( aName, xAttribs->getValueByIndex( i ));
[ + - ]
93 : : else
94 [ + - ]: 45094 : aAttributeIndexes.push_back( i );
95 : 45822 : }
96 : : }
97 : :
98 : : // current namespaces for this level
99 [ + - ]: 41584 : m_aNamespaceStack.push( aXMLNamespaces );
100 : :
101 : : try
102 : : {
103 : : // apply namespaces to all remaing attributes
104 [ + - + - ]: 173356 : for ( ::std::vector< sal_Int16 >::const_iterator i(
[ + + ]
105 [ + - ]: 41584 : aAttributeIndexes.begin());
106 : 86678 : i != aAttributeIndexes.end(); ++i )
107 : : {
108 [ + - ][ + - ]: 45094 : ::rtl::OUString aAttributeName = xAttribs->getNameByIndex( *i );
[ + - ]
109 [ + - ][ + - ]: 45094 : ::rtl::OUString aValue = xAttribs->getValueByIndex( *i );
[ + - ]
110 [ + - ]: 45094 : ::rtl::OUString aNamespaceAttributeName = aXMLNamespaces.applyNSToAttributeName( aAttributeName );
111 [ + - ]: 45094 : pNewList->AddAttribute( aNamespaceAttributeName, m_aXMLAttributeType, aValue );
112 : 45094 : }
113 : : }
114 [ # # ]: 0 : catch ( SAXException& e )
115 : : {
116 [ # # ]: 0 : e.Message = ::rtl::OUString( getErrorLineString() + e.Message );
117 : 0 : throw;
118 : : }
119 : :
120 : 41584 : ::rtl::OUString aNamespaceElementName;
121 : :
122 : : try
123 : : {
124 [ + - ]: 41584 : aNamespaceElementName = aXMLNamespaces.applyNSToElementName( rName );
125 : : }
126 [ # # ]: 0 : catch ( SAXException& e )
127 : : {
128 [ # # ]: 0 : e.Message = ::rtl::OUString( getErrorLineString() + e.Message );
129 : 0 : throw;
130 : : }
131 : :
132 [ + - ][ + - ]: 41584 : xDocumentHandler->startElement( aNamespaceElementName, pNewList );
[ + - ][ + - ]
[ + - ]
133 : 41584 : }
134 : :
135 : 41584 : void SAL_CALL SaxNamespaceFilter::endElement(const rtl::OUString& aName)
136 : : throw( SAXException, RuntimeException )
137 : : {
138 [ + - ]: 41584 : XMLNamespaces& aXMLNamespaces = m_aNamespaceStack.top();
139 : 41584 : ::rtl::OUString aNamespaceElementName;
140 : :
141 : : try
142 : : {
143 [ + - ]: 41584 : aNamespaceElementName = aXMLNamespaces.applyNSToElementName( aName );
144 : : }
145 [ # # ]: 0 : catch ( SAXException& e )
146 : : {
147 [ # # ]: 0 : e.Message = ::rtl::OUString( getErrorLineString() + e.Message );
148 : 0 : throw;
149 : : }
150 : :
151 [ + - ][ + - ]: 41584 : xDocumentHandler->endElement( aNamespaceElementName );
152 [ + - ]: 41584 : m_aNamespaceStack.pop();
153 : 41584 : }
154 : :
155 : 95608 : void SAL_CALL SaxNamespaceFilter::characters(const rtl::OUString& aChars)
156 : : throw( SAXException, RuntimeException )
157 : : {
158 : 95608 : xDocumentHandler->characters( aChars );
159 : 95608 : }
160 : :
161 : 0 : void SAL_CALL SaxNamespaceFilter::ignorableWhitespace(const rtl::OUString& aWhitespaces)
162 : : throw( SAXException, RuntimeException )
163 : : {
164 : 0 : xDocumentHandler->ignorableWhitespace( aWhitespaces );
165 : 0 : }
166 : :
167 : 0 : void SAL_CALL SaxNamespaceFilter::processingInstruction(
168 : : const rtl::OUString& aTarget, const rtl::OUString& aData)
169 : : throw( SAXException, RuntimeException )
170 : : {
171 : 0 : xDocumentHandler->processingInstruction( aTarget, aData );
172 : 0 : }
173 : :
174 : 1553 : void SAL_CALL SaxNamespaceFilter::setDocumentLocator(
175 : : const Reference< XLocator > &xLocator)
176 : : throw( SAXException, RuntimeException )
177 : : {
178 : 1553 : m_xLocator = xLocator;
179 : 1553 : xDocumentHandler->setDocumentLocator( xLocator );
180 : 1553 : }
181 : :
182 : 0 : ::rtl::OUString SaxNamespaceFilter::getErrorLineString()
183 : : {
184 : : char buffer[32];
185 : :
186 [ # # ]: 0 : if ( m_xLocator.is() )
187 : : {
188 [ # # ][ # # ]: 0 : snprintf( buffer, sizeof(buffer), "Line: %ld - ", static_cast<long>( m_xLocator->getLineNumber() ));
189 : 0 : return ::rtl::OUString::createFromAscii( buffer );
190 : : }
191 : : else
192 : 0 : return ::rtl::OUString();
193 : : }
194 : :
195 : : } // namespace
196 : :
197 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|