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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 #include "ChartPlotAreaOOoTContext.hxx"
31 #include "TransformerBase.hxx"
32 #include <xmloff/nmspmap.hxx>
33 #include "xmloff/xmlnmspe.hxx"
34 #include <xmloff/xmltoken.hxx>
35 #include "DeepTContext.hxx"
36 #include "ActionMapTypesOOo.hxx"
37 #include "MutableAttrList.hxx"
38 
39 using namespace ::com::sun::star;
40 using namespace ::xmloff::token;
41 
42 using ::com::sun::star::uno::Reference;
43 using ::rtl::OUString;
44 
45 class XMLAxisOOoContext : public XMLPersElemContentTContext
46 {
47 public:
48     TYPEINFO();
49 
50     XMLAxisOOoContext( XMLTransformerBase& rTransformer,
51                        const ::rtl::OUString& rQName );
52     ~XMLAxisOOoContext();
53 
54     virtual void StartElement( const Reference< xml::sax::XAttributeList >& rAttrList );
55 
56     bool IsCategoryAxis() const;
57 
58 private:
59     bool m_bIsCategoryAxis;
60 };
61 
62 TYPEINIT1( XMLAxisOOoContext, XMLPersElemContentTContext );
63 
64 XMLAxisOOoContext::XMLAxisOOoContext(
65     XMLTransformerBase& rTransformer,
66     const ::rtl::OUString& rQName ) :
67         XMLPersElemContentTContext( rTransformer, rQName ),
68         m_bIsCategoryAxis( false )
69 {}
70 
71 XMLAxisOOoContext::~XMLAxisOOoContext()
72 {}
73 
74 void XMLAxisOOoContext::StartElement(
75     const Reference< xml::sax::XAttributeList >& rAttrList )
76 {
77 	OUString aLocation, aMacroName;
78 	Reference< xml::sax::XAttributeList > xAttrList( rAttrList );
79 	XMLMutableAttributeList *pMutableAttrList = 0;
80 	sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
81 	for( sal_Int16 i=0; i < nAttrCount; i++ )
82 	{
83 		const OUString& rAttrName = xAttrList->getNameByIndex( i );
84 		OUString aLocalName;
85 		sal_uInt16 nPrefix =
86 			GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLocalName );
87 
88         if( nPrefix == XML_NAMESPACE_CHART &&
89             IsXMLToken( aLocalName, XML_CLASS ) )
90         {
91 			if( !pMutableAttrList )
92 			{
93 				pMutableAttrList = new XMLMutableAttributeList( xAttrList );
94 				xAttrList = pMutableAttrList;
95 			}
96 
97 			const OUString& rAttrValue = xAttrList->getValueByIndex( i );
98             XMLTokenEnum eToken = XML_TOKEN_INVALID;
99             if( IsXMLToken( rAttrValue, XML_DOMAIN ) ||
100                 IsXMLToken( rAttrValue, XML_CATEGORY ))
101             {
102                 eToken = XML_X;
103                 if( IsXMLToken( rAttrValue, XML_CATEGORY ) )
104                     m_bIsCategoryAxis = true;
105             }
106             else if( IsXMLToken( rAttrValue, XML_VALUE ))
107             {
108                 eToken = XML_Y;
109             }
110             else if( IsXMLToken( rAttrValue, XML_SERIES ))
111             {
112                 eToken = XML_Z;
113             }
114             else
115             {
116                 OSL_ENSURE( false, "ChartAxis: Invalid attribute value" );
117             }
118 
119             if( eToken != XML_TOKEN_INVALID )
120             {
121                 OUString aNewAttrQName(
122                     GetTransformer().GetNamespaceMap().GetQNameByKey(
123                         XML_NAMESPACE_CHART, GetXMLToken( XML_DIMENSION )));
124                 pMutableAttrList->RenameAttributeByIndex( i, aNewAttrQName );
125 				pMutableAttrList->SetValueByIndex( i, GetXMLToken( eToken ));
126             }
127         }
128     }
129 
130     XMLPersElemContentTContext::StartElement( xAttrList );
131 }
132 
133 bool XMLAxisOOoContext::IsCategoryAxis() const
134 {
135     return m_bIsCategoryAxis;
136 }
137 
138 
139 TYPEINIT1( XMLChartPlotAreaOOoTContext, XMLProcAttrTransformerContext )
140 
141 XMLChartPlotAreaOOoTContext::XMLChartPlotAreaOOoTContext(
142     XMLTransformerBase & rTransformer, const ::rtl::OUString & rQName ) :
143         XMLProcAttrTransformerContext( rTransformer, rQName, OOO_SHAPE_ACTIONS )
144 {
145 }
146 
147 XMLChartPlotAreaOOoTContext::~XMLChartPlotAreaOOoTContext()
148 {}
149 
150 XMLTransformerContext * XMLChartPlotAreaOOoTContext::CreateChildContext(
151     sal_uInt16 nPrefix,
152     const ::rtl::OUString& rLocalName,
153     const ::rtl::OUString& rQName,
154     const uno::Reference< xml::sax::XAttributeList >& xAttrList )
155 {
156 	XMLTransformerContext *pContext = 0;
157 
158 	if( XML_NAMESPACE_CHART == nPrefix &&
159 		IsXMLToken( rLocalName, XML_AXIS ) )
160 	{
161         XMLAxisOOoContext * pAxisContext( new XMLAxisOOoContext( GetTransformer(), rQName ));
162         AddContent( pAxisContext );
163         pContext = pAxisContext;
164     }
165     else if( XML_NAMESPACE_CHART == nPrefix &&
166              IsXMLToken( rLocalName, XML_CATEGORIES ) )
167     {
168         pContext = new XMLPersAttrListTContext( GetTransformer(), rQName );
169 
170         // put categories at correct axis
171         XMLAxisContextVector::iterator aIter = m_aChildContexts.begin();
172         bool bFound =false;
173 
174         // iterate over axis elements
175         for( ; ! bFound && aIter != m_aChildContexts.end(); ++aIter )
176         {
177             XMLAxisOOoContext * pAxisContext = (*aIter).get();
178             if( pAxisContext != 0 )
179             {
180                 // iterate over attributes to find category axis
181                 Reference< xml::sax::XAttributeList > xNewAttrList( pAxisContext->GetAttrList());
182                 sal_Int16 nAttrCount = xNewAttrList.is() ? xNewAttrList->getLength() : 0;
183 
184                 for( sal_Int16 i=0; i < nAttrCount; i++ )
185                 {
186                     const OUString & rAttrName = xNewAttrList->getNameByIndex( i );
187                     OUString aLocalName;
188                     sal_uInt16 nNewPrefix =
189                         GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
190                                                                              &aLocalName );
191                     if( nNewPrefix == XML_NAMESPACE_CHART &&
192                         pAxisContext->IsCategoryAxis() &&
193                         IsXMLToken( aLocalName, XML_DIMENSION ) )
194                     {
195                         // category axis found
196                         pAxisContext->AddContent( pContext );
197                         bFound = true;
198                         break;
199                     }
200                 }
201             }
202         }
203         OSL_ENSURE( bFound, "No suitable axis for categories found." );
204     }
205     else
206 	{
207         ExportContent();
208         pContext =  XMLProcAttrTransformerContext::CreateChildContext(
209             nPrefix, rLocalName, rQName, xAttrList );
210 	}
211 
212 	return pContext;
213 }
214 
215 void XMLChartPlotAreaOOoTContext::EndElement()
216 {
217     ExportContent();
218     XMLProcAttrTransformerContext::EndElement();
219 }
220 
221 void XMLChartPlotAreaOOoTContext::AddContent( XMLAxisOOoContext *pContext )
222 {
223 	OSL_ENSURE( pContext && pContext->IsPersistent(),
224 				"non-persistent context" );
225 	XMLAxisContextVector::value_type aVal( pContext );
226 	m_aChildContexts.push_back( aVal );
227 }
228 
229 
230 void XMLChartPlotAreaOOoTContext::ExportContent()
231 {
232 	XMLAxisContextVector::iterator aIter = m_aChildContexts.begin();
233 
234 	for( ; aIter != m_aChildContexts.end(); ++aIter )
235 	{
236 		(*aIter)->Export();
237 	}
238 
239     m_aChildContexts.clear();
240 }
241