Debian Codegraph @

xmloff/source/transform/ChartPlotAreaOASISTContext.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #include "ChartPlotAreaOASISTContext.hxx"
21 #include "TransformerBase.hxx"
22 #include <xmloff/namespacemap.hxx>
23 #include <xmloff/xmlnamespace.hxx>
24 #include <xmloff/xmltoken.hxx>
25 #include "DeepTContext.hxx"
26 #include "ActionMapTypesOASIS.hxx"
27 #include "MutableAttrList.hxx"
28 #include <osl/diagnose.h>
29
30 using namespace ::com::sun::star;
31 using namespace ::xmloff::token;
32
33 using ::com::sun::star::uno::Reference;
34
35 namespace {
36
37 class XMLAxisOASISContext : public XMLPersElemContentTContext
38 {
39 public:
40 XMLAxisOASISContext( XMLTransformerBase& rTransformer,
41 const OUString& rQName,
42 ::rtl::Reference< XMLPersAttrListTContext > & rOutCategoriesContext );
43
44 virtual rtl::Reference<XMLTransformerContext> CreateChildContext(
45 sal_uInt16 nPrefix,
46 const OUString& rLocalName,
47 const OUString& rQName,
48 const Reference< xml::sax::XAttributeList >& xAttrList ) override;
49
50 virtual void StartElement( const Reference< xml::sax::XAttributeList >& rAttrList ) override;
51 virtual void EndElement() override;
52
53 private:
54 ::rtl::Reference< XMLPersAttrListTContext > & m_rCategoriesContext;
55 bool m_bHasCategories;
56 };
57
58 }
59
60 XMLAxisOASISContext::XMLAxisOASISContext(
61 XMLTransformerBase& rTransformer,
62 const OUString& rQName,
63 ::rtl::Reference< XMLPersAttrListTContext > & rOutCategoriesContext ) :
64 XMLPersElemContentTContext( rTransformer, rQName ),
65 m_rCategoriesContext( rOutCategoriesContext ),
66 m_bHasCategories( false )
67 {}
68
69 rtl::Reference<XMLTransformerContext> XMLAxisOASISContext::CreateChildContext(
70 sal_uInt16 nPrefix,
71 const OUString& rLocalName,
72 const OUString& rQName,
73 const Reference< xml::sax::XAttributeList >& xAttrList )
74 {
75 rtl::Reference<XMLTransformerContext> pContext;
76
77 if( XML_NAMESPACE_CHART == nPrefix &&
78 IsXMLToken( rLocalName, XML_CATEGORIES ) )
79 {
80 // store categories element at parent
81 m_rCategoriesContext.set( new XMLPersAttrListTContext( GetTransformer(), rQName ));
82 m_bHasCategories = true;
83 pContext = m_rCategoriesContext.get();
84 }
85 else
86 {
87 pContext = XMLPersElemContentTContext::CreateChildContext(
88 nPrefix, rLocalName, rQName, xAttrList );
89 }
90
91 return pContext;
92 }
93
94 void XMLAxisOASISContext::StartElement(
95 const Reference< xml::sax::XAttributeList >& rAttrList )
96 {
97 Reference< xml::sax::XAttributeList > xAttrList( rAttrList );
98 rtl::Reference<XMLMutableAttributeList> pMutableAttrList;
99 sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
100 for( sal_Int16 i=0; i < nAttrCount; i++ )
101 {
102 const OUString aAttrName = xAttrList->getNameByIndex( i );
103 OUString aLocalName;
104 sal_uInt16 nPrefix =
105 GetTransformer().GetNamespaceMap().GetKeyByAttrName( aAttrName, &aLocalName );
106
107 if( nPrefix == XML_NAMESPACE_CHART &&
108 IsXMLToken( aLocalName, XML_DIMENSION ) )
109 {
110 if( !pMutableAttrList )
111 {
112 pMutableAttrList = new XMLMutableAttributeList( xAttrList );
113 xAttrList = pMutableAttrList;
114 }
115
116 const OUString aAttrValue = xAttrList->getValueByIndex( i );
117 XMLTokenEnum eToken = XML_TOKEN_INVALID;
118 if( IsXMLToken( aAttrValue, XML_X ))
119 {
120 eToken = XML_DOMAIN;
121 // has to be XML_CATEGORY for axes with a categories
122 // sub-element. The attribute is changed later (when it is
123 // known that there is a categories sub-element) in this case.
124 }
125 else if( IsXMLToken( aAttrValue, XML_Y ))
126 {
127 eToken = XML_VALUE;
128 }
129 else if( IsXMLToken( aAttrValue, XML_Z ))
130 {
131 eToken = XML_SERIES;
132 }
133 else
134 {
135 OSL_FAIL( "ChartAxis: Invalid attribute value" );
136 }
137
138 if( eToken != XML_TOKEN_INVALID )
139 {
140 OUString aNewAttrQName(
141 GetTransformer().GetNamespaceMap().GetQNameByKey(
142 XML_NAMESPACE_CHART, GetXMLToken( XML_CLASS )));
143 pMutableAttrList->RenameAttributeByIndex( i, aNewAttrQName );
144
145 pMutableAttrList->SetValueByIndex( i, GetXMLToken( eToken ));
146 }
147 }
148 }
149
150 XMLPersElemContentTContext::StartElement( xAttrList );
151 }
152
153 void XMLAxisOASISContext::EndElement()
154 {
155 // if we have categories, change the "class" attribute
156 if( m_bHasCategories &&
157 m_rCategoriesContext.is() )
158 {
159 OSL_ENSURE( GetAttrList().is(), "Invalid attribute list" );
160 rtl::Reference<XMLMutableAttributeList> pMutableAttrList =
161 new XMLMutableAttributeList( GetAttrList());
162 OUString aAttrQName( GetTransformer().GetNamespaceMap().GetQNameByKey(
163 XML_NAMESPACE_CHART, GetXMLToken( XML_CLASS )));
164 sal_Int16 nIndex = pMutableAttrList->GetIndexByName( aAttrQName );
165 if( nIndex != -1 )
166 {
167 OSL_ENSURE( IsXMLToken( pMutableAttrList->getValueByIndex( nIndex ),
168 XML_DOMAIN ), "Axis Dimension: invalid former value" );
169 pMutableAttrList->SetValueByIndex( nIndex, GetXMLToken( XML_CATEGORY ));
170 OSL_ENSURE( IsXMLToken( pMutableAttrList->getValueByIndex( nIndex ),
171 XML_CATEGORY ), "Axis Dimension: invalid new value" );
172 }
173
174 GetTransformer().GetDocHandler()->startElement(
175 GetExportQName(),
176 Reference< xml::sax::XAttributeList >( pMutableAttrList ));
177 ExportContent();
178 GetTransformer().GetDocHandler()->endElement( GetExportQName());
179 }
180 else
181 Export();
182 }
183
184
185 XMLChartPlotAreaOASISTContext::XMLChartPlotAreaOASISTContext(
186 XMLTransformerBase & rTransformer, const OUString & rQName ) :
187 XMLProcAttrTransformerContext( rTransformer, rQName, OASIS_SHAPE_ACTIONS )
188 {
189 }
190
191 XMLChartPlotAreaOASISTContext::~XMLChartPlotAreaOASISTContext()
192 {}
193
194 rtl::Reference<XMLTransformerContext> XMLChartPlotAreaOASISTContext::CreateChildContext(
195 sal_uInt16 nPrefix,
196 const OUString& rLocalName,
197 const OUString& rQName,
198 const uno::Reference< xml::sax::XAttributeList >& xAttrList )
199 {
200 rtl::Reference<XMLTransformerContext> pContext;
201
202 if( XML_NAMESPACE_CHART == nPrefix &&
203 IsXMLToken( rLocalName, XML_AXIS ) )
204 {
205 pContext.set(new XMLAxisOASISContext( GetTransformer(), rQName, m_rCategoriesContext ));
206 }
207 else
208 {
209 // export (and forget) categories if found in an axis-element
210 // otherwise export regularly
211 ExportCategories();
212 pContext = XMLProcAttrTransformerContext::CreateChildContext(
213 nPrefix, rLocalName, rQName, xAttrList );
214 }
215
216 return pContext;
217 }
218
219 void XMLChartPlotAreaOASISTContext::EndElement()
220 {
221 ExportCategories();
222 XMLProcAttrTransformerContext::EndElement();
223 }
224
225 void XMLChartPlotAreaOASISTContext::ExportCategories()
226 {
227 if( m_rCategoriesContext.is())
228 {
229 m_rCategoriesContext->Export();
230 m_rCategoriesContext.clear();
231 }
232 }
233
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */