merge the formfield patch from ooo-build
[ooovba.git] / svx / source / xml / xmlxtimp.cxx
blobfb10ef3e8e74f3faec2800d7876fe92d8ac53ca7
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlxtimp.cxx,v $
10 * $Revision: 1.17.264.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
33 #include <tools/debug.hxx>
34 #include <com/sun/star/document/XGraphicObjectResolver.hpp>
35 #include <com/sun/star/embed/ElementModes.hpp>
36 #include <com/sun/star/io/XActiveDataControl.hpp>
37 #include <com/sun/star/io/XActiveDataSource.hpp>
38 #include <com/sun/star/xml/sax/XParser.hpp>
39 #include <com/sun/star/container/XNameContainer.hpp>
40 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
41 #include <com/sun/star/uno/Sequence.hxx>
42 #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
43 #include <com/sun/star/drawing/LineDash.hpp>
44 #include <com/sun/star/awt/Gradient.hpp>
45 #include <com/sun/star/drawing/Hatch.hpp>
46 #include <com/sun/star/io/XOutputStream.hpp>
47 #ifndef _COM_SUN_STAR_IO_XSEEKABLE_HDL_
48 #include <com/sun/star/io/XSeekable.hdl>
49 #endif
50 #include <comphelper/processfactory.hxx>
51 #include <unotools/streamwrap.hxx>
52 #include <rtl/ustrbuf.hxx>
53 #include <sfx2/docfile.hxx>
54 #include <xmloff/xmluconv.hxx>
55 #include "xmloff/xmlnmspe.hxx"
56 #include "xmloff/nmspmap.hxx"
58 #include "xmloff/xmltoken.hxx"
59 #include "xmloff/xmlmetae.hxx"
60 #include "xmloff/DashStyle.hxx"
61 #include "xmloff/GradientStyle.hxx"
62 #include "xmloff/HatchStyle.hxx"
63 #include "xmloff/ImageStyle.hxx"
64 #include "xmloff/MarkerStyle.hxx"
65 #include <xmloff/xmlictxt.hxx>
66 #include "xmlgrhlp.hxx"
67 #include "xmloff/attrlist.hxx"
69 #include "xmlxtimp.hxx"
71 using namespace com::sun::star;
72 using namespace com::sun::star::container;
73 using namespace com::sun::star::document;
74 using namespace com::sun::star::uno;
75 using namespace com::sun::star::awt;
76 using namespace com::sun::star::lang;
77 using namespace com::sun::star::xml::sax;
78 using namespace ::rtl;
79 using namespace ::xmloff::token;
80 using namespace cppu;
82 sal_Char __READONLY_DATA sXML_np__office[] = "__office";
83 sal_Char __READONLY_DATA sXML_np__office_ooo[] = "___office";
84 sal_Char __READONLY_DATA sXML_np__draw[] = "__draw";
85 sal_Char __READONLY_DATA sXML_np__draw_ooo[] = "___draw";
86 sal_Char __READONLY_DATA sXML_np__ooo[] = "__ooo";
87 sal_Char __READONLY_DATA sXML_np__xlink[] = "__xlink";
89 ///////////////////////////////////////////////////////////////////////
91 enum SvxXMLTableImportContextEnum { stice_unknown, stice_color, stice_marker, stice_dash, stice_hatch, stice_gradient, stice_bitmap };
93 ///////////////////////////////////////////////////////////////////////
95 class SvxXMLTableImportContext : public SvXMLImportContext
97 public:
98 SvxXMLTableImportContext( SvXMLImport& rImport, USHORT nPrfx, const OUString& rLName, const uno::Reference< XAttributeList >& xAttrList, SvxXMLTableImportContextEnum eContext, const uno::Reference< XNameContainer >& xTable,
99 sal_Bool bOOoFormat );
100 virtual ~SvxXMLTableImportContext();
102 virtual SvXMLImportContext *CreateChildContext( USHORT nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList );
104 SvxXMLXTableImport& getImport() const { return *(SvxXMLXTableImport*)&GetImport(); }
106 protected:
107 void importColor( USHORT nPrfx, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName );
108 void importMarker( USHORT nPrfx, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName );
109 void importDash( USHORT nPrfx, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName );
110 void importHatch( USHORT nPrfx, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName );
111 void importGradient( USHORT nPrfx, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName );
112 void importBitmap( USHORT nPrfx, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName );
114 private:
115 uno::Reference< XNameContainer > mxTable;
116 SvxXMLTableImportContextEnum meContext;
117 sal_Bool mbOOoFormat;
120 ///////////////////////////////////////////////////////////////////////
122 SvxXMLTableImportContext::SvxXMLTableImportContext( SvXMLImport& rImport, USHORT nPrfx, const OUString& rLName, const uno::Reference< XAttributeList >&, SvxXMLTableImportContextEnum eContext, const uno::Reference< XNameContainer >& xTable, sal_Bool bOOoFormat )
123 : SvXMLImportContext( rImport, nPrfx, rLName ), mxTable( xTable ), meContext( eContext ),
124 mbOOoFormat( bOOoFormat )
128 SvxXMLTableImportContext::~SvxXMLTableImportContext()
132 SvXMLImportContext *SvxXMLTableImportContext::CreateChildContext( USHORT nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& rAttrList )
134 if( XML_NAMESPACE_DRAW == nPrefix )
136 uno::Reference< XAttributeList > xAttrList( rAttrList );
137 if( mbOOoFormat &&
138 (stice_dash == meContext || stice_hatch == meContext ||
139 stice_bitmap == meContext) )
141 SvXMLAttributeList *pAttrList = new SvXMLAttributeList( rAttrList );
142 xAttrList = pAttrList;
143 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
144 for( sal_Int16 i=0; i < nAttrCount; i++ )
146 const OUString& rAttrName = xAttrList->getNameByIndex( i );
147 OUString aLocalName;
148 sal_uInt16 nPrefix_ =
149 GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
150 &aLocalName );
151 if( XML_NAMESPACE_XLINK == nPrefix_ &&
152 stice_bitmap == meContext &&
153 IsXMLToken( aLocalName, XML_HREF ) )
155 const OUString rValue = xAttrList->getValueByIndex( i );
156 if( rValue.getLength() && '#' == rValue[0] )
157 pAttrList->SetValueByIndex( i, rValue.copy( 1 ) );
159 else if( XML_NAMESPACE_DRAW == nPrefix_ &&
160 ( ( stice_dash == meContext &&
161 (IsXMLToken( aLocalName, XML_DOTS1_LENGTH ) ||
162 IsXMLToken( aLocalName, XML_DOTS2_LENGTH ) ||
163 IsXMLToken( aLocalName, XML_DISTANCE )) ) ||
164 ( stice_hatch == meContext &&
165 IsXMLToken( aLocalName, XML_HATCH_DISTANCE ) ) ) )
167 const OUString rValue = xAttrList->getValueByIndex( i );
168 sal_Int32 nPos = rValue.getLength();
169 while( nPos && rValue[nPos-1] <= ' ' )
170 --nPos;
171 if( nPos > 2 &&
172 ('c'==rValue[nPos-2] || 'C'==rValue[nPos-2]) &&
173 ('h'==rValue[nPos-1] || 'H'==rValue[nPos-1]) )
175 pAttrList->SetValueByIndex( i, rValue.copy( 0, nPos-2 ) );
182 Any aAny;
183 OUString aName;
185 switch( meContext )
187 case stice_color:
188 importColor( nPrefix, rLocalName, xAttrList, aAny, aName );
189 break;
190 case stice_marker:
191 importMarker( nPrefix, rLocalName, xAttrList, aAny, aName );
192 break;
193 case stice_dash:
194 importDash( nPrefix, rLocalName, xAttrList, aAny, aName );
195 break;
196 case stice_hatch:
197 importHatch( nPrefix, rLocalName, xAttrList, aAny, aName );
198 break;
199 case stice_gradient:
200 importGradient( nPrefix, rLocalName, xAttrList, aAny, aName );
201 break;
202 case stice_bitmap:
203 importBitmap( nPrefix, rLocalName, xAttrList, aAny, aName );
204 break;
205 case stice_unknown:
206 break;
209 if( aName.getLength() && aAny.hasValue() )
211 if( mxTable->hasByName( aName ) )
213 mxTable->replaceByName( aName, aAny );
215 else
217 mxTable->insertByName( aName, aAny );
221 catch( uno::Exception& )
226 return new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
229 void SvxXMLTableImportContext::importColor( USHORT nPrfx, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName )
231 (void)nPrfx;
232 (void)rLocalName;
234 const sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
235 for( sal_Int16 i=0; i < nAttrCount; i++ )
237 const OUString& rFullAttrName = xAttrList->getNameByIndex( i );
238 OUString aLocalName;
239 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( rFullAttrName, &aLocalName );
242 if( XML_NAMESPACE_DRAW == nPrefix )
244 if( aLocalName == GetXMLToken(XML_NAME) )
246 rName = xAttrList->getValueByIndex( i );
248 else if( aLocalName == GetXMLToken(XML_COLOR) )
250 Color aColor;
251 SvXMLUnitConverter::convertColor(aColor, xAttrList->getValueByIndex( i ));
252 rAny <<= (sal_Int32)aColor.GetColor();
258 void SvxXMLTableImportContext::importMarker( USHORT nPrfx, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName )
260 (void)nPrfx;
261 (void)rLocalName;
265 XMLMarkerStyleImport aMarkerStyle( GetImport() );
266 aMarkerStyle.importXML( xAttrList, rAny, rName );
268 catch( Exception& )
270 DBG_ERROR("SvxXMLTableImportContext::importMarker(), exception caught!");
274 void SvxXMLTableImportContext::importDash( USHORT nPrfx, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName )
276 (void)nPrfx;
277 (void)rLocalName;
281 XMLDashStyleImport aDashStyle( GetImport() );
282 aDashStyle.importXML( xAttrList, rAny, rName );
284 catch( Exception& )
286 DBG_ERROR("SvxXMLTableImportContext::importDash(), exception caught!");
290 void SvxXMLTableImportContext::importHatch( USHORT nPrfx, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName )
292 (void)nPrfx;
293 (void)rLocalName;
297 XMLHatchStyleImport aHatchStyle( GetImport() );
298 aHatchStyle.importXML( xAttrList, rAny, rName );
300 catch( Exception& )
302 DBG_ERROR("SvxXMLTableImportContext::importHatch(), exception caught!");
306 void SvxXMLTableImportContext::importGradient( USHORT nPrfx, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName )
308 (void)nPrfx;
309 (void)rLocalName;
313 XMLGradientStyleImport aGradientStyle( GetImport() );
314 aGradientStyle.importXML( xAttrList, rAny, rName );
316 catch( Exception& )
318 DBG_ERROR("SvxXMLTableImportContext::importGradient(), exception caught!");
322 void SvxXMLTableImportContext::importBitmap( USHORT nPrfx, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList, Any& rAny, OUString& rName )
324 (void)nPrfx;
325 (void)rLocalName;
329 XMLImageStyle aImageStyle;
330 aImageStyle.importXML( xAttrList, rAny, rName, GetImport() );
332 catch( Exception& )
334 DBG_ERROR("SvxXMLTableImportContext::importBitmap(), exception caught!");
338 ///////////////////////////////////////////////////////////////////////
340 // #110680#
341 SvxXMLXTableImport::SvxXMLXTableImport(
342 const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xServiceFactory,
343 const uno::Reference< XNameContainer > & rTable,
344 uno::Reference< XGraphicObjectResolver >& xGrfResolver )
345 : SvXMLImport(xServiceFactory, 0),
346 mrTable( rTable )
348 SetGraphicResolver( xGrfResolver );
350 GetNamespaceMap().Add( OUString( RTL_CONSTASCII_USTRINGPARAM ( sXML_np__ooo ) ), GetXMLToken(XML_N_OOO), XML_NAMESPACE_OOO );
351 GetNamespaceMap().Add( OUString( RTL_CONSTASCII_USTRINGPARAM ( sXML_np__office ) ), GetXMLToken(XML_N_OFFICE), XML_NAMESPACE_OFFICE );
352 GetNamespaceMap().Add( OUString( RTL_CONSTASCII_USTRINGPARAM ( sXML_np__draw ) ), GetXMLToken(XML_N_DRAW), XML_NAMESPACE_DRAW );
353 GetNamespaceMap().Add( OUString( RTL_CONSTASCII_USTRINGPARAM ( sXML_np__xlink ) ), GetXMLToken(XML_N_XLINK), XML_NAMESPACE_XLINK );
355 // OOo namespaces for reading OOo 1.1 files
356 GetNamespaceMap().Add( OUString( RTL_CONSTASCII_USTRINGPARAM ( sXML_np__office_ooo ) ),
357 GetXMLToken(XML_N_OFFICE_OOO),
358 XML_NAMESPACE_OFFICE );
359 GetNamespaceMap().Add( OUString( RTL_CONSTASCII_USTRINGPARAM ( sXML_np__draw_ooo ) ),
360 GetXMLToken(XML_N_DRAW_OOO),
361 XML_NAMESPACE_DRAW );
364 SvxXMLXTableImport::~SvxXMLXTableImport() throw ()
368 sal_Bool SvxXMLXTableImport::load( const OUString& rUrl, const uno::Reference< XNameContainer >& xTable ) throw()
370 sal_Bool bRet = sal_True;
372 uno::Reference< XGraphicObjectResolver > xGrfResolver;
373 SvXMLGraphicHelper* pGraphicHelper = 0;
379 SfxMedium aMedium( rUrl, STREAM_READ | STREAM_NOCREATE, TRUE );
381 uno::Reference<lang::XMultiServiceFactory> xServiceFactory( ::comphelper::getProcessServiceFactory() );
382 if( !xServiceFactory.is() )
384 DBG_ERROR( "SvxXMLXTableImport::load: got no service manager" );
385 break;
388 uno::Reference< xml::sax::XParser > xParser( xServiceFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser" ) ) ), uno::UNO_QUERY_THROW );
389 uno::Reference < io::XStream > xIStm;
390 uno::Reference< io::XActiveDataSource > xSource;
392 xml::sax::InputSource aParserInput;
393 aParserInput.sSystemId = aMedium.GetName();
395 if( aMedium.IsStorage() )
397 uno::Reference < embed::XStorage > xStorage( aMedium.GetStorage( sal_False ), uno::UNO_QUERY_THROW );
399 const String aContentStmName( RTL_CONSTASCII_USTRINGPARAM( "Content.xml" ) );
400 xIStm.set( xStorage->openStreamElement( aContentStmName, embed::ElementModes::READ ), uno::UNO_QUERY_THROW );
401 if( !xIStm.is() )
403 DBG_ERROR( "could not open Content stream" );
404 break;
407 aParserInput.aInputStream = xIStm->getInputStream();
408 pGraphicHelper = SvXMLGraphicHelper::Create( xStorage, GRAPHICHELPER_MODE_READ );
409 xGrfResolver = pGraphicHelper;
411 else
413 aParserInput.aInputStream = aMedium.GetInputStream();
414 uno::Reference< io::XSeekable > xSeek( aParserInput.aInputStream, uno::UNO_QUERY_THROW );
415 xSeek->seek( 0 );
418 if( xSource.is() )
420 uno::Reference< io::XActiveDataControl > xSourceControl( xSource, UNO_QUERY_THROW );
421 xSourceControl->start();
424 // #110680#
425 // uno::Reference< XDocumentHandler > xHandler( new SvxXMLXTableImport( xTable, xGrfResolver ) );
426 uno::Reference< XDocumentHandler > xHandler( new SvxXMLXTableImport( xServiceFactory, xTable, xGrfResolver ) );
428 xParser->setDocumentHandler( xHandler );
429 xParser->parseStream( aParserInput );
431 while(0);
433 if( pGraphicHelper )
434 SvXMLGraphicHelper::Destroy( pGraphicHelper );
436 catch( uno::Exception& )
438 // CL: I disabled this assertion since its an error, but it happens
439 // each time you load a document with property tables that are not
440 // on the current machine. Maybe a better fix would be to place
441 // a file exists check before importing...
442 // DBG_ERROR("svx::SvxXMLXTableImport::load(), exception caught!");
443 bRet = sal_False;
446 return bRet;
449 SvXMLImportContext *SvxXMLXTableImport::CreateContext( sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference< XAttributeList >& xAttrList )
451 if( XML_NAMESPACE_OOO == nPrefix ||
452 XML_NAMESPACE_OFFICE == nPrefix )
454 sal_Bool bOOoFormat = (XML_NAMESPACE_OFFICE == nPrefix);
455 Type aType = mrTable->getElementType();
457 if( rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "color-table" ) ) )
459 if( aType == ::getCppuType((const sal_Int32*)0) )
460 return new SvxXMLTableImportContext( *this, nPrefix, rLocalName, xAttrList, stice_color, mrTable, bOOoFormat );
462 else if( rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "marker-table" ) ) )
464 if( aType == ::getCppuType((const drawing::PolyPolygonBezierCoords*)0) )
465 return new SvxXMLTableImportContext( *this, nPrefix, rLocalName, xAttrList, stice_marker, mrTable, bOOoFormat );
467 else if( rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "dash-table" ) ) )
469 if( aType == ::getCppuType((const drawing::LineDash*)0) )
470 return new SvxXMLTableImportContext( *this, nPrefix, rLocalName, xAttrList, stice_dash, mrTable, bOOoFormat );
472 else if( rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "hatch-table" ) ) )
474 if( aType == ::getCppuType((const drawing::Hatch*)0) )
475 return new SvxXMLTableImportContext( *this, nPrefix, rLocalName, xAttrList, stice_hatch, mrTable, bOOoFormat );
477 else if( rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "gradient-table" ) ) )
479 if( aType == ::getCppuType((const awt::Gradient*)0))
480 return new SvxXMLTableImportContext( *this, nPrefix, rLocalName, xAttrList, stice_gradient, mrTable, bOOoFormat );
482 else if( rLocalName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "bitmap-table" ) ) )
484 if( aType == ::getCppuType((const OUString*)0))
485 return new SvxXMLTableImportContext( *this, nPrefix, rLocalName, xAttrList, stice_bitmap, mrTable, bOOoFormat );
489 return new SvXMLImportContext( *this, nPrefix, rLocalName );