merge the formfield patch from ooo-build
[ooovba.git] / xmlsecurity / source / helper / xmlsignaturehelper2.cxx
blob9ae20706dd0665aa738beb16fd86883aa4818d11
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlsignaturehelper2.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmlsecurity.hxx"
34 #include <xmlsecurity/xmlsignaturehelper.hxx>
35 #include <xmlsignaturehelper2.hxx>
37 #include <unotools/streamhelper.hxx>
39 #include <com/sun/star/embed/XStorage.hpp>
40 #include <com/sun/star/embed/XStorageRawAccess.hpp>
41 #include <com/sun/star/embed/ElementModes.hpp>
42 #include <com/sun/star/beans/XPropertySet.hpp>
43 #include "rtl/uri.hxx"
45 using namespace com::sun::star;
47 ImplXMLSignatureListener::ImplXMLSignatureListener( const Link& rCreationResultListenerListener, const Link rVerifyResultListenerListener, const Link rStartSignatureElement )
49 maCreationResultListenerListener = rCreationResultListenerListener;
50 maVerifyResultListenerListener = rVerifyResultListenerListener;
51 maStartVerifySignatureElementListener = rStartSignatureElement;
54 ImplXMLSignatureListener::~ImplXMLSignatureListener()
58 void ImplXMLSignatureListener::setNextHandler(
59 uno::Reference< xml::sax::XDocumentHandler > xNextHandler)
61 m_xNextHandler = xNextHandler;
64 void SAL_CALL ImplXMLSignatureListener::signatureCreated( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
65 throw (com::sun::star::uno::RuntimeException)
67 XMLSignatureCreationResult aResult( securityId, nResult );
68 maCreationResultListenerListener.Call( &aResult );
71 void SAL_CALL ImplXMLSignatureListener::signatureVerified( sal_Int32 securityId, com::sun::star::xml::crypto::SecurityOperationStatus nResult )
72 throw (com::sun::star::uno::RuntimeException)
74 XMLSignatureVerifyResult aResult( securityId, nResult );
75 maVerifyResultListenerListener.Call( &aResult );
78 // ---------------------------------------------------------------------------------
79 // XDocumentHandler
80 // ---------------------------------------------------------------------------------
81 void SAL_CALL ImplXMLSignatureListener::startDocument( )
82 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
84 if (m_xNextHandler.is())
86 m_xNextHandler->startDocument();
90 void SAL_CALL ImplXMLSignatureListener::endDocument( )
91 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
93 if (m_xNextHandler.is())
95 m_xNextHandler->endDocument();
99 void SAL_CALL ImplXMLSignatureListener::startElement( const rtl::OUString& aName, const com::sun::star::uno::Reference< com::sun::star::xml::sax::XAttributeList >& xAttribs )
100 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
102 if ( aName == rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Signature")) )
104 maStartVerifySignatureElementListener.Call( (void*)&xAttribs );
107 if (m_xNextHandler.is())
109 m_xNextHandler->startElement( aName, xAttribs );
113 void SAL_CALL ImplXMLSignatureListener::endElement( const rtl::OUString& aName )
114 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
116 if (m_xNextHandler.is())
118 m_xNextHandler->endElement( aName );
122 void SAL_CALL ImplXMLSignatureListener::characters( const rtl::OUString& aChars )
123 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
125 if (m_xNextHandler.is())
127 m_xNextHandler->characters( aChars );
131 void SAL_CALL ImplXMLSignatureListener::ignorableWhitespace( const rtl::OUString& aWhitespaces )
132 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
134 if (m_xNextHandler.is())
136 m_xNextHandler->ignorableWhitespace( aWhitespaces );
140 void SAL_CALL ImplXMLSignatureListener::processingInstruction( const rtl::OUString& aTarget, const rtl::OUString& aData )
141 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
143 if (m_xNextHandler.is())
145 m_xNextHandler->processingInstruction( aTarget, aData );
149 void SAL_CALL ImplXMLSignatureListener::setDocumentLocator( const com::sun::star::uno::Reference< com::sun::star::xml::sax::XLocator >& xLocator )
150 throw (com::sun::star::xml::sax::SAXException, com::sun::star::uno::RuntimeException)
152 if (m_xNextHandler.is())
154 m_xNextHandler->setDocumentLocator( xLocator );
158 // ---------------------------------------------------------------------------------
159 // XUriBinding
160 // ---------------------------------------------------------------------------------
162 UriBindingHelper::UriBindingHelper()
166 UriBindingHelper::UriBindingHelper( const com::sun::star::uno::Reference < com::sun::star::embed::XStorage >& rxStorage )
168 mxStorage = rxStorage;
172 void SAL_CALL UriBindingHelper::setUriBinding( const rtl::OUString& /*uri*/, const uno::Reference< io::XInputStream >&)
173 throw (uno::Exception, uno::RuntimeException)
177 uno::Reference< io::XInputStream > SAL_CALL UriBindingHelper::getUriBinding( const rtl::OUString& uri )
178 throw (uno::Exception, uno::RuntimeException)
180 uno::Reference< io::XInputStream > xInputStream;
181 if ( mxStorage.is() )
183 xInputStream = OpenInputStream( mxStorage, uri );
185 else
187 SvFileStream* pStream = new SvFileStream( uri, STREAM_READ );
188 pStream->Seek( STREAM_SEEK_TO_END );
189 ULONG nBytes = pStream->Tell();
190 pStream->Seek( STREAM_SEEK_TO_BEGIN );
191 SvLockBytesRef xLockBytes = new SvLockBytes( pStream, TRUE );
192 xInputStream = new utl::OInputStreamHelper( xLockBytes, nBytes );
194 return xInputStream;
197 uno::Reference < io::XInputStream > UriBindingHelper::OpenInputStream( const uno::Reference < embed::XStorage >& rxStore, const rtl::OUString& rURI )
199 OSL_ASSERT(rURI.getLength());
200 uno::Reference < io::XInputStream > xInStream;
202 sal_Int32 nSepPos = rURI.indexOf( '/' );
203 if ( nSepPos == -1 )
205 // Cloning because of I can't keep all storage references open
206 // MBA with think about a better API...
207 const ::rtl::OUString sName = ::rtl::Uri::decode(
208 rURI, rtl_UriDecodeStrict, rtl_UriCharClassRelSegment);
209 if (sName.getLength() == 0 && rURI.getLength() != 0)
210 throw uno::Exception(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
211 "Could not decode URI for stream element.")), 0);
213 uno::Reference< io::XStream > xStream;
214 xStream = rxStore->cloneStreamElement( sName );
215 if ( !xStream.is() )
216 throw uno::RuntimeException();
217 xInStream = xStream->getInputStream();
219 else
221 const rtl::OUString aStoreName = ::rtl::Uri::decode(
222 rURI.copy( 0, nSepPos ), rtl_UriDecodeStrict, rtl_UriCharClassRelSegment);
223 if (aStoreName.getLength() == 0 && rURI.getLength() != 0)
224 throw uno::Exception(
225 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
226 "Could not decode URI for stream element.")), 0);
228 rtl::OUString aElement = rURI.copy( nSepPos+1 );
229 uno::Reference < embed::XStorage > xSubStore = rxStore->openStorageElement( aStoreName, embed::ElementModes::READ );
230 xInStream = OpenInputStream( xSubStore, aElement );
232 return xInStream;