Avoid potential negative array index access to cached text.
[LibreOffice.git] / oox / source / ole / axcontrolfragment.cxx
blob5091af660713c0e67d8445200a0edb61a67c5c90
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <oox/ole/axcontrolfragment.hxx>
22 #include <com/sun/star/io/XInputStream.hpp>
23 #include <oox/core/xmlfilterbase.hxx>
24 #include <oox/helper/attributelist.hxx>
25 #include <oox/helper/binaryinputstream.hxx>
26 #include <oox/ole/axcontrol.hxx>
27 #include <oox/ole/olehelper.hxx>
28 #include <oox/ole/olestorage.hxx>
29 #include <oox/token/namespaces.hxx>
30 #include <oox/token/tokens.hxx>
32 #include <osl/diagnose.h>
34 namespace oox::ole {
36 using namespace ::com::sun::star::io;
37 using namespace ::com::sun::star::uno;
39 using ::oox::core::ContextHandler2;
40 using ::oox::core::ContextHandlerRef;
41 using ::oox::core::FragmentHandler2;
42 using ::oox::core::XmlFilterBase;
44 AxControlPropertyContext::AxControlPropertyContext( FragmentHandler2 const & rFragment, ControlModelBase& rModel ) :
45 ContextHandler2( rFragment ),
46 mrModel( rModel ),
47 mnPropId( XML_TOKEN_INVALID )
51 ContextHandlerRef AxControlPropertyContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
53 switch( getCurrentElement() )
55 case AX_TOKEN( ocx ):
56 if( nElement == AX_TOKEN( ocxPr ) )
58 mnPropId = rAttribs.getToken( AX_TOKEN( name ), XML_TOKEN_INVALID );
59 switch( mnPropId )
61 case XML_TOKEN_INVALID:
62 return nullptr;
63 case XML_Picture:
64 case XML_MouseIcon:
65 return this; // import picture path from ax:picture child element
66 default:
67 mrModel.importProperty( mnPropId, rAttribs.getStringDefaulted( AX_TOKEN( value )) );
70 break;
72 case AX_TOKEN( ocxPr ):
73 if( nElement == AX_TOKEN( picture ) )
75 OUString aPicturePath = getFragmentPathFromRelId( rAttribs.getStringDefaulted( R_TOKEN( id )) );
76 if( !aPicturePath.isEmpty() )
78 BinaryXInputStream aInStrm( getFilter().openInputStream( aPicturePath ), true );
79 mrModel.importPictureData( mnPropId, aInStrm );
82 break;
84 return nullptr;
87 AxControlFragment::AxControlFragment( XmlFilterBase& rFilter, const OUString& rFragmentPath, EmbeddedControl& rControl ) :
88 FragmentHandler2( rFilter, rFragmentPath, true ),
89 mrControl( rControl )
93 ContextHandlerRef AxControlFragment::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
95 if( isRootElement() && (nElement == AX_TOKEN( ocx )) )
97 OUString aClassId = rAttribs.getStringDefaulted( AX_TOKEN( classid ));
98 switch( rAttribs.getToken( AX_TOKEN( persistence ), XML_TOKEN_INVALID ) )
100 case XML_persistPropertyBag:
101 if( ControlModelBase* pModel = mrControl.createModelFromGuid( aClassId ) )
102 return new AxControlPropertyContext( *this, *pModel );
103 break;
105 case XML_persistStreamInit:
107 OUString aFragmentPath = getFragmentPathFromRelId( rAttribs.getStringDefaulted( R_TOKEN( id )) );
108 if( !aFragmentPath.isEmpty() )
110 BinaryXInputStream aInStrm( getFilter().openInputStream( aFragmentPath ), true );
111 if( !aInStrm.isEof() )
113 // binary stream contains a copy of the class ID, must be equal to attribute value
114 OUString aStrmClassId = OleHelper::importGuid( aInStrm );
115 OSL_ENSURE( aClassId.equalsIgnoreAsciiCase( aStrmClassId ),
116 "AxControlFragment::importBinaryControl - form control class ID mismatch" );
117 if( ControlModelBase* pModel = mrControl.createModelFromGuid( aStrmClassId ) )
118 pModel->importBinaryModel( aInStrm );
122 break;
124 case XML_persistStorage:
126 OUString aFragmentPath = getFragmentPathFromRelId( rAttribs.getStringDefaulted( R_TOKEN( id )) );
127 if( !aFragmentPath.isEmpty() )
129 Reference< XInputStream > xStrgStrm = getFilter().openInputStream( aFragmentPath );
130 if( xStrgStrm.is() )
132 // Try to import as a parent control
133 bool bImportedAsParent = false;
134 OleStorage aStorage( getFilter().getComponentContext(), xStrgStrm, false );
135 BinaryXInputStream aInStrm( aStorage.openInputStream( "f" ), true );
136 if( !aInStrm.isEof() )
138 if( AxContainerModelBase* pModel = dynamic_cast< AxContainerModelBase* >( mrControl.createModelFromGuid( aClassId ) ) )
140 pModel->importBinaryModel( aInStrm );
141 bImportedAsParent = true;
144 // Import it as a non-parent control
145 if(!bImportedAsParent)
147 BinaryXInputStream aInStrm2(aStorage.openInputStream("contents"), true);
148 if (!aInStrm2.isEof())
150 if (ControlModelBase* pModel = mrControl.createModelFromGuid(aClassId))
152 pModel->importBinaryModel(aInStrm2);
159 break;
162 return nullptr;
165 } // namespace oox::ole
167 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */