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