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 #include "oox/helper/modelobjecthelper.hxx"
25 
26 #include <com/sun/star/awt/Gradient.hpp>
27 #include <com/sun/star/container/XNameContainer.hpp>
28 #include <com/sun/star/drawing/LineDash.hpp>
29 #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include "oox/helper/containerhelper.hxx"
32 #include "oox/helper/helper.hxx"
33 
34 namespace oox {
35 
36 // ============================================================================
37 
38 using namespace ::com::sun::star::awt;
39 using namespace ::com::sun::star::drawing;
40 using namespace ::com::sun::star::lang;
41 using namespace ::com::sun::star::uno;
42 
43 using ::rtl::OUString;
44 
45 // ============================================================================
46 
ObjectContainer(const Reference<XMultiServiceFactory> & rxModelFactory,const OUString & rServiceName)47 ObjectContainer::ObjectContainer( const Reference< XMultiServiceFactory >& rxModelFactory, const OUString& rServiceName ) :
48     mxModelFactory( rxModelFactory ),
49     maServiceName( rServiceName ),
50     mnIndex( 0 )
51 {
52     OSL_ENSURE( mxModelFactory.is(), "ObjectContainer::ObjectContainer - missing service factory" );
53 }
54 
~ObjectContainer()55 ObjectContainer::~ObjectContainer()
56 {
57 }
58 
hasObject(const OUString & rObjName) const59 bool ObjectContainer::hasObject( const OUString& rObjName ) const
60 {
61     createContainer();
62     return mxContainer.is() && mxContainer->hasByName( rObjName );
63 }
64 
getObject(const OUString & rObjName) const65 Any ObjectContainer::getObject( const OUString& rObjName ) const
66 {
67     createContainer();
68     if( mxContainer.is() ) try
69     {
70         return mxContainer->getByName( rObjName );
71     }
72     catch( Exception& )
73     {
74     }
75     return Any();
76 }
77 
insertObject(const OUString & rObjName,const Any & rObj,bool bInsertByUnusedName)78 OUString ObjectContainer::insertObject( const OUString& rObjName, const Any& rObj, bool bInsertByUnusedName )
79 {
80     createContainer();
81     if( mxContainer.is() )
82     {
83         if( bInsertByUnusedName )
84             return ContainerHelper::insertByUnusedName( mxContainer, rObjName + OUString::valueOf( ++mnIndex ), ' ', rObj );
85         if( ContainerHelper::insertByName( mxContainer, rObjName, rObj ) )
86             return rObjName;
87     }
88     return OUString();
89 }
90 
createContainer() const91 void ObjectContainer::createContainer() const
92 {
93     if( !mxContainer.is() && mxModelFactory.is() ) try
94     {
95         mxContainer.set( mxModelFactory->createInstance( maServiceName ), UNO_QUERY_THROW );
96         mxModelFactory.clear();
97     }
98     catch( Exception& )
99     {
100     }
101     OSL_ENSURE( mxContainer.is(), "ObjectContainer::createContainer - container not found" );
102 }
103 
104 // ============================================================================
105 
ModelObjectHelper(const Reference<XMultiServiceFactory> & rxModelFactory)106 ModelObjectHelper::ModelObjectHelper( const Reference< XMultiServiceFactory >& rxModelFactory ) :
107     maMarkerContainer(    rxModelFactory, CREATE_OUSTRING( "com.sun.star.drawing.MarkerTable" ) ),
108     maDashContainer(      rxModelFactory, CREATE_OUSTRING( "com.sun.star.drawing.DashTable" ) ),
109     maGradientContainer(  rxModelFactory, CREATE_OUSTRING( "com.sun.star.drawing.GradientTable" ) ),
110     maBitmapUrlContainer( rxModelFactory, CREATE_OUSTRING( "com.sun.star.drawing.BitmapTable" ) ),
111     maDashNameBase(      CREATE_OUSTRING( "msLineDash " ) ),
112     maGradientNameBase(  CREATE_OUSTRING( "msFillGradient " ) ),
113     maBitmapUrlNameBase( CREATE_OUSTRING( "msFillBitmap " ) )
114 {
115 }
116 
hasLineMarker(const OUString & rMarkerName) const117 bool ModelObjectHelper::hasLineMarker( const OUString& rMarkerName ) const
118 {
119     return maMarkerContainer.hasObject( rMarkerName );
120 }
121 
insertLineMarker(const OUString & rMarkerName,const PolyPolygonBezierCoords & rMarker)122 bool ModelObjectHelper::insertLineMarker( const OUString& rMarkerName, const PolyPolygonBezierCoords& rMarker )
123 {
124     OSL_ENSURE( rMarker.Coordinates.hasElements(), "ModelObjectHelper::insertLineMarker - line marker without coordinates" );
125     if( rMarker.Coordinates.hasElements() )
126         return maMarkerContainer.insertObject( rMarkerName, Any( rMarker ), false ).getLength() > 0;
127     return false;
128 }
129 
insertLineDash(const LineDash & rDash)130 OUString ModelObjectHelper::insertLineDash( const LineDash& rDash )
131 {
132     return maDashContainer.insertObject( maDashNameBase, Any( rDash ), true );
133 }
134 
insertFillGradient(const Gradient & rGradient)135 OUString ModelObjectHelper::insertFillGradient( const Gradient& rGradient )
136 {
137     return maGradientContainer.insertObject( maGradientNameBase, Any( rGradient ), true );
138 }
139 
insertFillBitmapUrl(const OUString & rGraphicUrl)140 OUString ModelObjectHelper::insertFillBitmapUrl( const OUString& rGraphicUrl )
141 {
142     if( rGraphicUrl.getLength() > 0 )
143         return maBitmapUrlContainer.insertObject( maBitmapUrlNameBase, Any( rGraphicUrl ), true );
144     return OUString();
145 }
146 
147 // ============================================================================
148 
149 } // namespace oox
150