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 <tools/debug.hxx> 27 #include <com/sun/star/beans/XPropertySet.hpp> 28 #include <com/sun/star/io/XOutputStream.hpp> 29 #include <xmloff/xmlimp.hxx> 30 #include <xmloff/xmltoken.hxx> 31 #include "xmloff/xmlnmspe.hxx" 32 #include <xmloff/nmspmap.hxx> 33 #include <xmloff/XMLBase64ImportContext.hxx> 34 #include "XMLReplacementImageContext.hxx" 35 36 using ::rtl::OUString; 37 using ::com::sun::star::uno::Reference; 38 using ::com::sun::star::uno::makeAny; 39 using namespace ::com::sun::star::xml::sax; 40 using namespace ::com::sun::star::beans; 41 42 TYPEINIT1( XMLReplacementImageContext, SvXMLImportContext ); 43 44 XMLReplacementImageContext::XMLReplacementImageContext( 45 SvXMLImport& rImport, 46 sal_uInt16 nPrfx, const OUString& rLName, 47 const Reference< XAttributeList > & rAttrList, 48 const Reference< XPropertySet > & rPropSet ) : 49 SvXMLImportContext( rImport, nPrfx, rLName ), 50 m_xPropSet( rPropSet ), 51 m_sGraphicURL(RTL_CONSTASCII_USTRINGPARAM("GraphicURL")) 52 { 53 UniReference < XMLTextImportHelper > xTxtImport = 54 GetImport().GetTextImport(); 55 const SvXMLTokenMap& rTokenMap = 56 xTxtImport->GetTextFrameAttrTokenMap(); 57 58 sal_Int16 nAttrCount = rAttrList.is() ? rAttrList->getLength() : 0; 59 for( sal_Int16 i=0; i < nAttrCount; i++ ) 60 { 61 const OUString& rAttrName = rAttrList->getNameByIndex( i ); 62 const OUString& rValue = rAttrList->getValueByIndex( i ); 63 64 OUString aLocalName; 65 sal_uInt16 nPrefix = 66 GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName, 67 &aLocalName ); 68 switch( rTokenMap.Get( nPrefix, aLocalName ) ) 69 { 70 case XML_TOK_TEXT_FRAME_HREF: 71 m_sHRef = rValue; 72 break; 73 } 74 } 75 } 76 77 XMLReplacementImageContext::~XMLReplacementImageContext() 78 { 79 } 80 81 void XMLReplacementImageContext::EndElement() 82 { 83 OSL_ENSURE( m_sHRef.getLength() > 0 || m_xBase64Stream.is(), 84 "neither URL nor base64 image data given" ); 85 UniReference < XMLTextImportHelper > xTxtImport = 86 GetImport().GetTextImport(); 87 OUString sHRef; 88 if( m_sHRef.getLength() ) 89 { 90 sal_Bool bForceLoad = xTxtImport->IsInsertMode() || 91 xTxtImport->IsBlockMode() || 92 xTxtImport->IsStylesOnlyMode() || 93 xTxtImport->IsOrganizerMode(); 94 sHRef = GetImport().ResolveGraphicObjectURL( m_sHRef, !bForceLoad ); 95 } 96 else if( m_xBase64Stream.is() ) 97 { 98 sHRef = GetImport().ResolveGraphicObjectURLFromBase64( m_xBase64Stream ); 99 m_xBase64Stream = 0; 100 } 101 102 Reference < XPropertySetInfo > xPropSetInfo = 103 m_xPropSet->getPropertySetInfo(); 104 if( xPropSetInfo->hasPropertyByName( m_sGraphicURL ) ) 105 m_xPropSet->setPropertyValue( m_sGraphicURL, makeAny( sHRef ) ); 106 } 107 108 SvXMLImportContext *XMLReplacementImageContext::CreateChildContext( 109 sal_uInt16 nPrefix, 110 const OUString& rLocalName, 111 const Reference< XAttributeList > & xAttrList ) 112 { 113 SvXMLImportContext *pContext = 0; 114 115 if( XML_NAMESPACE_OFFICE == nPrefix && 116 IsXMLToken( rLocalName, ::xmloff::token::XML_BINARY_DATA ) && 117 !m_xBase64Stream.is() ) 118 { 119 m_xBase64Stream = GetImport().GetStreamForGraphicObjectURLFromBase64(); 120 if( m_xBase64Stream.is() ) 121 pContext = new XMLBase64ImportContext( GetImport(), nPrefix, 122 rLocalName, xAttrList, 123 m_xBase64Stream ); 124 } 125 126 if( !pContext ) 127 pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName ); 128 129 return pContext; 130 } 131 132 133