1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 #include <tools/debug.hxx>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <com/sun/star/io/XOutputStream.hpp>
33 #include <xmloff/xmlimp.hxx>
34 #include <xmloff/xmltoken.hxx>
35 #include "xmloff/xmlnmspe.hxx"
36 #include <xmloff/nmspmap.hxx>
37 #include <xmloff/XMLBase64ImportContext.hxx>
38 #include "XMLReplacementImageContext.hxx"
39 
40 using ::rtl::OUString;
41 using ::com::sun::star::uno::Reference;
42 using ::com::sun::star::uno::makeAny;
43 using namespace ::com::sun::star::xml::sax;
44 using namespace ::com::sun::star::beans;
45 
46 TYPEINIT1( XMLReplacementImageContext, SvXMLImportContext );
47 
48 XMLReplacementImageContext::XMLReplacementImageContext(
49 		SvXMLImport& rImport,
50 		sal_uInt16 nPrfx, const OUString& rLName,
51 		const Reference< XAttributeList > & rAttrList,
52 		const Reference< XPropertySet > & rPropSet ) :
53 	SvXMLImportContext( rImport, nPrfx, rLName ),
54 	m_xPropSet( rPropSet ),
55 	m_sGraphicURL(RTL_CONSTASCII_USTRINGPARAM("GraphicURL"))
56 {
57 	UniReference < XMLTextImportHelper > xTxtImport =
58 		GetImport().GetTextImport();
59 	const SvXMLTokenMap& rTokenMap =
60 		xTxtImport->GetTextFrameAttrTokenMap();
61 
62 	sal_Int16 nAttrCount = rAttrList.is() ? rAttrList->getLength() : 0;
63 	for( sal_Int16 i=0; i < nAttrCount; i++ )
64 	{
65 		const OUString& rAttrName = rAttrList->getNameByIndex( i );
66 		const OUString& rValue = rAttrList->getValueByIndex( i );
67 
68 		OUString aLocalName;
69 		sal_uInt16 nPrefix =
70 			GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
71 															&aLocalName );
72 		switch( rTokenMap.Get( nPrefix, aLocalName ) )
73 		{
74 		case XML_TOK_TEXT_FRAME_HREF:
75 			m_sHRef = rValue;
76 			break;
77 		}
78 	}
79 }
80 
81 XMLReplacementImageContext::~XMLReplacementImageContext()
82 {
83 }
84 
85 void XMLReplacementImageContext::EndElement()
86 {
87 	OSL_ENSURE( m_sHRef.getLength() > 0 || m_xBase64Stream.is(),
88 				"neither URL nor base64 image data given" );
89 	UniReference < XMLTextImportHelper > xTxtImport =
90 		GetImport().GetTextImport();
91 	OUString sHRef;
92 	if( m_sHRef.getLength() )
93 	{
94 		sal_Bool bForceLoad = xTxtImport->IsInsertMode() ||
95 							  xTxtImport->IsBlockMode() ||
96 							  xTxtImport->IsStylesOnlyMode() ||
97 							  xTxtImport->IsOrganizerMode();
98 		sHRef = GetImport().ResolveGraphicObjectURL( m_sHRef, !bForceLoad );
99 	}
100 	else if( m_xBase64Stream.is() )
101 	{
102 		sHRef = GetImport().ResolveGraphicObjectURLFromBase64( m_xBase64Stream );
103 		m_xBase64Stream = 0;
104 	}
105 
106 	Reference < XPropertySetInfo > xPropSetInfo =
107 		m_xPropSet->getPropertySetInfo();
108 	if( xPropSetInfo->hasPropertyByName( m_sGraphicURL ) )
109 		m_xPropSet->setPropertyValue( m_sGraphicURL, makeAny( sHRef ) );
110 }
111 
112 SvXMLImportContext *XMLReplacementImageContext::CreateChildContext(
113 		sal_uInt16 nPrefix,
114 		const OUString& rLocalName,
115 		const Reference< XAttributeList > & xAttrList )
116 {
117 	SvXMLImportContext *pContext = 0;
118 
119 	if( XML_NAMESPACE_OFFICE == nPrefix &&
120 		IsXMLToken( rLocalName, ::xmloff::token::XML_BINARY_DATA ) &&
121 		!m_xBase64Stream.is() )
122 	{
123 		m_xBase64Stream = GetImport().GetStreamForGraphicObjectURLFromBase64();
124 		if( m_xBase64Stream.is() )
125 			pContext = new XMLBase64ImportContext( GetImport(), nPrefix,
126 													rLocalName, xAttrList,
127 													m_xBase64Stream );
128 	}
129 
130 	if( !pContext )
131 		pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
132 
133 	return pContext;
134 }
135 
136 
137