LibreOffice Module sw (master)  1
xmlbrsh.cxx
Go to the documentation of this file.
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 #include <hintids.hxx>
21 #include <editeng/memberids.h>
22 
23 #include <xmloff/nmspmap.hxx>
24 #include <xmloff/xmlnmspe.hxx>
25 #include <xmloff/xmlimp.hxx>
26 #include <xmloff/xmltkmap.hxx>
28 #include <vcl/GraphicObject.hxx>
29 #include <svx/unomid.hxx>
30 #include <editeng/brushitem.hxx>
31 #include <xmloff/xmluconv.hxx>
32 
33 #include "xmlbrshi.hxx"
34 #include "xmlbrshe.hxx"
35 #include "xmlexp.hxx"
36 #include "xmlimpit.hxx"
37 #include "xmlexpit.hxx"
38 
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::uno;
41 using namespace ::xmloff::token;
42 
44 {
52 };
53 
55 {
56  { XML_NAMESPACE_XLINK, XML_HREF, XML_TOK_BGIMG_HREF },
57  { XML_NAMESPACE_XLINK, XML_TYPE, XML_TOK_BGIMG_TYPE },
58  { XML_NAMESPACE_XLINK, XML_ACTUATE, XML_TOK_BGIMG_ACTUATE },
59  { XML_NAMESPACE_XLINK, XML_SHOW, XML_TOK_BGIMG_SHOW },
60  { XML_NAMESPACE_STYLE, XML_POSITION, XML_TOK_BGIMG_POSITION },
61  { XML_NAMESPACE_STYLE, XML_REPEAT, XML_TOK_BGIMG_REPEAT },
62  { XML_NAMESPACE_STYLE, XML_FILTER_NAME, XML_TOK_BGIMG_FILTER },
64 };
65 
66 
68  const uno::Reference< xml::sax::XAttributeList >& xAttrList,
69  const SvXMLUnitConverter& rUnitConv )
70 {
71  SvXMLTokenMap aTokenMap( aBGImgAttributesAttrTokenMap );
72 
73  sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
74  for( sal_Int16 i=0; i < nAttrCount; i++ )
75  {
76  const OUString& rAttrName = xAttrList->getNameByIndex( i );
77  OUString aLocalName;
78  const sal_uInt16 nPrefix =
80  &aLocalName );
81  const OUString& rValue = xAttrList->getValueByIndex( i );
82 
83  switch( aTokenMap.Get( nPrefix, aLocalName ) )
84  {
85  case XML_TOK_BGIMG_HREF:
87  break;
88  case XML_TOK_BGIMG_TYPE:
90  case XML_TOK_BGIMG_SHOW:
91  break;
94  *pItem, rValue, MID_GRAPHIC_POSITION, rUnitConv );
95  break;
98  *pItem, rValue, MID_GRAPHIC_REPEAT, rUnitConv );
99  break;
102  *pItem, rValue, MID_GRAPHIC_FILTER, rUnitConv );
103  break;
104  }
105  }
106 
107 }
108 
110  sal_uInt16 nPrefix, const OUString& rLocalName,
111  const uno::Reference< xml::sax::XAttributeList > & xAttrList )
112 {
113  SvXMLImportContext *pContext = nullptr;
115  {
116  if (!m_xBase64Stream.is())
117  {
119  if (m_xBase64Stream.is())
120  pContext = new XMLBase64ImportContext(GetImport(), nPrefix, rLocalName, xAttrList, m_xBase64Stream);
121  }
122  }
123  if (!pContext)
124  {
125  pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
126  }
127 
128  return pContext;
129 }
130 
132 {
133  if (m_xBase64Stream.is())
134  {
136  m_xBase64Stream = nullptr;
137  }
138 
139  if (m_xGraphic.is())
140  {
141  Graphic aGraphic(m_xGraphic);
142  SvxGraphicPosition eOldGraphicPos = pItem->GetGraphicPos();
143  pItem->SetGraphic(aGraphic);
144  if (GPOS_NONE == eOldGraphicPos && GPOS_NONE != pItem->GetGraphicPos())
145  pItem->SetGraphicPos(GPOS_TILED);
146  }
147 
148  if (!(pItem->GetGraphic()))
149  pItem->SetGraphicPos(GPOS_NONE);
150  else if (GPOS_NONE == pItem->GetGraphicPos())
151  pItem->SetGraphicPos(GPOS_TILED);
152 }
153 
155  SvXMLImport& rImport, sal_uInt16 nPrfx,
156  const OUString& rLName,
157  const uno::Reference< xml::sax::XAttributeList >& xAttrList,
158  const SvXMLUnitConverter& rUnitConv,
159  const SvxBrushItem& rItem ) :
160  SvXMLImportContext( rImport, nPrfx, rLName ),
161  pItem( new SvxBrushItem( rItem ) )
162 {
163  // delete any graphic that is existing
164  pItem->SetGraphicPos( GPOS_NONE );
165 
166  ProcessAttrs( xAttrList, rUnitConv );
167 }
168 
170  SvXMLImport& rImport, sal_uInt16 nPrfx,
171  const OUString& rLName,
172  const uno::Reference< xml::sax::XAttributeList > & xAttrList,
173  const SvXMLUnitConverter& rUnitConv,
174  sal_uInt16 nWhich ) :
175  SvXMLImportContext( rImport, nPrfx, rLName ),
176  pItem( new SvxBrushItem( nWhich ) )
177 {
178  ProcessAttrs( xAttrList, rUnitConv );
179 }
180 
182 {
183 }
184 
186  rExport( rExp )
187 {
188 }
189 
191 {
192 }
193 
195 {
197 
198  uno::Reference<graphic::XGraphic> xGraphic;
199 
200  const Graphic* pGraphic = rItem.GetGraphic();
201 
202  if (pGraphic)
203  xGraphic = pGraphic->GetXGraphic();
204 
205  if (xGraphic.is())
206  {
207  OUString sMimeType;
208  OUString sValue = GetExport().AddEmbeddedXGraphic(xGraphic, sMimeType);
209  if (!sValue.isEmpty())
210  {
211  GetExport().AddAttribute( XML_NAMESPACE_XLINK, XML_HREF, sValue );
212  GetExport().AddAttribute( XML_NAMESPACE_XLINK, XML_TYPE, XML_SIMPLE );
213  GetExport().AddAttribute( XML_NAMESPACE_XLINK, XML_ACTUATE, XML_ONLOAD );
214  }
215 
216  const SvXMLUnitConverter& rUnitConv = GetExport().GetTwipUnitConverter();
217  if (SvXMLExportItemMapper::QueryXMLValue(rItem, sValue, MID_GRAPHIC_POSITION, rUnitConv))
218  GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_POSITION, sValue );
219 
220  if (SvXMLExportItemMapper::QueryXMLValue(rItem, sValue, MID_GRAPHIC_REPEAT, rUnitConv))
221  GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_REPEAT, sValue );
222 
223  if (SvXMLExportItemMapper::QueryXMLValue(rItem, sValue, MID_GRAPHIC_FILTER, rUnitConv))
224  GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_FILTER_NAME, sValue );
225  }
226 
227  {
228  SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE, XML_BACKGROUND_IMAGE,
229  true, true );
230  if (xGraphic.is())
231  {
232  // optional office:binary-data
234  }
235  }
236 }
237 
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
css::uno::Reference< css::graphic::XGraphic > loadGraphicFromBase64(css::uno::Reference< css::io::XOutputStream > const &rxOutputStream)
virtual void EndElement() override
Definition: xmlbrsh.cxx:131
GPOS_NONE
css::uno::Reference< css::io::XOutputStream > m_xBase64Stream
Definition: xmlbrshi.hxx:40
void CheckAttrList()
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Definition: xmlbrsh.cxx:109
SvXMLImport & GetImport()
XML_POSITION
SvXMLNamespaceMap & GetNamespaceMap()
SwXMLBrushItemImportContext(SvXMLImport &rImport, sal_uInt16 nPrfx, const OUString &rLName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList, const SvXMLUnitConverter &rUnitConv, const SvxBrushItem &rItem)
css::uno::Reference< css::io::XOutputStream > GetStreamForGraphicObjectURLFromBase64()
SwXMLBrushItemExport(SwXMLExport &rExport)
Definition: xmlbrsh.cxx:185
css::uno::Reference< css::graphic::XGraphic > loadGraphicByURL(OUString const &rURL)
sal_uInt16 GetKeyByAttrName(const OUString &rAttrName, OUString *pPrefix, OUString *pLocalName, OUString *pNamespace) const
css::uno::Reference< css::graphic::XGraphic > GetXGraphic() const
virtual ~SwXMLBrushItemImportContext() override
Definition: xmlbrsh.cxx:181
void AddAttribute(sal_uInt16 nPrefix, const sal_Char *pName, const OUString &rValue)
#define XML_TOKEN_MAP_END
XML_SHOW
GPOS_TILED
css::uno::Reference< css::graphic::XGraphic > m_xGraphic
Definition: xmlbrshi.hxx:41
XML_TYPE
#define MID_GRAPHIC_REPEAT
const Graphic * GetGraphic(OUString const &referer=OUString()) const
XML_REPEAT
OUString AddEmbeddedXGraphic(css::uno::Reference< css::graphic::XGraphic > const &rxGraphic, OUString &rOutMimeType, OUString const &rRequestedName=OUString())
XML_ONLOAD
XML_BACKGROUND_IMAGE
std::unique_ptr< SvxBrushItem > pItem
Definition: xmlbrshi.hxx:43
int i
SvxGraphicPosition
XML_HREF
const SvXMLUnitConverter & GetTwipUnitConverter() const
Definition: xmlexp.hxx:137
static bool QueryXMLValue(const SfxPoolItem &rItem, OUString &rValue, sal_uInt16 nMemberId, const SvXMLUnitConverter &rUnitConverter)
Definition: xmlexpit.cxx:367
sal_uInt16 Get(sal_uInt16 nPrefix, const OUString &rLName) const
void exportXML(const SvxBrushItem &rItem)
Definition: xmlbrsh.cxx:194
#define MID_GRAPHIC_POSITION
static bool PutXMLValue(SfxPoolItem &rItem, const OUString &rValue, sal_uInt16 nMemberId, const SvXMLUnitConverter &rUnitConverter)
This method is called for every item that should be set based upon an XML attribute value...
Definition: xmlimpit.cxx:244
SwXMLExport & GetExport()
Definition: xmlbrshe.hxx:30
XML_ACTUATE
SvXMLImportContext(SvXMLImport &rImport, sal_uInt16 nPrfx, const OUString &rLName)
SvXMLTokenMapAttrs
static const SvXMLTokenMapEntry aBGImgAttributesAttrTokenMap[]
Definition: xmlbrsh.cxx:54
XML_FILTER_NAME
XML_SIMPLE
#define MID_GRAPHIC_FILTER
bool AddEmbeddedXGraphicAsBase64(css::uno::Reference< css::graphic::XGraphic > const &rxGraphic)
void ProcessAttrs(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList, const SvXMLUnitConverter &rUnitConv)
Definition: xmlbrsh.cxx:67