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