LibreOffice Module oox (master)  1
axcontrolfragment.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 
21 
22 #include <com/sun/star/io/XInputStream.hpp>
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>
31 
32 #include <osl/diagnose.h>
33 
34 namespace oox::ole {
35 
36 using namespace ::com::sun::star::io;
37 using namespace ::com::sun::star::uno;
38 
39 using ::oox::core::ContextHandler2;
41 using ::oox::core::FragmentHandler2;
42 using ::oox::core::XmlFilterBase;
43 
44 AxControlPropertyContext::AxControlPropertyContext( FragmentHandler2 const & rFragment, ControlModelBase& rModel ) :
45  ContextHandler2( rFragment ),
46  mrModel( rModel ),
47  mnPropId( XML_TOKEN_INVALID )
48 {
49 }
50 
52 {
53  switch( getCurrentElement() )
54  {
55  case AX_TOKEN( ocx ):
56  if( nElement == AX_TOKEN( ocxPr ) )
57  {
58  mnPropId = rAttribs.getToken( AX_TOKEN( name ), XML_TOKEN_INVALID );
59  switch( mnPropId )
60  {
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.getString( AX_TOKEN( value ), OUString() ) );
68  }
69  }
70  break;
71 
72  case AX_TOKEN( ocxPr ):
73  if( nElement == AX_TOKEN( picture ) )
74  {
75  OUString aPicturePath = getFragmentPathFromRelId( rAttribs.getString( R_TOKEN( id ), OUString() ) );
76  if( !aPicturePath.isEmpty() )
77  {
78  BinaryXInputStream aInStrm( getFilter().openInputStream( aPicturePath ), true );
80  }
81  }
82  break;
83  }
84  return nullptr;
85 }
86 
87 AxControlFragment::AxControlFragment( XmlFilterBase& rFilter, const OUString& rFragmentPath, EmbeddedControl& rControl ) :
88  FragmentHandler2( rFilter, rFragmentPath, true ),
89  mrControl( rControl )
90 {
91 }
92 
94 {
95  if( isRootElement() && (nElement == AX_TOKEN( ocx )) )
96  {
97  OUString aClassId = rAttribs.getString( AX_TOKEN( classid ), OUString() );
98  switch( rAttribs.getToken( AX_TOKEN( persistence ), XML_TOKEN_INVALID ) )
99  {
100  case XML_persistPropertyBag:
101  if( ControlModelBase* pModel = mrControl.createModelFromGuid( aClassId ) )
102  return new AxControlPropertyContext( *this, *pModel );
103  break;
104 
105  case XML_persistStreamInit:
106  {
107  OUString aFragmentPath = getFragmentPathFromRelId( rAttribs.getString( R_TOKEN( id ), OUString() ) );
108  if( !aFragmentPath.isEmpty() )
109  {
110  BinaryXInputStream aInStrm( getFilter().openInputStream( aFragmentPath ), true );
111  if( !aInStrm.isEof() )
112  {
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 );
119  }
120  }
121  }
122  break;
123 
124  case XML_persistStorage:
125  {
126  OUString aFragmentPath = getFragmentPathFromRelId( rAttribs.getString( R_TOKEN( id ), OUString() ) );
127  if( !aFragmentPath.isEmpty() )
128  {
129  Reference< XInputStream > xStrgStrm = getFilter().openInputStream( aFragmentPath );
130  if( xStrgStrm.is() )
131  {
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() )
137  {
138  if( AxContainerModelBase* pModel = dynamic_cast< AxContainerModelBase* >( mrControl.createModelFromGuid( aClassId ) ) )
139  {
140  pModel->importBinaryModel( aInStrm );
141  bImportedAsParent = true;
142  }
143  }
144  // Import it as a non-parent control
145  if(!bImportedAsParent)
146  {
147  BinaryXInputStream aInStrm2(aStorage.openInputStream("contents"), true);
148  if (!aInStrm2.isEof())
149  {
150  if (ControlModelBase* pModel = mrControl.createModelFromGuid(aClassId))
151  {
152  pModel->importBinaryModel(aInStrm2);
153  }
154  }
155  }
156  }
157  }
158  }
159  break;
160  }
161  }
162  return nullptr;
163 }
164 
165 } // namespace oox::ole
166 
167 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Wraps a UNO input stream and provides convenient access functions.
OOX_DLLPUBLIC OUString importGuid(BinaryInputStream &rInStrm)
Imports a GUID from the passed binary stream and returns its string representation (in uppercase char...
Definition: olehelper.cxx:260
Context handler for ActiveX form control model properties.
OptValue< OUString > getString(sal_Int32 nAttrToken) const
Returns the string value of the specified attribute.
Implements stream access for binary OLE storages.
Definition: olestorage.hxx:43
A form control embedded in a document draw page.
Definition: axcontrol.hxx:897
AxControlPropertyContext(::oox::core::FragmentHandler2 const &rFragment, ControlModelBase &rModel)
AxControlFragment(::oox::core::XmlFilterBase &rFilter, const OUString &rFragmentPath, EmbeddedControl &rControl)
sal_Int32 getCurrentElement() const
Returns the identifier of the currently processed element.
virtual ::oox::core::ContextHandlerRef onCreateContext(sal_Int32 nElement, const AttributeList &rAttribs) override
Will be called to create a context handler for the passed element.
XML_TOKEN_INVALID
OUString getFragmentPathFromRelId(const OUString &rRelId) const
Returns the full fragment path for the passed relation identifier.
Reference< XComponentContext > getComponentContext(Reference< XMultiServiceFactory > const &factory)
Provides access to attribute values of an element.
bool isRootElement() const
Returns true, if the element currently processed is the root element of the context or fragment handl...
css::uno::Reference< css::io::XInputStream > openInputStream(const OUString &rStreamName)
Opens and returns the specified input stream from the storage.
virtual void importPictureData(sal_Int32 nPropId, BinaryInputStream &rInStrm)
Derived classes set binary data (picture, mouse icon) at the model structure.
Definition: axcontrol.cxx:644
Base class for all models of form controls.
Definition: axcontrol.hxx:346
Base class for ActiveX container controls.
Definition: axcontrol.hxx:797
ChartModel & mrModel
ControlModelBase * createModelFromGuid(const OUString &rClassId)
Creates and returns the internal control model according to the passed MS class identifier.
Definition: axcontrol.cxx:2667
sal_Int32 mnPropId
Identifier of currently processed property.
XmlFilterBase & getFilter() const
Returns the filter instance.
::rtl::Reference< ContextHandler > ContextHandlerRef
const char * name
Any value
virtual void importProperty(sal_Int32 nPropId, const OUString &rValue)
Derived classes set specific OOXML properties at the model structure.
Definition: axcontrol.cxx:640
bool isEof() const
Returns true, if the stream position is invalid (EOF).
OptValue< sal_Int32 > getToken(sal_Int32 nAttrToken) const
Returns the token identifier of the value of the specified attribute.
virtual ::oox::core::ContextHandlerRef onCreateContext(sal_Int32 nElement, const AttributeList &rAttribs) override
Will be called to create a context handler for the passed element.