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 <editeng/memberids.h>
21 #include <vcl/graph.hxx>
22 
23 #include <xmloff/nmspmap.hxx>
24 #include <xmloff/xmlnmspe.hxx>
25 #include <xmloff/xmlimp.hxx>
26 #include <xmloff/xmltkmap.hxx>
28 #include <editeng/brushitem.hxx>
29 #include <xmloff/xmluconv.hxx>
30 
31 #include "xmlbrshi.hxx"
32 #include "xmlbrshe.hxx"
33 #include "xmlexp.hxx"
34 #include "xmlimpit.hxx"
35 #include "xmlexpit.hxx"
36 
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::uno;
39 using namespace ::xmloff::token;
40 
41 namespace {
42 
44 {
45  XML_TOK_BGIMG_HREF,
46  XML_TOK_BGIMG_TYPE,
47  XML_TOK_BGIMG_ACTUATE,
48  XML_TOK_BGIMG_SHOW,
49  XML_TOK_BGIMG_POSITION,
50  XML_TOK_BGIMG_REPEAT,
51  XML_TOK_BGIMG_FILTER,
52 };
53 
54 }
55 
57 {
58  { XML_NAMESPACE_XLINK, XML_HREF, XML_TOK_BGIMG_HREF },
59  { XML_NAMESPACE_XLINK, XML_TYPE, XML_TOK_BGIMG_TYPE },
60  { XML_NAMESPACE_XLINK, XML_ACTUATE, XML_TOK_BGIMG_ACTUATE },
61  { XML_NAMESPACE_XLINK, XML_SHOW, XML_TOK_BGIMG_SHOW },
62  { XML_NAMESPACE_STYLE, XML_POSITION, XML_TOK_BGIMG_POSITION },
63  { XML_NAMESPACE_STYLE, XML_REPEAT, XML_TOK_BGIMG_REPEAT },
64  { XML_NAMESPACE_STYLE, XML_FILTER_NAME, XML_TOK_BGIMG_FILTER },
66 };
67 
68 
70  const uno::Reference< xml::sax::XAttributeList >& xAttrList,
71  const SvXMLUnitConverter& rUnitConv )
72 {
73  SvXMLTokenMap aTokenMap( aBGImgAttributesAttrTokenMap );
74 
75  sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
76  for( sal_Int16 i=0; i < nAttrCount; i++ )
77  {
78  const OUString& rAttrName = xAttrList->getNameByIndex( i );
79  OUString aLocalName;
80  const sal_uInt16 nPrefix =
82  &aLocalName );
83  const OUString& rValue = xAttrList->getValueByIndex( i );
84 
85  switch( aTokenMap.Get( nPrefix, aLocalName ) )
86  {
87  case XML_TOK_BGIMG_HREF:
89  break;
90  case XML_TOK_BGIMG_TYPE:
91  case XML_TOK_BGIMG_ACTUATE:
92  case XML_TOK_BGIMG_SHOW:
93  break;
94  case XML_TOK_BGIMG_POSITION:
96  *pItem, rValue, MID_GRAPHIC_POSITION, rUnitConv );
97  break;
98  case XML_TOK_BGIMG_REPEAT:
100  *pItem, rValue, MID_GRAPHIC_REPEAT, rUnitConv );
101  break;
102  case XML_TOK_BGIMG_FILTER:
104  *pItem, rValue, MID_GRAPHIC_FILTER, rUnitConv );
105  break;
106  }
107  }
108 
109 }
110 
112  sal_uInt16 nPrefix, const OUString& rLocalName,
113  const uno::Reference< xml::sax::XAttributeList > & xAttrList )
114 {
115  SvXMLImportContext *pContext = nullptr;
117  {
118  if (!m_xBase64Stream.is())
119  {
121  if (m_xBase64Stream.is())
122  pContext = new XMLBase64ImportContext(GetImport(), nPrefix, rLocalName, xAttrList, m_xBase64Stream);
123  }
124  }
125 
126  return pContext;
127 }
128 
130 {
131  if (m_xBase64Stream.is())
132  {
134  m_xBase64Stream = nullptr;
135  }
136 
137  if (m_xGraphic.is())
138  {
139  Graphic aGraphic(m_xGraphic);
140  SvxGraphicPosition eOldGraphicPos = pItem->GetGraphicPos();
141  pItem->SetGraphic(aGraphic);
142  if (GPOS_NONE == eOldGraphicPos && GPOS_NONE != pItem->GetGraphicPos())
143  pItem->SetGraphicPos(GPOS_TILED);
144  }
145 
146  if (!(pItem->GetGraphic()))
147  pItem->SetGraphicPos(GPOS_NONE);
148  else if (GPOS_NONE == pItem->GetGraphicPos())
149  pItem->SetGraphicPos(GPOS_TILED);
150 }
151 
153  SvXMLImport& rImport, sal_uInt16 nPrfx,
154  const OUString& rLName,
155  const uno::Reference< xml::sax::XAttributeList >& xAttrList,
156  const SvXMLUnitConverter& rUnitConv,
157  const SvxBrushItem& rItem ) :
158  SvXMLImportContext( rImport, nPrfx, rLName ),
159  pItem( new SvxBrushItem( rItem ) )
160 {
161  // delete any graphic that is existing
162  pItem->SetGraphicPos( GPOS_NONE );
163 
164  ProcessAttrs( xAttrList, rUnitConv );
165 }
166 
168  SvXMLImport& rImport, sal_uInt16 nPrfx,
169  const OUString& rLName,
170  const uno::Reference< xml::sax::XAttributeList > & xAttrList,
171  const SvXMLUnitConverter& rUnitConv,
172  sal_uInt16 nWhich ) :
173  SvXMLImportContext( rImport, nPrfx, rLName ),
174  pItem( new SvxBrushItem( nWhich ) )
175 {
176  ProcessAttrs( xAttrList, rUnitConv );
177 }
178 
180 {
181 }
182 
184  rExport( rExp )
185 {
186 }
187 
189 {
190 }
191 
193 {
195 
196  uno::Reference<graphic::XGraphic> xGraphic;
197 
198  const Graphic* pGraphic = rItem.GetGraphic();
199 
200  if (pGraphic)
201  xGraphic = pGraphic->GetXGraphic();
202 
203  if (xGraphic.is())
204  {
205  OUString sMimeType;
206  OUString sValue = GetExport().AddEmbeddedXGraphic(xGraphic, sMimeType);
207  if (!sValue.isEmpty())
208  {
212  }
213 
214  const SvXMLUnitConverter& rUnitConv = GetExport().GetTwipUnitConverter();
215  if (SvXMLExportItemMapper::QueryXMLValue(rItem, sValue, MID_GRAPHIC_POSITION, rUnitConv))
217 
218  if (SvXMLExportItemMapper::QueryXMLValue(rItem, sValue, MID_GRAPHIC_REPEAT, rUnitConv))
220 
221  if (SvXMLExportItemMapper::QueryXMLValue(rItem, sValue, MID_GRAPHIC_FILTER, rUnitConv))
223  }
224 
225  {
227  true, true );
228  if (xGraphic.is())
229  {
230  // optional office:binary-data
232  }
233  }
234 }
235 
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
constexpr sal_uInt16 XML_NAMESPACE_STYLE
css::uno::Reference< css::graphic::XGraphic > loadGraphicFromBase64(css::uno::Reference< css::io::XOutputStream > const &rxOutputStream)
XMLTokenEnum const aTokenMap[]
virtual void EndElement() override
Definition: xmlbrsh.cxx:129
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:111
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)
SwXMLBrushItemExport(SwXMLExport &rExport)
Definition: xmlbrsh.cxx:183
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:179
#define XML_TOKEN_MAP_END
XML_SHOW
css::uno::Any const & rValue
GPOS_TILED
css::uno::Reference< css::graphic::XGraphic > m_xGraphic
Definition: xmlbrshi.hxx:41
XML_TYPE
void AddAttribute(sal_uInt16 nPrefix, const char *pName, const OUString &rValue)
#define MID_GRAPHIC_REPEAT
const Graphic * GetGraphic(OUString const &referer=OUString()) const
XML_REPEAT
css::uno::Reference< css::io::XOutputStream > GetStreamForGraphicObjectURLFromBase64() const
OUString AddEmbeddedXGraphic(css::uno::Reference< css::graphic::XGraphic > const &rxGraphic, OUString &rOutMimeType, OUString const &rRequestedName=OUString())
XML_ONLOAD
XML_BACKGROUND_IMAGE
int i
std::unique_ptr< SvxBrushItem > pItem
Definition: xmlbrshi.hxx:43
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:349
sal_uInt16 Get(sal_uInt16 nPrefix, const OUString &rLName) const
void exportXML(const SvxBrushItem &rItem)
Definition: xmlbrsh.cxx:192
#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:242
SwXMLExport & GetExport()
Definition: xmlbrshe.hxx:30
XML_ACTUATE
SvXMLTokenMapAttrs
constexpr sal_uInt16 XML_NAMESPACE_XLINK
const SvXMLTokenMapEntry aBGImgAttributesAttrTokenMap[]
Definition: xmlbrsh.cxx:56
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:69
css::uno::Any const SvXMLExport & rExport
Reference< XGraphic > xGraphic