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 836 : SaxNamespaceFilter::SaxNamespaceFilter( Reference< XDocumentHandler >& rSax1DocumentHandler ) :
39 : m_xLocator( 0 ),
40 : xDocumentHandler( rSax1DocumentHandler ),
41 : m_nDepth( 0 ),
42 : m_aXMLAttributeNamespace( "xmlns" ),
43 836 : m_aXMLAttributeType( "CDATA" )
44 : {
45 836 : }
46 :
47 1672 : SaxNamespaceFilter::~SaxNamespaceFilter()
48 : {
49 1672 : }
50 :
51 : // XDocumentHandler
52 836 : void SAL_CALL SaxNamespaceFilter::startDocument()
53 : throw ( SAXException, RuntimeException, std::exception )
54 : {
55 836 : }
56 :
57 836 : void SAL_CALL SaxNamespaceFilter::endDocument()
58 : throw( SAXException, RuntimeException, std::exception )
59 : {
60 836 : }
61 :
62 35065 : void SAL_CALL SaxNamespaceFilter::startElement(
63 : const OUString& rName, const Reference< XAttributeList > &xAttribs )
64 : throw( SAXException, RuntimeException, std::exception )
65 : {
66 35065 : XMLNamespaces aXMLNamespaces;
67 35065 : if ( !m_aNamespaceStack.empty() )
68 34821 : aXMLNamespaces = m_aNamespaceStack.top();
69 :
70 35065 : ::comphelper::AttributeList* pNewList = new ::comphelper::AttributeList();
71 :
72 : // examine all namespaces for this level
73 70130 : ::std::vector< sal_Int16 > aAttributeIndexes;
74 : {
75 68615 : for ( sal_Int16 i=0; i< xAttribs->getLength(); i++ )
76 : {
77 33550 : OUString aName = xAttribs->getNameByIndex( i );
78 33550 : if ( aName.startsWith( m_aXMLAttributeNamespace ) )
79 427 : aXMLNamespaces.addNamespace( aName, xAttribs->getValueByIndex( i ));
80 : else
81 33123 : aAttributeIndexes.push_back( i );
82 33550 : }
83 : }
84 :
85 : // current namespaces for this level
86 35065 : m_aNamespaceStack.push( aXMLNamespaces );
87 :
88 : try
89 : {
90 : // apply namespaces to all remaining attributes
91 204564 : for ( ::std::vector< sal_Int16 >::const_iterator i(
92 35065 : aAttributeIndexes.begin());
93 136376 : i != aAttributeIndexes.end(); ++i )
94 : {
95 33123 : OUString aAttributeName = xAttribs->getNameByIndex( *i );
96 66246 : OUString aValue = xAttribs->getValueByIndex( *i );
97 66246 : OUString aNamespaceAttributeName = aXMLNamespaces.applyNSToAttributeName( aAttributeName );
98 33123 : pNewList->AddAttribute( aNamespaceAttributeName, m_aXMLAttributeType, aValue );
99 33123 : }
100 : }
101 0 : catch ( SAXException& e )
102 : {
103 0 : e.Message = OUString( getErrorLineString() + e.Message );
104 0 : throw;
105 : }
106 :
107 70130 : OUString aNamespaceElementName;
108 :
109 : try
110 : {
111 35065 : 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 70130 : xDocumentHandler->startElement( aNamespaceElementName, pNewList );
120 35065 : }
121 :
122 35065 : void SAL_CALL SaxNamespaceFilter::endElement(const OUString& aName)
123 : throw( SAXException, RuntimeException, std::exception )
124 : {
125 35065 : XMLNamespaces& aXMLNamespaces = m_aNamespaceStack.top();
126 35065 : OUString aNamespaceElementName;
127 :
128 : try
129 : {
130 35065 : 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 35065 : xDocumentHandler->endElement( aNamespaceElementName );
139 35065 : m_aNamespaceStack.pop();
140 35065 : }
141 :
142 80561 : void SAL_CALL SaxNamespaceFilter::characters(const OUString& aChars)
143 : throw( SAXException, RuntimeException, std::exception )
144 : {
145 80561 : xDocumentHandler->characters( aChars );
146 80561 : }
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 836 : void SAL_CALL SaxNamespaceFilter::setDocumentLocator(
162 : const Reference< XLocator > &xLocator)
163 : throw( SAXException, RuntimeException, std::exception )
164 : {
165 836 : m_xLocator = xLocator;
166 836 : xDocumentHandler->setDocumentLocator( xLocator );
167 836 : }
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 : } // namespace
182 :
183 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|