xref: /AOO41X/main/sw/source/filter/xml/xmlbrsh.cxx (revision efeef26f81c84063fb0a91bde3856d4a51172d90)
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_sw.hxx"
26 
27 
28 
29 #include "hintids.hxx"
30 #include <tools/debug.hxx>
31 #include <com/sun/star/io/XOutputStream.hpp>
32 #include <editeng/memberids.hrc>
33 
34 #include <xmloff/nmspmap.hxx>
35 #include <xmloff/xmlnmspe.hxx>
36 #include <xmloff/xmlimp.hxx>
37 #include <xmloff/xmltkmap.hxx>
38 #include <xmloff/XMLBase64ImportContext.hxx>
39 #include <svtools/grfmgr.hxx>
40 #include <svx/unomid.hxx>
41 #include <editeng/brshitem.hxx>
42 #include <xmloff/xmluconv.hxx>
43 
44 #include "xmlbrshi.hxx"
45 #include "xmlbrshe.hxx"
46 #include "xmlexp.hxx"
47 #include "xmlimpit.hxx"
48 #include "xmlexpit.hxx"
49 
50 using ::rtl::OUString;
51 using namespace ::com::sun::star;
52 using namespace ::com::sun::star::uno;
53 using namespace ::xmloff::token;
54 
55 enum SvXMLTokenMapAttrs
56 {
57     XML_TOK_BGIMG_HREF,
58     XML_TOK_BGIMG_TYPE,
59     XML_TOK_BGIMG_ACTUATE,
60     XML_TOK_BGIMG_SHOW,
61     XML_TOK_BGIMG_POSITION,
62     XML_TOK_BGIMG_REPEAT,
63     XML_TOK_BGIMG_FILTER,
64     XML_TOK_NGIMG_END=XML_TOK_UNKNOWN
65 };
66 
67 static __FAR_DATA SvXMLTokenMapEntry aBGImgAttributesAttrTokenMap[] =
68 {
69     { XML_NAMESPACE_XLINK, XML_HREF,        XML_TOK_BGIMG_HREF      },
70     { XML_NAMESPACE_XLINK, XML_TYPE,        XML_TOK_BGIMG_TYPE      },
71     { XML_NAMESPACE_XLINK, XML_ACTUATE,     XML_TOK_BGIMG_ACTUATE   },
72     { XML_NAMESPACE_XLINK, XML_SHOW,        XML_TOK_BGIMG_SHOW      },
73     { XML_NAMESPACE_STYLE, XML_POSITION,    XML_TOK_BGIMG_POSITION  },
74     { XML_NAMESPACE_STYLE, XML_REPEAT,      XML_TOK_BGIMG_REPEAT    },
75     { XML_NAMESPACE_STYLE, XML_FILTER_NAME, XML_TOK_BGIMG_FILTER    },
76     XML_TOKEN_MAP_END
77 };
78 
79 TYPEINIT1( SwXMLBrushItemImportContext, SvXMLImportContext );
80 
ProcessAttrs(const uno::Reference<xml::sax::XAttributeList> & xAttrList,const SvXMLUnitConverter & rUnitConv)81 void SwXMLBrushItemImportContext::ProcessAttrs(
82     const uno::Reference< xml::sax::XAttributeList >& xAttrList,
83         const SvXMLUnitConverter& rUnitConv )
84 {
85     SvXMLTokenMap aTokenMap( aBGImgAttributesAttrTokenMap );
86 
87     sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
88     for( sal_Int16 i=0; i < nAttrCount; i++ )
89     {
90         const OUString& rAttrName = xAttrList->getNameByIndex( i );
91         OUString aLocalName;
92         sal_uInt16 nPrefix =
93             GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
94                                                             &aLocalName );
95         const OUString& rValue = xAttrList->getValueByIndex( i );
96 
97         switch( aTokenMap.Get( nPrefix, aLocalName ) )
98         {
99         case XML_TOK_BGIMG_HREF:
100             SvXMLImportItemMapper::PutXMLValue(
101                 *pItem, GetImport().ResolveGraphicObjectURL( rValue,sal_False),
102                 MID_GRAPHIC_LINK, rUnitConv );
103             break;
104         case XML_TOK_BGIMG_TYPE:
105         case XML_TOK_BGIMG_ACTUATE:
106         case XML_TOK_BGIMG_SHOW:
107             break;
108         case XML_TOK_BGIMG_POSITION:
109             SvXMLImportItemMapper::PutXMLValue(
110                 *pItem, rValue, MID_GRAPHIC_POSITION, rUnitConv );
111             break;
112         case XML_TOK_BGIMG_REPEAT:
113             SvXMLImportItemMapper::PutXMLValue(
114                 *pItem, rValue, MID_GRAPHIC_REPEAT, rUnitConv );
115             break;
116         case XML_TOK_BGIMG_FILTER:
117             SvXMLImportItemMapper::PutXMLValue(
118                 *pItem, rValue, MID_GRAPHIC_FILTER, rUnitConv );
119             break;
120         }
121     }
122 
123 }
124 
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLocalName,const uno::Reference<xml::sax::XAttributeList> & xAttrList)125 SvXMLImportContext *SwXMLBrushItemImportContext::CreateChildContext(
126         sal_uInt16 nPrefix, const OUString& rLocalName,
127         const uno::Reference< xml::sax::XAttributeList > & xAttrList )
128 {
129     SvXMLImportContext *pContext = 0;
130     if( xmloff::token::IsXMLToken( rLocalName,
131                                         xmloff::token::XML_BINARY_DATA ) )
132     {
133         if( !xBase64Stream.is() && !pItem->GetGraphicLink() )
134         {
135             const GraphicObject *pGrObj = pItem->GetGraphicObject();
136             if( !pGrObj || GRAPHIC_NONE == pGrObj->GetType() )
137             {
138                 xBase64Stream =
139                     GetImport().GetStreamForGraphicObjectURLFromBase64();
140                 if( xBase64Stream.is() )
141                     pContext = new XMLBase64ImportContext( GetImport(), nPrefix,
142                                                         rLocalName, xAttrList,
143                                                         xBase64Stream );
144             }
145         }
146     }
147     if( !pContext )
148     {
149         pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
150     }
151 
152     return pContext;
153 }
154 
EndElement()155 void SwXMLBrushItemImportContext::EndElement()
156 {
157     if( xBase64Stream.is() )
158     {
159         OUString sURL( GetImport().ResolveGraphicObjectURLFromBase64( xBase64Stream ) );
160         xBase64Stream = 0;
161         SvXMLImportItemMapper::PutXMLValue( *pItem, sURL, MID_GRAPHIC_LINK, GetImport().GetMM100UnitConverter() );
162     }
163 
164     if( !(pItem->GetGraphicLink() || pItem->GetGraphic() ) )
165         pItem->SetGraphicPos( GPOS_NONE );
166     else if( GPOS_NONE == pItem->GetGraphicPos() )
167         pItem->SetGraphicPos( GPOS_TILED );
168 }
169 
SwXMLBrushItemImportContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLName,const uno::Reference<xml::sax::XAttributeList> & xAttrList,const SvXMLUnitConverter & rUnitConv,const SvxBrushItem & rItem)170 SwXMLBrushItemImportContext::SwXMLBrushItemImportContext(
171         SvXMLImport& rImport, sal_uInt16 nPrfx,
172         const OUString& rLName,
173         const uno::Reference< xml::sax::XAttributeList >& xAttrList,
174         const SvXMLUnitConverter& rUnitConv,
175         const SvxBrushItem& rItem ) :
176     SvXMLImportContext( rImport, nPrfx, rLName ),
177     pItem( new SvxBrushItem( rItem ) )
178 {
179     // delete any grephic that is existing
180     pItem->SetGraphicPos( GPOS_NONE );
181 
182     ProcessAttrs( xAttrList, rUnitConv );
183 }
184 
SwXMLBrushItemImportContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const OUString & rLName,const uno::Reference<xml::sax::XAttributeList> & xAttrList,const SvXMLUnitConverter & rUnitConv,sal_uInt16 nWhich)185 SwXMLBrushItemImportContext::SwXMLBrushItemImportContext(
186         SvXMLImport& rImport, sal_uInt16 nPrfx,
187         const OUString& rLName,
188         const uno::Reference< xml::sax::XAttributeList > & xAttrList,
189         const SvXMLUnitConverter& rUnitConv,
190         sal_uInt16 nWhich ) :
191     SvXMLImportContext( rImport, nPrfx, rLName ),
192     pItem( new SvxBrushItem( nWhich ) )
193 {
194     ProcessAttrs( xAttrList, rUnitConv );
195 }
196 
~SwXMLBrushItemImportContext()197 SwXMLBrushItemImportContext::~SwXMLBrushItemImportContext()
198 {
199     delete pItem;
200 }
201 
SwXMLBrushItemExport(SwXMLExport & rExp)202 SwXMLBrushItemExport::SwXMLBrushItemExport( SwXMLExport& rExp ) :
203     rExport( rExp )
204 {
205 }
206 
~SwXMLBrushItemExport()207 SwXMLBrushItemExport::~SwXMLBrushItemExport()
208 {
209 }
210 
211 
exportXML(const SvxBrushItem & rItem)212 void SwXMLBrushItemExport::exportXML( const SvxBrushItem& rItem )
213 {
214     GetExport().CheckAttrList();
215 
216     OUString sValue, sURL;
217     const SvXMLUnitConverter& rUnitConv = GetExport().GetTwipUnitConverter();
218     if( SvXMLExportItemMapper::QueryXMLValue(
219             rItem, sURL, MID_GRAPHIC_LINK, rUnitConv ) )
220     {
221         sValue = GetExport().AddEmbeddedGraphicObject( sURL );
222         if( sValue.getLength() )
223         {
224             GetExport().AddAttribute( XML_NAMESPACE_XLINK, XML_HREF, sValue );
225             GetExport().AddAttribute( XML_NAMESPACE_XLINK, XML_TYPE, XML_SIMPLE );
226     //      AddAttribute( XML_NAMESPACE_XLINK, XML_SHOW, ACP2WS(sXML_embed) );
227             GetExport().AddAttribute( XML_NAMESPACE_XLINK, XML_ACTUATE, XML_ONLOAD );
228         }
229 
230         if( SvXMLExportItemMapper::QueryXMLValue(
231                 rItem, sValue, MID_GRAPHIC_POSITION, rUnitConv ) )
232             GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_POSITION, sValue );
233 
234         if( SvXMLExportItemMapper::QueryXMLValue(
235                 rItem, sValue, MID_GRAPHIC_REPEAT, rUnitConv ) )
236             GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_REPEAT, sValue );
237 
238         if( SvXMLExportItemMapper::QueryXMLValue(
239                 rItem, sValue, MID_GRAPHIC_FILTER, rUnitConv ) )
240             GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_FILTER_NAME, sValue );
241     }
242 
243     {
244         SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE, XML_BACKGROUND_IMAGE,
245                                   sal_True, sal_True );
246         if( sURL.getLength() )
247         {
248             // optional office:binary-data
249             GetExport().AddEmbeddedGraphicObjectAsBase64( sURL );
250         }
251     }
252 }
253 
254 
255