1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "oox/ole/axcontrolfragment.hxx"
29 
30 #include "oox/core/xmlfilterbase.hxx"
31 #include "oox/helper/binaryinputstream.hxx"
32 #include "oox/helper/binaryoutputstream.hxx"
33 #include "oox/ole/axcontrol.hxx"
34 #include "oox/ole/olehelper.hxx"
35 #include "oox/ole/olestorage.hxx"
36 
37 namespace oox {
38 namespace ole {
39 
40 // ============================================================================
41 
42 using namespace ::com::sun::star::io;
43 using namespace ::com::sun::star::uno;
44 
45 using ::oox::core::ContextHandler2;
46 using ::oox::core::ContextHandlerRef;
47 using ::oox::core::FragmentHandler2;
48 using ::oox::core::XmlFilterBase;
49 using ::rtl::OUString;
50 
51 // ============================================================================
52 
53 AxControlPropertyContext::AxControlPropertyContext( FragmentHandler2& rFragment, ControlModelBase& rModel ) :
54     ContextHandler2( rFragment ),
55     mrModel( rModel ),
56     mnPropId( XML_TOKEN_INVALID )
57 {
58 }
59 
60 ContextHandlerRef AxControlPropertyContext::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
61 {
62     switch( getCurrentElement() )
63     {
64         case AX_TOKEN( ocx ):
65             if( nElement == AX_TOKEN( ocxPr ) )
66             {
67                 mnPropId = rAttribs.getToken( AX_TOKEN( name ), XML_TOKEN_INVALID );
68                 switch( mnPropId )
69                 {
70                     case XML_TOKEN_INVALID:
71                         return 0;
72                     case XML_Picture:
73                     case XML_MouseIcon:
74                         return this;        // import picture path from ax:picture child element
75                     default:
76                         mrModel.importProperty( mnPropId, rAttribs.getString( AX_TOKEN( value ), OUString() ) );
77                 }
78             }
79         break;
80 
81         case AX_TOKEN( ocxPr ):
82             if( nElement == AX_TOKEN( picture ) )
83             {
84                 OUString aPicturePath = getFragmentPathFromRelId( rAttribs.getString( R_TOKEN( id ), OUString() ) );
85                 if( aPicturePath.getLength() > 0 )
86                 {
87                     BinaryXInputStream aInStrm( getFilter().openInputStream( aPicturePath ), true );
88                     mrModel.importPictureData( mnPropId, aInStrm );
89                 }
90             }
91         break;
92     }
93     return 0;
94 }
95 
96 // ============================================================================
97 
98 AxControlFragment::AxControlFragment( XmlFilterBase& rFilter, const OUString& rFragmentPath, EmbeddedControl& rControl ) :
99     FragmentHandler2( rFilter, rFragmentPath, true ),
100     mrControl( rControl )
101 {
102 }
103 
104 ContextHandlerRef AxControlFragment::onCreateContext( sal_Int32 nElement, const AttributeList& rAttribs )
105 {
106     if( isRootElement() && (nElement == AX_TOKEN( ocx )) )
107     {
108         OUString aClassId = rAttribs.getString( AX_TOKEN( classid ), OUString() );
109         switch( rAttribs.getToken( AX_TOKEN( persistence ), XML_TOKEN_INVALID ) )
110         {
111             case XML_persistPropertyBag:
112                 if( ControlModelBase* pModel = mrControl.createModelFromGuid( aClassId ) )
113                     return new AxControlPropertyContext( *this, *pModel );
114             break;
115 
116             case XML_persistStreamInit:
117             {
118                 OUString aFragmentPath = getFragmentPathFromRelId( rAttribs.getString( R_TOKEN( id ), OUString() ) );
119                 if( aFragmentPath.getLength() > 0 )
120                 {
121                     BinaryXInputStream aInStrm( getFilter().openInputStream( aFragmentPath ), true );
122                     if( !aInStrm.isEof() )
123                     {
124                         // binary stream contains a copy of the class ID, must be equal to attribute value
125                         OUString aStrmClassId = OleHelper::importGuid( aInStrm );
126                         OSL_ENSURE( aClassId.equalsIgnoreAsciiCase( aStrmClassId ),
127                             "AxControlFragment::importBinaryControl - form control class ID mismatch" );
128                         if( ControlModelBase* pModel = mrControl.createModelFromGuid( aStrmClassId ) )
129                             pModel->importBinaryModel( aInStrm );
130                     }
131                 }
132             }
133             break;
134 
135             case XML_persistStorage:
136             {
137                 OUString aFragmentPath = getFragmentPathFromRelId( rAttribs.getString( R_TOKEN( id ), OUString() ) );
138                 if( aFragmentPath.getLength() > 0 )
139                 {
140                     Reference< XInputStream > xStrgStrm = getFilter().openInputStream( aFragmentPath );
141                     if( xStrgStrm.is() )
142                     {
143                         OleStorage aStorage( getFilter().getComponentContext(), xStrgStrm, false );
144                         BinaryXInputStream aInStrm( aStorage.openInputStream( CREATE_OUSTRING( "f" ) ), true );
145                         if( !aInStrm.isEof() )
146                             if( AxContainerModelBase* pModel = dynamic_cast< AxContainerModelBase* >( mrControl.createModelFromGuid( aClassId ) ) )
147                                 pModel->importBinaryModel( aInStrm );
148                     }
149                 }
150             }
151             break;
152         }
153     }
154     return 0;
155 }
156 
157 // ============================================================================
158 
159 } // namespace ole
160 } // namespace oox
161