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