1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26 #include <xmloff/xmlimp.hxx>
27 #include <xmloff/xmluconv.hxx>
28 #include <com/sun/star/io/XOutputStream.hpp>
29 #include <xmloff/XMLBase64ImportContext.hxx>
30 
31 using ::rtl::OUString;
32 using ::rtl::OUStringBuffer;
33 
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::xml::sax;
36 using namespace ::com::sun::star::io;
37 
38 //-----------------------------------------------------------------------------
39 
40 TYPEINIT1( XMLBase64ImportContext, SvXMLImportContext );
41 
42 
XMLBase64ImportContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLName,const Reference<XAttributeList> &,const Reference<XOutputStream> & rOut)43 XMLBase64ImportContext::XMLBase64ImportContext(
44 		SvXMLImport& rImport, sal_uInt16 nPrfx, const OUString& rLName,
45 		const Reference< XAttributeList >&,
46 		const Reference< XOutputStream >& rOut ) :
47 	SvXMLImportContext( rImport, nPrfx, rLName ),
48 	xOut( rOut )
49 {
50 }
51 
~XMLBase64ImportContext()52 XMLBase64ImportContext::~XMLBase64ImportContext()
53 {
54 }
55 
56 
EndElement()57 void XMLBase64ImportContext::EndElement()
58 {
59 	xOut->closeOutput();
60 }
61 
Characters(const::rtl::OUString & rChars)62 void XMLBase64ImportContext::Characters( const ::rtl::OUString& rChars )
63 {
64 	OUString sTrimmedChars( rChars. trim() );
65 	if( sTrimmedChars.getLength() )
66 	{
67 		OUString sChars;
68 		if( !sBase64CharsLeft.isEmpty() )
69 		{
70 			sChars = sBase64CharsLeft;
71 			sChars += sTrimmedChars;
72 			sBase64CharsLeft = OUString();
73 		}
74 		else
75 		{
76 			sChars = sTrimmedChars;
77 		}
78 		Sequence< sal_Int8 > aBuffer( (sChars.getLength() / 4) * 3 );
79 		sal_Int32 nCharsDecoded =
80 			GetImport().GetMM100UnitConverter().
81 				decodeBase64SomeChars( aBuffer, sChars );
82 		xOut->writeBytes( aBuffer );
83 		if( nCharsDecoded != sChars.getLength() )
84 			sBase64CharsLeft = sChars.copy( nCharsDecoded );
85 	}
86 }
87 
88