Line data Source code
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 :
21 : /** Attention: stl headers must(!) be included at first. Otherwhise it can make trouble
22 : with solaris headers ...
23 : */
24 : #include <vector>
25 :
26 : #include <stdio.h>
27 :
28 : #include <xml/saxnamespacefilter.hxx>
29 :
30 : #include <comphelper/attributelist.hxx>
31 :
32 : #include <vcl/svapp.hxx>
33 : #include <rtl/logfile.hxx>
34 :
35 : using namespace ::com::sun::star::xml::sax;
36 : using namespace ::com::sun::star::uno;
37 :
38 : namespace framework{
39 :
40 :
41 827 : SaxNamespaceFilter::SaxNamespaceFilter( Reference< XDocumentHandler >& rSax1DocumentHandler ) :
42 827 : ThreadHelpBase( &Application::GetSolarMutex() ),
43 : m_xLocator( 0 ),
44 : xDocumentHandler( rSax1DocumentHandler ),
45 : m_nDepth( 0 ),
46 : m_aXMLAttributeNamespace( "xmlns" ),
47 827 : m_aXMLAttributeType( "CDATA" )
48 : {
49 827 : }
50 :
51 1654 : SaxNamespaceFilter::~SaxNamespaceFilter()
52 : {
53 1654 : }
54 :
55 : // XDocumentHandler
56 827 : void SAL_CALL SaxNamespaceFilter::startDocument(void)
57 : throw ( SAXException, RuntimeException )
58 : {
59 827 : }
60 :
61 827 : void SAL_CALL SaxNamespaceFilter::endDocument(void)
62 : throw( SAXException, RuntimeException )
63 : {
64 827 : }
65 :
66 20925 : void SAL_CALL SaxNamespaceFilter::startElement(
67 : const OUString& rName, const Reference< XAttributeList > &xAttribs )
68 : throw( SAXException, RuntimeException )
69 : {
70 20925 : XMLNamespaces aXMLNamespaces;
71 20925 : if ( !m_aNamespaceStack.empty() )
72 20720 : aXMLNamespaces = m_aNamespaceStack.top();
73 :
74 20925 : ::comphelper::AttributeList* pNewList = new ::comphelper::AttributeList();
75 :
76 : // examine all namespaces for this level
77 41850 : ::std::vector< sal_Int16 > aAttributeIndexes;
78 : {
79 41835 : for ( sal_Int16 i=0; i< xAttribs->getLength(); i++ )
80 : {
81 20910 : OUString aName = xAttribs->getNameByIndex( i );
82 20910 : if ( aName.startsWith( m_aXMLAttributeNamespace ) )
83 362 : aXMLNamespaces.addNamespace( aName, xAttribs->getValueByIndex( i ));
84 : else
85 20548 : aAttributeIndexes.push_back( i );
86 20910 : }
87 : }
88 :
89 : // current namespaces for this level
90 20925 : m_aNamespaceStack.push( aXMLNamespaces );
91 :
92 : try
93 : {
94 : // apply namespaces to all remaing attributes
95 124419 : for ( ::std::vector< sal_Int16 >::const_iterator i(
96 20925 : aAttributeIndexes.begin());
97 82946 : i != aAttributeIndexes.end(); ++i )
98 : {
99 20548 : OUString aAttributeName = xAttribs->getNameByIndex( *i );
100 41096 : OUString aValue = xAttribs->getValueByIndex( *i );
101 41096 : OUString aNamespaceAttributeName = aXMLNamespaces.applyNSToAttributeName( aAttributeName );
102 20548 : pNewList->AddAttribute( aNamespaceAttributeName, m_aXMLAttributeType, aValue );
103 20548 : }
104 : }
105 0 : catch ( SAXException& e )
106 : {
107 0 : e.Message = OUString( getErrorLineString() + e.Message );
108 0 : throw;
109 : }
110 :
111 41850 : OUString aNamespaceElementName;
112 :
113 : try
114 : {
115 20925 : aNamespaceElementName = aXMLNamespaces.applyNSToElementName( rName );
116 : }
117 0 : catch ( SAXException& e )
118 : {
119 0 : e.Message = OUString( getErrorLineString() + e.Message );
120 0 : throw;
121 : }
122 :
123 41850 : xDocumentHandler->startElement( aNamespaceElementName, pNewList );
124 20925 : }
125 :
126 20925 : void SAL_CALL SaxNamespaceFilter::endElement(const OUString& aName)
127 : throw( SAXException, RuntimeException )
128 : {
129 20925 : XMLNamespaces& aXMLNamespaces = m_aNamespaceStack.top();
130 20925 : OUString aNamespaceElementName;
131 :
132 : try
133 : {
134 20925 : aNamespaceElementName = aXMLNamespaces.applyNSToElementName( aName );
135 : }
136 0 : catch ( SAXException& e )
137 : {
138 0 : e.Message = OUString( getErrorLineString() + e.Message );
139 0 : throw;
140 : }
141 :
142 20925 : xDocumentHandler->endElement( aNamespaceElementName );
143 20925 : m_aNamespaceStack.pop();
144 20925 : }
145 :
146 47975 : void SAL_CALL SaxNamespaceFilter::characters(const OUString& aChars)
147 : throw( SAXException, RuntimeException )
148 : {
149 47975 : xDocumentHandler->characters( aChars );
150 47975 : }
151 :
152 0 : void SAL_CALL SaxNamespaceFilter::ignorableWhitespace(const OUString& aWhitespaces)
153 : throw( SAXException, RuntimeException )
154 : {
155 0 : xDocumentHandler->ignorableWhitespace( aWhitespaces );
156 0 : }
157 :
158 0 : void SAL_CALL SaxNamespaceFilter::processingInstruction(
159 : const OUString& aTarget, const OUString& aData)
160 : throw( SAXException, RuntimeException )
161 : {
162 0 : xDocumentHandler->processingInstruction( aTarget, aData );
163 0 : }
164 :
165 827 : void SAL_CALL SaxNamespaceFilter::setDocumentLocator(
166 : const Reference< XLocator > &xLocator)
167 : throw( SAXException, RuntimeException )
168 : {
169 827 : m_xLocator = xLocator;
170 827 : xDocumentHandler->setDocumentLocator( xLocator );
171 827 : }
172 :
173 0 : OUString SaxNamespaceFilter::getErrorLineString()
174 : {
175 : char buffer[32];
176 :
177 0 : if ( m_xLocator.is() )
178 : {
179 0 : snprintf( buffer, sizeof(buffer), "Line: %ld - ", static_cast<long>( m_xLocator->getLineNumber() ));
180 0 : return OUString::createFromAscii( buffer );
181 : }
182 : else
183 0 : return OUString();
184 : }
185 :
186 411 : } // namespace
187 :
188 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|