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 
21 #include "diagramdefinitioncontext.hxx"
22 #include "diagramfragmenthandler.hxx"
23 #include "datamodelcontext.hxx"
24 #include <drawingml/colorchoicecontext.hxx>
25 #include <oox/helper/attributelist.hxx>
26 #include <oox/token/namespaces.hxx>
27 
28 using namespace ::oox::core;
29 using namespace ::com::sun::star::xml::sax;
30 using namespace ::com::sun::star::uno;
31 
32 namespace oox::drawingml {
33 
DiagramDataFragmentHandler(XmlFilterBase & rFilter,const OUString & rFragmentPath,const DiagramDataPtr & rDataPtr)34 DiagramDataFragmentHandler::DiagramDataFragmentHandler( XmlFilterBase& rFilter,
35                                                         const OUString& rFragmentPath,
36                                                         const DiagramDataPtr& rDataPtr )
37     : FragmentHandler2( rFilter, rFragmentPath )
38     , mpDataPtr( rDataPtr )
39 {
40 }
41 
~DiagramDataFragmentHandler()42 DiagramDataFragmentHandler::~DiagramDataFragmentHandler( ) noexcept
43 {
44 
45 }
46 
endDocument()47 void SAL_CALL DiagramDataFragmentHandler::endDocument()
48 {
49 
50 }
51 
52 ContextHandlerRef
onCreateContext(::sal_Int32 aElement,const AttributeList &)53 DiagramDataFragmentHandler::onCreateContext( ::sal_Int32 aElement,
54                                              const AttributeList& )
55 {
56     switch( aElement )
57     {
58     case DGM_TOKEN( dataModel ):
59         return new DataModelContext( *this, mpDataPtr );
60     default:
61         break;
62     }
63 
64     return this;
65 }
66 
DiagramLayoutFragmentHandler(XmlFilterBase & rFilter,const OUString & rFragmentPath,const DiagramLayoutPtr & rDataPtr)67 DiagramLayoutFragmentHandler::DiagramLayoutFragmentHandler( XmlFilterBase& rFilter,
68                                                         const OUString& rFragmentPath,
69                                                         const DiagramLayoutPtr& rDataPtr )
70     : FragmentHandler2( rFilter, rFragmentPath )
71     , mpDataPtr( rDataPtr )
72 {
73 }
74 
~DiagramLayoutFragmentHandler()75 DiagramLayoutFragmentHandler::~DiagramLayoutFragmentHandler( ) noexcept
76 {
77 
78 }
79 
endDocument()80 void SAL_CALL DiagramLayoutFragmentHandler::endDocument()
81 {
82 
83 }
84 
85 ContextHandlerRef
onCreateContext(::sal_Int32 aElement,const AttributeList & rAttribs)86 DiagramLayoutFragmentHandler::onCreateContext( ::sal_Int32 aElement,
87                                                const AttributeList& rAttribs )
88 {
89     switch( aElement )
90     {
91     case DGM_TOKEN( layoutDef ):
92         return new DiagramDefinitionContext( *this, rAttribs, mpDataPtr );
93     default:
94         break;
95     }
96 
97     return this;
98 }
99 
DiagramQStylesFragmentHandler(XmlFilterBase & rFilter,const OUString & rFragmentPath,DiagramQStyleMap & rStylesMap)100 DiagramQStylesFragmentHandler::DiagramQStylesFragmentHandler( XmlFilterBase& rFilter,
101                                                               const OUString& rFragmentPath,
102                                                               DiagramQStyleMap& rStylesMap ) :
103     FragmentHandler2( rFilter, rFragmentPath ),
104     maStyleName(),
105     maStyleEntry(),
106     mrStylesMap( rStylesMap )
107 {}
108 
createStyleMatrixContext(sal_Int32 nElement,const AttributeList & rAttribs,ShapeStyleRef & o_rStyle)109 ::oox::core::ContextHandlerRef DiagramQStylesFragmentHandler::createStyleMatrixContext(
110     sal_Int32 nElement,
111     const AttributeList& rAttribs,
112     ShapeStyleRef& o_rStyle )
113 {
114     o_rStyle.mnThemedIdx = (nElement == A_TOKEN(fontRef)) ?
115         rAttribs.getToken( XML_idx, XML_none ) : rAttribs.getInteger( XML_idx, 0 );
116     return new ColorContext( *this, o_rStyle.maPhClr );
117 }
118 
onCreateContext(sal_Int32 nElement,const AttributeList & rAttribs)119 ::oox::core::ContextHandlerRef DiagramQStylesFragmentHandler::onCreateContext( sal_Int32 nElement,
120                                                                                const AttributeList& rAttribs )
121 {
122     // state-table like way of navigating the color fragment. we
123     // currently ignore everything except styleLbl in the styleDef
124     // element
125     switch( getCurrentElement() )
126     {
127         case XML_ROOT_CONTEXT:
128             return nElement == DGM_TOKEN(styleDef) ? this : nullptr;
129         case DGM_TOKEN(styleDef):
130             return nElement == DGM_TOKEN(styleLbl) ? this : nullptr;
131         case DGM_TOKEN(styleLbl):
132             return nElement == DGM_TOKEN(style) ? this : nullptr;
133         case DGM_TOKEN(style):
134         {
135             switch( nElement )
136             {
137                 case A_TOKEN(lnRef):     // CT_StyleMatrixReference
138                     return createStyleMatrixContext(nElement,rAttribs,
139                                                     maStyleEntry.maLineStyle);
140                 case A_TOKEN(fillRef):   // CT_StyleMatrixReference
141                     return createStyleMatrixContext(nElement,rAttribs,
142                                                     maStyleEntry.maFillStyle);
143                 case A_TOKEN(effectRef): // CT_StyleMatrixReference
144                     return createStyleMatrixContext(nElement,rAttribs,
145                                                     maStyleEntry.maEffectStyle);
146                 case A_TOKEN(fontRef):   // CT_FontReference
147                     return createStyleMatrixContext(nElement,rAttribs,
148                                                     maStyleEntry.maTextStyle);
149             }
150             return nullptr;
151         }
152     }
153 
154     return nullptr;
155 }
156 
onStartElement(const AttributeList & rAttribs)157 void DiagramQStylesFragmentHandler::onStartElement( const AttributeList& rAttribs )
158 {
159     if( getCurrentElement() == DGM_TOKEN( styleLbl ) )
160     {
161         maStyleName = rAttribs.getString( XML_name, OUString() );
162         maStyleEntry = mrStylesMap[maStyleName];
163     }
164 }
165 
onEndElement()166 void DiagramQStylesFragmentHandler::onEndElement( )
167 {
168     if( getCurrentElement() == DGM_TOKEN(styleLbl) )
169         mrStylesMap[maStyleName] = maStyleEntry;
170 }
171 
ColorFragmentHandler(::oox::core::XmlFilterBase & rFilter,const OUString & rFragmentPath,DiagramColorMap & rColorsMap)172 ColorFragmentHandler::ColorFragmentHandler( ::oox::core::XmlFilterBase& rFilter,
173                                             const OUString& rFragmentPath,
174                                             DiagramColorMap& rColorsMap ) :
175     FragmentHandler2(rFilter,rFragmentPath),
176     maColorName(),
177     maColorEntry(),
178     mrColorsMap(rColorsMap)
179 {}
180 
onCreateContext(sal_Int32 nElement,const AttributeList &)181 ::oox::core::ContextHandlerRef ColorFragmentHandler::onCreateContext( sal_Int32 nElement,
182                                                                       const AttributeList& /*rAttribs*/ )
183 {
184     // state-table like way of navigating the color fragment. we
185     // currently ignore everything except styleLbl in the colorsDef
186     // element
187     switch( getCurrentElement() )
188     {
189         case XML_ROOT_CONTEXT:
190             return nElement == DGM_TOKEN(colorsDef) ? this : nullptr;
191         case DGM_TOKEN(colorsDef):
192             return nElement == DGM_TOKEN(styleLbl) ? this : nullptr;
193         case DGM_TOKEN(styleLbl):
194         {
195             switch( nElement )
196             {
197                 // the actual colors - defer to color fragment handlers.
198 
199                 case DGM_TOKEN(fillClrLst):
200                     return new ColorsContext( *this, maColorEntry.maFillColors );
201                 case DGM_TOKEN(linClrLst):
202                     return new ColorsContext( *this, maColorEntry.maLineColors );
203                 case DGM_TOKEN(effectClrLst):
204                     return new ColorsContext( *this, maColorEntry.maEffectColors );
205                 case DGM_TOKEN(txFillClrLst):
206                     return new ColorsContext( *this, maColorEntry.maTextFillColors );
207                 case DGM_TOKEN(txLinClrLst):
208                     return new ColorsContext( *this, maColorEntry.maTextLineColors );
209                 case DGM_TOKEN(txEffectClrLst):
210                     return new ColorsContext( *this, maColorEntry.maTextEffectColors );
211             }
212             break;
213         }
214     }
215 
216     return nullptr;
217 }
218 
onStartElement(const AttributeList & rAttribs)219 void ColorFragmentHandler::onStartElement( const AttributeList& rAttribs )
220 {
221     if( getCurrentElement() == DGM_TOKEN(styleLbl) )
222     {
223         maColorName = rAttribs.getString( XML_name, OUString() );
224         maColorEntry = mrColorsMap[maColorName];
225     }
226 }
227 
onEndElement()228 void ColorFragmentHandler::onEndElement( )
229 {
230     if( getCurrentElement() == DGM_TOKEN(styleLbl) )
231         mrColorsMap[maColorName] = maColorEntry;
232 }
233 
234 }
235 
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
237