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