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 "layoutnodecontext.hxx"
25
26 #include "oox/helper/attributelist.hxx"
27 #include "oox/drawingml/diagram/diagram.hxx"
28 #include "oox/drawingml/shapecontext.hxx"
29 #include "diagramdefinitioncontext.hxx"
30
31 using namespace ::oox::core;
32 using namespace ::com::sun::star::uno;
33 using namespace ::com::sun::star::xml::sax;
34 using ::rtl::OUString;
35
36 namespace oox { namespace drawingml {
37
38 class IfContext
39 : public LayoutNodeContext
40 {
41 public:
IfContext(ContextHandler & rParent,const Reference<XFastAttributeList> & xAttribs,const LayoutAtomPtr & pNode)42 IfContext( ContextHandler& rParent,
43 const Reference< XFastAttributeList >& xAttribs,
44 const LayoutAtomPtr & pNode )
45 : LayoutNodeContext( rParent, xAttribs, pNode )
46 {
47 ConditionAtomPtr pAtom( boost::dynamic_pointer_cast< ConditionAtom >(pNode) );
48 OSL_ENSURE( pAtom, "Must pass a ConditionAtom" );
49
50 pAtom->iterator().loadFromXAttr( xAttribs );
51 pAtom->cond().loadFromXAttr( xAttribs );
52 }
53 };
54
55
56
57 class AlgorithmContext
58 : public ContextHandler
59 {
60 public:
AlgorithmContext(ContextHandler & rParent,const Reference<XFastAttributeList> & xAttribs,const LayoutAtomPtr & pNode)61 AlgorithmContext( ContextHandler& rParent, const Reference< XFastAttributeList >& xAttribs, const LayoutAtomPtr & pNode )
62 : ContextHandler( rParent )
63 , mnRevision( 0 )
64 , mnType( 0 )
65 , mpNode( pNode )
66 {
67 AttributeList aAttribs( xAttribs );
68 mnRevision = aAttribs.getInteger( XML_rev, 0 );
69 mnType = xAttribs->getOptionalValueToken( XML_type, 0 );
70 }
71
72 private:
73 sal_Int32 mnRevision;
74 sal_Int32 mnType;
75 LayoutAtomPtr mpNode;
76 };
77
78
79 class ChooseContext
80 : public ContextHandler
81 {
82 public:
ChooseContext(ContextHandler & rParent,const Reference<XFastAttributeList> & xAttribs,const LayoutAtomPtr & pNode)83 ChooseContext( ContextHandler& rParent, const Reference< XFastAttributeList >& xAttribs, const LayoutAtomPtr & pNode )
84 : ContextHandler( rParent )
85 , mbHasElse( false )
86 , mpNode( pNode )
87 {
88 msName = xAttribs->getOptionalValue( XML_name );
89 }
90
91 virtual Reference< XFastContextHandler > SAL_CALL
createFastChildContext(::sal_Int32 aElement,const Reference<XFastAttributeList> & xAttribs)92 createFastChildContext( ::sal_Int32 aElement,
93 const Reference< XFastAttributeList >& xAttribs )
94 {
95 Reference< XFastContextHandler > xRet;
96
97 switch( aElement )
98 {
99 case XML_if:
100 {
101 // CT_When
102 LayoutAtomPtr pAtom( new ConditionAtom( false ) );
103 mpNode->addChild( pAtom );
104 xRet.set( new IfContext( *this, xAttribs, pAtom ) );
105 break;
106 }
107 case XML_else:
108 // CT_Otherwise
109 if( !mbHasElse )
110 {
111 LayoutAtomPtr pAtom( new ConditionAtom( true ) );
112 mpNode->addChild( pAtom );
113 xRet.set( new IfContext( *this, xAttribs, pAtom ) );
114 mbHasElse = true;
115 }
116 else
117 {
118 OSL_TRACE( "ignoring second else clause" );
119 }
120 break;
121 default:
122 break;
123 }
124
125 if( !xRet.is() )
126 xRet.set(this);
127
128 return xRet;
129 }
130 private:
131 bool mbHasElse;
132 OUString msName;
133 LayoutAtomPtr mpNode;
134 };
135
136
137
138
139 class ForEachContext
140 : public LayoutNodeContext
141 {
142 public:
ForEachContext(ContextHandler & rParent,const Reference<XFastAttributeList> & xAttribs,const LayoutAtomPtr & pNode)143 ForEachContext( ContextHandler& rParent, const Reference< XFastAttributeList >& xAttribs, const LayoutAtomPtr & pNode )
144 : LayoutNodeContext( rParent, xAttribs, pNode )
145 {
146 ForEachAtomPtr pAtom( boost::dynamic_pointer_cast< ForEachAtom >(pNode) );
147 OSL_ENSURE( pAtom, "Must pass a ForEachAtom" );
148 xAttribs->getOptionalValue( XML_ref );
149
150 pAtom->iterator().loadFromXAttr( xAttribs );
151 }
152 };
153
154
155 // CT_LayoutVariablePropertySet
156 class LayoutVariablePropertySetContext
157 : public ContextHandler
158 {
159 public:
LayoutVariablePropertySetContext(ContextHandler & rParent,LayoutNode::VarMap & aVar)160 LayoutVariablePropertySetContext( ContextHandler& rParent, LayoutNode::VarMap & aVar )
161 : ContextHandler( rParent )
162 , mVariables( aVar )
163 {
164 }
165
~LayoutVariablePropertySetContext()166 virtual ~LayoutVariablePropertySetContext()
167 {
168 }
169
createFastChildContext(::sal_Int32 aElement,const Reference<XFastAttributeList> & xAttribs)170 virtual Reference< XFastContextHandler > SAL_CALL createFastChildContext( ::sal_Int32 aElement, const Reference< XFastAttributeList >& xAttribs )
171 {
172 Reference< XFastContextHandler > xRet;
173
174 sal_Int32 nIdx = LayoutNodeContext::tagToVarIdx( getBaseToken( aElement ) );
175 if( nIdx != -1 )
176 {
177 mVariables[ nIdx ] = makeAny( xAttribs->getOptionalValue( XML_val ) );
178 }
179 if( !xRet.is() )
180 xRet.set(this);
181
182 return xRet;
183 }
184 private:
185 LayoutNode::VarMap & mVariables;
186 };
187
188
189 // CT_LayoutNode
LayoutNodeContext(ContextHandler & rParent,const Reference<XFastAttributeList> & xAttribs,const LayoutAtomPtr & pNode)190 LayoutNodeContext::LayoutNodeContext( ContextHandler& rParent,
191 const Reference< XFastAttributeList >& xAttribs,
192 const LayoutAtomPtr &pNode )
193 : ContextHandler( rParent )
194 , mpNode( pNode )
195 {
196 OSL_ENSURE( pNode, "Node must NOT be NULL" );
197 mpNode->setName( xAttribs->getOptionalValue( XML_name ) );
198 // TODO shall we even bother?
199 // b or t
200 // sal_Int32 nChOrder = xAttributes->getOptionalValueToken( XML_chOrder, XML_b );
201 // OUString sMoveWith = xAttributes->getOptionalValue( XML_moveWith );
202 // OUString sStyleLbl = xAttributes->getOptionalValue( XML_styleLbl );
203 }
204
205
~LayoutNodeContext()206 LayoutNodeContext::~LayoutNodeContext()
207 {
208 }
209
endFastElement(::sal_Int32)210 void SAL_CALL LayoutNodeContext::endFastElement( ::sal_Int32 )
211 {
212
213 }
214
215 /** convert the XML tag to a variable index in the array
216 * @param aTag the tag, without namespace
217 * @return the variable index. -1 is an error
218 */
tagToVarIdx(sal_Int32 aTag)219 sal_Int32 LayoutNodeContext::tagToVarIdx( sal_Int32 aTag )
220 {
221 sal_Int32 nIdx = -1;
222 switch( aTag )
223 {
224 case DGM_TOKEN( animLvl ):
225 nIdx = LayoutNode::VAR_animLvl;
226 break;
227 case DGM_TOKEN( animOne ):
228 nIdx = LayoutNode::VAR_animOne;
229 break;
230 case DGM_TOKEN( bulletEnabled ):
231 nIdx = LayoutNode::VAR_bulletEnabled;
232 break;
233 case DGM_TOKEN( chMax ):
234 nIdx = LayoutNode::VAR_chMax;
235 break;
236 case DGM_TOKEN( chPref ):
237 nIdx = LayoutNode::VAR_chPref;
238 break;
239 case DGM_TOKEN( dir ):
240 nIdx = LayoutNode::VAR_dir;
241 break;
242 case DGM_TOKEN( hierBranch ):
243 nIdx = LayoutNode::VAR_hierBranch;
244 break;
245 case DGM_TOKEN( orgChart ):
246 nIdx = LayoutNode::VAR_orgChart;
247 break;
248 case DGM_TOKEN( resizeHandles ):
249 nIdx = LayoutNode::VAR_resizeHandles;
250 break;
251 default:
252 break;
253 }
254 return nIdx;
255 }
256
257
258 Reference< XFastContextHandler > SAL_CALL
createFastChildContext(::sal_Int32 aElement,const Reference<XFastAttributeList> & xAttribs)259 LayoutNodeContext::createFastChildContext( ::sal_Int32 aElement,
260 const Reference< XFastAttributeList >& xAttribs )
261 {
262 Reference< XFastContextHandler > xRet;
263
264 switch( aElement )
265 {
266 case DGM_TOKEN( layoutNode ):
267 {
268 LayoutNodePtr pNode( new LayoutNode() );
269 mpNode->addChild( pNode );
270 xRet.set( new LayoutNodeContext( *this, xAttribs, pNode ) );
271 break;
272 }
273 case DGM_TOKEN( shape ):
274 {
275 ShapePtr pShape( new Shape() );
276 xRet.set( new ShapeContext( *this, ShapePtr(), pShape ) );
277 break;
278 }
279 case DGM_TOKEN( extLst ):
280 return xRet;
281 case DGM_TOKEN( alg ):
282 {
283 // CT_Algorithm
284 LayoutAtomPtr pAtom( new AlgAtom );
285 mpNode->addChild( pAtom );
286 xRet.set( new AlgorithmContext( *this, xAttribs, pAtom ) );
287 break;
288 }
289 case DGM_TOKEN( choose ):
290 {
291 // CT_Choose
292 LayoutAtomPtr pAtom( new ChooseAtom );
293 mpNode->addChild( pAtom );
294 xRet.set( new ChooseContext( *this, xAttribs, pAtom ) );
295 break;
296 }
297 case DGM_TOKEN( forEach ):
298 {
299 // CT_ForEach
300 LayoutAtomPtr pAtom( new ForEachAtom );
301 mpNode->addChild( pAtom );
302 xRet.set( new ForEachContext( *this, xAttribs, pAtom ) );
303 break;
304 }
305 case DGM_TOKEN( constrLst ):
306 // CT_Constraints
307 // TODO
308 break;
309 case DGM_TOKEN( presOf ):
310 {
311 // CT_PresentationOf
312 // TODO
313 xAttribs->getOptionalValue( XML_axis );
314 xAttribs->getOptionalValue( XML_cnt );
315 xAttribs->getOptionalValue( XML_hideLastTrans );
316 xAttribs->getOptionalValue( XML_ptType );
317 xAttribs->getOptionalValue( XML_st );
318 xAttribs->getOptionalValue( XML_step );
319 break;
320 }
321 case DGM_TOKEN( ruleLst ):
322 // CT_Rules
323 // TODO
324 break;
325 case DGM_TOKEN( varLst ):
326 {
327 LayoutNodePtr pNode( boost::dynamic_pointer_cast< LayoutNode >( mpNode ) );
328 if( pNode )
329 {
330 xRet.set( new LayoutVariablePropertySetContext( *this, pNode->variables() ) );
331 }
332 else
333 {
334 OSL_TRACE( "OOX: encountered a varLst in a non layoutNode context" );
335 }
336 break;
337 }
338 default:
339 break;
340 }
341 if( !xRet.is() )
342 xRet.set(this);
343
344 return xRet;
345 }
346
347
348 } }
349